|
| 1 | +import { execFileSync } from 'node:child_process'; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { afterAll, describe, expect, it } from 'vitest'; |
| 6 | +import { createOutcomeUnknown } from '../agents/client-outcome.ts'; |
| 7 | + |
| 8 | +// A failed create leaves its object possibly present only if the request may have reached the daemon (#51 item 1). |
| 9 | +describe('create outcome classification', () => { |
| 10 | + const dir = mkdtempSync(join(tmpdir(), 'client-outcome-')); |
| 11 | + afterAll(() => rmSync(dir, { recursive: true, force: true })); |
| 12 | + const failure = (run: () => unknown) => { try { run(); } catch (error) { return error; } throw new Error('did not fail'); }; |
| 13 | + |
| 14 | + it('treats a client that never started as having created nothing', () => { |
| 15 | + expect(createOutcomeUnknown(failure(() => execFileSync('codeboost-no-such-docker', [], |
| 16 | + { env: { PATH: dir }, stdio: 'ignore' })))).toBe(false); |
| 17 | + const notExecutable = join(dir, 'docker'); |
| 18 | + writeFileSync(notExecutable, '#!/bin/sh\n', { mode: 0o644 }); |
| 19 | + expect(createOutcomeUnknown(failure(() => execFileSync(notExecutable, [], { stdio: 'ignore' })))).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('treats a daemon-answered failure as known and a killed client as unknown', () => { |
| 23 | + expect(createOutcomeUnknown(failure(() => execFileSync('sh', ['-c', 'exit 3'], { stdio: 'ignore' })))).toBe(false); |
| 24 | + expect(createOutcomeUnknown(failure(() => execFileSync('sleep', ['5'], |
| 25 | + { timeout: 50, killSignal: 'SIGKILL', stdio: 'ignore' })))).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('reads the cause of a wrapped client error', () => { |
| 29 | + expect(createOutcomeUnknown({ status: null, cause: { code: 'ENOENT' } })).toBe(false); |
| 30 | + expect(createOutcomeUnknown({ status: null, cause: { code: 'ETIMEDOUT' } })).toBe(true); |
| 31 | + expect(createOutcomeUnknown({ status: null })).toBe(true); |
| 32 | + }); |
| 33 | +}); |
0 commit comments