import { beforeEach, afterEach, describe, expect, it } from 'vitest'; import { spawnSync } from 'child_process'; import { promises as fs } from 'fs'; import * as os from 'os'; import * as path from 'path'; import { snapshotTree } from './decisions/curation-fixtures.js'; import { requireBuiltCli, sandboxEnv } from './helpers.js'; const CLI = requireBuiltCli(); const INIT_ARGS = ['--recommended', '--no-ambient', '--no-memory', '--no-knowledge', '--no-rules']; /** The slowest of 18 local CLI spawns took 318 ms. Leave room for host load. */ const SUBPROCESS_TIMEOUT_MS = 20_000; const SCENARIO_TIMEOUT_MS = 120_000; let home: string; const claudeDir = (): string => path.join(home, '.claude'); function run(...args: string[]): { status: number | null; output: string } { const result = spawnSync(process.execPath, [CLI, ...args], { cwd: home, env: sandboxEnv(home), encoding: 'utf-8', timeout: SUBPROCESS_TIMEOUT_MS, killSignal: 'SIGKILL', stdio: ['ignore', 'pipe', 'pipe'], }); if (result.error) { throw new Error( `devflow ${args.join(' ')} failed (limit ${SUBPROCESS_TIMEOUT_MS} ms): ${result.error.message}`, { cause: result.error }, ); } return { status: result.status, output: `${result.stdout ?? ''}${result.stderr ?? ''}` }; } function ok(...args: string[]): string { const { status, output } = run(...args); expect(status, output).toBe(0); return output; } async function manifest(): Promise { return JSON.parse(await fs.readFile(path.join(home, '.devflow', 'manifest.json'), 'utf-8')); } beforeEach(async () => { home = await fs.mkdtemp(path.join(os.tmpdir(), 'devflow-design-e2e-')); await fs.mkdir(claudeDir()); }); afterEach(async () => { await fs.rm(home, { recursive: true, force: true }); }); describe('devflow design', () => { it.each([ { label: 'on', enabled: true, args: ['--compliance', 'gdpr'] }, { label: 'off', enabled: false, args: ['--no-compliance'] }, ])('restores every installed byte with compliance $label', async ({ enabled, args }) => { const expectState = async (design: boolean): Promise => { expect(await manifest()).toMatchObject({ features: { design, compliance: { enabled } }, installedVariants: { design, compliance: enabled }, }); }; ok('init', ...INIT_ARGS, ...args); await expectState(false); const before = snapshotTree(claudeDir()); const agent = path.join('agents', 'devflow', 'evaluate.md'); const command = path.join('commands', 'devflow', 'implement.md'); expect(before.get(agent)?.length).toBeGreaterThan(0); expect(before.get(command)?.length).toBeGreaterThan(0); const designTrace = /DESIGN_FILES|UI design attached|Design check:|Design files not read/; for (const [file, text] of before) { if (file.startsWith('skills/devflow:ui-design/') || file === 'rules/devflow/ui-design.md') continue; expect(text, file).not.toMatch(designTrace); } ok('design', '--enable'); await expectState(true); const afterEnable = snapshotTree(claudeDir()); expect(afterEnable.get(agent)).toContain('Design files not read:'); expect(afterEnable.get(command)).toContain('UI design attached'); expect(ok('design', '--status')).toMatch(/State: +enabled[\s\S]*Prompts: +on/); ok('design', '--disable'); await expectState(false); expect(ok('design', '--status')).toMatch(/State: +disabled[\s\S]*Prompts: +off/); expect(snapshotTree(claudeDir())).toEqual(before); }, SCENARIO_TIMEOUT_MS); it('keeps design on at plain re-init and sets it off at reset', async () => { ok('init', ...INIT_ARGS); ok('design', '--enable'); ok('init', ...INIT_ARGS); expect(await manifest()).toMatchObject({ features: { design: true }, installedVariants: { design: true } }); ok('init', ...INIT_ARGS, '--reset'); expect(await manifest()).toMatchObject({ features: { design: false }, installedVariants: { design: false } }); }, SCENARIO_TIMEOUT_MS); it('shows usage with no flag and refuses conflicting or valued flags', () => { expect(ok('design')).toContain('devflow design --enable | --disable | --status'); expect(run('design', '--enable', '--status')).toMatchObject({ status: 1, output: expect.stringContaining('Use only one of') }); expect(run('design', '--enable=false').status).not.toBe(0); expect(run('init', '--design')).toMatchObject({ status: 1, output: expect.stringContaining('unknown option') }); }, SCENARIO_TIMEOUT_MS); });