The windows-test-parity ratchet greps test source for fs.rmSync-without-
maxRetries (and six other Windows-portability anti-patterns), failing when an
integer offender COUNT exceeds a frozen baseline (rmSync: 95). A count ratchet
is a Goodhart metric: fixing one offender and adding another keeps the count
constant, so a new defect slips through green. Replace it — and every other
count ratchet in the repo — with a layered, masking-proof design.
Behavioral seam test
- tests/helpers-cleanup.test.cjs proves helpers.cleanup() carries the Windows
EBUSY retry budget. cleanup() delegates retries to Node's fs.rmSync via
maxRetries (it owns no loop), so the test asserts the option contract
(recursive/force/maxRetries>0/retryDelay>0) + real-FS removal + the cwd-guard,
rather than a loop that does not exist. The EBUSY risk is now tested ONCE at
the helper, not approximated textually at every call site.
Write-time ESLint rule (AST-accurate, replaces the grep)
- eslint-rules/no-raw-rmsync-in-tests.cjs (error in tests/**/*.test.cjs) bans
raw fs.rmSync, steering to cleanup(). Catches member, computed (fs['rmSync']),
destructured and aliased forms; escape hatch is inline
`// eslint-disable-next-line local/no-raw-rmsync-in-tests -- <reason>` only.
- Migrated 336 raw fs.rmSync teardown calls across ~116 test files to cleanup().
~18 genuinely load-bearing sites (mid-test SUT/fault-injection removals,
error-swallowing or name-colliding local teardown helpers) keep the raw call
with an inline eslint-disable + reason.
Shared anti-ratchet primitive
- scripts/lib/allowlist-ratchet.cjs:
- assertWithinAllowlist: fails on NOVEL ids (new offender introduced) AND on
STALE ids (a known offender was fixed but not pruned) — identity, not count,
and a ratchet DOWN toward zero.
- assertTightCeiling: a size/length budget whose ceiling must stay within a
grace band of the high-water mark, so budgets may only tighten, never creep.
Ratchets converted onto the primitive
- windows-test-parity-guard.test.cjs: rmSync rule deleted (now ESLint-enforced);
the remaining six patterns moved from integer baselines to named-set
allowlists with ratchet-down.
- scripts/lint-test-file-count.{cjs,allowlist.json}: per-module integer counts →
named filename sets (closes the swap-a-file-keep-the-count blind spot); a
module dropping under cap now FAILS to force pruning its allowlist entry.
- enh-2790 skill-count `<= 63` → named skill allowlist (ratchets toward ~58).
Size budgets hardened (tighten-only)
- agent-size / workflow-size / feat-3039 help-tiered: ceilings lowered to the
current high-water mark and an assertTightCeiling anti-creep check added per
tier. Fixed external-contract limits (description ≤100 chars, agent ≤100 KB)
are intentionally left as-is — they are not grandfathered creeping budgets.
No user-facing behavior change (tests + tooling only); no USER_FACING_PREFIXES
touched, so no changeset fragment is required.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
346 lines
13 KiB
JavaScript
346 lines
13 KiB
JavaScript
/**
|
|
* GSD Tools Tests - Agent Skills Injection
|
|
*
|
|
* CLI integration tests for the `agent-skills` command that reads
|
|
* `agent_skills` from .planning/config.json and returns a formatted
|
|
* skills block for injection into Task() prompts.
|
|
*
|
|
* Migrated (#455): uses `--json` flag to get typed IR
|
|
* { agent_type, block, skills_count }
|
|
* instead of asserting on raw XML output text.
|
|
*/
|
|
|
|
const { test, describe, beforeEach, afterEach } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { runGsdTools, createTempProject, cleanup } = require('./helpers.cjs');
|
|
|
|
// ─── helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
function writeConfig(tmpDir, obj) {
|
|
const configPath = path.join(tmpDir, '.planning', 'config.json');
|
|
fs.writeFileSync(configPath, JSON.stringify(obj, null, 2), 'utf-8');
|
|
}
|
|
|
|
function readConfig(tmpDir) {
|
|
const configPath = path.join(tmpDir, '.planning', 'config.json');
|
|
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
}
|
|
|
|
// Run agent-skills with --json for typed IR assertions
|
|
function runAgentSkillsJson(args, tmpDir, env) {
|
|
// Insert --json after 'agent-skills' subcommand
|
|
const allArgs = Array.isArray(args) ? args : [args];
|
|
const cmdIdx = allArgs.indexOf('agent-skills');
|
|
const withJson = [...allArgs];
|
|
if (cmdIdx !== -1) {
|
|
withJson.splice(cmdIdx + 1, 0, '--json');
|
|
}
|
|
const result = runGsdTools(withJson, tmpDir, env || { HOME: tmpDir, USERPROFILE: tmpDir });
|
|
if (!result.success) return { success: false, error: result.error, ir: null };
|
|
try {
|
|
return { success: true, ir: JSON.parse(result.output) };
|
|
} catch (e) {
|
|
return { success: false, error: `JSON parse failed: ${e.message} output=${result.output}`, ir: null };
|
|
}
|
|
}
|
|
|
|
// ─── agent-skills command ────────────────────────────────────────────────────
|
|
|
|
describe('agent-skills command', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('returns empty block when no config exists', () => {
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.agent_type, 'gsd-executor');
|
|
assert.strictEqual(r.ir.block, '', 'block must be empty when no skills configured');
|
|
});
|
|
|
|
test('returns empty block when config has no agent_skills section', () => {
|
|
writeConfig(tmpDir, { model_profile: 'balanced' });
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.block, '');
|
|
});
|
|
|
|
test('returns empty block for unconfigured agent type', () => {
|
|
writeConfig(tmpDir, {
|
|
agent_skills: {
|
|
'gsd-executor': ['skills/test-skill'],
|
|
},
|
|
});
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-planner'], tmpDir);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.agent_type, 'gsd-planner');
|
|
assert.strictEqual(r.ir.block, '');
|
|
});
|
|
|
|
test('returns block containing agent_skills XML for configured agent', () => {
|
|
const skillDir = path.join(tmpDir, 'skills', 'test-skill');
|
|
fs.mkdirSync(skillDir, { recursive: true });
|
|
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '# Test Skill\n');
|
|
|
|
writeConfig(tmpDir, {
|
|
agent_skills: {
|
|
'gsd-executor': ['skills/test-skill'],
|
|
},
|
|
});
|
|
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.agent_type, 'gsd-executor');
|
|
assert.ok(r.ir.block.includes('<agent_skills>'), `block must contain <agent_skills> tag, got: ${r.ir.block}`);
|
|
assert.ok(r.ir.block.includes('</agent_skills>'), 'block must contain closing tag');
|
|
assert.ok(r.ir.block.includes('skills/test-skill/SKILL.md'), 'block must contain skill path');
|
|
});
|
|
|
|
test('skills_count reflects configured skill paths for agent type', () => {
|
|
const skillDir = path.join(tmpDir, 'skills', 'test-skill');
|
|
fs.mkdirSync(skillDir, { recursive: true });
|
|
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '# Test Skill\n');
|
|
|
|
writeConfig(tmpDir, {
|
|
agent_skills: {
|
|
'gsd-executor': ['skills/test-skill'],
|
|
},
|
|
});
|
|
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.skills_count, 1, 'skills_count must be 1 for single configured skill path');
|
|
});
|
|
|
|
test('returns block for configured agent with single string path', () => {
|
|
const skillDir = path.join(tmpDir, 'skills', 'my-skill');
|
|
fs.mkdirSync(skillDir, { recursive: true });
|
|
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '# My Skill\n');
|
|
|
|
writeConfig(tmpDir, {
|
|
agent_skills: {
|
|
'gsd-executor': 'skills/my-skill',
|
|
},
|
|
});
|
|
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.ok(r.ir.block.includes('skills/my-skill/SKILL.md'), 'block must contain skill path');
|
|
assert.strictEqual(r.ir.skills_count, 1, 'skills_count must be 1 for single string path');
|
|
});
|
|
|
|
test('handles multiple skill paths', () => {
|
|
const skill1 = path.join(tmpDir, 'skills', 'skill-a');
|
|
const skill2 = path.join(tmpDir, 'skills', 'skill-b');
|
|
fs.mkdirSync(skill1, { recursive: true });
|
|
fs.mkdirSync(skill2, { recursive: true });
|
|
fs.writeFileSync(path.join(skill1, 'SKILL.md'), '# Skill A\n');
|
|
fs.writeFileSync(path.join(skill2, 'SKILL.md'), '# Skill B\n');
|
|
|
|
writeConfig(tmpDir, {
|
|
agent_skills: {
|
|
'gsd-executor': ['skills/skill-a', 'skills/skill-b'],
|
|
},
|
|
});
|
|
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.ok(r.ir.block.includes('skills/skill-a/SKILL.md'), 'block must contain first skill');
|
|
assert.ok(r.ir.block.includes('skills/skill-b/SKILL.md'), 'block must contain second skill');
|
|
assert.strictEqual(r.ir.skills_count, 2, 'skills_count must be 2 for two configured paths');
|
|
});
|
|
|
|
test('warns for nonexistent skill path but does not error', () => {
|
|
writeConfig(tmpDir, {
|
|
agent_skills: {
|
|
'gsd-executor': ['skills/nonexistent'],
|
|
},
|
|
});
|
|
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(r.success, 'Command should succeed even with missing skill paths');
|
|
assert.strictEqual(r.ir.block, '', 'block must be empty when all skill paths are missing');
|
|
});
|
|
|
|
test('validates path safety — rejects traversal attempts', () => {
|
|
writeConfig(tmpDir, {
|
|
agent_skills: {
|
|
'gsd-executor': ['../../../etc/passwd'],
|
|
},
|
|
});
|
|
|
|
const r = runAgentSkillsJson(['agent-skills', 'gsd-executor'], tmpDir);
|
|
assert.ok(!r.ir || !r.ir.block.includes('/etc/passwd'), 'block must not include traversal path');
|
|
});
|
|
|
|
test('returns typed empty IR when no agent type argument provided', () => {
|
|
const r = runAgentSkillsJson(['agent-skills'], tmpDir);
|
|
// With --json and no agent type, the command outputs the empty-string IR
|
|
assert.ok(r.success, 'Command should succeed');
|
|
// Output is JSON, either empty string or empty object
|
|
const parsed = JSON.parse(r.success ? JSON.stringify(r.ir) : '""');
|
|
assert.ok(parsed === '' || (typeof parsed === 'object'), 'Should return empty or empty-agent IR');
|
|
});
|
|
});
|
|
|
|
// ─── config-ensure-section includes agent_skills ────────────────────────────
|
|
|
|
describe('config-ensure-section with agent_skills', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('new configs include agent_skills key', () => {
|
|
const result = runGsdTools('config-ensure-section', tmpDir, { HOME: tmpDir, USERPROFILE: tmpDir });
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const config = readConfig(tmpDir);
|
|
assert.ok('agent_skills' in config, 'config should have agent_skills key');
|
|
assert.deepStrictEqual(config.agent_skills, {}, 'agent_skills should default to empty object');
|
|
});
|
|
});
|
|
|
|
// ─── config-set agent_skills ─────────────────────────────────────────────────
|
|
|
|
describe('config-set agent_skills', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
// Ensure config exists first
|
|
runGsdTools('config-ensure-section', tmpDir, { HOME: tmpDir, USERPROFILE: tmpDir });
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('can set agent_skills via dot notation', () => {
|
|
const result = runGsdTools(
|
|
['config-set', 'agent_skills.gsd-executor', '["skills/my-skill"]'],
|
|
tmpDir,
|
|
{ HOME: tmpDir, USERPROFILE: tmpDir }
|
|
);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const config = readConfig(tmpDir);
|
|
assert.deepStrictEqual(
|
|
config.agent_skills['gsd-executor'],
|
|
['skills/my-skill'],
|
|
'Should store array of skill paths'
|
|
);
|
|
});
|
|
});
|
|
|
|
// ─── global: prefix support (#1992) ──────────────────────────────────────────
|
|
|
|
describe('agent-skills global: prefix', () => {
|
|
let tmpDir;
|
|
let fakeHome;
|
|
let globalSkillsDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
// Create a fake HOME with ~/.claude/skills/ structure
|
|
fakeHome = fs.mkdtempSync(path.join(require('os').tmpdir(), 'gsd-1992-home-'));
|
|
globalSkillsDir = path.join(fakeHome, '.claude', 'skills');
|
|
fs.mkdirSync(globalSkillsDir, { recursive: true });
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
cleanup(fakeHome);
|
|
});
|
|
|
|
function createGlobalSkill(name) {
|
|
const skillDir = path.join(globalSkillsDir, name);
|
|
fs.mkdirSync(skillDir, { recursive: true });
|
|
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), `# ${name}\nGlobal skill content.\n`);
|
|
return skillDir;
|
|
}
|
|
|
|
test('global:valid-skill resolves to $HOME/.claude/skills/valid-skill/SKILL.md', () => {
|
|
createGlobalSkill('valid-skill');
|
|
writeConfig(tmpDir, {
|
|
agent_skills: { 'gsd-executor': ['global:valid-skill'] },
|
|
});
|
|
|
|
const r = runAgentSkillsJson(
|
|
['agent-skills', 'gsd-executor'], tmpDir, { HOME: fakeHome, USERPROFILE: fakeHome }
|
|
);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.ok(r.ir.block.includes('valid-skill/SKILL.md'), `block must reference the global skill: ${r.ir.block}`);
|
|
assert.ok(r.ir.block.includes('<agent_skills>'), 'block must emit agent_skills XML');
|
|
});
|
|
|
|
test('global:invalid!name is rejected by regex and skipped', () => {
|
|
writeConfig(tmpDir, {
|
|
agent_skills: { 'gsd-executor': ['global:invalid!name'] },
|
|
});
|
|
|
|
const r = runAgentSkillsJson(
|
|
['agent-skills', 'gsd-executor'], tmpDir, { HOME: fakeHome, USERPROFILE: fakeHome }
|
|
);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.block, '', 'block must be empty when invalid name is rejected');
|
|
});
|
|
|
|
test('global:missing-skill is skipped when directory is absent', () => {
|
|
writeConfig(tmpDir, {
|
|
agent_skills: { 'gsd-executor': ['global:missing-skill'] },
|
|
});
|
|
|
|
const r = runAgentSkillsJson(
|
|
['agent-skills', 'gsd-executor'], tmpDir, { HOME: fakeHome, USERPROFILE: fakeHome }
|
|
);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.block, '', 'block must be empty when skill is missing');
|
|
});
|
|
|
|
test('mix of global: and project-relative paths both resolve correctly', () => {
|
|
createGlobalSkill('shadcn');
|
|
|
|
const projectSkillDir = path.join(tmpDir, 'skills', 'local-skill');
|
|
fs.mkdirSync(projectSkillDir, { recursive: true });
|
|
fs.writeFileSync(path.join(projectSkillDir, 'SKILL.md'), '# local\n');
|
|
|
|
writeConfig(tmpDir, {
|
|
agent_skills: { 'gsd-executor': ['global:shadcn', 'skills/local-skill'] },
|
|
});
|
|
|
|
const r = runAgentSkillsJson(
|
|
['agent-skills', 'gsd-executor'], tmpDir, { HOME: fakeHome, USERPROFILE: fakeHome }
|
|
);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.ok(r.ir.block.includes('shadcn/SKILL.md'), 'block must include global shadcn');
|
|
assert.ok(r.ir.block.includes('skills/local-skill/SKILL.md'), 'block must include project-relative skill');
|
|
assert.strictEqual(r.ir.skills_count, 2, 'skills_count must be 2 for both configured paths');
|
|
});
|
|
|
|
test('global: with empty name produces clear warning and skips', () => {
|
|
writeConfig(tmpDir, {
|
|
agent_skills: { 'gsd-executor': ['global:'] },
|
|
});
|
|
|
|
const r = runAgentSkillsJson(
|
|
['agent-skills', 'gsd-executor'], tmpDir, { HOME: fakeHome, USERPROFILE: fakeHome }
|
|
);
|
|
assert.ok(r.success, `Command failed: ${r.error}`);
|
|
assert.strictEqual(r.ir.block, '', 'block must be empty for empty global: prefix');
|
|
});
|
|
});
|