config-audit/tests/scanners/campaign-export-cli.test.mjs
Kjell Tore Guttormsen 749b710de7 feat(scanners): the write gate now runs in code, not in the templates' prose
`write-scope.mjs` has existed since M-BUG-41, but only one writer ever called
it. Measured 2026-08-12: 9 files under `scanners/` write to disk, 1 imported
the gate; 21 command templates, 17 mention a write, 5 call `write-scope-cli`.
Five templates paraphrasing one policy is the shape that put the lever table in
five copies (#61) — one level up.

The defect was never "8 ungated writers = 8 bugs". Four of them write the
plugin's own bookkeeping and must STAY ungated: a gate that fires on every run
gets switched off, and then it guards nothing. The defect is that nothing
declared WHICH, so the question was answered by reading, and answered
differently each time it was asked.

`tests/lib/write-gate-coverage.test.mjs` makes the answer structural: every
writer either imports the gate or holds an EXEMPT entry naming where the bytes
land. Seen RED against today's tree before the fix (4 ungated writers), and
each of its four assertions was separately seen red against its own defect.

Two premises in the plan text were falsified by measuring them first:

  - `scan-orchestrator` was carried as "plugin-managed, legitimately exempt".
    `--save-baseline` derives its path from the SCAN TARGET, so `--global`
    lands `~/.claude/.config-audit-baseline.json` — user-scope, require-ok. It
    is gated. `lib/baseline.mjs` is the genuinely exempt one.
  - the first sweep scored 9 writers with a regex that could not match
    `writeFileSync(`, so `lib/backup.mjs` — a real writer — read as clean. The
    guard covers sync and async forms, strips comments before matching, and
    asserts non-emptiness so a regex that stops matching cannot make every
    other assertion vacuously green (#63, #64).

Gated: fix-engine, rollback-engine, campaign-export-cli, scan-orchestrator.
All five call sites share ONE reduction, `evaluateWriteTargets` — four copies
of classify/strongestGate/dedup is the drift this exists to prevent.

`campaign export` still DISCLOSES rather than refuses: cross-repo is by design
there, and tightening it into a refusal would break the feature. A dry run is
still not a write, so it is never gated (#63). A refusal is a verdict about a
config that WAS examined, so it rides in the payload and keeps the 0/1/2 exit
contract (#62) — and the verdict now reaches the success payload too, since
stderr is discarded by `2>/dev/null` (F3's class).

commands/fix.md carries `--approve-scope` from the answer the user gives, with
the rule stated where it can be read: classifying is not approving.

Dogfooded end to end: a target outside the session root refuses with zero bytes
written, then applies under `--approve-scope`.

Suite 1703 -> 1707/0. Frozen v5.0.0 + default-output snapshots: 0 changed files.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pkn22uGCgk6QZA738zNmHL
2026-08-12 21:15:16 +02:00

159 lines
6.8 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { resolve, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { execFileSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { createLedger, addRepo, setRepoStatus, saveLedger } from '../../scanners/lib/campaign-ledger.mjs';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const CLI = resolve(__dirname, '../../scanners/campaign-export-cli.mjs');
const NOW = '2026-06-23';
const SESSION = '20260623_101500';
const PLAN_BODY = '## Executive summary\n\n2 actions, 1 auto-fixable.\n\n## Action 1\nFix the thing.\n';
// Every test points --ledger-file + --sessions-dir at temp dirs, so the CLI never touches the
// real ~/.claude/ — hermetic by construction.
function runCli(extraArgs) {
try {
const stdout = execFileSync('node', [CLI, ...extraArgs], { encoding: 'utf-8', timeout: 15000 });
return { status: 0, stdout };
} catch (err) {
return { status: err.status, stdout: err.stdout || '', stderr: err.stderr || '' };
}
}
/**
* Build a hermetic world: a temp dir holding the ledger + a sessions/ store + a repo/ dir.
* `opts.withSession` links a sessionId; `opts.withPlan` writes that session's action-plan.md.
*/
function world({ withSession = true, withPlan = true } = {}) {
const root = mkdtempSync(join(tmpdir(), 'camp-exp-'));
const sessionsDir = join(root, 'sessions');
const repoDir = join(root, 'repo');
mkdirSync(sessionsDir, { recursive: true });
mkdirSync(repoDir, { recursive: true });
// A repo in the ledger is a real repo. Without the marker the export target
// is in no project at all, which the Q1 scope gate classifies `require-ok`
// and withholds — so a fixture missing `.git` would be testing a case the
// ledger cannot produce, and would hide the by-design cross-repo path.
mkdirSync(join(repoDir, '.git'), { recursive: true });
let l = createLedger({ now: NOW });
l = addRepo(l, { path: repoDir, name: 'repo' }, { now: NOW });
l = setRepoStatus(l, repoDir, 'planned', {
now: NOW,
findingsBySeverity: { critical: 0, high: 1, medium: 0, low: 2 },
sessionId: withSession ? SESSION : undefined,
});
const ledgerFile = join(root, 'campaign-ledger.json');
// saveLedger is async via the lib; call synchronously through writeFileSync to keep tests simple.
writeFileSync(ledgerFile, `${JSON.stringify(l, null, 2)}\n`);
if (withSession && withPlan) {
mkdirSync(join(sessionsDir, SESSION), { recursive: true });
writeFileSync(join(sessionsDir, SESSION, 'action-plan.md'), PLAN_BODY);
}
return { root, sessionsDir, repoDir, ledgerFile };
}
const baseArgs = (w) => ['--ledger-file', w.ledgerFile, '--sessions-dir', w.sessionsDir, '--reference-date', NOW];
describe('campaign-export-cli — preview (read-only default)', () => {
it('exits 0 and returns an exportable preview without writing', () => {
const w = world();
const { status, stdout } = runCli([...baseArgs(w), '--repo', w.repoDir]);
assert.equal(status, 0);
const out = JSON.parse(stdout);
assert.equal(out.exportable, true);
assert.equal(out.written, false);
assert.deepEqual(out.problems, []);
assert.equal(out.targetPath, join(w.repoDir, 'docs', `config-audit-plan-${SESSION}.md`));
assert.ok(out.document.startsWith('# Config-Audit Action Plan — repo'));
assert.ok(out.document.includes('## Action 1\nFix the thing.'));
// preview must NOT have created the file
assert.equal(existsSync(out.targetPath), false);
});
});
describe('campaign-export-cli — --write', () => {
it('writes a byte-exact copy of the assembled document into the repo docs/', () => {
const w = world();
const preview = JSON.parse(runCli([...baseArgs(w), '--repo', w.repoDir]).stdout);
const { status, stdout } = runCli([...baseArgs(w), '--repo', w.repoDir, '--write']);
assert.equal(status, 0);
const out = JSON.parse(stdout);
assert.equal(out.written, true);
assert.ok(existsSync(out.targetPath));
// file on disk equals the document the preview reported (no drift)
assert.equal(readFileSync(out.targetPath, 'utf-8'), preview.document);
});
it('creates the docs/ dir if missing', () => {
const w = world();
const out = JSON.parse(runCli([...baseArgs(w), '--repo', w.repoDir, '--write']).stdout);
assert.ok(existsSync(join(w.repoDir, 'docs')));
assert.ok(existsSync(out.targetPath));
});
});
describe('campaign-export-cli — gates (advisory exit 1)', () => {
it('exits 1 with no-session-linked when the repo has no sessionId', () => {
const w = world({ withSession: false });
const { status, stdout } = runCli([...baseArgs(w), '--repo', w.repoDir]);
assert.equal(status, 1);
const out = JSON.parse(stdout);
assert.equal(out.exportable, false);
assert.deepEqual(out.problems, ['no-session-linked']);
assert.equal(out.written, false);
});
it('exits 1 with no-action-plan when the linked session has no action-plan.md', () => {
const w = world({ withPlan: false });
const { status, stdout } = runCli([...baseArgs(w), '--repo', w.repoDir]);
assert.equal(status, 1);
const out = JSON.parse(stdout);
assert.equal(out.exportable, false);
assert.deepEqual(out.problems, ['no-action-plan']);
});
it('--write writes nothing when the repo is not exportable', () => {
const w = world({ withPlan: false });
const { status, stdout } = runCli([...baseArgs(w), '--repo', w.repoDir, '--write']);
assert.equal(status, 1);
assert.equal(JSON.parse(stdout).written, false);
assert.equal(existsSync(join(w.repoDir, 'docs')), false);
});
});
describe('campaign-export-cli — errors (exit 3)', () => {
it('exits 3 when --repo is missing', () => {
const w = world();
assert.equal(runCli(baseArgs(w)).status, 3);
});
it('exits 3 when the repo is not tracked in the ledger', () => {
const w = world();
assert.equal(runCli([...baseArgs(w), '--repo', join(w.root, 'other')]).status, 3);
});
it('exits 3 when no ledger file exists', () => {
const w = world();
const status = runCli(['--ledger-file', join(w.root, 'nope.json'), '--sessions-dir', w.sessionsDir, '--repo', w.repoDir]).status;
assert.equal(status, 3);
});
});
describe('campaign-export-cli — --output-file', () => {
it('writes the payload JSON to the file and stays silent on stdout', () => {
const w = world();
const out = join(w.root, 'report.json');
const { stdout } = runCli([...baseArgs(w), '--repo', w.repoDir, '--output-file', out]);
assert.equal(stdout.trim(), '');
const written = JSON.parse(readFileSync(out, 'utf-8'));
assert.equal(written.exportable, true);
assert.equal(written.targetPath, join(w.repoDir, 'docs', `config-audit-plan-${SESSION}.md`));
});
});