* test(#3072): add failing-first coverage for the mcp served catalog 55 input-class rows from the phase test matrix, across four suites: the catalog module over injected readFile/readDir seams, the protocol surface through handleMessage, the install-vs-catalog parity gate, and fast-check properties for uri round-trip, traversal refusal, and pagination partition. src/mcp-catalog.cts lands as a skeleton whose functions throw, so the suites fail on BEHAVIOR rather than on a missing module. The REASON enum is real so tests assert typed codes instead of message prose. Hostile coverage for the one client-controlled path surface (resources/read): dot-dot and backslash traversal, percent- and double-encoded traversal, absolute posix and windows paths, file:// scheme, null byte, symlink escape, unindexed sibling, non-string and empty uri, wrong root segment. IO faults are injected by monkeypatching the seam, never chmod 0o000 - root bypasses mode bits, so a permission-based test silently passes with zero coverage in root CI. Refs #3072 * feat(#3072): serve the mcp catalog as resources and prompts gsd-mcp-server now serves GSD's own content alongside its three tools: the workflow, reference and command tree as MCP resources (resources/list, cursor paginated, and resources/read over gsd://<segment>/<relpath> uris) and the 71 commands/gsd/*.md as MCP prompts keyed by bare command name. initialize advertises resources and prompts, and deliberately does not advertise subscribe or listChanged - the catalog is fixed for a server process lifetime, so declaring a notification we never send would be a lie a host acts on. Composition scope is SHARED, not re-declared. shouldCompose lives in src/mcp-catalog.cts and bin/install.js now imports it instead of carrying its own regex, so the served catalog and the installed file floor cannot drift on what gets composed. Proven behavior-preserving across all 2871 tracked paths plus windows-backslash, absolute and near-miss-prefix cases: zero mismatches. tests/mcp-catalog-parity.test.cjs asserts served text equals the installer composition-stage text over the real tree, with anti-vacuity guards requiring both a marker-bearing workflow and a non-composed file in the comparison set. Two measurements corrected the literal issue text. Composition is scoped to gsd-core/workflows/ only, because a reference or command that documents marker syntax with an unfenced example would otherwise be parsed as carrying a real marker and have that line lossily dropped. And parity is asserted at the composition stage rather than against an emitted runtime tree, since install applies per-runtime path rewrites afterwards and the catalog is host-agnostic, so byte equality with any one runtime would be false by construction. resources/read is the one client-controlled path surface and is guarded in two independent layers: the uri must be an exact key in the prebuilt index, which defeats every traversal string by construction, and the mapped path is then re-checked with validatePath so a symlink planted inside a root after indexing is still refused. Also fixes a real drift defect found while here: SERVER_VERSION was hardcoded 1.7.0 while the package is at 1.9.1. It now resolves lazily from VERSION or package.json, reusing the precedent in runtime-artifact-conversion. Closes #3072 * test(#3072): make the catalog parity gate drive the real installer Review found the parity gate vacuous: it never imported or spawned bin/install.js, and recomputed the installer side with the SAME shouldCompose and composeWorkflow the catalog calls internally. It therefore proved only that src/mcp-catalog.cts is self-consistent. The old row 52 compared shouldCompose against a regex literal frozen in the test file rather than against the installer at all. An inline divergent regex re-added to bin/install.js - the exact regression ADR-1671 asks this gate to catch - would have left the suite green. The gate now spawns a real bin/install.js and compares the composition DECISION, observed as gsd:section marker survival, against what the catalog serves for the same files. Marker presence is the right observable because the installer applies per-runtime path rewrites after composing while the catalog applies none, so raw byte equality between the two surfaces is false by construction and must not be asserted. Sensitivity was proven, not assumed: overlaying the shouldCompose export that bin/install.js imports so it always returns false makes a real spawned install leave autonomous.md's markers in place while the catalog still strips them, and the row 48 assertion diverges. Anti-vacuity guards are kept and extended - the comparison set must be non-empty, must contain a workflow that actually carries markers, must contain a file the predicate declines to compose, and the install must have emitted a non-zero file count. The marker-documenting reference case has no instance in the real tree, so it uses an overlay fixture built with the same technique workflow-fragments-emission.install.test.cjs already uses. Renamed to .install.test.cjs so it lands in the install suite it now belongs to. Refs #3072 * test(#3072): retarget the unknown-method assertion off a now-implemented method tests/gsd-mcp-server.test.cjs used 'resources/read' as its example of an UNKNOWN JSON-RPC method. The served catalog implements that method, so it now returns -32602 (no uri supplied) rather than -32601. The remote runner caught it deterministically on both linux lanes: -32602 !== -32601. The test's intent is still correct and worth keeping, so it is corrected rather than deleted or weakened. It now uses 'resources/subscribe', which the server deliberately does not implement and deliberately does not advertise in initialize's capabilities, because it never sends the corresponding notification. That turns the assertion into a real contract - the advertised capability surface and the implemented method surface agree - instead of an arbitrary method name a future feature could invalidate the same way. Swept the rest of the suite for other assertions pinning the newly implemented methods; this was the only one. Refs #3072 * chore(#3072): backfill changeset PR number 3083 * test(#3072): make the catalog fake fs separator-agnostic for windows CI caught this on windows-latest (22 and 24): every catalog fixture indexed ZERO entries, surfaced by the anti-vacuity guards as 'fixture catalog must actually index resources for this property to mean anything'. Mechanism: makeFakeFs keyed its dirMap/fileMap on POSIX-joined paths (${root}/${rel}), while production buildCatalog looks paths up with path.join, which is backslash-separated on Windows. Every lookup missed, tryReadDir returned null, and the catalog came back empty. Production is NOT at fault and is unchanged. The same CI run proves it: on windows-latest the real-filesystem tests all passed, including 'installer composition decision matches the served catalog for every file in the real installed tree' and the row-51 non-vacuity proof against a real spawned installer. A real Windows fs accepts both separators; the FAKE did not, so the fake was the unfaithful one and is what changed. Lookup keys are now normalized unconditionally with .replace(/\\/g,'/') in readDir and readFile - never path.sep-conditional, never platform-gated. The row-42/43 injected-fault wrappers got the same treatment, since they compared raw production paths against POSIX-literal fixtures. No assertion was weakened, and the anti-vacuity guards that caught this are untouched - they are the reason this surfaced as a loud failure instead of a suite that silently asserted nothing on Windows. Refs #3072 --------- Co-authored-by: sim <sim@local>
180 lines
7.3 KiB
JavaScript
180 lines
7.3 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Failing-first property-based tests for src/mcp-catalog.cts (compiled to
|
|
* gsd-core/bin/lib/mcp-catalog.cjs) — issue #3072 (epic #1671 Phase B),
|
|
* `.gsd/phase/feat-3072-mcp-served-catalog/40-design.md`.
|
|
*
|
|
* Covers 50-test-matrix.md rows 53-55:
|
|
* (a) every indexed uri round-trips through readResource
|
|
* (b) no generated traversal-shaped string ever resolves to a read
|
|
* (c) pagination partitions the resource list for any page size
|
|
*
|
|
* Uses the shared `fast-check` global config (`tests/helpers/
|
|
* fast-check-setup.cjs`: numRuns 200, seed 42, overridable via
|
|
* `GSD_FC_SEED`) — same pattern as `tests/adr-parser.property.test.cjs`.
|
|
* Fixtures are built over the SAME injected `readFile`/`readDir` seam used
|
|
* throughout `tests/mcp-catalog.test.cjs` — never real filesystem
|
|
* permission tricks.
|
|
*/
|
|
|
|
const { describe, test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fc = require('./helpers/fast-check-setup.cjs');
|
|
|
|
const { buildCatalog, readResource, listResources, getPrompt } = require('../gsd-core/bin/lib/mcp-catalog.cjs');
|
|
|
|
// ─── shared fixture: a small, fixed catalog over injected seams ────────────
|
|
|
|
function dirEntry(name, isDir) {
|
|
return { name, isDirectory: () => isDir };
|
|
}
|
|
|
|
function makeFakeFs(root, files) {
|
|
const abs = (rel) => (rel ? `${root}/${rel}` : root);
|
|
const fileMap = new Map(Object.entries(files).map(([rel, content]) => [abs(rel), content]));
|
|
const dirMap = new Map();
|
|
for (const rel of Object.keys(files)) {
|
|
const parts = rel.split('/');
|
|
for (let i = 0; i < parts.length; i++) {
|
|
const dirRel = parts.slice(0, i).join('/');
|
|
const dirAbs = abs(dirRel);
|
|
const isLast = i === parts.length - 1;
|
|
if (!dirMap.has(dirAbs)) dirMap.set(dirAbs, new Map());
|
|
dirMap.get(dirAbs).set(parts[i], dirEntry(parts[i], !isLast));
|
|
}
|
|
}
|
|
// Production (`buildCatalog`) resolves paths via `path.join`, which is
|
|
// backslash-separated on Windows; this fake's maps are keyed POSIX. A real
|
|
// `fs` accepts both separators, so the fake must too — normalize the
|
|
// incoming lookup key unconditionally (never process.platform-gated).
|
|
// Caught by CI on windows-latest: every lookup missed, the fake indexed
|
|
// zero entries, and the anti-vacuity guards correctly flagged it.
|
|
const norm = (p) => String(p).replace(/\\/g, '/');
|
|
const readDir = (absPath) => {
|
|
const m = dirMap.get(norm(absPath));
|
|
if (!m) throw new Error(`ENOENT (fake fs): ${absPath}`);
|
|
return [...m.values()];
|
|
};
|
|
const readFile = (absPath) => {
|
|
const key = norm(absPath);
|
|
if (!fileMap.has(key)) throw new Error(`ENOENT (fake fs): ${absPath}`);
|
|
return fileMap.get(key);
|
|
};
|
|
return { root, readFile, readDir };
|
|
}
|
|
|
|
const FIXTURE_RESOURCE_COUNT = 9;
|
|
const FIXTURE_PROMPT_COUNT = 4;
|
|
|
|
function buildFixtureCatalog() {
|
|
const files = {};
|
|
for (let i = 0; i < FIXTURE_RESOURCE_COUNT; i++) {
|
|
const segment = i % 2 === 0 ? 'workflows' : 'references';
|
|
files[`gsd-core/${segment}/entry-${i}.md`] = `# entry ${i}\n\nbody text for entry ${i}\n`;
|
|
}
|
|
for (let i = 0; i < FIXTURE_PROMPT_COUNT; i++) {
|
|
files[`commands/gsd/cmd-${i}.md`] = `# cmd ${i}\n\nprompt body ${i}\n`;
|
|
}
|
|
const fake = makeFakeFs('/fake-root-property', files);
|
|
return buildCatalog({ root: fake.root, readFile: fake.readFile, readDir: fake.readDir });
|
|
}
|
|
|
|
// ─── row 53: every indexed uri round-trips ──────────────────────────────────
|
|
|
|
describe('property: every indexed uri round-trips (row 53)', () => {
|
|
test('readResource succeeds and returns the same uri for every indexed resource', () => {
|
|
const catalog = buildFixtureCatalog();
|
|
const uris = [...catalog.resources.keys()];
|
|
assert.ok(uris.length > 0, 'fixture catalog must actually index resources for this property to mean anything');
|
|
|
|
fc.assert(
|
|
fc.property(fc.constantFrom(...uris), (uri) => {
|
|
const result = readResource(catalog, uri);
|
|
assert.equal(result.uri, uri);
|
|
assert.equal(typeof result.text, 'string');
|
|
})
|
|
);
|
|
});
|
|
|
|
test('getPrompt succeeds and returns the same name for every indexed prompt', () => {
|
|
const catalog = buildFixtureCatalog();
|
|
const names = [...catalog.prompts.keys()];
|
|
assert.ok(names.length > 0, 'fixture catalog must actually index prompts for this property to mean anything');
|
|
|
|
fc.assert(
|
|
fc.property(fc.constantFrom(...names), (name) => {
|
|
const result = getPrompt(catalog, name);
|
|
assert.equal(typeof result.description, 'string');
|
|
assert.ok(Array.isArray(result.messages) && result.messages.length >= 1);
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
// ─── row 54: no generated traversal string ever resolves ───────────────────
|
|
|
|
describe('property: generated traversal strings never resolve (row 54)', () => {
|
|
test('readResource always refuses arbitrary traversal-shaped uris', () => {
|
|
const catalog = buildFixtureCatalog();
|
|
|
|
const traversalSegment = fc.constantFrom('..', '..%2f', '%2e%2e', '..\\', '%252e%252e%252f');
|
|
const rootSegment = fc.constantFrom('workflows', 'references');
|
|
const suffix = fc.stringMatching(/^[a-z0-9-]{0,12}$/);
|
|
|
|
const traversalUri = fc
|
|
.tuple(rootSegment, fc.array(traversalSegment, { minLength: 1, maxLength: 6 }), suffix)
|
|
.map(([root, segments, tail]) => `gsd://${root}/${segments.join('/')}${tail}.md`);
|
|
|
|
fc.assert(
|
|
fc.property(traversalUri, (uri) => {
|
|
assert.throws(
|
|
() => readResource(catalog, uri),
|
|
undefined,
|
|
`a traversal-shaped uri must never resolve to a successful read: ${uri}`
|
|
);
|
|
})
|
|
);
|
|
});
|
|
|
|
test('readResource always refuses arbitrary absolute-path-shaped uris', () => {
|
|
const catalog = buildFixtureCatalog();
|
|
|
|
const absolutePath = fc.oneof(
|
|
fc.stringMatching(/^\/[a-z0-9/_-]{1,40}$/),
|
|
fc.stringMatching(/^[A-Z]:\\[A-Za-z0-9\\_-]{1,40}$/)
|
|
);
|
|
|
|
fc.assert(
|
|
fc.property(absolutePath, (p) => {
|
|
assert.throws(() => readResource(catalog, p), undefined, `an absolute-path-shaped uri must never resolve to a successful read: ${p}`);
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
// ─── row 55: pagination is a partition ──────────────────────────────────────
|
|
|
|
describe('property: pagination partitions the list for any page size (row 55)', () => {
|
|
test('pages concatenate to the full list with no dupes and no gaps, for any page size >= 1', () => {
|
|
const catalog = buildFixtureCatalog();
|
|
const full = listResources(catalog, { pageSize: 10_000 }).resources.map((r) => r.uri);
|
|
assert.ok(full.length > 0);
|
|
|
|
fc.assert(
|
|
fc.property(fc.integer({ min: 1, max: full.length + 3 }), (pageSize) => {
|
|
const collected = [];
|
|
let cursor;
|
|
for (let guard = 0; guard < full.length + 5; guard++) {
|
|
const page = listResources(catalog, { pageSize, cursor });
|
|
assert.ok(page.resources.length <= pageSize, `a page must never exceed the requested pageSize ${pageSize}`);
|
|
collected.push(...page.resources.map((r) => r.uri));
|
|
if (page.nextCursor === undefined) break;
|
|
cursor = page.nextCursor;
|
|
}
|
|
assert.deepEqual(collected, full, `pages for pageSize=${pageSize} must reassemble the full list with no dupes/gaps`);
|
|
})
|
|
);
|
|
});
|
|
});
|