|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +// runDocker must keep the daemon's answer when a cancel lands after the client exited (#51 item 2). |
| 4 | +const calls = vi.hoisted(() => ({ finish: undefined as undefined | ((error: unknown, stdout: string, stderr: string) => void) })); |
| 5 | +vi.mock('node:child_process', () => ({ |
| 6 | + execFile: vi.fn((_file: string, _args: string[], _options: unknown, callback: (error: unknown, stdout: string, |
| 7 | + stderr: string) => void) => { calls.finish = callback; return {}; }), |
| 8 | +})); |
| 9 | +const { runDocker } = await import('../agents/docker.ts'); |
| 10 | +const { createOutcomeUnknown } = await import('../agents/client-outcome.ts'); |
| 11 | + |
| 12 | +describe('runDocker exit classification', () => { |
| 13 | + afterEach(() => { calls.finish = undefined; }); |
| 14 | + |
| 15 | + it('keeps a daemon-answered exit status when the signal is aborted before the callback runs', async () => { |
| 16 | + const controller = new AbortController(); |
| 17 | + const pending = runDocker(['create', '--name', 'taken'], { timeoutMs: 5_000, signal: controller.signal }); |
| 18 | + // The client already exited 1 (name conflict); the cancel arrives before Node delivers the result. |
| 19 | + controller.abort(); |
| 20 | + calls.finish!(Object.assign(new Error('Command failed'), { code: 1, killed: false }), '', 'Conflict. The name is in use'); |
| 21 | + const outcome = await pending; |
| 22 | + expect(outcome).toMatchObject({ status: 1, stderr: 'Conflict. The name is in use' }); |
| 23 | + expect(outcome.error).toBeUndefined(); |
| 24 | + expect(createOutcomeUnknown({ status: outcome.status })).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + it('reports a client killed by the abort as cancelled, with the outcome unknown', async () => { |
| 28 | + const controller = new AbortController(); |
| 29 | + const pending = runDocker(['create', '--name', 'x'], { timeoutMs: 5_000, signal: controller.signal }); |
| 30 | + controller.abort(); |
| 31 | + calls.finish!(Object.assign(new Error('The operation was aborted'), { name: 'AbortError', code: 'ABORT_ERR' }), '', ''); |
| 32 | + const outcome = await pending; |
| 33 | + expect(outcome.status).toBeNull(); |
| 34 | + expect(outcome.error?.message).toContain('was cancelled'); |
| 35 | + expect(createOutcomeUnknown({ status: null, cause: outcome.error })).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it('reports a client killed at its deadline as a timeout', async () => { |
| 39 | + const pending = runDocker(['create'], { timeoutMs: 5_000 }); |
| 40 | + calls.finish!(Object.assign(new Error('Command failed'), { code: null, killed: true, signal: 'SIGKILL' }), '', ''); |
| 41 | + const outcome = await pending; |
| 42 | + expect(outcome.status).toBeNull(); |
| 43 | + expect((outcome.error as { code?: unknown }).code).toBe('ETIMEDOUT'); |
| 44 | + }); |
| 45 | +}); |
0 commit comments