|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { afterAll, describe, expect, it, vi } from 'vitest' |
| 5 | +import { FileWorker } from '@/file-worker' |
| 6 | +import { FileOperationError } from '@/files' |
| 7 | + |
| 8 | +/** |
| 9 | + * A stand-in worker that answers every call with the frames `FAKE_REPLY` names, so the daemon |
| 10 | + * can be fed a reply the real worker never sends. Plain JavaScript, framed as `ipc.ts` frames |
| 11 | + * (u32 length, u8 kind, u32 id), so it runs under whichever runtime executes the suite. |
| 12 | + */ |
| 13 | +const FAKE_WORKER = ` |
| 14 | +const REPLIES = { |
| 15 | + end: [[4, null]], |
| 16 | + 'result-then-end': [[1, '{"size":0}'], [4, null]], |
| 17 | +} |
| 18 | +const frame = (kind, id, text) => { |
| 19 | + const payload = Buffer.from(text ?? '') |
| 20 | + const header = Buffer.alloc(9) |
| 21 | + header.writeUInt32BE(payload.length, 0) |
| 22 | + header.writeUInt8(kind, 4) |
| 23 | + header.writeUInt32BE(id, 5) |
| 24 | + return Buffer.concat([header, payload]) |
| 25 | +} |
| 26 | +let buffered = Buffer.alloc(0) |
| 27 | +process.stdin.on('data', (chunk) => { |
| 28 | + buffered = Buffer.concat([buffered, chunk]) |
| 29 | + while (buffered.length >= 9 && buffered.length >= 9 + buffered.readUInt32BE(0)) { |
| 30 | + const kind = buffered.readUInt8(4) |
| 31 | + const id = buffered.readUInt32BE(5) |
| 32 | + buffered = buffered.subarray(9 + buffered.readUInt32BE(0)) |
| 33 | + if (kind !== 0) continue |
| 34 | + for (const [replyKind, text] of REPLIES[process.env.FAKE_REPLY]) { |
| 35 | + process.stdout.write(frame(replyKind, id, text)) |
| 36 | + } |
| 37 | + } |
| 38 | +}) |
| 39 | +` |
| 40 | + |
| 41 | +const directory = mkdtempSync(path.join(tmpdir(), 'sim-file-worker-')) |
| 42 | +const fakeWorker = path.join(directory, 'worker.cjs') |
| 43 | +writeFileSync(fakeWorker, FAKE_WORKER) |
| 44 | + |
| 45 | +vi.mock('@/self', () => ({ |
| 46 | + AGENT_MODES: { fileWorker: 'file-worker' }, |
| 47 | + selfCommand: () => ({ file: process.execPath, args: [fakeWorker] }), |
| 48 | +})) |
| 49 | + |
| 50 | +afterAll(() => { |
| 51 | + rmSync(directory, { recursive: true, force: true }) |
| 52 | +}) |
| 53 | + |
| 54 | +function workerReplying(reply: 'end' | 'result-then-end'): FileWorker { |
| 55 | + return new FileWorker({ name: 'user', home: directory }, { FAKE_REPLY: reply }) |
| 56 | +} |
| 57 | + |
| 58 | +describe('file worker replies', () => { |
| 59 | + it('rejects a call the worker ends without ever settling it', { timeout: 5_000 }, async () => { |
| 60 | + const error = await workerReplying('end') |
| 61 | + .stat('notes.txt') |
| 62 | + .catch((caught: unknown) => caught) |
| 63 | + expect(error).toBeInstanceOf(FileOperationError) |
| 64 | + }) |
| 65 | + |
| 66 | + it('rejects a read whose stream ends before its result arrives', { timeout: 5_000 }, async () => { |
| 67 | + const error = await workerReplying('end') |
| 68 | + .read('notes.txt', undefined) |
| 69 | + .catch((caught: unknown) => caught) |
| 70 | + expect(error).toBeInstanceOf(FileOperationError) |
| 71 | + }) |
| 72 | + |
| 73 | + it('accepts the stream terminator once the read result has arrived', async () => { |
| 74 | + const { size, body } = await workerReplying('result-then-end').read('notes.txt', undefined) |
| 75 | + expect(size).toBe(0) |
| 76 | + expect(await new Response(body).arrayBuffer()).toEqual(new ArrayBuffer(0)) |
| 77 | + }) |
| 78 | +}) |
0 commit comments