graceful-handoff/tests/skill-structure.test.mjs
Kjell Tore Guttormsen 5334097c84 fix(graceful-handoff): two defects found by the first real smoke test (v3.2.1)
The pipeline had never been run against an actual repository — every test in
the suite is either a prose-grep over SKILL.md or a unit test that asserts key
presence. Running it against scratch repos (private remote, and public `open/`
remote with and without a gitignored STATE.md) found two defects living under
a 42/42-green suite.

1. dirty_files truncated the first path. gitOk() trims every command's output,
   but `git status --porcelain` puts the worktree status in column 2, so a
   modified-but-unstaged file is " M path". The trim ate the leading space and
   the fixed slice(3) then ate the first character: app.js was reported as
   pp.js. Only the first line is affected, which is why it survived — no test
   asserted dirty_files VALUES, only that the key existed. Porcelain now goes
   through a non-trimming gitOkRaw().

2. The commit message claimed a STATE.md update it did not contain. The
   message was hardcoded to "oppdater STATE.md" regardless of what was staged.
   On every `open/` repo STATE.md is gitignored, so the handoff commit carries
   only the --also paths. Git history is the regime's long-term log; it was
   systematically wrong about its own contents.

Also promotes the leak condition from advisory to hard gate. A public remote
whose STATE.md is not yet gitignored is the state a FRESH open/ repo starts
in, and should_commit_state was true there — the ritual only mentioned
leak_warning, then committed. It now lands in errors[] (step 2 stops on a
non-empty errors[]), should_commit_state is false, and --commit refuses to
stage STATE.md. Explicit --also paths are still honoured: the gate protects
STATE.md, not the commit as a whole.

And corrects SKILL.md's justification for the single-line rule. It claimed a
wrapped rationale= replaces the board's next step with garbage; board.sh in
repo-mailbox 0.20.3 tracks a comment to its closer, so that no longer follows.
The rule stands, restated with the risk that is still real: a rationale
containing the closer sequence ends its own comment early.

Tests 42 -> 48, all six written failing first.

Still unverified: that /graceful-handoff loads as a user command (#26251), and
that a cross-plugin Skill invocation of repo-mailbox:route passes from a
sub-scoped skill. Both need the catalog ref bumped so the version is installed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvLY4FwbzY157oVqwnkHD8
2026-08-09 21:49:08 +02:00

164 lines
6.1 KiB
JavaScript

// skill-structure.test.mjs — Verifies SKILL.md frontmatter (v3.0) and commands/ deletion.
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { existsSync, readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PLUGIN_ROOT = join(__dirname, '..');
const SKILL = join(PLUGIN_ROOT, 'skills', 'graceful-handoff', 'SKILL.md');
const REPO_CLAUDE_MD = join(PLUGIN_ROOT, 'CLAUDE.md');
function skill() {
return readFileSync(SKILL, 'utf-8');
}
test('SKILL.md exists at expected path', () => {
assert.ok(existsSync(SKILL), `SKILL.md missing at ${SKILL}`);
});
test('commands/ directory is deleted (hard cut to skills/)', () => {
assert.ok(!existsSync(join(PLUGIN_ROOT, 'commands')), 'commands/ should be deleted');
});
test('hooks/ directory is deleted (v3.0 removed all hooks)', () => {
assert.ok(!existsSync(join(PLUGIN_ROOT, 'hooks')), 'hooks/ should be deleted in v3.0');
});
test('SKILL.md has disable-model-invocation: true', () => {
assert.match(skill(), /^disable-model-invocation: true$/m);
});
test('SKILL.md has NO model: pin (inherits session model for quality synthesis)', () => {
const fm = skill().match(/^---\n[\s\S]*?\n---/)[0];
assert.doesNotMatch(fm, /^model:/m);
});
test('SKILL.md allowed-tools is Bash sub-scoped and includes Write', () => {
const line = skill().match(/^allowed-tools:.*$/m);
assert.ok(line, 'allowed-tools line missing');
assert.match(line[0], /Bash\(git:\*\)/);
assert.match(line[0], /Bash\(node:\*\)/);
assert.match(line[0], /\bWrite\b/);
});
test('SKILL.md does not pre-approve curl or wget', () => {
const line = skill().match(/^allowed-tools:.*$/m);
assert.ok(line, 'allowed-tools line missing');
assert.doesNotMatch(line[0], /\bcurl\b/);
assert.doesNotMatch(line[0], /\bwget\b/);
});
test('SKILL.md body references handoff-pipeline.mjs', () => {
assert.match(skill(), /handoff-pipeline\.mjs/);
});
test('SKILL.md body mandates the 👉 NESTE — START HER block', () => {
assert.match(skill(), /👉 NESTE — START HER/);
});
test('SKILL.md body has Tidsbudsjett (time budget) note', () => {
assert.match(skill(), /Tidsbudsjett/);
});
test('SKILL.md is STATE.md-centric (overwrites the nearest STATE.md)', () => {
const s = skill();
assert.match(s, /STATE\.md/);
assert.match(s, /overskriv/i);
});
// --- v3.2.0: the ritual matches the global CLAUDE.md session-end mechanism ---
test('SKILL.md allowed-tools includes Skill (needed to invoke repo-mailbox:route)', () => {
const line = skill().match(/^allowed-tools:.*$/m);
assert.ok(line, 'allowed-tools line missing');
assert.match(line[0], /\bSkill\b/);
});
test('SKILL.md NESTE template carries all three STATE header comment lines', () => {
const s = skill();
assert.match(s, /<!-- board: status=/);
assert.match(s, /<!-- route: path=/);
assert.match(s, /<!-- route-last: model=/);
});
test('SKILL.md board line template uses the closed status token set', () => {
assert.match(skill(), /planned.*in-progress.*blocked.*deferred.*done/);
});
test('SKILL.md warns that the three comment lines must stay single-line (board.sh)', () => {
const s = skill();
assert.match(s, /board\.sh/);
assert.match(s, /ÉN linje/);
});
test('the single-line rule no longer rests on a consequence board.sh has since fixed', () => {
// repo-mailbox 0.20.3's board.sh tracks an HTML comment to its closer, so a
// wrapped rationale no longer leaks comment internals into the board excerpt.
// The rule stands; the stated consequence had to stop being the reason.
const s = skill();
assert.doesNotMatch(s, /erstatter derfor neste steg på tavlen med søppel/);
assert.match(s, /closer/, 'must name the real remaining risk (a rationale containing the closer)');
});
test('SKILL.md hard-gates the leak case instead of merely mentioning it', () => {
const s = skill();
const step2 = s.slice(s.indexOf('### 2.'), s.indexOf('### 3.'));
assert.match(step2, /leak_warning/);
assert.match(step2, /STOPP|stopp/, 'step 2 must stop the ritual, not just report');
assert.match(step2, /\.gitignore/, 'step 2 must name the remedy');
});
test('SKILL.md routes the next session via the repo-mailbox route skill', () => {
assert.match(skill(), /repo-mailbox:route/);
});
test('the route step runs BEFORE STATE.md is written (lines are spliced into the file)', () => {
const s = skill();
const routeStep = s.indexOf('repo-mailbox:route');
const writeStep = s.indexOf('UFRAVIKELIG FORMAT');
assert.ok(routeStep > -1 && writeStep > -1, 'both steps must exist');
assert.ok(
routeStep < writeStep,
'route must be scored before the STATE.md Write, else the commit is dirtied afterwards'
);
});
test('SKILL.md documents a fallback when repo-mailbox is not installed', () => {
assert.match(skill(), /ikke installert/);
});
test('SKILL.md closing line requires all six mandatory fields', () => {
const s = skill();
for (const field of [
'STATE.md:',
'Innboks:',
'Neste sesjon:',
'Commit/push:',
'Modell neste økt:',
'Oppstartskommando:',
]) {
assert.ok(s.includes(`**${field}**`), `closing-line field missing: ${field}`);
}
});
test('SKILL.md no longer claims the closing line has three items', () => {
assert.doesNotMatch(skill(), /eksakt tre ting/);
});
test('SKILL.md forbids asserting an empty inbox that was never seen', () => {
assert.match(skill(), /Innboks/);
assert.match(skill(), /ingen innboks ble injisert/i);
});
test('repo CLAUDE.md points at SKILL.md as the STATE-format authority (one copy)', () => {
const c = readFileSync(REPO_CLAUDE_MD, 'utf-8');
const section = c.match(/## STATE\.md-format[\s\S]*?(?=\n## )/);
assert.ok(section, 'STATE.md-format section missing from repo CLAUDE.md');
assert.match(section[0], /SKILL\.md/, 'section must name SKILL.md as the authority');
assert.match(section[0], /Ikke gjenopprett/, 'section must forbid restoring a second copy');
// The template must NOT be restated here — two copies drift (the model-rubric precedent).
assert.doesNotMatch(section[0], /# STATE — </, 'the template itself must not reappear here');
});