import { expect, test } from 'bun:test' const cli = new URL('../src/cli.ts', import.meta.url).pathname async function run(args: string[], stdin = '') { const child = Bun.spawn([process.execPath, cli, ...args], { stdin: new Blob([stdin]), stdout: 'pipe', stderr: 'pipe' }) const [code, stdout, stderr] = await Promise.all([ child.exited, new Response(child.stdout).text(), new Response(child.stderr).text() ]) return { code, stdout, stderr } } test('CLI help documents file and stdin input without loading a model', async () => { const result = await run(['--help']) expect(result.code).toBe(0) expect(result.stdout).toContain('JSON file path') expect(result.stdout).toContain('--device') expect(result.stderr).toBe('') }) test.each([[], ['-', '--device', 'unknown'], ['-', '--dtype', 'unknown'], ['--unknown']])( 'CLI rejects invalid arguments %j', async (...args) => { const result = await run(args) expect(result.code).toBe(1) expect(result.stdout).toBe('') expect(result.stderr).toContain('error:') } ) test('CLI reports malformed stdin as JSON on stderr', async () => { const result = await run(['-'], '{') expect(result.code).toBe(1) expect(result.stdout).toBe('') expect(JSON.parse(result.stderr).error.type).toBe('invalid-json') }) test('CLI validates metadata from stdin before inference', async () => { const result = await run(['-'], JSON.stringify({ state: null, decisions: [] })) expect(result.code).toBe(1) expect(result.stdout).toBe('') expect(JSON.parse(result.stderr).error).toMatchObject({ type: 'invalid-input', path: 'decisions' }) }) test('CLI reports unreadable files', async () => { const result = await run([`${cli}/missing.json`]) expect(result.code).toBe(1) expect(result.stdout).toBe('') expect(JSON.parse(result.stderr).error.type).toBe('input-read-error') })