Files
msd-core/tests/codebuddy-install.test.cjs
Tom Boucher b8c33647d8 refactor(tests): retire output-grep & source-grep via typed surfaces (finish #2974) (#462)
* refactor(#455): implement typed surfaces to retire grep tests

Production surfaces added:
- hooks/managed-hooks-registry.cjs: new CJS module exporting MANAGED_HOOKS
  as a typed array; gsd-check-update-worker.js now requires it instead of
  declaring an inline array
- bin/install.js: elevate inline gsdHooks to module-level GSD_UNINSTALL_HOOKS,
  export it alongside runtimeMap/allRuntimes (already exported)
- scripts/build-hooks.js: export HOOKS_TO_COPY; guard build() behind
  require.main===module so tests can require the file without triggering a build
- get-shit-done/bin/lib/init.cjs: add --json mode to agent-skills command,
  emitting typed IR { agent_type, block, skills_count } for test assertions
- get-shit-done/bin/gsd-tools.cjs: wire --json flag for agent-skills dispatch

Category-B source-grep migrations:
- tests/managed-hooks.test.cjs: require MANAGED_HOOKS from registry, drop fs.readFileSync+regex
- tests/orphaned-hooks.test.cjs: require MANAGED_HOOKS+HOOKS_TO_COPY as typed exports
- tests/hooks-opt-in.test.cjs: replace gsdHooks regex-parse with GSD_UNINSTALL_HOOKS import
- tests/install-minimal-hooks.test.cjs: replace gsdHooks regex-parse with GSD_UNINSTALL_HOOKS
- tests/copilot-install.test.cjs: replace src.includes() checks with typed
  assertions on runtimeMap, allRuntimes, parseRuntimeInput, buildRuntimePromptText
- tests/agent-skills.test.cjs: migrate to --json typed IR assertions

pending-migration-to-typed-ir token cleared (87 of 87 files):
- 78 files already had source-text-is-the-product; removed duplicate token
- 5 files already used typed assertions; reclassified or annotated
- 4 files required individual reclassification to source-text-is-the-product
  or architectural-invariant

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#455): update workflow-guard test to typed GSD_UNINSTALL_HOOKS import; isolate HOME in runtime-launcher (D) test

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#455): guard install.js main() behind require.main===module so the typed export is require-safe

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(#455): document --json typed surfaces for agent-skills, progress, validate context

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(#455): add changeset fragment for new --json surfaces

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(#455): complete grep migration for files flagged by lint-tests

The branch commit 4e630d99 stripped `allow-test-rule: pending-migration-to-typed-ir`
from ~80 test files without replacing their assertions or adding the correct
exemption annotation. The files were NOT source-grep tests — they read .md
workflow/agent/command/reference files (source-text-is-the-product) or hook
source files for structural invariants (structural-regression-guard). No
assertion logic was changed; only the correct allow-test-rule annotation was
added to each file per CONTRIBUTING.md exception matrix.

73 files: `source-text-is-the-product` — workflow/agent/command/reference .md
7 files:  `structural-regression-guard` — hook .js / bin/install.js structural checks

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: CI Rebase Check <ci@gsd-redux>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-29 11:39:52 -04:00

269 lines
9.7 KiB
JavaScript

// allow-test-rule: source-text-is-the-product
// Workflow .md / agent .md / command .md / reference .md files — their text
// IS what the runtime loads. Testing text content tests the deployed contract.
// Per CONTRIBUTING.md exception matrix.
process.env.GSD_TEST_MODE = '1';
const { test, describe, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const { createTempDir, cleanup } = require('./helpers.cjs');
const {
getDirName,
getGlobalDir,
getConfigDirFromHome,
convertClaudeToCodebuddyMarkdown,
convertClaudeCommandToCodebuddySkill,
convertClaudeAgentToCodebuddyAgent,
install,
uninstall,
writeManifest,
installRuntimeArtifacts,
} = require('../bin/install.js');
// ─── Profile resolution for installRuntimeArtifacts tests ────────────────────
const _gsdLibDir = path.join(__dirname, '..', 'get-shit-done', 'bin', 'lib');
const { loadSkillsManifest, resolveProfile } = require(path.join(_gsdLibDir, 'install-profiles.cjs'));
const _manifest = loadSkillsManifest();
const resolvedProfileFull = resolveProfile({ modes: [], manifest: _manifest });
describe('CodeBuddy runtime directory mapping', () => {
test('maps CodeBuddy to .codebuddy for local installs', () => {
assert.strictEqual(getDirName('codebuddy'), '.codebuddy');
});
test('maps CodeBuddy to ~/.codebuddy for global installs', () => {
assert.strictEqual(getGlobalDir('codebuddy'), path.join(os.homedir(), '.codebuddy'));
});
test('returns .codebuddy config fragments for local and global installs', () => {
assert.strictEqual(getConfigDirFromHome('codebuddy', false), "'.codebuddy'");
assert.strictEqual(getConfigDirFromHome('codebuddy', true), "'.codebuddy'");
});
});
describe('getGlobalDir (CodeBuddy)', () => {
let originalCodebuddyConfigDir;
beforeEach(() => {
originalCodebuddyConfigDir = process.env.CODEBUDDY_CONFIG_DIR;
});
afterEach(() => {
if (originalCodebuddyConfigDir !== undefined) {
process.env.CODEBUDDY_CONFIG_DIR = originalCodebuddyConfigDir;
} else {
delete process.env.CODEBUDDY_CONFIG_DIR;
}
});
test('returns ~/.codebuddy with no env var or explicit dir', () => {
delete process.env.CODEBUDDY_CONFIG_DIR;
const result = getGlobalDir('codebuddy');
assert.strictEqual(result, path.join(os.homedir(), '.codebuddy'));
});
test('returns explicit dir when provided', () => {
const result = getGlobalDir('codebuddy', '/custom/codebuddy-path');
assert.strictEqual(result, '/custom/codebuddy-path');
});
test('respects CODEBUDDY_CONFIG_DIR env var', () => {
process.env.CODEBUDDY_CONFIG_DIR = '~/custom-codebuddy';
const result = getGlobalDir('codebuddy');
assert.strictEqual(result, path.join(os.homedir(), 'custom-codebuddy'));
});
test('explicit dir takes priority over CODEBUDDY_CONFIG_DIR', () => {
process.env.CODEBUDDY_CONFIG_DIR = '~/from-env';
const result = getGlobalDir('codebuddy', '/explicit/path');
assert.strictEqual(result, '/explicit/path');
});
test('does not break other runtimes', () => {
assert.strictEqual(getGlobalDir('claude'), path.join(os.homedir(), '.claude'));
assert.strictEqual(getGlobalDir('codex'), path.join(os.homedir(), '.codex'));
});
});
describe('CodeBuddy markdown conversion', () => {
test('converts Claude-specific references to CodeBuddy equivalents', () => {
const input = [
'Claude Code reads CLAUDE.md before using .claude/skills/.',
'Run /gsd:plan-phase with $ARGUMENTS.',
'Use Bash(command) and Edit(file).',
].join('\n');
const result = convertClaudeToCodebuddyMarkdown(input);
assert.ok(result.includes('CodeBuddy reads CODEBUDDY.md before using .codebuddy/skills/.'), result);
assert.ok(result.includes('/gsd-plan-phase'), result);
assert.ok(result.includes('{{GSD_ARGS}}'), result);
// CodeBuddy uses the same tool names as Claude Code — no conversion needed
assert.ok(result.includes('Bash('), result);
assert.ok(result.includes('Edit('), result);
});
test('converts commands and agents to CodeBuddy frontmatter', () => {
const command = `---
name: gsd:new-project
description: Initialize a project
---
Use .claude/skills/ and /gsd:help.
`;
const agent = `---
name: gsd-planner
description: Planner agent
tools: Read, Write
color: blue
---
Read CLAUDE.md before acting.
`;
const convertedCommand = convertClaudeCommandToCodebuddySkill(command, 'gsd-new-project');
const convertedAgent = convertClaudeAgentToCodebuddyAgent(agent);
assert.ok(convertedCommand.includes('name: gsd-new-project'), convertedCommand);
assert.ok(convertedCommand.includes('.codebuddy/skills/'), convertedCommand);
assert.ok(convertedCommand.includes('/gsd-help'), convertedCommand);
assert.ok(convertedAgent.includes('name: gsd-planner'), convertedAgent);
assert.ok(!convertedAgent.includes('color:'), convertedAgent);
assert.ok(convertedAgent.includes('CODEBUDDY.md'), convertedAgent);
});
});
describe('installRuntimeArtifacts (codebuddy integration)', () => {
// Pivoted from copyCommandsAsCodebuddySkills(srcDir, skillsDir, 'gsd', '$HOME/.codebuddy/', 'codebuddy')
// shim to installRuntimeArtifacts('codebuddy', configDir, 'local', resolvedProfileFull).
// Output layout: <configDir>/skills/gsd-<stem>/SKILL.md (destSubpath='skills', prefix='gsd-').
let configDir;
beforeEach(() => {
configDir = createTempDir('gsd-codebuddy-copy-');
});
afterEach(() => {
cleanup(configDir);
});
test('creates one skill directory per GSD command', () => {
installRuntimeArtifacts('codebuddy', configDir, 'local', resolvedProfileFull);
const generated = path.join(configDir, 'skills', 'gsd-help', 'SKILL.md');
assert.ok(fs.existsSync(generated), generated);
const content = fs.readFileSync(generated, 'utf8');
assert.ok(content.includes('name: gsd-help'), content);
});
});
describe('CodeBuddy local install/uninstall', () => {
let tmpDir;
let previousCwd;
beforeEach(() => {
tmpDir = createTempDir('gsd-codebuddy-install-');
previousCwd = process.cwd();
process.chdir(tmpDir);
});
afterEach(() => {
process.chdir(previousCwd);
cleanup(tmpDir);
});
test('installs GSD into ./.codebuddy and removes it cleanly', () => {
const result = install(false, 'codebuddy');
const targetDir = path.join(tmpDir, '.codebuddy');
// CodeBuddy supports settings.json hooks (Claude Code compatible)
assert.strictEqual(result.runtime, 'codebuddy');
assert.ok(result.settingsPath, 'should have settingsPath (CodeBuddy supports hooks)');
assert.ok(fs.existsSync(path.join(targetDir, 'skills', 'gsd-help', 'SKILL.md')));
assert.ok(fs.existsSync(path.join(targetDir, 'get-shit-done', 'VERSION')));
assert.ok(fs.existsSync(path.join(targetDir, 'agents')));
const manifest = writeManifest(targetDir, 'codebuddy');
assert.ok(Object.keys(manifest.files).some(file => file.startsWith('skills/gsd-help/')), JSON.stringify(manifest));
uninstall(false, 'codebuddy');
assert.ok(!fs.existsSync(path.join(targetDir, 'skills', 'gsd-help')), 'CodeBuddy skill directory removed');
assert.ok(!fs.existsSync(path.join(targetDir, 'get-shit-done')), 'get-shit-done removed');
});
});
describe('E2E: CodeBuddy uninstall skills cleanup', () => {
let tmpDir;
let previousCwd;
beforeEach(() => {
tmpDir = createTempDir('gsd-codebuddy-uninstall-');
previousCwd = process.cwd();
process.chdir(tmpDir);
});
afterEach(() => {
process.chdir(previousCwd);
cleanup(tmpDir);
});
test('removes all gsd-* skill directories on --codebuddy --uninstall', () => {
const targetDir = path.join(tmpDir, '.codebuddy');
install(false, 'codebuddy');
const skillsDir = path.join(targetDir, 'skills');
assert.ok(fs.existsSync(skillsDir), 'skills dir exists after install');
const installedSkills = fs.readdirSync(skillsDir, { withFileTypes: true })
.filter(e => e.isDirectory() && e.name.startsWith('gsd-'));
assert.ok(installedSkills.length > 0, `found ${installedSkills.length} gsd-* skill dirs before uninstall`);
uninstall(false, 'codebuddy');
if (fs.existsSync(skillsDir)) {
const remainingGsd = fs.readdirSync(skillsDir, { withFileTypes: true })
.filter(e => e.isDirectory() && e.name.startsWith('gsd-'));
assert.strictEqual(remainingGsd.length, 0,
`Expected 0 gsd-* skill dirs after uninstall, found: ${remainingGsd.map(e => e.name).join(', ')}`);
}
});
test('preserves non-GSD skill directories during --codebuddy --uninstall', () => {
const targetDir = path.join(tmpDir, '.codebuddy');
install(false, 'codebuddy');
const customSkillDir = path.join(targetDir, 'skills', 'my-custom-skill');
fs.mkdirSync(customSkillDir, { recursive: true });
fs.writeFileSync(path.join(customSkillDir, 'SKILL.md'), '# My Custom Skill\n');
assert.ok(fs.existsSync(path.join(customSkillDir, 'SKILL.md')), 'custom skill exists before uninstall');
uninstall(false, 'codebuddy');
assert.ok(fs.existsSync(path.join(customSkillDir, 'SKILL.md')),
'Non-GSD skill directory should be preserved after CodeBuddy uninstall');
});
test('removes engine directory on --codebuddy --uninstall', () => {
const targetDir = path.join(tmpDir, '.codebuddy');
install(false, 'codebuddy');
assert.ok(fs.existsSync(path.join(targetDir, 'get-shit-done', 'VERSION')),
'engine exists before uninstall');
uninstall(false, 'codebuddy');
assert.ok(!fs.existsSync(path.join(targetDir, 'get-shit-done')),
'get-shit-done engine should be removed after CodeBuddy uninstall');
});
});