|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { isRequestCanceledError } from '@agent-device/kernel/errors'; |
| 4 | +import { createRequestGuard } from './request-guard.ts'; |
| 5 | + |
| 6 | +function canceledOutcome(promise: Promise<unknown>): Promise<unknown> { |
| 7 | + return promise.then( |
| 8 | + () => { |
| 9 | + throw new Error('expected rejection'); |
| 10 | + }, |
| 11 | + (error: unknown) => error, |
| 12 | + ); |
| 13 | +} |
| 14 | + |
| 15 | +test('a guard without a signal never refuses and never interferes', async () => { |
| 16 | + const guard = createRequestGuard({ signal: undefined, requestId: 'req-none' }); |
| 17 | + guard.refuseIfAborted(); |
| 18 | + assert.equal(await guard.guard(async () => 'sent'), 'sent'); |
| 19 | +}); |
| 20 | + |
| 21 | +test('refuseIfAborted refuses an already-aborted call with dispatched no', async () => { |
| 22 | + const controller = new AbortController(); |
| 23 | + controller.abort(new Error('caller gave up')); |
| 24 | + const guard = createRequestGuard({ signal: controller.signal, requestId: 'req-pre' }); |
| 25 | + assert.throws( |
| 26 | + () => guard.refuseIfAborted(), |
| 27 | + (error: unknown) => |
| 28 | + isRequestCanceledError(error) && |
| 29 | + (error as { details?: Record<string, unknown> }).details?.dispatched === 'no' && |
| 30 | + (error as { details?: Record<string, unknown> }).details?.requestId === 'req-pre', |
| 31 | + ); |
| 32 | +}); |
| 33 | + |
| 34 | +test('guard settles an in-flight send with the typed canceled error whatever the abort reason', async () => { |
| 35 | + const controller = new AbortController(); |
| 36 | + const guard = createRequestGuard({ signal: controller.signal, requestId: 'req-flight' }); |
| 37 | + let settleSend: (() => void) | undefined; |
| 38 | + const pending = guard.guard( |
| 39 | + () => new Promise<string>((resolve) => (settleSend = () => resolve('late'))), |
| 40 | + ); |
| 41 | + controller.abort(new Error('anything')); |
| 42 | + const error = await canceledOutcome(pending); |
| 43 | + assert.equal(isRequestCanceledError(error), true); |
| 44 | + assert.equal((error as { details?: Record<string, unknown> }).details?.dispatched, 'unknown'); |
| 45 | + assert.equal((error as { details?: Record<string, unknown> }).details?.requestId, 'req-flight'); |
| 46 | + // A transport that ignores the signal may still resolve later; that must not |
| 47 | + // turn the already-rejected caller promise into a second outcome. |
| 48 | + settleSend?.(); |
| 49 | +}); |
| 50 | + |
| 51 | +test('guard keeps a send error when no abort ever fires', async () => { |
| 52 | + const controller = new AbortController(); |
| 53 | + const guard = createRequestGuard({ signal: controller.signal }); |
| 54 | + const failure = new Error('daemon refused'); |
| 55 | + await assert.rejects( |
| 56 | + guard.guard(async () => { |
| 57 | + throw failure; |
| 58 | + }), |
| 59 | + (error: unknown) => error === failure, |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +test('guard returns the send value when it settles before any abort', async () => { |
| 64 | + const controller = new AbortController(); |
| 65 | + const guard = createRequestGuard({ signal: controller.signal }); |
| 66 | + const value = await guard.guard(async () => 'response'); |
| 67 | + controller.abort(); |
| 68 | + assert.equal(value, 'response'); |
| 69 | +}); |
0 commit comments