import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { tmpdir } from "node:os"; let dir: string; let mod: typeof import("../memory-slash-commands.ts"); let extensionDefault: typeof import("../memory-slash-commands.ts").default; function writeDoc(relPath: string, frontmatter: string, body: string): string { const full = join(dir, relPath); writeFileSync(full, `---\n${frontmatter}\n---\n\n${body}`, "utf8"); return full; } before(async () => { dir = mkdtempSync(join(tmpdir(), "memory-slash-commands-")); mod = await import("../memory-slash-commands.ts"); extensionDefault = mod.default; }); after(() => { rmSync(dir, { recursive: true, force: true }); }); test("slashCommandName renders a nested path name with `:`, leaves a flat name unchanged", () => { assert.equal(mod.slashCommandName("research-alpha"), "research-alpha"); assert.equal(mod.slashCommandName("taste/foo"), "taste:foo"); assert.equal(mod.slashCommandName("a/b/c"), "a:b:c"); }); test("discoverSlashDocs only registers docs frontmatter-flagged slash: true", async () => { const flagged = writeDoc("flagged.md", "kind: knowledge\nslash: true", "flagged body"); const unflagged = writeDoc("unflagged.md", "kind: knowledge", "unflagged body"); const explicitFalse = writeDoc("explicit-false.md", "kind: knowledge\nslash: false", "explicit false body"); mod.__setListMemoryDocsForTest(async () => [ { name: "flagged", path: flagged, short_form: "Flagged command", is_dir: false }, { name: "unflagged", path: unflagged, short_form: "Unflagged document", is_dir: false }, { name: "explicit-false", path: explicitFalse, short_form: "Explicitly disabled", is_dir: false }, ]); const docs = await mod.discoverSlashDocs(); assert.deepEqual(docs, [{ name: "flagged", commandName: "flagged", description: "Flagged command" }]); mod.__setListMemoryDocsForTest(undefined); }); test("discoverSlashDocs skips directory INDEX entries", async () => { const dirIndex = writeDoc("dir-index.md", "kind: knowledge\nslash: true", "index body"); mod.__setListMemoryDocsForTest(async () => [{ name: "taste/", path: dirIndex, short_form: "Index", is_dir: true }]); assert.deepEqual(await mod.discoverSlashDocs(), []); mod.__setListMemoryDocsForTest(undefined); }); // Regression guard: `crtr memory list --json` already returns docs in scope // precedence order (project > profile > user > builtin); a doc identity that // resolves higher-precedence (first occurrence) must win even when a // lower-precedence doc of the SAME name is also slash-flagged. test("discoverSlashDocs respects scope precedence — first occurrence of a name wins, even if only the lower-precedence one is slash-flagged", async () => { const projectDoc = writeDoc("project-shadow.md", "kind: knowledge", "project version, not flagged"); const userDoc = writeDoc("user-shadow.md", "kind: knowledge\nslash: true", "user version, flagged"); mod.__setListMemoryDocsForTest(async () => [ { name: "shadow", path: projectDoc, short_form: "Project", is_dir: false }, // project — comes first, wins identity { name: "shadow", path: userDoc, short_form: "User", is_dir: false }, // user — same name, shadowed ]); // The project doc wins the identity and it isn't flagged, so no command registers. assert.deepEqual(await mod.discoverSlashDocs(), []); mod.__setListMemoryDocsForTest(undefined); }); test("discoverSlashDocs silently skips a doc it fails to read", async () => { mod.__setListMemoryDocsForTest(async () => [{ name: "missing", path: join(dir, "does-not-exist.md"), short_form: "Missing", is_dir: false }]); assert.deepEqual(await mod.discoverSlashDocs(), []); mod.__setListMemoryDocsForTest(undefined); }); test("composeSlashPrompt substitutes every $ARGUMENTS occurrence and adds no header when the body uses it", () => { const body = "Topic: $ARGUMENTS\n\nGo research $ARGUMENTS thoroughly."; const result = mod.composeSlashPrompt("research-alpha", " agent memory systems ", body); assert.equal(result, "Topic: agent memory systems\n\nGo research agent memory systems thoroughly."); assert.ok(!result.includes("You invoked")); }); test("composeSlashPrompt substitutes $ARGUMENTS with an empty string when no args were given", () => { const body = "Topic: $ARGUMENTS"; const result = mod.composeSlashPrompt("research-alpha", "", body); assert.equal(result, "Topic: "); assert.ok(!result.includes("You invoked")); }); test("composeSlashPrompt falls back to a header + full body when $ARGUMENTS is absent, args given", () => { const body = "Do the thing."; const result = mod.composeSlashPrompt("my-doc", "some topic", body); assert.match(result, /^You invoked \/my-doc with: some topic/); assert.ok(result.endsWith("Do the thing.")); assert.ok(result.includes(body)); }); test("composeSlashPrompt falls back to a no-args header + full body when $ARGUMENTS is absent, no args given", () => { const body = "Do the thing."; const result = mod.composeSlashPrompt("my-doc", " ", body); assert.match(result, /^You invoked \/my-doc with no arguments\./); assert.ok(result.endsWith("Do the thing.")); }); test("registers deterministic expansion metadata and submission consumes that exact expansion", async () => { const flagged = writeDoc("live.md", "kind: knowledge\nslash: true", "stale body on disk"); mod.__setListMemoryDocsForTest(async () => [{ name: "live", path: flagged, short_form: "Live command description", is_dir: false }]); let readCalls: string[] = []; mod.__setReadMemoryDocForTest(async (name: string) => { readCalls.push(name); return "Topic: $ARGUMENTS"; }); const registered = new Map Promise }>(); const sent: any[] = []; const pi = { registerCommand(name: string, options: any) { registered.set(name, options); }, sendMessage(message: any, options: any) { sent.push({ message, options }); }, } as any; await extensionDefault(pi); assert.ok(registered.has("live")); const command = registered.get("live")! as typeof registered extends Map ? T & { expansion: unknown } : never; assert.equal(command.description, "Live command description"); assert.deepEqual(command.expansion, { kind: "memory-slash", commandName: "live", body: "Topic: $ARGUMENTS", }); assert.deepEqual(readCalls, ["live"]); // registration captures the disclosed body once const ctx = { ui: { notify() {} } }; await command.handler("some topic", ctx); assert.deepEqual(readCalls, ["live"]); // handler reuses the disclosed expansion assert.equal(sent.length, 1); assert.equal(sent[0].options.triggerTurn, true); assert.equal(sent[0].message.content, "Topic: some topic"); mod.__setListMemoryDocsForTest(undefined); mod.__setReadMemoryDocForTest(undefined); }); test("a command whose registration read fails has no preview metadata and notifies on invoke", async () => { const flagged = writeDoc("broken.md", "kind: knowledge\nslash: true", "body"); mod.__setListMemoryDocsForTest(async () => [{ name: "broken", path: flagged, short_form: "Broken command", is_dir: false }]); mod.__setReadMemoryDocForTest(async () => { throw new Error("boom"); }); const registered = new Map Promise }>(); const sent: any[] = []; const notified: any[] = []; const pi = { registerCommand(name: string, options: any) { registered.set(name, options); }, sendMessage(message: any, options: any) { sent.push({ message, options }); }, } as any; await extensionDefault(pi); assert.equal((registered.get("broken") as any).expansion, undefined); const ctx = { ui: { notify(msg: string, level: string) { notified.push({ msg, level }); }, }, }; await registered.get("broken")!.handler("", ctx); assert.equal(sent.length, 0); assert.equal(notified.length, 1); assert.equal(notified[0].level, "error"); assert.match(notified[0].msg, /boom/); mod.__setListMemoryDocsForTest(undefined); mod.__setReadMemoryDocForTest(undefined); });