Files
msd-core/tests/settings-jsonc.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

192 lines
5.8 KiB
JavaScript

// allow-test-rule: structural-regression-guard
// Reads hook .js or bin/install.js source to assert structural invariants
// (search array order, function wiring, path constants) that cannot be
// verified by observing runtime outputs alone. Per CONTRIBUTING.md exception matrix.
/**
* GSD Tools Tests - settings.json JSONC (JSON with comments) support
*
* Validates that the installer's readSettings() correctly handles
* settings.json files containing comments (line and block) without
* silently overwriting them with empty objects.
*
* Closes: #1461
*/
const { test, describe, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
// ─── inline stripJsonComments (mirrors install.js logic) ─────────────────────
function stripJsonComments(text) {
let result = '';
let i = 0;
let inString = false;
let stringChar = '';
while (i < text.length) {
if (inString) {
if (text[i] === '\\') {
result += text[i] + (text[i + 1] || '');
i += 2;
continue;
}
if (text[i] === stringChar) {
inString = false;
}
result += text[i];
i++;
continue;
}
if (text[i] === '"' || text[i] === "'") {
inString = true;
stringChar = text[i];
result += text[i];
i++;
continue;
}
if (text[i] === '/' && text[i + 1] === '/') {
while (i < text.length && text[i] !== '\n') i++;
continue;
}
if (text[i] === '/' && text[i + 1] === '*') {
i += 2;
while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) i++;
i += 2;
continue;
}
result += text[i];
i++;
}
return result.replace(/,\s*([}\]])/g, '$1');
}
// ─── tests ───────────────────────────────────────────────────────────────────
describe('stripJsonComments (#1461)', () => {
test('strips line comments', () => {
const input = `{
// This is a comment
"key": "value"
}`;
const result = JSON.parse(stripJsonComments(input));
assert.deepStrictEqual(result, { key: 'value' });
});
test('strips block comments', () => {
const input = `{
/* Block comment */
"key": "value"
}`;
const result = JSON.parse(stripJsonComments(input));
assert.deepStrictEqual(result, { key: 'value' });
});
test('strips multi-line block comments', () => {
const input = `{
/*
* Multi-line
* block comment
*/
"key": "value"
}`;
const result = JSON.parse(stripJsonComments(input));
assert.deepStrictEqual(result, { key: 'value' });
});
test('preserves comments inside string values', () => {
const input = `{
"url": "https://example.com/path",
"description": "Use // for line comments"
}`;
const result = JSON.parse(stripJsonComments(input));
assert.strictEqual(result.url, 'https://example.com/path');
assert.strictEqual(result.description, 'Use // for line comments');
});
test('handles trailing commas', () => {
const input = `{
"a": 1,
"b": 2,
}`;
const result = JSON.parse(stripJsonComments(input));
assert.deepStrictEqual(result, { a: 1, b: 2 });
});
test('handles inline comments after values', () => {
const input = `{
"timeout": 5000, // milliseconds
"retries": 3 // max attempts
}`;
const result = JSON.parse(stripJsonComments(input));
assert.strictEqual(result.timeout, 5000);
assert.strictEqual(result.retries, 3);
});
test('handles standard JSON (no comments) unchanged', () => {
const input = '{"key": "value", "num": 42}';
const result = JSON.parse(stripJsonComments(input));
assert.deepStrictEqual(result, { key: 'value', num: 42 });
});
test('handles empty object', () => {
const result = JSON.parse(stripJsonComments('{}'));
assert.deepStrictEqual(result, {});
});
test('handles real-world settings.json with comments', () => {
const input = `{
// My configuration
"hooks": {
"SessionStart": [
{
"matcher": "", /* match all */
"hooks": [
{
"type": "command",
"command": "node ~/.claude/hooks/gsd-statusline.js"
}
]
}
]
},
"statusLine": {
"command": "node ~/.claude/hooks/gsd-statusline.js",
"refreshInterval": 10
}
}`;
const result = JSON.parse(stripJsonComments(input));
assert.ok(result.hooks, 'should have hooks');
assert.ok(result.statusLine, 'should have statusLine');
assert.strictEqual(result.statusLine.refreshInterval, 10);
});
});
describe('readSettings null return on malformed files (#1461)', () => {
test('install.js contains JSONC stripping in readSettings', () => {
const installPath = path.join(__dirname, '..', 'bin', 'install.js');
const content = fs.readFileSync(installPath, 'utf8');
assert.ok(content.includes('stripJsonComments'),
'install.js should use stripJsonComments in readSettings');
});
test('readSettings returns null on truly malformed files (not empty object)', () => {
const installPath = path.join(__dirname, '..', 'bin', 'install.js');
const content = fs.readFileSync(installPath, 'utf8');
assert.ok(content.includes('return null'),
'readSettings should return null on parse failure, not empty object');
});
test('callers guard against null readSettings return', () => {
const installPath = path.join(__dirname, '..', 'bin', 'install.js');
const content = fs.readFileSync(installPath, 'utf8');
// Should have null guards at the settings configuration call sites
assert.ok(
content.includes('=== null') || content.includes('rawSettings === null'),
'callers should check for null return from readSettings'
);
});
});