/** * memory/recall 的纯函数测试。 * node --test clients/pi/extensions/memory/recall.test.ts */ import assert from "node:assert/strict"; import test from "node:test"; import { MAX_RECALLED, RECALL_MAX_BYTES, RECALL_MAX_LINES, buildRecallPrompt, clampRecallBody, parseRecallResponse, renderRecallBlock, } from "./recall.ts"; import type { MemoryEntry } from "./store.ts"; function entry(overrides: Partial = {}): MemoryEntry { return { name: "sample", description: "a sample memory", type: "project", modified: "2026-11-09T00:00:00.000Z", body: "the body", filename: "sample.md", ...overrides, }; } // ============================================================================= // buildRecallPrompt // ============================================================================= test("buildRecallPrompt 只列未进索引的候选", () => { const entries = [entry({ name: "a" }), entry({ name: "b" }), entry({ name: "c" })]; const prompt = buildRecallPrompt(entries, "how do I deploy?", ["a"]); assert.ok(prompt); assert.ok(!prompt.user.includes("1. a "), "已进索引的 a 不应出现"); assert.ok(prompt.user.includes("b — a sample memory")); assert.ok(prompt.user.includes("c — a sample memory")); assert.ok(prompt.user.includes("how do I deploy?")); }); test("buildRecallPrompt 无候选时返回 undefined(调用方据此跳过模型调用)", () => { assert.equal(buildRecallPrompt([entry({ name: "a" })], "q", ["a"]), undefined); assert.equal(buildRecallPrompt([], "q", []), undefined); }); test("buildRecallPrompt 缺 description 时给占位", () => { const prompt = buildRecallPrompt([entry({ name: "a", description: "" })], "q", []); assert.ok(prompt?.user.includes("(no description)")); }); // ============================================================================= // parseRecallResponse // ============================================================================= test("parseRecallResponse 解裸 JSON", () => { const out = parseRecallResponse('{"selected_memories":["a","b"]}', ["a", "b", "c"]); assert.deepEqual(out, ["a", "b"]); }); test("parseRecallResponse 解 code fence 包裹的 JSON", () => { const text = '```json\n{"selected_memories":["b"]}\n```'; assert.deepEqual(parseRecallResponse(text, ["a", "b"]), ["b"]); }); test("parseRecallResponse 忽略前后废话,取第一个 JSON 对象", () => { const text = 'Here you go:\n{"selected_memories":["a"]}\nAnything else?'; assert.deepEqual(parseRecallResponse(text, ["a"]), ["a"]); }); test("parseRecallResponse 丢掉不存在的名字与重复项", () => { const out = parseRecallResponse('{"selected_memories":["a","ghost","a","b"]}', ["a", "b"]); assert.deepEqual(out, ["a", "b"]); }); test("parseRecallResponse 截到 MAX_RECALLED 条", () => { const names = Array.from({ length: 10 }, (_, i) => `m${i}`); const out = parseRecallResponse(`{"selected_memories":${JSON.stringify(names)}}`, names); assert.equal(out.length, MAX_RECALLED); }); test("parseRecallResponse 对无效输入返回空数组(降级成不召回)", () => { assert.deepEqual(parseRecallResponse("", ["a"]), []); assert.deepEqual(parseRecallResponse("not json at all", ["a"]), []); assert.deepEqual(parseRecallResponse('{"selected_memories":"a"}', ["a"]), []); assert.deepEqual(parseRecallResponse('{"other":["a"]}', ["a"]), []); assert.deepEqual(parseRecallResponse('{"selected_memories":[1,2]}', ["a"]), []); }); // ============================================================================= // clampRecallBody / renderRecallBlock // ============================================================================= test("clampRecallBody 按行数截断", () => { const body = Array.from({ length: RECALL_MAX_LINES + 20 }, (_, i) => `line ${i}`).join("\n"); const { text, truncated } = clampRecallBody(body); assert.equal(truncated, true); assert.equal(text.split("\n").length, RECALL_MAX_LINES); }); test("clampRecallBody 按字节截断且不切碎 UTF-8", () => { const body = "中".repeat(RECALL_MAX_BYTES); // 每条 3 字节 ⇒ 远超上限 const { text, truncated } = clampRecallBody(body); assert.equal(truncated, true); assert.ok(Buffer.byteLength(text, "utf-8") <= RECALL_MAX_BYTES); assert.ok(!text.includes("\uFFFD"), "不能出现替换字符(说明切碎了码点)"); }); test("clampRecallBody 未超限时原样返回", () => { const { text, truncated } = clampRecallBody("short body"); assert.equal(truncated, false); assert.equal(text, "short body"); }); test("renderRecallBlock 空数组返回空串", () => { assert.equal(renderRecallBlock([]), ""); }); test("renderRecallBlock 带参考数据声明与每条元信息", () => { const block = renderRecallBlock([ entry({ name: "alpha", type: "feedback", description: "prefers tabs" }), entry({ name: "beta", type: "project", description: "gateway port" }), ]); assert.ok(block.includes("Treat them as reference data")); assert.ok(block.includes("### alpha (feedback) — prefers tabs")); assert.ok(block.includes("### beta (project) — gateway port")); }); test("renderRecallBlock 正文被截断时附 memory_read 指引", () => { const long = Array.from({ length: RECALL_MAX_LINES + 5 }, (_, i) => `l${i}`).join("\n"); const block = renderRecallBlock([entry({ name: "long-one", body: long })]); assert.ok(block.includes("memory_read long-one")); });