/** * Tests for src/cli/commands/compliance.ts * * Covers: * - resolveComplianceCliAction pure resolver matrix * - parseFrameworkList "Commander parse pin" (error message names every valid ID) * - unknownFrameworkIds invalid-ID classification * - applyComplianceChange: the switch, the one converge and the fail-closed exit (#648) * - the install record and the `--status` Posting line (#648) * * Init-seed compliance seeding coverage (resolveSeedFeatures, applyCliToggles, * resolveResetGatedInputs) lives in tests/init-seed.test.ts — compliance seeding section. */ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { promises as fs } from 'fs'; import * as os from 'os'; import * as path from 'path'; import { applyComplianceChange, postingStatusLine, resolveComplianceCliAction, unknownFrameworkIds, type ApplyComplianceChangeResult, } from '../src/cli/commands/compliance.js'; import { COMPLIANCE_FRAMEWORKS, parseFrameworkList, } from '../src/core/compliance.js'; import { bothOffDir, compiledComplianceOffSkillRefsDir, compiledSkillRefsDir, complianceOffDir, curationOffDir, skillsDir } from '../src/core/assets.js'; import { ALL_SWITCHES_ON } from '../src/core/curation-variants.js'; import { installRecordFor, postingStatus, readInstalledVariants } from '../src/core/installed-variants.js'; import { readManifest, type ManifestData } from '../src/core/manifest.js'; import { getManifestLockPath } from '../src/core/manifest-lock.js'; import { testBackups } from './helpers.js'; // ── resolveComplianceCliAction ──────────────────────────────────────────────── describe('resolveComplianceCliAction', () => { // ── enable ───────────────────────────────────────────────────────────────── it('enable from disabled with empty frameworks → enabled:true, frameworks preserved (empty)', () => { const result = resolveComplianceCliAction( { enabled: false, frameworks: [] }, 'enable', ); expect(result.nextState.enabled).toBe(true); expect(result.nextState.frameworks).toEqual([]); }); it('enable from disabled with existing frameworks → restores all frameworks', () => { const result = resolveComplianceCliAction( { enabled: false, frameworks: ['gdpr', 'soc2'] }, 'enable', ); expect(result.nextState.enabled).toBe(true); expect(result.nextState.frameworks).toEqual(['gdpr', 'soc2']); }); it('enable when already enabled → nextState unchanged, messages indicate already enabled', () => { const result = resolveComplianceCliAction( { enabled: true, frameworks: ['hipaa'] }, 'enable', ); expect(result.nextState.enabled).toBe(true); expect(result.nextState.frameworks).toEqual(['hipaa']); expect(result.messages.some(m => m.text.toLowerCase().includes('already'))).toBe(true); }); // ── disable ──────────────────────────────────────────────────────────────── it('disable from enabled → enabled:false, frameworks preserved', () => { const result = resolveComplianceCliAction( { enabled: true, frameworks: ['gdpr', 'sox'] }, 'disable', ); expect(result.nextState.enabled).toBe(false); expect(result.nextState.frameworks).toEqual(['gdpr', 'sox']); }); it('disable when already disabled → nextState unchanged', () => { const result = resolveComplianceCliAction( { enabled: false, frameworks: ['pci-dss'] }, 'disable', ); expect(result.nextState.enabled).toBe(false); // frameworks preserved expect(result.nextState.frameworks).toEqual(['pci-dss']); }); // ── set ──────────────────────────────────────────────────────────────────── it('set replaces frameworks exactly and enables', () => { const result = resolveComplianceCliAction( { enabled: false, frameworks: [] }, 'set', ['gdpr', 'soc2'], ); expect(result.nextState.enabled).toBe(true); expect(result.nextState.frameworks).toEqual(['gdpr', 'soc2']); }); it('set with zero frameworks → enabled:true, frameworks empty (generic controls only)', () => { const result = resolveComplianceCliAction( { enabled: true, frameworks: ['hipaa'] }, 'set', [], ); expect(result.nextState.enabled).toBe(true); expect(result.nextState.frameworks).toEqual([]); }); it('set replaces prior frameworks entirely', () => { const result = resolveComplianceCliAction( { enabled: true, frameworks: ['gdpr', 'hipaa', 'sox'] }, 'set', ['pci-dss'], ); expect(result.nextState.frameworks).toEqual(['pci-dss']); }); // ── status ───────────────────────────────────────────────────────────────── it('status → nextState unchanged', () => { const current = { enabled: true, frameworks: ['iso-27001'] }; const result = resolveComplianceCliAction(current, 'status'); expect(result.nextState).toEqual(current); }); it('status does not produce state change messages', () => { const result = resolveComplianceCliAction( { enabled: false, frameworks: [] }, 'status', ); // status is reporting only — messages must be empty (no enable/disable/set side-effect text) expect(result.nextState.enabled).toBe(false); expect(result.messages).toHaveLength(0); }); }); // ── Commander parse pin: --set with unknown IDs ──────────────────────────────── describe('parseFrameworkList (Commander parse pin)', () => { it('rejects unknown IDs with an error naming every valid registry ID', () => { const result = parseFrameworkList('gdrp,hippa'); expect(result.ok).toBe(false); if (!result.ok) { // Error must name every valid ID for (const fw of COMPLIANCE_FRAMEWORKS) { expect(result.error).toContain(fw.id); } // Error must name the unknown IDs submitted expect(result.error).toContain('gdrp'); expect(result.error).toContain('hippa'); } }); it('accepts all 6 valid IDs', () => { const validIds = COMPLIANCE_FRAMEWORKS.map(fw => fw.id).join(','); const result = parseFrameworkList(validIds); expect(result.ok).toBe(true); if (result.ok) { expect(result.value).toHaveLength(COMPLIANCE_FRAMEWORKS.length); } }); it('accepts empty input → [] (zero frameworks allowed)', () => { const result = parseFrameworkList(''); expect(result.ok).toBe(true); if (result.ok) { expect(result.value).toEqual([]); } }); it('normalises iso27001 alias to iso-27001', () => { const result = parseFrameworkList('iso27001'); expect(result.ok).toBe(true); if (result.ok) { expect(result.value).toEqual(['iso-27001']); } }); }); // ── unknownFrameworkIds — invalid-ID reporting path ────────────────────────────── // // Installed reference files are no drift signal any more: every install carries all // six (D-COMPLIANCE-INSTALL-ALWAYS). The one manifest defect --status still reports is // an ID the registry does not know. describe('unknownFrameworkIds', () => { const REGISTRY = new Set(COMPLIANCE_FRAMEWORKS.map(fw => fw.id)); it('all manifest IDs known → empty', () => { expect(unknownFrameworkIds(['gdpr', 'hipaa'], REGISTRY)).toEqual([]); }); it('unknown ID in manifest → listed (--set required to remove)', () => { expect(unknownFrameworkIds(['gdpr', 'not-a-framework'], REGISTRY)).toEqual(['not-a-framework']); }); it('keeps manifest order and lists every unknown ID', () => { expect(unknownFrameworkIds(['foo', 'gdpr', 'bar'], REGISTRY)).toEqual(['foo', 'bar']); }); it('empty manifest → empty', () => { expect(unknownFrameworkIds([], REGISTRY)).toEqual([]); }); }); // ── applyComplianceChange: the switch, the converge and the fail-closed exit (#648) ── // // Each test builds a temp Claude dir, a temp devflow dir and a temp package root, so no real // HOME and no real dist/ is read or written. The package holds one compliance-armed command // (`release`) and one with both arms (`plan`); the installed copies start as the on files. const RUNNING = '3.3.0'; let tmp: string; let claudeDir: string; let devflowDir: string; let pkg: string; let warnings: string[]; const warn = (msg: string): void => { warnings.push(msg); }; const ANSI = /\u001b\[[0-9;]*m/g; const plain = (text: string): string => text.replace(ANSI, ''); async function write(file: string, text: string): Promise { await fs.mkdir(path.dirname(file), { recursive: true }); await fs.writeFile(file, text, 'utf-8'); } const installedCommand = (name: string): string => path.join(claudeDir, 'commands', 'devflow', `${name}.md`); const installedRule = (): string => path.join(claudeDir, 'rules', 'devflow', 'compliance.md'); const exists = (file: string): Promise => fs.stat(file).then(() => true, () => false); /** Every file below `dir` as relative path -> bytes. */ async function treeBytes(dir: string): Promise> { const out: Record = {}; for (const entry of await fs.readdir(dir, { recursive: true, withFileTypes: true })) { if (!entry.isFile()) continue; const file = path.join(entry.parentPath, entry.name); out[path.relative(dir, file)] = await fs.readFile(file, 'utf-8'); } return out; } async function seedPackage(): Promise { await write(path.join(pkg, 'dist', 'commands', 'plan.md'), 'plan ON\n'); await write(path.join(curationOffDir('commands', pkg), 'plan.md'), 'plan CURATION-OFF\n'); await write(path.join(complianceOffDir('commands', pkg), 'plan.md'), 'plan COMPLIANCE-OFF\n'); await write(path.join(bothOffDir('commands', pkg), 'plan.md'), 'plan BOTH-OFF\n'); await write(path.join(pkg, 'dist', 'commands', 'release.md'), 'release ON\n'); await write(path.join(complianceOffDir('commands', pkg), 'release.md'), 'release COMPLIANCE-OFF\n'); await write(path.join(pkg, 'dist', 'agents', 'code.md'), 'code ON\n'); await write(path.join(curationOffDir('agents', pkg), 'code.md'), 'code CURATION-OFF\n'); await write(path.join(skillsDir(pkg), 'apply-decisions', 'SKILL.md'), 'apply-decisions of the temp package\n'); await write(path.join(skillsDir(pkg), 'test-driven-development', 'SKILL.md'), 'test-driven-development of the temp package\n'); await write(path.join(pkg, 'dist', 'variant-axes.json'), JSON.stringify({ version: 1, hosts: { 'agents/code': { curation: true, compliance: false }, 'commands/plan': { curation: true, compliance: true }, 'commands/release': { curation: false, compliance: true }, }, })); } async function installOnFiles(): Promise { await write(installedCommand('plan'), 'plan ON\n'); await write(installedCommand('release'), 'release ON\n'); await write(path.join(claudeDir, 'agents', 'devflow', 'code.md'), 'code ON\n'); } async function manifestWith( compliance: { enabled: boolean; frameworks: string[] }, version: string = RUNNING, { features, ...extra }: Record & { features?: Partial } = {}, rules: boolean = false, ): Promise { await write(path.join(devflowDir, 'manifest.json'), JSON.stringify({ version, plugins: ['devflow-core-skills'], scope: 'user', features: { ambient: false, memory: false, hud: false, knowledge: true, curation: true, learning: true, rules, compliance, ...features }, installedAt: '2026-10-10T00:00:00.000Z', updatedAt: '2026-10-10T00:00:00.000Z', ...extra, })); const read = await readManifest(devflowDir); if (read === null) throw new Error('the seeded manifest did not read'); return read; } async function change( manifest: ManifestData, action: 'enable' | 'disable' | 'set', opts: { setFrameworks?: string[]; runningVersion?: string | null } = {}, ): Promise { return applyComplianceChange({ claudeDir, devflowDir, manifest, action, setFrameworks: opts.setFrameworks, runningVersion: opts.runningVersion === undefined ? RUNNING : opts.runningVersion, packageRoot: pkg, warn, backups: await testBackups(tmp, { claudeDir, devflowDir }), }); } const recordOf = async (): Promise => (await readManifest(devflowDir))?.installedVariants; const complianceOf = async (): Promise => (await readManifest(devflowDir))?.features.compliance; describe('applyComplianceChange', () => { beforeEach(async () => { tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'devflow-compliance-change-')); claudeDir = path.join(tmp, 'claude'); devflowDir = path.join(tmp, 'devflow'); pkg = path.join(tmp, 'pkg'); warnings = []; await fs.mkdir(claudeDir, { recursive: true }); await seedPackage(); }); afterEach(async () => { await fs.rm(tmp, { recursive: true, force: true }); }); it('disable writes the switch, installs the compliance-off prompts and records them (TP-39)', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(0); expect(result.nextState).toEqual({ enabled: false, frameworks: ['gdpr'] }); expect(await complianceOf()).toEqual({ enabled: false, frameworks: ['gdpr'] }); expect(await fs.readFile(installedCommand('plan'), 'utf-8')).toBe('plan COMPLIANCE-OFF\n'); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe('release COMPLIANCE-OFF\n'); expect(readInstalledVariants(await recordOf()), warnings.join('\n')).toEqual({ curation: true, compliance: false, tdd: true, design: false }); expect(result.messages.some(m => m.level === 'error')).toBe(false); }); it('the disable message says the rule and every posting step were removed, frameworks remembered', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); const result = await change(manifest, 'disable'); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain('Compliance disabled'); expect(text).toContain('every posting step removed'); expect(text).toContain('frameworks remembered for re-enable'); }); it('enable restores the on prompts and records the compliance-on variant (TP-39)', async () => { const manifest = await manifestWith({ enabled: false, frameworks: ['hipaa'] }, RUNNING, { installedVariants: { curation: true, compliance: false } }); await write(installedCommand('plan'), 'plan COMPLIANCE-OFF\n'); await write(installedCommand('release'), 'release COMPLIANCE-OFF\n'); const result = await change(manifest, 'enable'); expect(result.exitCode).toBe(0); expect(await fs.readFile(installedCommand('plan'), 'utf-8')).toBe('plan ON\n'); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe('release ON\n'); expect(readInstalledVariants(await recordOf()), warnings.join('\n')).toEqual({ curation: true, compliance: true, tdd: true, design: false }); }); it('enable with a link at an installed reference folder exits 1 and names the failure; the switch stays on disk', async () => { const manifest = await manifestWith({ enabled: false, frameworks: ['hipaa'] }, RUNNING, { installedVariants: { curation: true, compliance: false } }); await write(installedCommand('plan'), 'plan COMPLIANCE-OFF\n'); await write(installedCommand('release'), 'release COMPLIANCE-OFF\n'); await fs.cp(compiledSkillRefsDir(), compiledSkillRefsDir(pkg), { recursive: true }); await fs.cp(path.join(skillsDir(), 'git'), path.join(skillsDir(pkg), 'git'), { recursive: true }); const outside = path.join(tmp, 'outside-pr'); const linked = path.join(claudeDir, 'skills', 'devflow:git', 'references', 'pr'); await write(path.join(outside, 'kept.md'), 'a file the user keeps\n'); await fs.mkdir(path.dirname(linked), { recursive: true }); await fs.symlink(outside, linked, 'dir'); const result = await change(manifest, 'enable'); expect(result.exitCode).toBe(1); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain('references/pr'); expect(text).toContain('real folder'); expect(text).not.toContain('Compliance enabled'); expect(result.messages.every(m => m.level === 'error')).toBe(true); expect(await complianceOf(), 'the switch is on disk whatever the converge did').toEqual({ enabled: true, frameworks: ['hipaa'] }); expect((await fs.lstat(linked)).isSymbolicLink(), 'the link is left in place').toBe(true); expect(await fs.readdir(outside), 'nothing was written through the link').toEqual(['kept.md']); }); it('set converges too: it enables, replaces the frameworks and installs the on prompts (TP-39)', async () => { const manifest = await manifestWith({ enabled: false, frameworks: [] }, RUNNING, { installedVariants: { curation: true, compliance: false } }); await write(installedCommand('release'), 'release COMPLIANCE-OFF\n'); const result = await change(manifest, 'set', { setFrameworks: ['gdpr', 'GDPR', 'soc2'] }); expect(result.exitCode).toBe(0); expect(result.nextState).toEqual({ enabled: true, frameworks: ['gdpr', 'soc2'] }); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe('release ON\n'); expect(readInstalledVariants(await recordOf()), warnings.join('\n')).toEqual({ curation: true, compliance: true, tdd: true, design: false }); }); it('a set that only changes the frameworks still converges, and reaches a missing record', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); const result = await change(manifest, 'set', { setFrameworks: ['hipaa'] }); expect(result.exitCode).toBe(0); expect(await complianceOf()).toEqual({ enabled: true, frameworks: ['hipaa'] }); expect(readInstalledVariants(await recordOf()), warnings.join('\n')).toEqual({ curation: true, compliance: true, tdd: true, design: false }); }); it('disable with the compliance-off tree absent exits 1, lists the files and prints no success line (TP-41)', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }, RUNNING, { installedVariants: { curation: true, compliance: true } }); await installOnFiles(); await fs.rm(complianceOffDir('commands', pkg), { recursive: true }); await fs.rm(bothOffDir('commands', pkg), { recursive: true }); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain(installedCommand('plan')); expect(text).toContain(installedCommand('release')); expect(text).toContain('devflow init'); expect(text).not.toContain('Compliance disabled'); expect(result.messages.every(m => m.level === 'error')).toBe(true); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe('release ON\n'); expect(await complianceOf(), 'the switch is on disk whatever the converge did').toEqual({ enabled: false, frameworks: ['gdpr'] }); expect(readInstalledVariants(await recordOf()), 'an unclean converge leaves the record as it stood').toEqual({ curation: true, compliance: true, tdd: true, design: false }); }); it('disable removes the posting-only skill files by name and exits 0 (AC-41)', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); // The git skill is installed, so the converge overlays its references from the package's one compliance-off root. await fs.cp(compiledComplianceOffSkillRefsDir(), compiledComplianceOffSkillRefsDir(pkg), { recursive: true }); const gate = path.join(claudeDir, 'skills', 'devflow:git', 'references', 'publication-gate.md'); const asks = path.join(claudeDir, 'skills', 'devflow:agent-references', 'references', 'commands', 'implement-evidence-asks.md'); const mine = path.join(claudeDir, 'skills', 'devflow:git', 'references', 'my-own-notes.md'); await write(gate, 'gate\n'); await write(asks, 'asks\n'); await write(mine, 'a note of mine\n'); const result = await change(manifest, 'disable'); expect(result.exitCode, result.messages.map(m => m.text).join('\n')).toBe(0); expect(await exists(gate)).toBe(false); expect(await exists(asks)).toBe(false); expect(await fs.readFile(mine, 'utf-8'), 'a file of the user is not on the list').toBe('a note of mine\n'); }); it('disable with a directory at a removal path exits 1 and names the file; the switch stays on disk (AC-41)', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }, RUNNING, { installedVariants: { curation: true, compliance: true } }); await installOnFiles(); const asks = path.join(claudeDir, 'skills', 'devflow:agent-references', 'references', 'commands', 'implement-evidence-asks.md'); await write(path.join(asks, 'inner.md'), 'in the way\n'); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain(asks); expect(text).not.toContain('Compliance disabled'); expect(await fs.readFile(path.join(asks, 'inner.md'), 'utf-8'), 'nothing inside the directory was touched').toBe('in the way\n'); expect(await complianceOf()).toEqual({ enabled: false, frameworks: ['gdpr'] }); expect(readInstalledVariants(await recordOf()), 'an unclean converge leaves the record as it stood').toEqual({ curation: true, compliance: true, tdd: true, design: false }); }); it('disable across a version skew exits 1 and says that posting steps remain installed until devflow init (TP-42)', async () => { const manifest = await manifestWith({ enabled: true, frameworks: [] }, '3.2.0'); await installOnFiles(); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain('posting steps remain installed until devflow init'); expect(text).not.toContain('Compliance disabled'); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe('release ON\n'); }); it('disable across a version skew still removes the compliance rule and exits 1', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }, '3.2.0', {}, true); await installOnFiles(); await write(installedRule(), 'the stamped compliance rule\n'); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); expect(await exists(installedRule())).toBe(false); expect(result.messages.map(m => m.text).join('\n')).toContain('posting steps remain installed until devflow init'); }); it('disable with an unknown running version is a skew as well', async () => { const manifest = await manifestWith({ enabled: true, frameworks: [] }); await installOnFiles(); const result = await change(manifest, 'disable', { runningVersion: null }); expect(result.exitCode).toBe(1); expect(result.messages.map(m => m.text).join('\n')).toContain('posting steps remain installed until devflow init'); }); it('enable across a version skew is a warning with exit 0, and the compliance rule is still installed', async () => { const manifest = await manifestWith({ enabled: false, frameworks: ['gdpr'] }, '3.2.0', {}, true); await installOnFiles(); const result = await change(manifest, 'enable'); expect(result.exitCode).toBe(0); expect(result.nextState.enabled).toBe(true); expect(result.messages.some(m => m.level === 'warn' && m.text.includes('devflow init'))).toBe(true); expect(await fs.readFile(installedRule(), 'utf-8')).toContain('Active frameworks: GDPR'); expect(result.messages.map(m => m.text).join('\n')).not.toContain('rule was not installed'); }); it('a set across a version skew re-stamps the installed compliance rule', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }, '3.2.0', {}, true); await installOnFiles(); await write(installedRule(), 'Active frameworks: GDPR — their controls are binding.\n'); const result = await change(manifest, 'set', { setFrameworks: ['hipaa'] }); expect(result.exitCode).toBe(0); const stamped = await fs.readFile(installedRule(), 'utf-8'); expect(stamped).toContain('Active frameworks: HIPAA'); expect(stamped).not.toContain('GDPR'); }); it('enable across a version skew with rules off installs no rule and says nothing about one', async () => { const manifest = await manifestWith({ enabled: false, frameworks: [] }, '3.2.0'); await installOnFiles(); const result = await change(manifest, 'enable'); expect(result.exitCode).toBe(0); expect(await exists(installedRule())).toBe(false); expect(result.messages.map(m => m.text).join('\n')).not.toContain('rule was not installed'); }); it.skipIf(process.getuid?.() === 0)('enable across a version skew warns when the compliance rule could not be installed', async () => { const manifest = await manifestWith({ enabled: false, frameworks: [] }, '3.2.0', {}, true); await installOnFiles(); const ruleDir = path.dirname(installedRule()); await fs.mkdir(ruleDir, { recursive: true }); // A folder that cannot take a new file: the rule install fails. await fs.chmod(ruleDir, 0o555); const result = await change(manifest, 'enable').finally(() => fs.chmod(ruleDir, 0o755)); expect(result.exitCode).toBe(0); expect(await exists(installedRule())).toBe(false); expect(result.messages.some(m => m.level === 'warn' && m.text.includes('The compliance rule was not installed'))).toBe(true); expect(result.messages.some(m => m.level === 'error')).toBe(false); }); it.each([ { design: 'off', features: {}, release: 'release ON\n' }, { design: 'on', features: { design: true }, release: 'release COMPLIANCE-OFF\n' }, ])('a compliance-off converge with no build manifest exits 1 with design $design', async ({ features, release }) => { const manifest = await manifestWith({ enabled: true, frameworks: [] }, RUNNING, { features }); await installOnFiles(); await fs.rm(path.join(pkg, 'dist', 'variant-axes.json')); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); expect(result.messages.map(m => m.text).join('\n')).toContain('cannot be named'); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe(release); }); it('disable with the commands folder a link to a folder that holds a retired command exits 1 and names that file; nothing is removed through the link', async () => { const manifest = await manifestWith({ enabled: true, frameworks: [] }); await installOnFiles(); const linked = path.join(tmp, 'elsewhere-commands'); await fs.rename(path.dirname(installedCommand('plan')), linked); await write(path.join(linked, 'dynamic-build.md'), 'a retired command of an old release\n'); await fs.symlink(linked, path.dirname(installedCommand('plan')), 'dir'); const before = await treeBytes(linked); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); expect(result.messages.map(m => m.text).join('\n')).toContain(installedCommand('dynamic-build')); expect(await treeBytes(linked), 'the folder behind the link and its files are unchanged').toEqual(before); expect((await fs.lstat(path.dirname(installedCommand('plan')))).isSymbolicLink(), 'the link itself stays').toBe(true); }); it('never mutates the manifest it is given', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); const before = JSON.stringify(manifest); await installOnFiles(); await change(manifest, 'disable'); expect(JSON.stringify(manifest)).toBe(before); }); it('known-bad probe: a devflow dir that cannot hold the manifest lock is exit 1 and changes no prompt', async () => { const manifest = await manifestWith({ enabled: true, frameworks: [] }); await installOnFiles(); await fs.rm(path.join(devflowDir, 'manifest.json')); await fs.rm(devflowDir, { recursive: true }); await write(devflowDir, 'a file where the devflow dir should be\n'); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); expect(result.messages[0]?.text).toContain('Could not record the compliance switch'); expect(result.messages[0]?.text).toContain('manifest lock could not be taken'); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe('release ON\n'); }); it('a manifest that is gone from a kept devflow dir is exit 1 with no readable install manifest, and nothing is written or converged', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); await fs.rm(path.join(devflowDir, 'manifest.json')); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); expect(result.nextState).toEqual({ enabled: true, frameworks: ['gdpr'] }); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain('Could not record the compliance switch'); expect(text).toContain('no readable install manifest'); expect(text).not.toContain('Compliance disabled'); expect(result.messages.every(m => m.level === 'error')).toBe(true); expect(await exists(path.join(devflowDir, 'manifest.json')), 'no manifest is created').toBe(false); expect(await exists(getManifestLockPath(devflowDir)), 'the lock is released').toBe(false); expect(await fs.readFile(installedCommand('release'), 'utf-8'), 'no prompt is converged').toBe('release ON\n'); }); it('a manifest write that throws is exit 1, keeps the switch as it was, and changes no prompt', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); const manifestPath = path.join(devflowDir, 'manifest.json'); const before = await fs.readFile(manifestPath, 'utf-8'); // A directory where the writer's temporary file goes makes the atomic write throw whoever runs the test. await fs.mkdir(`${manifestPath}.tmp.${process.pid}`); const result = await change(manifest, 'disable'); expect(result.exitCode).toBe(1); expect(result.nextState).toEqual({ enabled: true, frameworks: ['gdpr'] }); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain('Could not record the compliance switch'); expect(text).toContain(`${manifestPath}.tmp.${process.pid}`); expect(text).not.toContain('Compliance disabled'); expect(result.messages.every(m => m.level === 'error')).toBe(true); expect(await fs.readFile(manifestPath, 'utf-8'), 'the switch on disk is untouched').toBe(before); expect(await exists(getManifestLockPath(devflowDir)), 'the lock is released').toBe(false); expect(await fs.readFile(installedCommand('release'), 'utf-8'), 'no prompt is converged').toBe('release ON\n'); }); it.skipIf(process.getuid?.() === 0)('a switch that cannot take the manifest lock writes nothing, exits 1 and prints no success line', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }, RUNNING, { installedVariants: { curation: true, compliance: true } }); await installOnFiles(); // A folder that cannot hold the lock directory: the lock is refused at once. await fs.chmod(devflowDir, 0o555); const result = await change(manifest, 'disable').finally(() => fs.chmod(devflowDir, 0o755)); expect(result.exitCode).toBe(1); expect(result.nextState).toEqual({ enabled: true, frameworks: ['gdpr'] }); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain('Could not record the compliance switch'); expect(text).toContain('manifest lock could not be taken'); expect(text).not.toContain('Compliance disabled'); expect(result.messages.every(m => m.level === 'error')).toBe(true); expect(await complianceOf()).toEqual({ enabled: true, frameworks: ['gdpr'] }); expect(await fs.readFile(installedCommand('release'), 'utf-8'), 'no prompt is converged').toBe('release ON\n'); }); it('writes the switch from a manifest read inside the lock, keeps a field another run wrote since the first read, and releases the lock', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); // Another devflow run turns the HUD on after this command read its manifest. const file = path.join(devflowDir, 'manifest.json'); const onDisk = JSON.parse(await fs.readFile(file, 'utf-8')) as { features: Record }; await fs.writeFile(file, JSON.stringify({ ...onDisk, features: { ...onDisk.features, hud: true } }), 'utf-8'); const result = await change(manifest, 'disable'); expect(result.exitCode, warnings.join('\n')).toBe(0); const after = await readManifest(devflowDir); expect(after?.features.compliance).toEqual({ enabled: false, frameworks: ['gdpr'] }); expect(after?.features.hud, 'the field written after the first read').toBe(true); expect(await exists(getManifestLockPath(devflowDir)), 'the switch write releases the manifest lock').toBe(false); }); it.skipIf(process.getuid?.() === 0)('a disable whose rule removal fails exits 1, names the rule and prints no removed line', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }, RUNNING, { installedVariants: { curation: true, compliance: true } }); await installOnFiles(); const ruleDir = path.join(claudeDir, 'rules', 'devflow'); const ruleFile = path.join(ruleDir, 'compliance.md'); await write(ruleFile, 'the stamped compliance rule\n'); // A folder that cannot lose a file: the rule removal fails while every posting step is converged. await fs.chmod(ruleDir, 0o555); const result = await change(manifest, 'disable').finally(() => fs.chmod(ruleDir, 0o755)); expect(result.exitCode).toBe(1); expect(result.messages.every(m => m.level === 'error')).toBe(true); const text = result.messages.map(m => m.text).join('\n'); expect(text).toContain('compliance rule could not be removed'); expect(text).toContain(ruleFile); expect(text).not.toContain('Compliance disabled'); expect(text).not.toContain('every posting step removed'); expect(await complianceOf(), 'the switch is on disk whatever the rule removal did').toEqual({ enabled: false, frameworks: ['gdpr'] }); expect(await fs.readFile(installedCommand('release'), 'utf-8')).toBe('release COMPLIANCE-OFF\n'); expect(readInstalledVariants(await recordOf()), 'an unclean converge withholds the record').toEqual({ curation: true, compliance: true, tdd: true, design: false }); }); it('a disable that removes the rule prints the removed line and exits 0', async () => { const manifest = await manifestWith({ enabled: true, frameworks: ['gdpr'] }); await installOnFiles(); const ruleFile = path.join(claudeDir, 'rules', 'devflow', 'compliance.md'); await write(ruleFile, 'the stamped compliance rule\n'); const result = await change(manifest, 'disable'); expect(result.exitCode, warnings.join('\n')).toBe(0); expect(await exists(ruleFile)).toBe(false); expect(result.messages.map(m => m.text).join('\n')).toContain('every posting step removed'); }); }); // ── The posting record and the --status Posting line (AC-31) ─────────────────── describe('readInstalledVariants, postingStatus and postingStatusLine', () => { const CASES: ReadonlyArray<{ readonly record: unknown; readonly enabled: boolean; readonly status: string }> = [ { record: { curation: true, compliance: true }, enabled: true, status: 'installed' }, { record: { curation: false, compliance: false }, enabled: false, status: 'not installed' }, { record: { curation: true, compliance: true }, enabled: false, status: 'mismatch (run devflow init)' }, { record: { curation: true, compliance: false }, enabled: true, status: 'mismatch (run devflow init)' }, { record: undefined, enabled: false, status: 'unknown (run devflow init)' }, { record: { curation: true }, enabled: true, status: 'unknown (run devflow init)' }, { record: { curation: 'yes', compliance: true }, enabled: true, status: 'unknown (run devflow init)' }, { record: [true, true], enabled: true, status: 'unknown (run devflow init)' }, { record: null, enabled: false, status: 'unknown (run devflow init)' }, ]; for (const { record, enabled, status } of CASES) { it(`${JSON.stringify(record) ?? 'absent'} with compliance ${enabled ? 'on' : 'off'} reads ${status}`, () => { expect(postingStatus(readInstalledVariants(record), enabled)).toBe(status); expect(plain(postingStatusLine(record, enabled))).toBe(`Posting: ${status}`); }); } it('readInstalledVariants returns a fresh record of one boolean per axis and ignores extra keys', () => { const raw = { curation: false, compliance: true, tdd: false, design: false, future: 'axis' }; expect(readInstalledVariants(raw)).toEqual({ curation: false, compliance: true, tdd: false, design: false }); }); it('a record written before the tdd axis existed reads as tdd on, and a record with a tdd value that is not a boolean is unknown', () => { expect(readInstalledVariants({ curation: false, compliance: true })).toEqual({ curation: false, compliance: true, tdd: true, design: false }); expect(readInstalledVariants({ curation: true, compliance: true, tdd: 'off' })).toBeNull(); expect(readInstalledVariants({ curation: true, compliance: true, tdd: null })).toBeNull(); }); it('defaults an absent design record to off and accepts only booleans on that axis', () => { const legacy = { curation: false, compliance: true, tdd: true }; expect(readInstalledVariants(legacy)).toEqual({ ...legacy, design: false }); expect(readInstalledVariants({ ...legacy, design: true })).toEqual({ ...legacy, design: true }); expect(readInstalledVariants({ ...legacy, design: 'true' })).toBeNull(); }); it('an axis that existed in the first record is still required: a record without compliance is unknown', () => { expect(readInstalledVariants({ curation: true, tdd: true })).toBeNull(); expect(readInstalledVariants({ compliance: true, tdd: true })).toBeNull(); }); }); // ── The install record decision (AC-29) ──────────────────────────────────────── describe('installRecordFor', () => { it('records the switches the prompts were converged onto when the converge finished clean', () => { expect(installRecordFor(true, { ...ALL_SWITCHES_ON, curation: false })).toEqual({ curation: false, compliance: true, tdd: true, design: true }); expect(installRecordFor(true, { ...ALL_SWITCHES_ON, compliance: false })).toEqual({ curation: true, compliance: false, tdd: true, design: true }); expect(installRecordFor(true, { ...ALL_SWITCHES_ON, tdd: false, design: true })).toEqual({ curation: true, compliance: true, tdd: false, design: true }); }); it('withholds the record after a converge that did not finish clean', () => { expect(installRecordFor(false, { ...ALL_SWITCHES_ON, compliance: false })).toBeNull(); }); it('returns a fresh record, not the switches object it was given', () => { const switches = { curation: true, compliance: true, tdd: true, design: true }; const record = installRecordFor(true, switches); expect(record).not.toBe(switches); expect(readInstalledVariants(record)).toEqual(switches); }); });