ktg-plugin-marketplace/plugins/config-audit/tests/snapshot-default-output.test.mjs
Kjell Tore Guttormsen fc8808d6e4 docs(humanizer): v5.1.0 release notes across plugin + marketplace docs
- Plugin README: add "What's New in v5.1.0" section with humanizer overview,
  before/after example, plain-language vocabulary table, --raw flag docs.
  Bump version badge 5.0.0 → 5.1.0. Add Version History row.
- Plugin CLAUDE.md: add humanizer.mjs + humanizer-data.mjs to Scanner Lib
  table. Add "Plain-Language Output (v5.1.0)" section documenting output
  modes, vocabularies, and Wave 5 lessons. Bump test count 635 → 792 across
  52 test files.
- Marketplace root README: bump config-audit entry 5.0.0 → 5.1.0, update
  one-line description to mention plain-language UX, add bullet for the
  v5.1.0 humanizer, bump test count 635+ → 792+.

Test-normalizer hardening (consequence of growing CLAUDE.md):
walkClaudeMdCascade walks upward from the marketplace-medium fixture into
this plugin's own CLAUDE.md, so any docs edit ripples into
`scanners[*].activeConfig.claudeMdEstimatedTokens`. The v5.0.0 byte-stability
contract is about scanner internals being unchanged, not ancestor input
content being frozen. Normalizers in json-backcompat, raw-backcompat,
posture-humanizer, scan-orchestrator-humanizer, and snapshot-default-output
now strip claudeMdEstimatedTokens to <ANCESTOR_DERIVED>. The
default-output snapshot for scan-orchestrator was re-seeded via
UPDATE_SNAPSHOT=1 (intent: Wave 6 docs additions; humanizer prose
unchanged).

Verify:
- grep -E "5\.1\.0|v5\.1\.0" README.md CLAUDE.md ../../README.md | wc -l = 12
- node --test 'tests/**/*.test.mjs' = 792/792 pass
- self-audit configGrade A (97), pluginGrade A (100), readmeCheck.passed true
2026-05-01 20:35:24 +02:00

171 lines
5.5 KiB
JavaScript

/**
* SC-5 — default-output snapshot test (Wave 4 Step 12).
*
* Captures the humanized stdout of three representative CLIs running in
* default mode against tests/fixtures/marketplace-medium and asserts
* byte-equal output against tests/snapshots/default-output/<cli>.json.
*
* Set UPDATE_SNAPSHOT=1 to seed or refresh a snapshot. Subsequent runs
* assert byte-equal — any drift fails the test, so humanizer prose
* changes must be intentional and re-approved by re-running with
* UPDATE_SNAPSHOT=1.
*
* Time-varying fields are normalized before comparison (timestamp,
* target path, duration_ms). Humanizer-added prose fields
* (titleHumanized / descriptionHumanized / recommendationHumanized,
* userImpactCategory, userActionLanguage, relevanceContext) are kept —
* they are the contract being snapshotted.
*/
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { readFile, writeFile } from 'node:fs/promises';
const exec = promisify(execFile);
const __dirname = dirname(fileURLToPath(import.meta.url));
const REPO = resolve(__dirname, '..');
const FIXTURE = resolve(REPO, 'tests/fixtures/marketplace-medium');
const SNAPSHOT_DIR = resolve(REPO, 'tests/snapshots/default-output');
const UPDATE = process.env.UPDATE_SNAPSHOT === '1';
async function runCli(scriptPath, args) {
try {
const { stdout, stderr } = await exec('node', [scriptPath, ...args], {
timeout: 60000,
cwd: REPO,
maxBuffer: 10 * 1024 * 1024,
});
return { stdout: stdout || '', stderr: stderr || '' };
} catch (err) {
return { stdout: err.stdout || '', stderr: err.stderr || '' };
}
}
// ---------------------------------------------------------------------------
// Normalizers — same shape per CLI as json-backcompat / cli-humanizer tests.
// ---------------------------------------------------------------------------
function normalizeScanOrchestrator(env) {
const out = JSON.parse(JSON.stringify(env));
if (out.meta) {
out.meta.target = '<TARGET>';
out.meta.timestamp = '<TIMESTAMP>';
}
if (Array.isArray(out.scanners)) {
for (const s of out.scanners) {
s.duration_ms = 0;
// claudeMdEstimatedTokens reflects walkClaudeMdCascade walking up from
// the fixture into this plugin's own CLAUDE.md; any docs edit ripples
// into it independently of scanner internals. Strip to keep the
// default-output snapshot focused on humanizer prose stability.
if (s.activeConfig && 'claudeMdEstimatedTokens' in s.activeConfig) {
s.activeConfig.claudeMdEstimatedTokens = '<ANCESTOR_DERIVED>';
}
}
}
return out;
}
function normalizeTokenHotspots(p) {
const out = JSON.parse(JSON.stringify(p));
out.duration_ms = 0;
return out;
}
const CLIS = [
{
name: 'scan-orchestrator',
script: 'scanners/scan-orchestrator.mjs',
snapshotName: 'scan-orchestrator.json',
normalize: normalizeScanOrchestrator,
captureStream: 'stdout',
},
{
name: 'token-hotspots',
script: 'scanners/token-hotspots-cli.mjs',
snapshotName: 'token-hotspots.json',
normalize: normalizeTokenHotspots,
captureStream: 'stdout',
},
{
name: 'posture',
script: 'scanners/posture.mjs',
snapshotName: 'posture.json',
// Posture default mode emits the humanized scorecard to stderr; stdout is
// empty unless --json/--raw. Snapshot the scorecard text.
normalize: (s) => s.replace(/\(\d+ms\)/g, '(0ms)'),
captureStream: 'stderr-text',
},
];
async function captureForCli(cli) {
const script = resolve(REPO, cli.script);
const { stdout, stderr } = await runCli(script, [FIXTURE]);
if (cli.captureStream === 'stdout') {
const parsed = JSON.parse(stdout);
return {
kind: 'json',
payload: cli.normalize(parsed),
};
}
if (cli.captureStream === 'stderr-text') {
return {
kind: 'text',
payload: cli.normalize(stderr.trim()),
};
}
throw new Error(`unknown captureStream: ${cli.captureStream}`);
}
async function loadSnapshot(snapshotPath) {
const raw = await readFile(snapshotPath, 'utf8');
// Snapshot files are stored as JSON envelopes — text snapshots are wrapped
// as { kind: 'text', payload: '...' } so all snapshots look uniform on disk.
return JSON.parse(raw);
}
async function writeSnapshot(snapshotPath, captured) {
const serialized = JSON.stringify(captured, null, 2) + '\n';
await writeFile(snapshotPath, serialized, 'utf8');
}
describe('SC-5 default-output snapshot test', () => {
for (const cli of CLIS) {
it(`${cli.name} default mode matches tests/snapshots/default-output/${cli.snapshotName}`, async () => {
const captured = await captureForCli(cli);
const snapshotPath = resolve(SNAPSHOT_DIR, cli.snapshotName);
if (UPDATE) {
await writeSnapshot(snapshotPath, captured);
return;
}
let expected;
try {
expected = await loadSnapshot(snapshotPath);
} catch (err) {
if (err.code === 'ENOENT') {
assert.fail(
`Snapshot missing: ${snapshotPath}. ` +
`Re-run with UPDATE_SNAPSHOT=1 to seed it.`,
);
}
throw err;
}
assert.deepStrictEqual(
captured,
expected,
`${cli.name}: default-output drift detected. ` +
`If intentional, re-run with UPDATE_SNAPSHOT=1.`,
);
});
}
});