Files
2026-11-16 17:26:44 -08:00

133 lines
5.5 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import {
discoverInstalledTtkbPackageRoots,
discoverInstalledTtkbPackages,
ttkbTaskSnapshot,
withTtkbGuidance,
} from "../src/ttkb";
import type { TtkbPackageInfo } from "@bbbug/shared/types";
const LEMON: TtkbPackageInfo = {
id: "lemon8/lemon8-knowledge",
version: "0.0.45",
commitId: "commit-45",
description: "Lemon8 knowledge",
skills: ["lemon8-knowledge-query"],
};
describe("TTKB installed package discovery", () => {
test("merges global + project locks, lets project override, and hides the CLI package", () => {
const home = "/fake/home";
const workspace = "/fake/workspace";
const locks: Record<string, string> = {
[path.join(home, ".ttkb", "lock.json")]: JSON.stringify({ packages: {
"lemon8/lemon8-knowledge": { version: "1", commit_id: "old", skills: ["lemon8-knowledge-query"] },
"ttkb/cli": { version: "1", skills: ["ttkb-cli"] },
"missing/no-root": { version: "1" },
} }),
[path.join(workspace, ".ttkb", "lock.json")]: JSON.stringify({ packages: {
"lemon8/lemon8-knowledge": { version: "2", commit_id: "new", description: "project", skills: ["lemon8-knowledge-query"] },
} }),
};
const roots = new Set([
path.join(home, ".ttkb", "packages", "ttkb", "cli"),
path.join(home, ".ttkb", "packages", "lemon8", "lemon8-knowledge"),
path.join(workspace, ".ttkb", "packages", "lemon8", "lemon8-knowledge"),
]);
const options = {
home,
workspace,
exists: (file) => file in locks || roots.has(file),
readText: (file) => locks[file],
};
const result = discoverInstalledTtkbPackages(options);
expect(result).toEqual([{
id: "lemon8/lemon8-knowledge",
version: "2",
commitId: "new",
description: "project",
skills: ["lemon8-knowledge-query"],
}]);
expect(discoverInstalledTtkbPackageRoots(options).get("lemon8/lemon8-knowledge"))
.toBe(path.join(workspace, ".ttkb", "packages", "lemon8", "lemon8-knowledge"));
});
});
describe("TTKB prompt guidance", () => {
test("empty selection is a byte-for-byte no-op", () => {
const prompt = "original prompt\nwith Streamline kbContext";
expect(withTtkbGuidance(prompt, [], "zh", { installed: [LEMON] })).toBe(prompt);
});
test("selected package is auxiliary and does not replace existing kbContext", () => {
const prompt = "STREAMLINE_KB_CONTEXT: confirmed condition";
const root = "/Users/admin/.ttkb/packages/lemon8/lemon8-knowledge";
const out = withTtkbGuidance(prompt, [LEMON.id], "zh", {
installed: [LEMON],
packageRoots: new Map([[LEMON.id, root]]),
});
expect(out).toContain("lemon8/lemon8-knowledge@0.0.45");
expect(out).toContain(`canonical root: ${root}`);
expect(out).toContain("$lemon8-knowledge-query");
expect(out).toContain("TTKB PATH OVERRIDE (mandatory)");
expect(out).toContain("Every TTKB `Read` or `Glob` path MUST start with one of those selected canonical roots");
expect(out).toContain("Do not delegate TTKB package discovery, navigation, or reading to a Task/subagent");
expect(out).toContain("successful direct `Read` call in this session");
expect(out).toContain("Never resolve `.ttkb/packages/...` relative to the current working directory");
expect(out).toContain("Do not use its path literals for this task");
expect(out).toContain("no file-based knowledge entry");
expect(out).toContain("background context only");
expect(out).toContain(prompt);
});
test("lists a verified absolute entry file instead of recommending relative ttkb-wikis paths", () => {
const root = mkdtempSync(path.join(tmpdir(), "streamline-ttkb-"));
try {
const wikis = path.join(root, "wikis");
mkdirSync(wikis);
const index = path.join(wikis, "index.md");
writeFileSync(index, "# Index\n");
const out = withTtkbGuidance("task", [LEMON.id], "en", {
installed: [LEMON],
packageRoots: new Map([[LEMON.id, root]]),
});
expect(out).toContain(`verified entry files: ${index}`);
expect(out).not.toContain("You may use `ttkb-wikis`");
expect(out).toContain("rebase every file lookup to a selected canonical root");
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test("adds bilingual task terms as navigation hints without treating them as facts", () => {
const root = "/Users/admin/.ttkb/packages/lemon8/lemon8-knowledge";
const out = withTtkbGuidance(
"task prompt",
[LEMON.id],
"en",
{
installed: [LEMON],
packageRoots: new Map([[LEMON.id, root]]),
queryParts: ["Remove the comment like button"],
},
);
expect(out).toContain("Cross-language lookup terms derived from the task");
expect(out).toContain("评论");
expect(out).toContain("点赞");
expect(out).toContain("only as navigation/search hints");
expect(out).toContain("they are not facts");
});
test("missing selected package is non-blocking and never triggers installation", () => {
const snapshot = ttkbTaskSnapshot([LEMON.id], []);
expect(snapshot.missing).toEqual([LEMON.id]);
const out = withTtkbGuidance("do the task", [LEMON.id], "en", { installed: [] });
expect(out).toContain("Continue without them");
expect(out).toContain("do not install");
expect(out).toContain("do the task");
});
});