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 { scanTtkbUsageLog, ttkbUsageForWorkspace } from "../src/ttkb_usage"; function toolUse(id: string, file: string): string { return JSON.stringify({ type: "assistant", message: { content: [{ type: "tool_use", name: "Read", id, input: { path: file } }] } }); } function toolResult(id: string, isError = false): string { return JSON.stringify({ type: "user", message: { content: [{ type: "tool_result", tool_use_id: id, is_error: isError, content: "result" }] } }); } describe("TTKB document usage", () => { test("counts only successful absolute Markdown file reads", async () => { const root = mkdtempSync(path.join(tmpdir(), "streamline-ttkb-usage-")); try { const packageRoot = path.join(root, ".ttkb", "packages", "lemon8", "lemon8-knowledge"); const article = path.join(packageRoot, "wikis", "interaction", "scenario.md"); const directory = path.join(packageRoot, "wikis", "interaction"); mkdirSync(directory, { recursive: true }); writeFileSync(article, "# 评论互动机制\n正文\n"); const log = path.join(root, "eval.log"); writeFileSync(log, [ toolUse("ok", article), toolResult("ok"), toolUse("failed-directory", directory), toolResult("failed-directory", true), toolUse("relative", ".ttkb/packages/lemon8/lemon8-knowledge/wikis/index.md"), toolResult("relative"), toolUse("code", path.join(root, "repo", "README.md")), toolResult("code"), ].join("\n")); const reads = await scanTtkbUsageLog(log, "evaluation", 1); expect(reads).toHaveLength(1); expect(reads[0]).toMatchObject({ packageId: "lemon8/lemon8-knowledge", relativePath: "wikis/interaction/scenario.md", stage: "evaluation", round: 1, }); } finally { rmSync(root, { recursive: true, force: true }); } }); test("aggregates repeated reads across rounds and puts articles before indexes", async () => { const workspace = mkdtempSync(path.join(tmpdir(), "streamline-ttkb-workspace-")); const packageBase = mkdtempSync(path.join(tmpdir(), "streamline-ttkb-package-")); try { const packageRoot = path.join(packageBase, ".ttkb", "packages", "lemon8", "lemon8-knowledge"); const index = path.join(packageRoot, "wikis", "index.md"); const article = path.join(packageRoot, "wikis", "interaction", "scenario.md"); mkdirSync(path.dirname(article), { recursive: true }); writeFileSync(index, "# Lemon8 业务知识\n"); writeFileSync(article, "# 场景:互动\n"); const logs = path.join(workspace, "logs"); mkdirSync(logs); writeFileSync(path.join(logs, "eval-r1.log"), [ toolUse("index", index), toolUse("article-1", article), toolResult("index"), toolResult("article-1"), ].join("\n")); writeFileSync(path.join(logs, "session-r2.log"), [toolUse("article-2", article), toolResult("article-2")].join("\n")); const usage = await ttkbUsageForWorkspace(workspace); expect(usage.documentCount).toBe(2); expect(usage.packageCount).toBe(1); expect(usage.documents[0]).toMatchObject({ title: "场景:互动", navigation: false, stages: ["evaluation", "execution"], readCount: 2, }); expect(usage.documents[1]).toMatchObject({ title: "Lemon8 业务知识", navigation: true }); expect(JSON.stringify(usage)).not.toContain(packageBase); } finally { rmSync(workspace, { recursive: true, force: true }); rmSync(packageBase, { recursive: true, force: true }); } }); });