Merge pull request #253 from yunyingk/mac15/add-protocol-test-closure
Add protocol test closures for secure writes, aliases, help and cli docs
This commit is contained in:
4 files changed
+260
-1
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { AREA_ALIASES, UPDATE_ALIASES, normalizeCliArea } from "./area-aliases.js";
|
||||
import { KNOWN_AREAS } from "./runner.js";
|
||||
|
||||
/**
|
||||
* Drift guard for the shared alias table.
|
||||
*
|
||||
* `read-only.ts` once hand-copied the alias pairs from `runner.ts` and the two
|
||||
* silently diverged — the exact class of bug the 2026-10-17 architecture
|
||||
* review called "two sources of truth". The tables now live in one module and
|
||||
* are imported by dispatch, help, and the read-only gateway; these tests make
|
||||
* the remaining cross-module contract explicit: an alias may only point at an
|
||||
* area the dispatcher actually handles, may not chain into another alias, and
|
||||
* must not shadow a real area name.
|
||||
*/
|
||||
|
||||
describe("area alias integrity", () => {
|
||||
test("every alias resolves to a dispatchable area", () => {
|
||||
const dangling = Object.entries(AREA_ALIASES)
|
||||
.filter(([, target]) => !KNOWN_AREAS.has(target))
|
||||
.map(([alias, target]) => `${alias} -> ${target}`);
|
||||
|
||||
expect(dangling).toEqual([]);
|
||||
});
|
||||
|
||||
test("no alias chains into another alias", () => {
|
||||
// normalizeCliArea applies one lookup; a chained alias would dispatch the
|
||||
// raw alias spelling as a command area and fail with "Unknown command".
|
||||
const chained = Object.values(AREA_ALIASES).filter((target) => target in AREA_ALIASES);
|
||||
|
||||
expect(chained).toEqual([]);
|
||||
});
|
||||
|
||||
test("alias spellings never shadow a real area name", () => {
|
||||
// KNOWN_AREAS is the handler tables plus the alias keys; subtracting the
|
||||
// alias keys recovers the real dispatch areas. An alias colliding with
|
||||
// one of them would be dead weight at best and ambiguous at worst.
|
||||
const realAreas = new Set([...KNOWN_AREAS].filter((area) => !(area in AREA_ALIASES)));
|
||||
const shadowing = Object.keys(AREA_ALIASES).filter((alias) => realAreas.has(alias));
|
||||
|
||||
expect(shadowing).toEqual([]);
|
||||
});
|
||||
|
||||
test("every update spelling normalizes onto the real update area", () => {
|
||||
for (const spelling of UPDATE_ALIASES) {
|
||||
expect(normalizeCliArea(spelling)).toBe("update");
|
||||
}
|
||||
expect(KNOWN_AREAS.has("update")).toBe(true);
|
||||
});
|
||||
|
||||
test("normalization is idempotent and case-insensitive", () => {
|
||||
expect(normalizeCliArea(normalizeCliArea("corp"))).toBe("corps");
|
||||
expect(normalizeCliArea("CORP")).toBe("corps");
|
||||
expect(normalizeCliArea("Self-Update")).toBe("update");
|
||||
});
|
||||
|
||||
test("every alias and update spelling resolves through KNOWN_AREAS", () => {
|
||||
// Alias keys must be members outright: help lookup and the read-only
|
||||
// gateway resolve the raw spelling before any normalization. Update
|
||||
// spellings are normalized first, so their canonical target is the
|
||||
// dispatchable name.
|
||||
for (const alias of Object.keys(AREA_ALIASES)) {
|
||||
expect(KNOWN_AREAS.has(alias)).toBe(true);
|
||||
}
|
||||
for (const spelling of UPDATE_ALIASES) {
|
||||
expect(KNOWN_AREAS.has(normalizeCliArea(spelling))).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { readFileSync, readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { parseArgs } from "./parser.js";
|
||||
import { KNOWN_AREAS, UPDATE_ALIASES } from "./runner.js";
|
||||
|
||||
/**
|
||||
@@ -67,3 +68,79 @@ describe("documentation references live commands", () => {
|
||||
expect(stale).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Usage-level closure for the generated cli.md block.
|
||||
*
|
||||
* `verify:cli-md` proves the block byte-matches what HELP_REGISTRY renders,
|
||||
* and parser.test.ts proves registry usage flags are registered — but nothing
|
||||
* fed the rendered lines back through the real parser. That is exactly the
|
||||
* hole the 2026-10-17 review found in the opposite direction: 22 parser tests
|
||||
* green while nine documented flags were rejected at runtime. Rendering is
|
||||
* code too, so the doc reader's copy-paste path gets its own parse.
|
||||
*/
|
||||
|
||||
const CLI_DOC_PATH = join(REPO_ROOT, "skills/seal-use/references/cli.md");
|
||||
|
||||
function generatedUsageLines(markdown: string): string[] {
|
||||
const block = markdown.match(
|
||||
/<!-- BEGIN GENERATED CLI COMMANDS -->([\s\S]*?)<!-- END GENERATED CLI COMMANDS -->/
|
||||
);
|
||||
if (!block?.[1]) throw new Error(`generated command block missing in ${CLI_DOC_PATH}`);
|
||||
return block[1].split("\n").filter((line) => line.startsWith("seal-use "));
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a documented usage line into argv the way a reader would type it:
|
||||
* keep one alternative of each `a|b` group, unwrap `[...]` optionality, and
|
||||
* replace `<value hints>` with a neutral placeholder token. Single quotes are
|
||||
* respected so `--json '{...}'` payloads survive as one argument.
|
||||
*/
|
||||
function usageToArgv(line: string): string[] {
|
||||
// `--json '{...}'` is a fill-in placeholder in the docs, not literal JSON;
|
||||
// substitute a valid empty object so the parser sees a well-formed payload.
|
||||
const hinted = line.slice("seal-use ".length)
|
||||
.replace(/<[^<>]*>/g, " __HINT__ ")
|
||||
.replace(/'\{\.\.\.\}'/g, "'{}'");
|
||||
const unbracketed = hinted.replace(/[[\]]/g, " ");
|
||||
|
||||
const tokens: string[] = [];
|
||||
let current = "";
|
||||
let quoted = false;
|
||||
for (const char of unbracketed) {
|
||||
if (char === "'") {
|
||||
quoted = !quoted;
|
||||
continue;
|
||||
}
|
||||
if (!quoted && /\s/.test(char)) {
|
||||
if (current) tokens.push(current);
|
||||
current = "";
|
||||
continue;
|
||||
}
|
||||
current += char;
|
||||
}
|
||||
if (current) tokens.push(current);
|
||||
|
||||
return tokens.map((token) => token.split("|")[0]!).filter((token) => token.length > 0);
|
||||
}
|
||||
|
||||
describe("generated cli.md usage lines parse", () => {
|
||||
test("every documented usage line is accepted by parseArgs", () => {
|
||||
const lines = generatedUsageLines(readFileSync(CLI_DOC_PATH, "utf8"));
|
||||
// Format guard: if the markers or the `seal-use ` line convention change,
|
||||
// the extraction silently yields nothing and this suite would pass while
|
||||
// checking zero lines.
|
||||
expect(lines.length).toBeGreaterThan(100);
|
||||
|
||||
const rejected: Record<string, string> = {};
|
||||
for (const line of lines) {
|
||||
try {
|
||||
parseArgs(usageToArgv(line));
|
||||
} catch (error) {
|
||||
rejected[line] = error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
}
|
||||
|
||||
expect(rejected).toEqual({});
|
||||
});
|
||||
});
|
||||
+12
-1
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { extractHelpRequest, renderHelp } from "./help.js";
|
||||
import { extractHelpRequest, HELP_REGISTRY, renderHelp } from "./help.js";
|
||||
import { KNOWN_AREAS, runCli } from "./runner.js";
|
||||
|
||||
describe("extractHelpRequest", () => {
|
||||
@@ -210,6 +210,17 @@ describe("help coverage", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("every help registry area is dispatchable", () => {
|
||||
// The forward direction is checked above. The reverse catches a registry
|
||||
// area that renders help but no longer reaches a handler table — the
|
||||
// "documented for two releases after the dispatcher dropped it" failure
|
||||
// mode this suite exists to prevent, applied to `seal-use help` itself.
|
||||
const dead = HELP_REGISTRY.map((entry) => entry.area)
|
||||
.filter((area) => !KNOWN_AREAS.has(area));
|
||||
|
||||
expect(dead).toEqual([]);
|
||||
});
|
||||
|
||||
test("stop shortcut and server alias resolve to help", () => {
|
||||
expect(renderHelp(["server"])).toContain("seal-use mcp");
|
||||
});
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { writeSecureFile } from "./secure-file.js";
|
||||
|
||||
/**
|
||||
* Direct unit tests for the credential write path.
|
||||
*
|
||||
* Indirect coverage exists through loader/token-store mode assertions, but
|
||||
* this helper is the single choke point that keeps existing world-readable
|
||||
* credential files from staying world-readable — the P0 from the 2026-10-17
|
||||
* architecture review. Its guarantees deserve their own colocated tests so a
|
||||
* refactor of either caller cannot silently drop them.
|
||||
*/
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function tempDir(): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), "seal-secure-file-"));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
function fileMode(path: string): number {
|
||||
return statSync(path).mode & 0o777;
|
||||
}
|
||||
|
||||
describe("writeSecureFile", () => {
|
||||
test("creates a new credential file as 0600 with exact content", () => {
|
||||
const dir = tempDir();
|
||||
const target = join(dir, "tokens.json");
|
||||
|
||||
writeSecureFile(target, '{"token":"secret"}\n');
|
||||
|
||||
expect(fileMode(target)).toBe(0o600);
|
||||
expect(readFileSync(target, "utf8")).toBe('{"token":"secret"}\n');
|
||||
});
|
||||
|
||||
test("tightens a pre-existing 0644 file to 0600 on write", () => {
|
||||
const dir = tempDir();
|
||||
const target = join(dir, "corp.json");
|
||||
writeFileSync(target, '{"old":"legacy-inherited"}', { mode: 0o644 });
|
||||
chmodSync(target, 0o644);
|
||||
expect(fileMode(target)).toBe(0o644);
|
||||
|
||||
writeSecureFile(target, '{"new":"rotated-secret"}');
|
||||
|
||||
expect(fileMode(target)).toBe(0o600);
|
||||
expect(readFileSync(target, "utf8")).toBe('{"new":"rotated-secret"}');
|
||||
});
|
||||
|
||||
test("truncates longer previous content so no secret residue survives", () => {
|
||||
const dir = tempDir();
|
||||
const target = join(dir, "tokens.json");
|
||||
writeSecureFile(target, '{"access":"a","refresh":"b","extra":"padding-padding"}');
|
||||
|
||||
writeSecureFile(target, "{}");
|
||||
|
||||
expect(readFileSync(target, "utf8")).toBe("{}");
|
||||
});
|
||||
|
||||
test("accepts binary content such as embedded skill assets", () => {
|
||||
const dir = tempDir();
|
||||
const target = join(dir, "asset.png");
|
||||
const bytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x00, 0xff]);
|
||||
|
||||
writeSecureFile(target, bytes);
|
||||
|
||||
expect(fileMode(target)).toBe(0o600);
|
||||
expect(new Uint8Array(readFileSync(target))).toEqual(bytes);
|
||||
});
|
||||
|
||||
test("creates missing parent directories as 0700", () => {
|
||||
const dir = tempDir();
|
||||
const target = join(dir, "nested", "deeper", "tokens.json");
|
||||
|
||||
writeSecureFile(target, "{}");
|
||||
|
||||
expect(fileMode(target)).toBe(0o600);
|
||||
expect(fileMode(join(dir, "nested"))).toBe(0o700);
|
||||
expect(fileMode(join(dir, "nested", "deeper"))).toBe(0o700);
|
||||
});
|
||||
|
||||
test("does not rewrite the mode of a user-owned parent directory", () => {
|
||||
const dir = tempDir();
|
||||
const shared = join(dir, "shared");
|
||||
mkdirSync(shared, { mode: 0o755 });
|
||||
chmodSync(shared, 0o755);
|
||||
|
||||
writeSecureFile(join(shared, "corp.json"), "{}");
|
||||
|
||||
expect(fileMode(shared)).toBe(0o755);
|
||||
expect(fileMode(join(shared, "corp.json"))).toBe(0o600);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user