import { expect, test } from 'bun:test'; import { mkdtemp, rm, stat, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; import { validateArguments } from '../workers/reference'; test('reference CLI rejects contradictory modes, unknown flags, and missing paths before touching data', () => { expect(() => validateArguments(['--production', '--fixture'])).toThrow('not both'); expect(() => validateArguments(['--production', '--offline-typo'])).toThrow('Unknown'); expect(() => validateArguments(['--production', '--directory', '--offline'])).toThrow('missing its value'); expect(() => validateArguments(['--production', '--check', '--rollback'])).toThrow('not both'); expect(() => validateArguments(['--production', '--offline', '--directory', '/tmp/example'])).not.toThrow(); }); test('production CLI honors the shared REFERENCE_DIR instead of the archive directory', async () => { const directory = await mkdtemp(join(tmpdir(), 'reference-cli-')); const output = join(directory, 'published'); const archiveDir = join(directory, 'archives'); try { const preload = join(directory, 'no-network.ts'); await writeFile(preload, 'globalThis.fetch = (() => { throw new Error("Network disabled in CLI test"); }) as typeof fetch;'); // Missing offline input deliberately prevents a build. The failed attempt // still has to be recorded in the application artifact directory, not the // archive-cache directory or an ephemeral container working directory. const child = Bun.spawn(['bun', '--preload', preload, resolve('workers/reference.ts'), '--production', '--offline'], { cwd: directory, env: { ...process.env, REFERENCE_DIR: output, REFERENCE_LIVE_DIR: archiveDir }, stdout: 'pipe', stderr: 'pipe', }); const [code] = await Promise.all([child.exited, new Response(child.stdout).text(), new Response(child.stderr).text()]); expect(code).toBe(1); const manifest = await Bun.file(join(output, 'manifest.json')).json(); expect(manifest.lastAttempt.status).toBe('failed'); expect(manifest.lastAttempt.profile).toBe('production'); expect(await stat(join(archiveDir, 'manifest.json')).catch(() => null)).toBeNull(); expect(await stat(join(directory, 'data', 'reference-live', 'manifest.json')).catch(() => null)).toBeNull(); } finally { await rm(directory, { recursive: true, force: true }); } });