@agent-wiki/mcp 0.17.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +7 -0
- package/README.md +171 -0
- package/architecture.svg +58 -0
- package/dist/atlassian.d.ts +63 -0
- package/dist/atlassian.d.ts.map +1 -0
- package/dist/atlassian.js +596 -0
- package/dist/atlassian.js.map +1 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +391 -0
- package/dist/cli.js.map +1 -0
- package/dist/cobol/__tests__/call-boundary-lineage.test.d.ts +2 -0
- package/dist/cobol/__tests__/call-boundary-lineage.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/call-boundary-lineage.test.js +505 -0
- package/dist/cobol/__tests__/call-boundary-lineage.test.js.map +1 -0
- package/dist/cobol/__tests__/code-analysis.test.d.ts +2 -0
- package/dist/cobol/__tests__/code-analysis.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/code-analysis.test.js +465 -0
- package/dist/cobol/__tests__/code-analysis.test.js.map +1 -0
- package/dist/cobol/__tests__/db2-table-lineage.test.d.ts +2 -0
- package/dist/cobol/__tests__/db2-table-lineage.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/db2-table-lineage.test.js +182 -0
- package/dist/cobol/__tests__/db2-table-lineage.test.js.map +1 -0
- package/dist/cobol/__tests__/evidence-mapping.test.d.ts +2 -0
- package/dist/cobol/__tests__/evidence-mapping.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/evidence-mapping.test.js +49 -0
- package/dist/cobol/__tests__/evidence-mapping.test.js.map +1 -0
- package/dist/cobol/__tests__/extractors.test.d.ts +2 -0
- package/dist/cobol/__tests__/extractors.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/extractors.test.js +189 -0
- package/dist/cobol/__tests__/extractors.test.js.map +1 -0
- package/dist/cobol/__tests__/field-lineage.test.d.ts +2 -0
- package/dist/cobol/__tests__/field-lineage.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/field-lineage.test.js +329 -0
- package/dist/cobol/__tests__/field-lineage.test.js.map +1 -0
- package/dist/cobol/__tests__/graph.test.d.ts +2 -0
- package/dist/cobol/__tests__/graph.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/graph.test.js +456 -0
- package/dist/cobol/__tests__/graph.test.js.map +1 -0
- package/dist/cobol/__tests__/integration.test.d.ts +2 -0
- package/dist/cobol/__tests__/integration.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/integration.test.js +971 -0
- package/dist/cobol/__tests__/integration.test.js.map +1 -0
- package/dist/cobol/__tests__/lexer.test.d.ts +2 -0
- package/dist/cobol/__tests__/lexer.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/lexer.test.js +111 -0
- package/dist/cobol/__tests__/lexer.test.js.map +1 -0
- package/dist/cobol/__tests__/parser.test.d.ts +2 -0
- package/dist/cobol/__tests__/parser.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/parser.test.js +180 -0
- package/dist/cobol/__tests__/parser.test.js.map +1 -0
- package/dist/cobol/__tests__/variable-tracer.test.d.ts +2 -0
- package/dist/cobol/__tests__/variable-tracer.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/variable-tracer.test.js +256 -0
- package/dist/cobol/__tests__/variable-tracer.test.js.map +1 -0
- package/dist/cobol/__tests__/wiki-gen.test.d.ts +2 -0
- package/dist/cobol/__tests__/wiki-gen.test.d.ts.map +1 -0
- package/dist/cobol/__tests__/wiki-gen.test.js +76 -0
- package/dist/cobol/__tests__/wiki-gen.test.js.map +1 -0
- package/dist/cobol/call-boundary-lineage.d.ts +49 -0
- package/dist/cobol/call-boundary-lineage.d.ts.map +1 -0
- package/dist/cobol/call-boundary-lineage.js +159 -0
- package/dist/cobol/call-boundary-lineage.js.map +1 -0
- package/dist/cobol/db2-table-lineage.d.ts +45 -0
- package/dist/cobol/db2-table-lineage.d.ts.map +1 -0
- package/dist/cobol/db2-table-lineage.js +127 -0
- package/dist/cobol/db2-table-lineage.js.map +1 -0
- package/dist/cobol/evidence-mapping.d.ts +31 -0
- package/dist/cobol/evidence-mapping.d.ts.map +1 -0
- package/dist/cobol/evidence-mapping.js +27 -0
- package/dist/cobol/evidence-mapping.js.map +1 -0
- package/dist/cobol/extractors.d.ts +88 -0
- package/dist/cobol/extractors.d.ts.map +1 -0
- package/dist/cobol/extractors.js +297 -0
- package/dist/cobol/extractors.js.map +1 -0
- package/dist/cobol/field-lineage.d.ts +101 -0
- package/dist/cobol/field-lineage.d.ts.map +1 -0
- package/dist/cobol/field-lineage.js +603 -0
- package/dist/cobol/field-lineage.js.map +1 -0
- package/dist/cobol/graph.d.ts +182 -0
- package/dist/cobol/graph.d.ts.map +1 -0
- package/dist/cobol/graph.js +453 -0
- package/dist/cobol/graph.js.map +1 -0
- package/dist/cobol/lexer.d.ts +15 -0
- package/dist/cobol/lexer.d.ts.map +1 -0
- package/dist/cobol/lexer.js +251 -0
- package/dist/cobol/lexer.js.map +1 -0
- package/dist/cobol/parser.d.ts +13 -0
- package/dist/cobol/parser.d.ts.map +1 -0
- package/dist/cobol/parser.js +584 -0
- package/dist/cobol/parser.js.map +1 -0
- package/dist/cobol/plugin.d.ts +23 -0
- package/dist/cobol/plugin.d.ts.map +1 -0
- package/dist/cobol/plugin.js +507 -0
- package/dist/cobol/plugin.js.map +1 -0
- package/dist/cobol/types.d.ts +73 -0
- package/dist/cobol/types.d.ts.map +1 -0
- package/dist/cobol/types.js +9 -0
- package/dist/cobol/types.js.map +1 -0
- package/dist/cobol/variable-tracer.d.ts +31 -0
- package/dist/cobol/variable-tracer.d.ts.map +1 -0
- package/dist/cobol/variable-tracer.js +361 -0
- package/dist/cobol/variable-tracer.js.map +1 -0
- package/dist/cobol/wiki-gen.d.ts +18 -0
- package/dist/cobol/wiki-gen.d.ts.map +1 -0
- package/dist/cobol/wiki-gen.js +222 -0
- package/dist/cobol/wiki-gen.js.map +1 -0
- package/dist/code-analysis.d.ts +160 -0
- package/dist/code-analysis.d.ts.map +1 -0
- package/dist/code-analysis.js +53 -0
- package/dist/code-analysis.js.map +1 -0
- package/dist/evidence-search.d.ts +43 -0
- package/dist/evidence-search.d.ts.map +1 -0
- package/dist/evidence-search.js +112 -0
- package/dist/evidence-search.js.map +1 -0
- package/dist/evidence-search.test.d.ts +2 -0
- package/dist/evidence-search.test.d.ts.map +1 -0
- package/dist/evidence-search.test.js +98 -0
- package/dist/evidence-search.test.js.map +1 -0
- package/dist/evidence.d.ts +32 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +16 -0
- package/dist/evidence.js.map +1 -0
- package/dist/evidence.test.d.ts +2 -0
- package/dist/evidence.test.d.ts.map +1 -0
- package/dist/evidence.test.js +51 -0
- package/dist/evidence.test.js.map +1 -0
- package/dist/extraction.d.ts +40 -0
- package/dist/extraction.d.ts.map +1 -0
- package/dist/extraction.js +283 -0
- package/dist/extraction.js.map +1 -0
- package/dist/extraction.test.d.ts +5 -0
- package/dist/extraction.test.d.ts.map +1 -0
- package/dist/extraction.test.js +137 -0
- package/dist/extraction.test.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/queue.d.ts +52 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +139 -0
- package/dist/queue.js.map +1 -0
- package/dist/queue.test.d.ts +2 -0
- package/dist/queue.test.d.ts.map +1 -0
- package/dist/queue.test.js +161 -0
- package/dist/queue.test.js.map +1 -0
- package/dist/search.d.ts +163 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +653 -0
- package/dist/search.js.map +1 -0
- package/dist/search.test.d.ts +6 -0
- package/dist/search.test.d.ts.map +1 -0
- package/dist/search.test.js +551 -0
- package/dist/search.test.js.map +1 -0
- package/dist/server.d.ts +33 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +2530 -0
- package/dist/server.js.map +1 -0
- package/dist/server.test.d.ts +8 -0
- package/dist/server.test.d.ts.map +1 -0
- package/dist/server.test.js +2397 -0
- package/dist/server.test.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +5 -0
- package/dist/version.js.map +1 -0
- package/dist/wiki.d.ts +496 -0
- package/dist/wiki.d.ts.map +1 -0
- package/dist/wiki.js +2677 -0
- package/dist/wiki.js.map +1 -0
- package/dist/wiki.test.d.ts +8 -0
- package/dist/wiki.test.d.ts.map +1 -0
- package/dist/wiki.test.js +1881 -0
- package/dist/wiki.test.js.map +1 -0
- package/docs/code-analysis-plugins.md +169 -0
- package/docs/configuration.md +135 -0
- package/docs/diagrams/cobol-analysis-flow.png +0 -0
- package/docs/diagrams/cobol-analysis-flow.svg +156 -0
- package/docs/evidence-envelope.md +176 -0
- package/docs/graph-1.gif +0 -0
- package/docs/legacy-code-knowledge-compiler-prd.md +511 -0
- package/docs/product-direction.md +120 -0
- package/docs/request-optimization.md +201 -0
- package/docs/search.md +266 -0
- package/docs/tools.md +269 -0
- package/graph-viewer/dist/cli.d.ts +3 -0
- package/graph-viewer/dist/cli.d.ts.map +1 -0
- package/graph-viewer/dist/cli.js +97 -0
- package/graph-viewer/dist/cli.js.map +1 -0
- package/graph-viewer/dist/graph.d.ts +14 -0
- package/graph-viewer/dist/graph.d.ts.map +1 -0
- package/graph-viewer/dist/graph.js +96 -0
- package/graph-viewer/dist/graph.js.map +1 -0
- package/graph-viewer/dist/index.d.ts +6 -0
- package/graph-viewer/dist/index.d.ts.map +1 -0
- package/graph-viewer/dist/index.js +4 -0
- package/graph-viewer/dist/index.js.map +1 -0
- package/graph-viewer/dist/parse.d.ts +10 -0
- package/graph-viewer/dist/parse.d.ts.map +1 -0
- package/graph-viewer/dist/parse.js +62 -0
- package/graph-viewer/dist/parse.js.map +1 -0
- package/graph-viewer/dist/server.d.ts +15 -0
- package/graph-viewer/dist/server.d.ts.map +1 -0
- package/graph-viewer/dist/server.js +168 -0
- package/graph-viewer/dist/server.js.map +1 -0
- package/graph-viewer/dist/types.d.ts +48 -0
- package/graph-viewer/dist/types.d.ts.map +1 -0
- package/graph-viewer/dist/types.js +2 -0
- package/graph-viewer/dist/types.js.map +1 -0
- package/graph-viewer/public/app.js +525 -0
- package/graph-viewer/public/index.html +68 -0
- package/graph-viewer/public/style.css +68 -0
- package/package.json +73 -0
- package/skills/aceclaw/SKILL.md +173 -0
- package/skills/agent-wiki/SKILL.md +184 -0
- package/skills/agent-wiki/references/tools-reference.md +368 -0
|
@@ -0,0 +1,2397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for MCP server tool handlers.
|
|
3
|
+
*
|
|
4
|
+
* These test the handleTool dispatch and JSON serialization
|
|
5
|
+
* without starting a real MCP transport.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { Wiki } from "./wiki.js";
|
|
11
|
+
import { handleTool, tryImageBlock } from "./server.js";
|
|
12
|
+
import { splitSections, buildToc } from "./wiki.js";
|
|
13
|
+
const TEST_ROOT = join(import.meta.dirname ?? ".", "__test_server__");
|
|
14
|
+
function freshWiki() {
|
|
15
|
+
return Wiki.init(TEST_ROOT);
|
|
16
|
+
}
|
|
17
|
+
function cleanUp() {
|
|
18
|
+
if (existsSync(TEST_ROOT)) {
|
|
19
|
+
rmSync(TEST_ROOT, { recursive: true, force: true });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// We test the Wiki methods that the server handlers call,
|
|
23
|
+
// since createServer() requires MCP transport setup.
|
|
24
|
+
// This verifies the data layer that backs every tool.
|
|
25
|
+
describe("server tool: raw_add + raw_list", () => {
|
|
26
|
+
beforeEach(cleanUp);
|
|
27
|
+
afterEach(cleanUp);
|
|
28
|
+
it("add then list returns the document", () => {
|
|
29
|
+
const wiki = freshWiki();
|
|
30
|
+
wiki.rawAdd("article.md", {
|
|
31
|
+
content: "# Article\nBody text.",
|
|
32
|
+
sourceUrl: "https://example.com/article",
|
|
33
|
+
description: "Test article",
|
|
34
|
+
tags: ["test"],
|
|
35
|
+
});
|
|
36
|
+
const docs = wiki.rawList();
|
|
37
|
+
expect(docs).toHaveLength(1);
|
|
38
|
+
expect(docs[0].path).toBe("article.md");
|
|
39
|
+
expect(docs[0].sourceUrl).toBe("https://example.com/article");
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe("server tool: raw_read", () => {
|
|
43
|
+
beforeEach(cleanUp);
|
|
44
|
+
afterEach(cleanUp);
|
|
45
|
+
it("reads text content", async () => {
|
|
46
|
+
const wiki = freshWiki();
|
|
47
|
+
wiki.rawAdd("text.txt", { content: "Hello MCP" });
|
|
48
|
+
const result = await wiki.rawRead("text.txt");
|
|
49
|
+
expect(result.binary).toBe(false);
|
|
50
|
+
expect(result.content).toBe("Hello MCP");
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe("server tool: raw_verify", () => {
|
|
54
|
+
beforeEach(cleanUp);
|
|
55
|
+
afterEach(cleanUp);
|
|
56
|
+
it("validates integrity", () => {
|
|
57
|
+
const wiki = freshWiki();
|
|
58
|
+
wiki.rawAdd("ok.txt", { content: "safe" });
|
|
59
|
+
const results = wiki.rawVerify();
|
|
60
|
+
expect(results.every(r => r.status === "ok")).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
describe("server tool: raw_coverage", () => {
|
|
64
|
+
beforeEach(cleanUp);
|
|
65
|
+
afterEach(cleanUp);
|
|
66
|
+
it("returns JSON report with coverage ratio and uncovered list", async () => {
|
|
67
|
+
const wiki = freshWiki();
|
|
68
|
+
wiki.rawAdd("seen.pdf", { content: "x" });
|
|
69
|
+
wiki.rawAdd("unseen.pdf", { content: "y" });
|
|
70
|
+
wiki.write("p.md", "---\ntitle: P\ntype: concept\nsources: [raw/seen.pdf]\n---\n");
|
|
71
|
+
const result = await handleTool(wiki, "raw_coverage", {});
|
|
72
|
+
const parsed = JSON.parse(result);
|
|
73
|
+
expect(parsed.totalRaw).toBe(2);
|
|
74
|
+
expect(parsed.coveredRaw).toBe(1);
|
|
75
|
+
expect(parsed.uncoveredRaw).toBe(1);
|
|
76
|
+
expect(parsed.coverageRatio).toBe(0.5);
|
|
77
|
+
expect(parsed.uncovered[0].path).toBe("unseen.pdf");
|
|
78
|
+
expect(parsed.truncated).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
it("passes through limit and sort parameters", async () => {
|
|
81
|
+
const wiki = freshWiki();
|
|
82
|
+
wiki.rawAdd("small.txt", { content: "a" });
|
|
83
|
+
wiki.rawAdd("big.txt", { content: "a".repeat(1000) });
|
|
84
|
+
const result = await handleTool(wiki, "raw_coverage", { limit: 1, sort: "largest" });
|
|
85
|
+
const parsed = JSON.parse(result);
|
|
86
|
+
expect(parsed.uncovered).toHaveLength(1);
|
|
87
|
+
expect(parsed.uncovered[0].path).toBe("big.txt");
|
|
88
|
+
expect(parsed.truncated).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
it("rejects invalid sort value", async () => {
|
|
91
|
+
const wiki = freshWiki();
|
|
92
|
+
await expect(handleTool(wiki, "raw_coverage", { sort: "random" })).rejects.toThrow(/Invalid sort/);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
describe("server tool: wiki_write + wiki_read", () => {
|
|
96
|
+
beforeEach(cleanUp);
|
|
97
|
+
afterEach(cleanUp);
|
|
98
|
+
it("write then read round-trips", () => {
|
|
99
|
+
const wiki = freshWiki();
|
|
100
|
+
wiki.write("note-test.md", "---\ntitle: Note\ntype: note\n---\nTest body.");
|
|
101
|
+
const page = wiki.read("note-test.md");
|
|
102
|
+
expect(page.title).toBe("Note");
|
|
103
|
+
expect(page.content).toContain("Test body.");
|
|
104
|
+
});
|
|
105
|
+
it("returns plain text for small pages (< default limit)", async () => {
|
|
106
|
+
const wiki = freshWiki();
|
|
107
|
+
wiki.write("note-small.md", "---\ntitle: Small\ntype: note\n---\nJust a few lines.");
|
|
108
|
+
const result = await handleTool(wiki, "wiki_read", { page: "note-small.md" });
|
|
109
|
+
// Small page: backwards-compatible plain text, not JSON
|
|
110
|
+
expect(typeof result).toBe("string");
|
|
111
|
+
expect(result).toContain("Just a few lines.");
|
|
112
|
+
});
|
|
113
|
+
it("returns paginated JSON for large pages", async () => {
|
|
114
|
+
const wiki = freshWiki();
|
|
115
|
+
const body = Array.from({ length: 300 }, (_, i) => `Line ${i + 1}`).join("\n");
|
|
116
|
+
wiki.write("note-large.md", `---\ntitle: Large\ntype: note\n---\n${body}`);
|
|
117
|
+
const result = await handleTool(wiki, "wiki_read", { page: "note-large.md" });
|
|
118
|
+
const parsed = JSON.parse(result);
|
|
119
|
+
expect(parsed.truncated).toBe(true);
|
|
120
|
+
expect(parsed.total_lines).toBeGreaterThan(200);
|
|
121
|
+
expect(parsed.lines_returned).toBe(200);
|
|
122
|
+
expect(parsed.next_offset).toBe(200);
|
|
123
|
+
});
|
|
124
|
+
it("supports offset and limit for chunked reading", async () => {
|
|
125
|
+
const wiki = freshWiki();
|
|
126
|
+
const body = Array.from({ length: 300 }, (_, i) => `Line ${i + 1}`).join("\n");
|
|
127
|
+
wiki.write("note-large.md", `---\ntitle: Large\ntype: note\n---\n${body}`);
|
|
128
|
+
const result = await handleTool(wiki, "wiki_read", { page: "note-large.md", offset: 100, limit: 50 });
|
|
129
|
+
const parsed = JSON.parse(result);
|
|
130
|
+
expect(parsed.offset).toBe(100);
|
|
131
|
+
expect(parsed.lines_returned).toBe(50);
|
|
132
|
+
expect(parsed.content).toContain("Line 98"); // offset=100 → 0-indexed line 100 = "Line 98" (after 4 frontmatter lines)
|
|
133
|
+
});
|
|
134
|
+
it("caps limit at 500", async () => {
|
|
135
|
+
const wiki = freshWiki();
|
|
136
|
+
const body = Array.from({ length: 600 }, (_, i) => `Line ${i + 1}`).join("\n");
|
|
137
|
+
wiki.write("note-huge.md", `---\ntitle: Huge\ntype: note\n---\n${body}`);
|
|
138
|
+
const result = await handleTool(wiki, "wiki_read", { page: "note-huge.md", limit: 9999 });
|
|
139
|
+
const parsed = JSON.parse(result);
|
|
140
|
+
expect(parsed.lines_returned).toBe(500);
|
|
141
|
+
});
|
|
142
|
+
it("includes TOC when page is truncated", async () => {
|
|
143
|
+
const wiki = freshWiki();
|
|
144
|
+
const body = [
|
|
145
|
+
"## Overview", ...Array(50).fill("overview text"),
|
|
146
|
+
"## Installation", ...Array(50).fill("install text"),
|
|
147
|
+
"## Usage", ...Array(150).fill("usage text"),
|
|
148
|
+
].join("\n");
|
|
149
|
+
wiki.write("note-toc.md", `---\ntitle: Guide\ntype: how-to\n---\n${body}`);
|
|
150
|
+
const result = await handleTool(wiki, "wiki_read", { page: "note-toc.md" });
|
|
151
|
+
const parsed = JSON.parse(result);
|
|
152
|
+
expect(parsed.truncated).toBe(true);
|
|
153
|
+
expect(parsed.toc).toContain("## Overview");
|
|
154
|
+
expect(parsed.toc).toContain("## Installation");
|
|
155
|
+
expect(parsed.toc).toContain("## Usage");
|
|
156
|
+
});
|
|
157
|
+
it("reads a specific section by heading", async () => {
|
|
158
|
+
const wiki = freshWiki();
|
|
159
|
+
const content = [
|
|
160
|
+
"---", "title: Guide", "type: how-to", "---",
|
|
161
|
+
"## Overview", "This is the overview.",
|
|
162
|
+
"## Installation", "Run npm install here.",
|
|
163
|
+
"## Usage", "Import the module.",
|
|
164
|
+
].join("\n");
|
|
165
|
+
wiki.write("note-sections.md", content);
|
|
166
|
+
const result = await handleTool(wiki, "wiki_read", { page: "note-sections.md", section: "Installation" });
|
|
167
|
+
const parsed = JSON.parse(result);
|
|
168
|
+
expect(parsed.section).toContain("Installation");
|
|
169
|
+
expect(parsed.content).toContain("npm install");
|
|
170
|
+
expect(parsed.content).not.toContain("Import the module");
|
|
171
|
+
});
|
|
172
|
+
it("throws with available sections when section not found", async () => {
|
|
173
|
+
const wiki = freshWiki();
|
|
174
|
+
wiki.write("note-secs.md", "---\ntitle: G\ntype: note\n---\n## Intro\nHello.\n## Details\nMore.");
|
|
175
|
+
await expect(handleTool(wiki, "wiki_read", { page: "note-secs.md", section: "Nonexistent" })).rejects.toThrow(/Intro|Details/);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
describe("server tool: batch", () => {
|
|
179
|
+
beforeEach(cleanUp);
|
|
180
|
+
afterEach(cleanUp);
|
|
181
|
+
// ── Batch reads ──────────────────────────────────────────────
|
|
182
|
+
it("reads multiple wiki pages in one call", async () => {
|
|
183
|
+
const wiki = freshWiki();
|
|
184
|
+
wiki.write("note-a.md", "---\ntitle: A\ntype: note\n---\nContent A.");
|
|
185
|
+
wiki.write("note-b.md", "---\ntitle: B\ntype: note\n---\nContent B.");
|
|
186
|
+
const result = await handleTool(wiki, "batch", {
|
|
187
|
+
operations: [
|
|
188
|
+
{ tool: "wiki_read", args: { page: "note-a.md" } },
|
|
189
|
+
{ tool: "wiki_read", args: { page: "note-b.md" } },
|
|
190
|
+
],
|
|
191
|
+
});
|
|
192
|
+
const parsed = JSON.parse(result);
|
|
193
|
+
expect(parsed.count).toBe(2);
|
|
194
|
+
expect(JSON.stringify(parsed.results[0].result)).toContain("Content A");
|
|
195
|
+
expect(JSON.stringify(parsed.results[1].result)).toContain("Content B");
|
|
196
|
+
});
|
|
197
|
+
it("returns per-op error without failing the batch", async () => {
|
|
198
|
+
const wiki = freshWiki();
|
|
199
|
+
wiki.write("note-ok.md", "---\ntitle: OK\ntype: note\n---\nGood.");
|
|
200
|
+
const result = await handleTool(wiki, "batch", {
|
|
201
|
+
operations: [
|
|
202
|
+
{ tool: "wiki_read", args: { page: "note-ok.md" } },
|
|
203
|
+
{ tool: "wiki_read", args: { page: "nonexistent.md" } },
|
|
204
|
+
],
|
|
205
|
+
});
|
|
206
|
+
const parsed = JSON.parse(result);
|
|
207
|
+
expect(parsed.count).toBe(2);
|
|
208
|
+
expect(JSON.stringify(parsed.results[0].result)).toContain("Good");
|
|
209
|
+
expect(parsed.results[1].error).toMatch(/not found/i);
|
|
210
|
+
});
|
|
211
|
+
// ── Batch writes ─────────────────────────────────────────────
|
|
212
|
+
it("writes multiple wiki pages in one call", async () => {
|
|
213
|
+
const wiki = freshWiki();
|
|
214
|
+
const result = await handleTool(wiki, "batch", {
|
|
215
|
+
operations: [
|
|
216
|
+
{ tool: "wiki_write", args: { page: "note-x.md", content: "---\ntitle: X\ntype: note\n---\nBody X." } },
|
|
217
|
+
{ tool: "wiki_write", args: { page: "note-y.md", content: "---\ntitle: Y\ntype: note\n---\nBody Y." } },
|
|
218
|
+
],
|
|
219
|
+
});
|
|
220
|
+
const parsed = JSON.parse(result);
|
|
221
|
+
expect(parsed.count).toBe(2);
|
|
222
|
+
expect(parsed.results[0].result.ok).toBe(true);
|
|
223
|
+
expect(parsed.results[1].result.ok).toBe(true);
|
|
224
|
+
expect(wiki.read("note-x.md").title).toBe("X");
|
|
225
|
+
expect(wiki.read("note-y.md").title).toBe("Y");
|
|
226
|
+
});
|
|
227
|
+
it("continues on individual write errors", async () => {
|
|
228
|
+
const wiki = freshWiki();
|
|
229
|
+
wiki.write("lang/js/concept-js.md", "---\ntitle: JS\ntype: concept\n---\nJS.");
|
|
230
|
+
wiki.rebuildIndex();
|
|
231
|
+
const result = await handleTool(wiki, "batch", {
|
|
232
|
+
operations: [
|
|
233
|
+
{ tool: "wiki_write", args: { page: "note-ok.md", content: "---\ntitle: OK\ntype: note\n---\nFine." } },
|
|
234
|
+
{ tool: "wiki_write", args: { page: "lang/js/index.md", content: "---\ntitle: Bad\n---\nReserved." } },
|
|
235
|
+
],
|
|
236
|
+
});
|
|
237
|
+
const parsed = JSON.parse(result);
|
|
238
|
+
expect(parsed.results[0].result.ok).toBe(true);
|
|
239
|
+
expect(parsed.results[1].error).toMatch(/reserved/i);
|
|
240
|
+
});
|
|
241
|
+
it("auto-classifies content in batch writes", async () => {
|
|
242
|
+
const wiki = freshWiki();
|
|
243
|
+
const result = await handleTool(wiki, "batch", {
|
|
244
|
+
operations: [
|
|
245
|
+
{ tool: "wiki_write", args: { page: "guide.md", content: "---\ntitle: Step by Step Guide\n---\nStep 1: Install. Step 2: Configure. Step 3: Run." } },
|
|
246
|
+
],
|
|
247
|
+
});
|
|
248
|
+
const parsed = JSON.parse(result);
|
|
249
|
+
expect(parsed.results[0].result.autoClassified.type).toBe("how-to");
|
|
250
|
+
});
|
|
251
|
+
// ── Mixed operations ─────────────────────────────────────────
|
|
252
|
+
it("mixes reads, writes, searches, and raw_add in one call", async () => {
|
|
253
|
+
const wiki = freshWiki();
|
|
254
|
+
wiki.write("existing.md", "---\ntitle: Existing\ntype: note\n---\nAlready here.");
|
|
255
|
+
const result = await handleTool(wiki, "batch", {
|
|
256
|
+
operations: [
|
|
257
|
+
{ tool: "wiki_read", args: { page: "existing.md" } },
|
|
258
|
+
{ tool: "wiki_write", args: { page: "new-page.md", content: "---\ntitle: New\ntype: note\n---\nFresh." } },
|
|
259
|
+
{ tool: "wiki_search", args: { query: "Existing" } },
|
|
260
|
+
{ tool: "raw_add", args: { filename: "source.txt", content: "raw data here" } },
|
|
261
|
+
{ tool: "wiki_list", args: {} },
|
|
262
|
+
],
|
|
263
|
+
});
|
|
264
|
+
const parsed = JSON.parse(result);
|
|
265
|
+
expect(parsed.count).toBe(5);
|
|
266
|
+
// wiki_read result
|
|
267
|
+
expect(JSON.stringify(parsed.results[0].result)).toContain("Already here");
|
|
268
|
+
// wiki_write result
|
|
269
|
+
expect(parsed.results[1].result.ok).toBe(true);
|
|
270
|
+
// wiki_search result
|
|
271
|
+
expect(parsed.results[2].result.count).toBeGreaterThan(0);
|
|
272
|
+
// raw_add result
|
|
273
|
+
expect(parsed.results[3].result.ok).toBe(true);
|
|
274
|
+
// wiki_list result
|
|
275
|
+
expect(parsed.results[4].result.count).toBeGreaterThan(0);
|
|
276
|
+
});
|
|
277
|
+
it("batch reads multiple raw files", async () => {
|
|
278
|
+
const wiki = freshWiki();
|
|
279
|
+
wiki.rawAdd("doc-a.txt", { content: "Alpha doc" });
|
|
280
|
+
wiki.rawAdd("doc-b.txt", { content: "Beta doc" });
|
|
281
|
+
const result = await handleTool(wiki, "batch", {
|
|
282
|
+
operations: [
|
|
283
|
+
{ tool: "raw_read", args: { filename: "doc-a.txt" } },
|
|
284
|
+
{ tool: "raw_read", args: { filename: "doc-b.txt" } },
|
|
285
|
+
],
|
|
286
|
+
});
|
|
287
|
+
const parsed = JSON.parse(result);
|
|
288
|
+
expect(parsed.count).toBe(2);
|
|
289
|
+
expect(parsed.results[0].result.content).toBe("Alpha doc");
|
|
290
|
+
expect(parsed.results[1].result.content).toBe("Beta doc");
|
|
291
|
+
});
|
|
292
|
+
it("batch adds multiple raw files", async () => {
|
|
293
|
+
const wiki = freshWiki();
|
|
294
|
+
const result = await handleTool(wiki, "batch", {
|
|
295
|
+
operations: [
|
|
296
|
+
{ tool: "raw_add", args: { filename: "file1.txt", content: "content 1" } },
|
|
297
|
+
{ tool: "raw_add", args: { filename: "file2.txt", content: "content 2" } },
|
|
298
|
+
{ tool: "raw_add", args: { filename: "file3.txt", content: "content 3" } },
|
|
299
|
+
],
|
|
300
|
+
});
|
|
301
|
+
const parsed = JSON.parse(result);
|
|
302
|
+
expect(parsed.count).toBe(3);
|
|
303
|
+
expect(parsed.results.every((r) => r.result.ok)).toBe(true);
|
|
304
|
+
expect(wiki.rawList()).toHaveLength(3);
|
|
305
|
+
});
|
|
306
|
+
it("batch deletes multiple wiki pages", async () => {
|
|
307
|
+
const wiki = freshWiki();
|
|
308
|
+
wiki.write("del-a.md", "---\ntitle: A\n---\nA");
|
|
309
|
+
wiki.write("del-b.md", "---\ntitle: B\n---\nB");
|
|
310
|
+
const result = await handleTool(wiki, "batch", {
|
|
311
|
+
operations: [
|
|
312
|
+
{ tool: "wiki_delete", args: { page: "del-a.md" } },
|
|
313
|
+
{ tool: "wiki_delete", args: { page: "del-b.md" } },
|
|
314
|
+
],
|
|
315
|
+
});
|
|
316
|
+
const parsed = JSON.parse(result);
|
|
317
|
+
expect(parsed.results[0].result.ok).toBe(true);
|
|
318
|
+
expect(parsed.results[1].result.ok).toBe(true);
|
|
319
|
+
expect(wiki.read("del-a.md")).toBeNull();
|
|
320
|
+
expect(wiki.read("del-b.md")).toBeNull();
|
|
321
|
+
});
|
|
322
|
+
// ── Edge cases ───────────────────────────────────────────────
|
|
323
|
+
it("rejects empty operations array", async () => {
|
|
324
|
+
const wiki = freshWiki();
|
|
325
|
+
await expect(handleTool(wiki, "batch", { operations: [] })).rejects.toThrow(/non-empty/);
|
|
326
|
+
});
|
|
327
|
+
it("rejects nested batch calls", async () => {
|
|
328
|
+
const wiki = freshWiki();
|
|
329
|
+
const result = await handleTool(wiki, "batch", {
|
|
330
|
+
operations: [
|
|
331
|
+
{ tool: "batch", args: { operations: [{ tool: "wiki_list" }] } },
|
|
332
|
+
],
|
|
333
|
+
});
|
|
334
|
+
const parsed = JSON.parse(result);
|
|
335
|
+
expect(parsed.results[0].error).toMatch(/nest/i);
|
|
336
|
+
});
|
|
337
|
+
it("preserves inline image data in batch responses", async () => {
|
|
338
|
+
const wiki = freshWiki();
|
|
339
|
+
const pngBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
|
340
|
+
const srcPath = join(TEST_ROOT, "batch-img.png");
|
|
341
|
+
writeFileSync(srcPath, pngBytes);
|
|
342
|
+
const result = await handleTool(wiki, "batch", {
|
|
343
|
+
operations: [
|
|
344
|
+
{ tool: "raw_add", args: { filename: "img.png", source_path: srcPath } },
|
|
345
|
+
],
|
|
346
|
+
});
|
|
347
|
+
const parsed = JSON.parse(result);
|
|
348
|
+
// Result should be the full ContentBlock[] with image data preserved
|
|
349
|
+
const blocks = parsed.results[0].result;
|
|
350
|
+
expect(Array.isArray(blocks)).toBe(true);
|
|
351
|
+
const imgBlock = blocks.find((b) => b.type === "image");
|
|
352
|
+
expect(imgBlock).toBeDefined();
|
|
353
|
+
expect(imgBlock.data).toBe(pngBytes.toString("base64"));
|
|
354
|
+
expect(imgBlock.mimeType).toBe("image/png");
|
|
355
|
+
});
|
|
356
|
+
it("does not allow external callers to skip rebuild via args", async () => {
|
|
357
|
+
const wiki = freshWiki();
|
|
358
|
+
// Even if a caller passes _skipRebuild in args, it should be ignored
|
|
359
|
+
await handleTool(wiki, "wiki_write", {
|
|
360
|
+
page: "sneaky.md",
|
|
361
|
+
content: "---\ntitle: Sneaky\ntype: note\n---\nBody.",
|
|
362
|
+
_skipRebuild: true,
|
|
363
|
+
});
|
|
364
|
+
// Index should still have been rebuilt (rebuildIndex checks opts, not args)
|
|
365
|
+
const idx = readFileSync(join(wiki.config.wikiDir, "index.md"), "utf-8");
|
|
366
|
+
expect(idx).toContain("sneaky");
|
|
367
|
+
});
|
|
368
|
+
it("rejects batch exceeding max operations limit", async () => {
|
|
369
|
+
const wiki = freshWiki();
|
|
370
|
+
const ops = Array.from({ length: 51 }, (_, i) => ({
|
|
371
|
+
tool: "wiki_list",
|
|
372
|
+
}));
|
|
373
|
+
await expect(handleTool(wiki, "batch", { operations: ops })).rejects.toThrow(/limit/i);
|
|
374
|
+
});
|
|
375
|
+
it("deduplicates wiki_rebuild within batch", async () => {
|
|
376
|
+
const wiki = freshWiki();
|
|
377
|
+
wiki.write("page-a.md", "---\ntitle: A\ntype: note\n---\nA.");
|
|
378
|
+
const result = await handleTool(wiki, "batch", {
|
|
379
|
+
operations: [
|
|
380
|
+
{ tool: "wiki_write", args: { page: "page-b.md", content: "---\ntitle: B\ntype: note\n---\nB." } },
|
|
381
|
+
{ tool: "wiki_rebuild" },
|
|
382
|
+
{ tool: "wiki_write", args: { page: "page-c.md", content: "---\ntitle: C\ntype: note\n---\nC." } },
|
|
383
|
+
],
|
|
384
|
+
});
|
|
385
|
+
const parsed = JSON.parse(result);
|
|
386
|
+
// wiki_rebuild should be deferred, not executed inline
|
|
387
|
+
expect(parsed.results[1].result.deferred).toBeTruthy();
|
|
388
|
+
// All pages should be in the index (rebuilt once at end)
|
|
389
|
+
const idx = readFileSync(join(wiki.config.wikiDir, "index.md"), "utf-8");
|
|
390
|
+
expect(idx).toContain("page-b");
|
|
391
|
+
expect(idx).toContain("page-c");
|
|
392
|
+
// Timeline should also be rebuilt since wiki_rebuild was in the batch
|
|
393
|
+
expect(existsSync(join(wiki.config.wikiDir, "timeline.md"))).toBe(true);
|
|
394
|
+
});
|
|
395
|
+
it("handles ops with no args", async () => {
|
|
396
|
+
const wiki = freshWiki();
|
|
397
|
+
wiki.write("p.md", "---\ntitle: P\ntype: note\n---\nP.");
|
|
398
|
+
const result = await handleTool(wiki, "batch", {
|
|
399
|
+
operations: [{ tool: "wiki_list" }],
|
|
400
|
+
});
|
|
401
|
+
const parsed = JSON.parse(result);
|
|
402
|
+
expect(parsed.results[0].result.count).toBeGreaterThan(0);
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
describe("batch vs individual: request count comparison", () => {
|
|
406
|
+
beforeEach(cleanUp);
|
|
407
|
+
afterEach(cleanUp);
|
|
408
|
+
it("batch uses 1 handleTool call instead of N for reads", async () => {
|
|
409
|
+
const wiki = freshWiki();
|
|
410
|
+
const N = 5;
|
|
411
|
+
for (let i = 0; i < N; i++) {
|
|
412
|
+
wiki.write(`note-${i}.md`, `---\ntitle: Note ${i}\ntype: note\n---\nContent ${i}.`);
|
|
413
|
+
}
|
|
414
|
+
// Approach A: N individual calls
|
|
415
|
+
let individualCalls = 0;
|
|
416
|
+
for (let i = 0; i < N; i++) {
|
|
417
|
+
await handleTool(wiki, "wiki_read", { page: `note-${i}.md` });
|
|
418
|
+
individualCalls++;
|
|
419
|
+
}
|
|
420
|
+
// Approach B: 1 batch call
|
|
421
|
+
let batchCalls = 0;
|
|
422
|
+
const result = await handleTool(wiki, "batch", {
|
|
423
|
+
operations: Array.from({ length: N }, (_, i) => ({ tool: "wiki_read", args: { page: `note-${i}.md` } })),
|
|
424
|
+
});
|
|
425
|
+
batchCalls++;
|
|
426
|
+
const parsed = JSON.parse(result);
|
|
427
|
+
expect(parsed.count).toBe(N);
|
|
428
|
+
// N individual → 1 batch = saves N-1 requests
|
|
429
|
+
expect(individualCalls - batchCalls).toBe(N - 1);
|
|
430
|
+
});
|
|
431
|
+
it("batch uses 1 handleTool call instead of N for writes", async () => {
|
|
432
|
+
const wiki = freshWiki();
|
|
433
|
+
const N = 5;
|
|
434
|
+
let individualCalls = 0;
|
|
435
|
+
for (let i = 0; i < N; i++) {
|
|
436
|
+
await handleTool(wiki, "wiki_write", {
|
|
437
|
+
page: `ind-${i}.md`,
|
|
438
|
+
content: `---\ntitle: Ind ${i}\ntype: note\n---\nBody ${i}.`,
|
|
439
|
+
});
|
|
440
|
+
individualCalls++;
|
|
441
|
+
}
|
|
442
|
+
let batchCalls = 0;
|
|
443
|
+
const result = await handleTool(wiki, "batch", {
|
|
444
|
+
operations: Array.from({ length: N }, (_, i) => ({
|
|
445
|
+
tool: "wiki_write",
|
|
446
|
+
args: { page: `bat-${i}.md`, content: `---\ntitle: Bat ${i}\ntype: note\n---\nBody ${i}.` },
|
|
447
|
+
})),
|
|
448
|
+
});
|
|
449
|
+
batchCalls++;
|
|
450
|
+
const parsed = JSON.parse(result);
|
|
451
|
+
expect(parsed.results.filter((r) => r.result?.ok).length).toBe(N);
|
|
452
|
+
expect(individualCalls - batchCalls).toBe(N - 1);
|
|
453
|
+
});
|
|
454
|
+
it("batch returns same content as individual reads", async () => {
|
|
455
|
+
const wiki = freshWiki();
|
|
456
|
+
wiki.write("cmp-a.md", "---\ntitle: A\ntype: note\n---\nAlpha content.");
|
|
457
|
+
wiki.write("cmp-b.md", "---\ntitle: B\ntype: note\n---\nBeta content.");
|
|
458
|
+
const readA = await handleTool(wiki, "wiki_read", { page: "cmp-a.md" });
|
|
459
|
+
const readB = await handleTool(wiki, "wiki_read", { page: "cmp-b.md" });
|
|
460
|
+
const batchResult = await handleTool(wiki, "batch", {
|
|
461
|
+
operations: [
|
|
462
|
+
{ tool: "wiki_read", args: { page: "cmp-a.md" } },
|
|
463
|
+
{ tool: "wiki_read", args: { page: "cmp-b.md" } },
|
|
464
|
+
],
|
|
465
|
+
});
|
|
466
|
+
const parsed = JSON.parse(batchResult);
|
|
467
|
+
expect(JSON.stringify(parsed.results[0].result)).toContain("Alpha content");
|
|
468
|
+
expect(JSON.stringify(parsed.results[1].result)).toContain("Beta content");
|
|
469
|
+
expect(readA).toContain("Alpha content");
|
|
470
|
+
expect(readB).toContain("Beta content");
|
|
471
|
+
});
|
|
472
|
+
it("batch mixes raw_read + raw_add: saves N-1 requests", async () => {
|
|
473
|
+
const wiki = freshWiki();
|
|
474
|
+
const N = 4;
|
|
475
|
+
// Individual: N calls
|
|
476
|
+
let individualCalls = 0;
|
|
477
|
+
for (let i = 0; i < N; i++) {
|
|
478
|
+
await handleTool(wiki, "raw_add", { filename: `ind-${i}.txt`, content: `data ${i}` });
|
|
479
|
+
individualCalls++;
|
|
480
|
+
}
|
|
481
|
+
// Batch: 1 call
|
|
482
|
+
let batchCalls = 0;
|
|
483
|
+
const result = await handleTool(wiki, "batch", {
|
|
484
|
+
operations: Array.from({ length: N }, (_, i) => ({
|
|
485
|
+
tool: "raw_add",
|
|
486
|
+
args: { filename: `bat-${i}.txt`, content: `data ${i}` },
|
|
487
|
+
})),
|
|
488
|
+
});
|
|
489
|
+
batchCalls++;
|
|
490
|
+
expect(JSON.parse(result).count).toBe(N);
|
|
491
|
+
expect(individualCalls - batchCalls).toBe(N - 1);
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
describe("splitSections", () => {
|
|
495
|
+
it("splits by headings", () => {
|
|
496
|
+
const md = "---\ntitle: X\n---\n## Intro\nHello.\n## Details\nMore info.";
|
|
497
|
+
const sections = splitSections(md);
|
|
498
|
+
expect(sections.some(s => s.heading === "## Intro")).toBe(true);
|
|
499
|
+
expect(sections.some(s => s.heading === "## Details")).toBe(true);
|
|
500
|
+
});
|
|
501
|
+
it("does not treat # inside code fences as headings", () => {
|
|
502
|
+
const md = "## Setup\n\n```bash\n# not a heading\nnpm install\n```\n\n## Usage\nDone.";
|
|
503
|
+
const sections = splitSections(md);
|
|
504
|
+
const headings = sections.map(s => s.heading).filter(Boolean);
|
|
505
|
+
expect(headings).toEqual(["## Setup", "## Usage"]);
|
|
506
|
+
expect(headings).not.toContain("# not a heading");
|
|
507
|
+
});
|
|
508
|
+
it("buildToc indentation is relative to shallowest heading (not absolute level)", () => {
|
|
509
|
+
// All H2/H3 — H2 should show flush left, H3 indented once
|
|
510
|
+
const md = "## Overview\n\nText.\n\n### Details\n\nMore.\n\n## Summary\n\nEnd.";
|
|
511
|
+
const sections = splitSections(md);
|
|
512
|
+
const toc = buildToc(sections);
|
|
513
|
+
const lines = toc.split("\n");
|
|
514
|
+
expect(lines[0]).toBe("## Overview"); // H2, minLevel=2 → 0 indent
|
|
515
|
+
expect(lines[1]).toBe(" ### Details"); // H3 → 1 indent (2 spaces)
|
|
516
|
+
expect(lines[2]).toBe("## Summary");
|
|
517
|
+
});
|
|
518
|
+
it("includes sub-sections under parent", async () => {
|
|
519
|
+
const wiki = freshWiki();
|
|
520
|
+
const content = [
|
|
521
|
+
"---", "title: Doc", "type: note", "---",
|
|
522
|
+
"## API", "Top-level API.",
|
|
523
|
+
"### Method A", "Details of A.",
|
|
524
|
+
"### Method B", "Details of B.",
|
|
525
|
+
"## Examples", "Example content.",
|
|
526
|
+
].join("\n");
|
|
527
|
+
wiki.write("note-sub.md", content);
|
|
528
|
+
const result = await handleTool(wiki, "wiki_read", { page: "note-sub.md", section: "API" });
|
|
529
|
+
const parsed = JSON.parse(result);
|
|
530
|
+
expect(parsed.content).toContain("Method A");
|
|
531
|
+
expect(parsed.content).toContain("Method B");
|
|
532
|
+
expect(parsed.content).not.toContain("Example content");
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
describe("server tool: wiki_delete", () => {
|
|
536
|
+
beforeEach(cleanUp);
|
|
537
|
+
afterEach(cleanUp);
|
|
538
|
+
it("deletes a page", () => {
|
|
539
|
+
const wiki = freshWiki();
|
|
540
|
+
wiki.write("deleteme.md", "---\ntitle: D\n---\nBody");
|
|
541
|
+
expect(wiki.delete("deleteme.md")).toBe(true);
|
|
542
|
+
expect(wiki.read("deleteme.md")).toBeNull();
|
|
543
|
+
});
|
|
544
|
+
it("rejects deletion of nested index.md system pages", async () => {
|
|
545
|
+
const wiki = freshWiki();
|
|
546
|
+
wiki.write("lang/js/concept-js.md", "---\ntitle: JS\ntype: concept\n---\nJS.");
|
|
547
|
+
wiki.rebuildIndex();
|
|
548
|
+
expect(existsSync(join(wiki.config.wikiDir, "lang/js/index.md"))).toBe(true);
|
|
549
|
+
// Nested index.md is a system page — deletion should be rejected
|
|
550
|
+
await expect(handleTool(wiki, "wiki_delete", { page: "lang/js/index.md" })).rejects.toThrow("Cannot delete system page");
|
|
551
|
+
});
|
|
552
|
+
it("rejects writing to nested index.md paths", () => {
|
|
553
|
+
const wiki = freshWiki();
|
|
554
|
+
expect(() => wiki.write("lang/js/index.md", "---\ntitle: Custom\n---\nContent")).toThrow("Cannot write to reserved path");
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
describe("server tool: wiki_list", () => {
|
|
558
|
+
beforeEach(cleanUp);
|
|
559
|
+
afterEach(cleanUp);
|
|
560
|
+
it("lists pages with filter", () => {
|
|
561
|
+
const wiki = freshWiki();
|
|
562
|
+
wiki.write("concept-x.md", "---\ntitle: X\ntype: concept\n---\nX");
|
|
563
|
+
wiki.write("event-y.md", "---\ntitle: Y\ntype: event\n---\nY");
|
|
564
|
+
const concepts = wiki.list("concept");
|
|
565
|
+
expect(concepts).toContain("concept-x.md");
|
|
566
|
+
expect(concepts).not.toContain("event-y.md");
|
|
567
|
+
});
|
|
568
|
+
});
|
|
569
|
+
describe("server tool: wiki_search", () => {
|
|
570
|
+
beforeEach(cleanUp);
|
|
571
|
+
afterEach(cleanUp);
|
|
572
|
+
it("finds by keyword", () => {
|
|
573
|
+
const wiki = freshWiki();
|
|
574
|
+
wiki.write("search-target.md", "---\ntitle: Transformer Architecture\n---\nAttention is all you need.");
|
|
575
|
+
const results = wiki.search("attention");
|
|
576
|
+
expect(results.length).toBeGreaterThan(0);
|
|
577
|
+
expect(results[0].path).toBe("search-target.md");
|
|
578
|
+
});
|
|
579
|
+
it("returns snippets only when include_content is false", async () => {
|
|
580
|
+
const wiki = freshWiki();
|
|
581
|
+
wiki.write("page.md", "---\ntitle: Test\ntype: note\n---\n## Overview\nDetailed content here about algorithms.");
|
|
582
|
+
const result = await handleTool(wiki, "wiki_search", { query: "algorithms" });
|
|
583
|
+
const parsed = JSON.parse(result);
|
|
584
|
+
expect(parsed.results[0].snippet).toBeDefined();
|
|
585
|
+
expect(parsed.results[0].content).toBeUndefined();
|
|
586
|
+
});
|
|
587
|
+
it("includes page content when include_content is true", async () => {
|
|
588
|
+
const wiki = freshWiki();
|
|
589
|
+
wiki.write("page.md", "---\ntitle: Test\ntype: note\n---\n## Overview\nDetailed content here about algorithms.");
|
|
590
|
+
const result = await handleTool(wiki, "wiki_search", { query: "algorithms", include_content: true });
|
|
591
|
+
const parsed = JSON.parse(result);
|
|
592
|
+
expect(parsed.results[0].content).toContain("Detailed content here about algorithms");
|
|
593
|
+
});
|
|
594
|
+
it("returns matched section content when include_content is true", async () => {
|
|
595
|
+
const wiki = freshWiki();
|
|
596
|
+
const content = [
|
|
597
|
+
"---", "title: Guide", "type: how-to", "---",
|
|
598
|
+
"## Setup", "Install the package.",
|
|
599
|
+
"## Configuration", "Set the API key for algorithms.",
|
|
600
|
+
"## Usage", "Import and call the function.",
|
|
601
|
+
].join("\n");
|
|
602
|
+
wiki.write("guide.md", content);
|
|
603
|
+
const result = await handleTool(wiki, "wiki_search", { query: "algorithms", include_content: true });
|
|
604
|
+
const parsed = JSON.parse(result);
|
|
605
|
+
const hit = parsed.results.find((r) => r.path === "guide.md");
|
|
606
|
+
expect(hit).toBeDefined();
|
|
607
|
+
// Should return section content, not the whole page
|
|
608
|
+
expect(hit.content).toContain("API key");
|
|
609
|
+
expect(hit.content).not.toContain("Import and call");
|
|
610
|
+
});
|
|
611
|
+
it("truncates large sections at 200 lines with metadata", async () => {
|
|
612
|
+
const wiki = freshWiki();
|
|
613
|
+
const longSection = Array.from({ length: 300 }, (_, i) => `Algorithm line ${i + 1}`).join("\n");
|
|
614
|
+
const content = `---\ntitle: Big\ntype: note\n---\n## Algorithms\n${longSection}\n## Other\nEnd.`;
|
|
615
|
+
wiki.write("big-section.md", content);
|
|
616
|
+
const result = await handleTool(wiki, "wiki_search", { query: "Algorithm", include_content: true });
|
|
617
|
+
const parsed = JSON.parse(result);
|
|
618
|
+
const hit = parsed.results.find((r) => r.path === "big-section.md");
|
|
619
|
+
expect(hit).toBeDefined();
|
|
620
|
+
expect(hit.truncated).toBe(true);
|
|
621
|
+
expect(hit.total_lines).toBeGreaterThan(200);
|
|
622
|
+
expect(hit.content.split("\n").length).toBe(200);
|
|
623
|
+
expect(hit.content).not.toContain("End.");
|
|
624
|
+
});
|
|
625
|
+
it("eliminates the need for follow-up wiki_read (1 request instead of 2)", async () => {
|
|
626
|
+
const wiki = freshWiki();
|
|
627
|
+
wiki.write("doc-a.md", "---\ntitle: Alpha\ntype: note\n---\nAlpha details about neural networks.");
|
|
628
|
+
wiki.write("doc-b.md", "---\ntitle: Beta\ntype: note\n---\nBeta details about neural networks.");
|
|
629
|
+
// Without include_content: need search + batch read = 2 requests
|
|
630
|
+
let calls = 0;
|
|
631
|
+
await handleTool(wiki, "wiki_search", { query: "neural networks" });
|
|
632
|
+
calls++;
|
|
633
|
+
await handleTool(wiki, "batch", {
|
|
634
|
+
operations: [
|
|
635
|
+
{ tool: "wiki_read", args: { page: "doc-a.md" } },
|
|
636
|
+
{ tool: "wiki_read", args: { page: "doc-b.md" } },
|
|
637
|
+
],
|
|
638
|
+
});
|
|
639
|
+
calls++;
|
|
640
|
+
expect(calls).toBe(2);
|
|
641
|
+
// With include_content: 1 request
|
|
642
|
+
let optimizedCalls = 0;
|
|
643
|
+
const result = await handleTool(wiki, "wiki_search", { query: "neural networks", include_content: true });
|
|
644
|
+
optimizedCalls++;
|
|
645
|
+
const parsed = JSON.parse(result);
|
|
646
|
+
expect(parsed.results.length).toBe(2);
|
|
647
|
+
expect(parsed.results.every((r) => r.content !== null)).toBe(true);
|
|
648
|
+
expect(optimizedCalls).toBe(1);
|
|
649
|
+
// Saved 1 request
|
|
650
|
+
expect(calls - optimizedCalls).toBe(1);
|
|
651
|
+
});
|
|
652
|
+
it("has_subsections is true when truncated section has child headings", async () => {
|
|
653
|
+
const wiki = freshWiki();
|
|
654
|
+
// 201 body lines + a sub-heading → section will be truncated and has sub-sections
|
|
655
|
+
const longBody = Array.from({ length: 201 }, (_, i) => `Line ${i + 1} about subsections`).join("\n");
|
|
656
|
+
const content = ["---", "title: Deep Guide", "type: how-to", "---",
|
|
657
|
+
"## Main", longBody, "### Sub A", "Sub-section detail."].join("\n");
|
|
658
|
+
wiki.write("deep-guide.md", content);
|
|
659
|
+
const result = await handleTool(wiki, "wiki_search", { query: "subsections", include_content: true });
|
|
660
|
+
const parsed = JSON.parse(result);
|
|
661
|
+
const hit = parsed.results.find((r) => r.path === "deep-guide.md");
|
|
662
|
+
expect(hit).toBeDefined();
|
|
663
|
+
expect(hit.truncated).toBe(true);
|
|
664
|
+
expect(hit.has_subsections).toBe(true);
|
|
665
|
+
});
|
|
666
|
+
it("has_subsections is omitted when section is not truncated", async () => {
|
|
667
|
+
const wiki = freshWiki();
|
|
668
|
+
const content = ["---", "title: Small Guide", "type: how-to", "---",
|
|
669
|
+
"## Main", "Short content about subsections.", "### Sub A", "Sub content."].join("\n");
|
|
670
|
+
wiki.write("small-guide.md", content);
|
|
671
|
+
const result = await handleTool(wiki, "wiki_search", { query: "subsections", include_content: true });
|
|
672
|
+
const parsed = JSON.parse(result);
|
|
673
|
+
const hit = parsed.results.find((r) => r.path === "small-guide.md");
|
|
674
|
+
expect(hit).toBeDefined();
|
|
675
|
+
expect(hit.truncated).toBeUndefined();
|
|
676
|
+
expect(hit.has_subsections).toBeUndefined();
|
|
677
|
+
});
|
|
678
|
+
it("inline_budget limits inlined content; over-budget results return budget_exceeded", async () => {
|
|
679
|
+
const wiki = freshWiki();
|
|
680
|
+
wiki.write("page-a.md", "---\ntitle: Alpha\ntype: note\n---\nContent about inline budget alpha.");
|
|
681
|
+
wiki.write("page-b.md", "---\ntitle: Beta\ntype: note\n---\nContent about inline budget beta.");
|
|
682
|
+
// Budget of 10 chars — far smaller than any result's content → all should exceed
|
|
683
|
+
const result = await handleTool(wiki, "wiki_search", {
|
|
684
|
+
query: "inline budget",
|
|
685
|
+
include_content: true,
|
|
686
|
+
inline_budget: 10,
|
|
687
|
+
});
|
|
688
|
+
const parsed = JSON.parse(result);
|
|
689
|
+
const exceeded = parsed.results.filter((r) => r.budget_exceeded === true);
|
|
690
|
+
expect(exceeded.length).toBeGreaterThan(0);
|
|
691
|
+
// Exceeded results still carry snippet (from base search result)
|
|
692
|
+
expect(exceeded[0].snippet).toBeDefined();
|
|
693
|
+
expect(exceeded[0].content).toBeUndefined();
|
|
694
|
+
});
|
|
695
|
+
it("no inline_budget returns content for all results", async () => {
|
|
696
|
+
const wiki = freshWiki();
|
|
697
|
+
wiki.write("page-a.md", "---\ntitle: Alpha\ntype: note\n---\nContent about budget test.");
|
|
698
|
+
wiki.write("page-b.md", "---\ntitle: Beta\ntype: note\n---\nContent about budget test.");
|
|
699
|
+
const result = await handleTool(wiki, "wiki_search", { query: "budget test", include_content: true });
|
|
700
|
+
const parsed = JSON.parse(result);
|
|
701
|
+
expect(parsed.results.every((r) => r.budget_exceeded !== true)).toBe(true);
|
|
702
|
+
expect(parsed.results.every((r) => r.content !== undefined)).toBe(true);
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
describe("wiki_search + wiki_search_read: evidence envelope (phase 1)", () => {
|
|
706
|
+
beforeEach(cleanUp);
|
|
707
|
+
afterEach(cleanUp);
|
|
708
|
+
it("includes an evidence envelope on every wiki_search response", async () => {
|
|
709
|
+
const wiki = freshWiki();
|
|
710
|
+
wiki.write("topic.md", "---\ntitle: Transformer Models\nsources: [raw/paper.pdf]\n---\nAttention.");
|
|
711
|
+
const result = await handleTool(wiki, "wiki_search", { query: "Transformer" });
|
|
712
|
+
const parsed = JSON.parse(result);
|
|
713
|
+
expect(parsed.evidence).toBeDefined();
|
|
714
|
+
expect(["strong", "weak", "absent"]).toContain(parsed.evidence.confidence);
|
|
715
|
+
expect(["deterministic", "inferred", "synthesized", "unsupported"]).toContain(parsed.evidence.basis);
|
|
716
|
+
expect(typeof parsed.evidence.abstain).toBe("boolean");
|
|
717
|
+
expect(typeof parsed.evidence.rationale).toBe("string");
|
|
718
|
+
expect(Array.isArray(parsed.evidence.provenance)).toBe(true);
|
|
719
|
+
});
|
|
720
|
+
it("returns abstain envelope when no results found", async () => {
|
|
721
|
+
const wiki = freshWiki();
|
|
722
|
+
wiki.write("unrelated.md", "---\ntitle: Soup\n---\nA recipe.");
|
|
723
|
+
const result = await handleTool(wiki, "wiki_search", { query: "kubernetes orchestration" });
|
|
724
|
+
const parsed = JSON.parse(result);
|
|
725
|
+
expect(parsed.results).toHaveLength(0);
|
|
726
|
+
expect(parsed.evidence.abstain).toBe(true);
|
|
727
|
+
expect(parsed.evidence.confidence).toBe("absent");
|
|
728
|
+
expect(parsed.evidence.basis).toBe("unsupported");
|
|
729
|
+
});
|
|
730
|
+
it("wiki_search_read response carries an evidence envelope", async () => {
|
|
731
|
+
const wiki = freshWiki();
|
|
732
|
+
wiki.write("page.md", "---\ntitle: Test\nsources: [raw/source.md]\n---\n## Section\nContent about widgets.");
|
|
733
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "widgets", readTopN: 1 });
|
|
734
|
+
const parsed = JSON.parse(result);
|
|
735
|
+
expect(parsed.evidence).toBeDefined();
|
|
736
|
+
expect(parsed.evidence.rationale).toBeTypeOf("string");
|
|
737
|
+
});
|
|
738
|
+
it("wiki_search_read downgrades envelope when top page has no sources", async () => {
|
|
739
|
+
const wiki = freshWiki();
|
|
740
|
+
// Two pages, both about the topic. The top match has empty sources.
|
|
741
|
+
wiki.write("a.md", "---\ntitle: Widget Guide\nsources: []\n---\nWidgets and gadgets and gizmos.");
|
|
742
|
+
wiki.write("b.md", "---\ntitle: Different\nsources: [raw/x.md]\n---\nWidgets in passing.");
|
|
743
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Widget Guide gadgets gizmos", readTopN: 1 });
|
|
744
|
+
const parsed = JSON.parse(result);
|
|
745
|
+
expect(parsed.evidence.rationale).toMatch(/no sources/);
|
|
746
|
+
});
|
|
747
|
+
it("wiki_search_read does NOT downgrade for explicit synthesis pages", async () => {
|
|
748
|
+
const wiki = freshWiki();
|
|
749
|
+
wiki.write("compiled.md", "---\ntitle: Compiled View\nsources: []\nsynthesis: true\n---\nAggregated knowledge here.");
|
|
750
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Compiled aggregated knowledge", readTopN: 1 });
|
|
751
|
+
const parsed = JSON.parse(result);
|
|
752
|
+
expect(parsed.evidence.rationale).not.toMatch(/no sources/);
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
describe("server tool: wiki_search_read", () => {
|
|
756
|
+
beforeEach(cleanUp);
|
|
757
|
+
afterEach(cleanUp);
|
|
758
|
+
it("returns search results and page content for top results", async () => {
|
|
759
|
+
const wiki = freshWiki();
|
|
760
|
+
wiki.write("page-a.md", "---\ntitle: A\ntype: note\n---\nAlpha algorithms.");
|
|
761
|
+
wiki.write("page-b.md", "---\ntitle: B\ntype: note\n---\nBeta algorithms.");
|
|
762
|
+
wiki.write("page-c.md", "---\ntitle: C\ntype: note\n---\nGamma algorithms.");
|
|
763
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "algorithms", readTopN: 2 });
|
|
764
|
+
const parsed = JSON.parse(result);
|
|
765
|
+
expect(parsed.count).toBe(3);
|
|
766
|
+
expect(parsed.pages.length).toBe(2);
|
|
767
|
+
expect(parsed.pagesRead).toBe(2);
|
|
768
|
+
expect(parsed.nextReads.length).toBe(1);
|
|
769
|
+
expect(parsed.pages.every((p) => p.content !== null)).toBe(true);
|
|
770
|
+
});
|
|
771
|
+
it("deduplicates multiple hits on same page", async () => {
|
|
772
|
+
const wiki = freshWiki();
|
|
773
|
+
wiki.write("multi.md", "---\ntitle: Multi\ntype: note\n---\n## Intro\nalgorithm overview\n## Details\nalgorithm details\n## Summary\nalgorithm summary");
|
|
774
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "algorithm", readTopN: 5 });
|
|
775
|
+
const parsed = JSON.parse(result);
|
|
776
|
+
// Multiple search hits but only 1 unique page → pages should have 1 entry
|
|
777
|
+
expect(parsed.pages.length).toBe(1);
|
|
778
|
+
expect(parsed.pages[0].path).toBe("multi.md");
|
|
779
|
+
});
|
|
780
|
+
it("readTopN defaults to 3", async () => {
|
|
781
|
+
const wiki = freshWiki();
|
|
782
|
+
for (let i = 0; i < 5; i++) {
|
|
783
|
+
wiki.write(`note-${i}.md`, `---\ntitle: Note ${i}\ntype: note\n---\nKeyword${i} searchable.`);
|
|
784
|
+
}
|
|
785
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "searchable" });
|
|
786
|
+
const parsed = JSON.parse(result);
|
|
787
|
+
expect(parsed.pages.length).toBe(3);
|
|
788
|
+
expect(parsed.nextReads.length).toBe(2);
|
|
789
|
+
});
|
|
790
|
+
it("section filter reads only matched section", async () => {
|
|
791
|
+
const wiki = freshWiki();
|
|
792
|
+
wiki.write("guide-a.md", "---\ntitle: Guide A\ntype: how-to\n---\n## Setup\nInstall step A.\n## Usage\nRun it A.");
|
|
793
|
+
wiki.write("guide-b.md", "---\ntitle: Guide B\ntype: how-to\n---\n## Setup\nInstall step B.\n## Usage\nRun it B.");
|
|
794
|
+
const result = await handleTool(wiki, "wiki_search_read", {
|
|
795
|
+
query: "Install",
|
|
796
|
+
readTopN: 2,
|
|
797
|
+
section: "Setup",
|
|
798
|
+
});
|
|
799
|
+
const parsed = JSON.parse(result);
|
|
800
|
+
for (const page of parsed.pages) {
|
|
801
|
+
if (page.content) {
|
|
802
|
+
expect(page.content).toContain("Install");
|
|
803
|
+
expect(page.content).not.toContain("Run it");
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
it("section not found returns null content with toc", async () => {
|
|
808
|
+
const wiki = freshWiki();
|
|
809
|
+
wiki.write("has-it.md", "---\ntitle: Has\ntype: note\n---\n## API\nEndpoint details.\n## Other\nStuff.");
|
|
810
|
+
wiki.write("missing-it.md", "---\ntitle: Missing\ntype: note\n---\n## Overview\nGeneral info about API.");
|
|
811
|
+
const result = await handleTool(wiki, "wiki_search_read", {
|
|
812
|
+
query: "API",
|
|
813
|
+
readTopN: 2,
|
|
814
|
+
section: "API",
|
|
815
|
+
});
|
|
816
|
+
const parsed = JSON.parse(result);
|
|
817
|
+
const hasIt = parsed.pages.find((p) => p.path === "has-it.md");
|
|
818
|
+
const missingIt = parsed.pages.find((p) => p.path === "missing-it.md");
|
|
819
|
+
expect(hasIt.content).toContain("Endpoint");
|
|
820
|
+
expect(missingIt.content).toBeNull();
|
|
821
|
+
expect(missingIt.error).toMatch(/not found/i);
|
|
822
|
+
expect(missingIt.toc).toBeTruthy();
|
|
823
|
+
});
|
|
824
|
+
it("truncates pages at perPageLimit lines", async () => {
|
|
825
|
+
const wiki = freshWiki();
|
|
826
|
+
const body = Array.from({ length: 300 }, (_, i) => `Searchable line ${i}`).join("\n");
|
|
827
|
+
wiki.write("big.md", `---\ntitle: Big\ntype: note\n---\n${body}`);
|
|
828
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Searchable", perPageLimit: 100 });
|
|
829
|
+
const parsed = JSON.parse(result);
|
|
830
|
+
expect(parsed.pages[0].truncated).toBe(true);
|
|
831
|
+
expect(parsed.pages[0].total_lines).toBeGreaterThan(100);
|
|
832
|
+
expect(parsed.pages[0].content.split("\n").length).toBe(100);
|
|
833
|
+
});
|
|
834
|
+
it("includeToc adds TOC to truncated pages", async () => {
|
|
835
|
+
const wiki = freshWiki();
|
|
836
|
+
const body = Array.from({ length: 300 }, (_, i) => `Searchable line ${i}`).join("\n");
|
|
837
|
+
wiki.write("toc-page.md", `---\ntitle: Toc\ntype: note\n---\n## First\n${body}\n## Second\nMore.`);
|
|
838
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Searchable", includeToc: true });
|
|
839
|
+
const parsed = JSON.parse(result);
|
|
840
|
+
expect(parsed.pages[0].truncated).toBe(true);
|
|
841
|
+
expect(parsed.pages[0].toc).toContain("## First");
|
|
842
|
+
expect(parsed.pages[0].toc).toContain("## Second");
|
|
843
|
+
});
|
|
844
|
+
it("includeToc=false omits TOC even when truncated", async () => {
|
|
845
|
+
const wiki = freshWiki();
|
|
846
|
+
const body = Array.from({ length: 300 }, (_, i) => `Searchable line ${i}`).join("\n");
|
|
847
|
+
wiki.write("no-toc.md", `---\ntitle: NoToc\ntype: note\n---\n## Heading\n${body}`);
|
|
848
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Searchable" });
|
|
849
|
+
const parsed = JSON.parse(result);
|
|
850
|
+
expect(parsed.pages[0].truncated).toBe(true);
|
|
851
|
+
expect(parsed.pages[0].toc).toBeUndefined();
|
|
852
|
+
});
|
|
853
|
+
it("handles deleted page gracefully", async () => {
|
|
854
|
+
const wiki = freshWiki();
|
|
855
|
+
wiki.write("alive.md", "---\ntitle: Alive\ntype: note\n---\nKeyword here.");
|
|
856
|
+
wiki.write("doomed.md", "---\ntitle: Doomed\ntype: note\n---\nKeyword there.");
|
|
857
|
+
// Search first to index both, then delete one
|
|
858
|
+
wiki.search("Keyword");
|
|
859
|
+
rmSync(join(wiki.config.wikiDir, "doomed.md"));
|
|
860
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Keyword", readTopN: 5 });
|
|
861
|
+
const parsed = JSON.parse(result);
|
|
862
|
+
const alive = parsed.pages.find((p) => p.path === "alive.md");
|
|
863
|
+
const doomed = parsed.pages.find((p) => p.path === "doomed.md");
|
|
864
|
+
expect(alive.content).toContain("Keyword");
|
|
865
|
+
expect(doomed.content).toBeNull();
|
|
866
|
+
expect(doomed.error).toBeTruthy();
|
|
867
|
+
});
|
|
868
|
+
it("no results returns empty arrays and knowledge_gap", async () => {
|
|
869
|
+
const wiki = freshWiki();
|
|
870
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "zzzznonexistent" });
|
|
871
|
+
const parsed = JSON.parse(result);
|
|
872
|
+
expect(parsed.results).toEqual([]);
|
|
873
|
+
expect(parsed.pages).toEqual([]);
|
|
874
|
+
expect(parsed.nextReads).toEqual([]);
|
|
875
|
+
expect(parsed.count).toBe(0);
|
|
876
|
+
expect(parsed.pagesRead).toBe(0);
|
|
877
|
+
expect(parsed.knowledge_gap).toBeDefined();
|
|
878
|
+
});
|
|
879
|
+
it("readTopN capped at 10", async () => {
|
|
880
|
+
const wiki = freshWiki();
|
|
881
|
+
for (let i = 0; i < 12; i++) {
|
|
882
|
+
wiki.write(`p-${i}.md`, `---\ntitle: P${i}\ntype: note\n---\nFindable content.`);
|
|
883
|
+
}
|
|
884
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Findable", readTopN: 99, limit: 12 });
|
|
885
|
+
const parsed = JSON.parse(result);
|
|
886
|
+
expect(parsed.pages.length).toBeLessThanOrEqual(10);
|
|
887
|
+
});
|
|
888
|
+
it("perPageLimit capped at 500", async () => {
|
|
889
|
+
const wiki = freshWiki();
|
|
890
|
+
const body = Array.from({ length: 600 }, (_, i) => `Searchable line ${i}`).join("\n");
|
|
891
|
+
wiki.write("huge.md", `---\ntitle: Huge\ntype: note\n---\n${body}`);
|
|
892
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "Searchable", perPageLimit: 9999 });
|
|
893
|
+
const parsed = JSON.parse(result);
|
|
894
|
+
expect(parsed.pages[0].content.split("\n").length).toBeLessThanOrEqual(500);
|
|
895
|
+
});
|
|
896
|
+
it("eliminates 2-request search+batch workflow", async () => {
|
|
897
|
+
const wiki = freshWiki();
|
|
898
|
+
wiki.write("doc-x.md", "---\ntitle: X\ntype: note\n---\nNeural network details.");
|
|
899
|
+
wiki.write("doc-y.md", "---\ntitle: Y\ntype: note\n---\nNeural network training.");
|
|
900
|
+
// Old: 2 requests
|
|
901
|
+
let oldCalls = 0;
|
|
902
|
+
await handleTool(wiki, "wiki_search", { query: "neural network" });
|
|
903
|
+
oldCalls++;
|
|
904
|
+
await handleTool(wiki, "batch", {
|
|
905
|
+
operations: [
|
|
906
|
+
{ tool: "wiki_read", args: { page: "doc-x.md" } },
|
|
907
|
+
{ tool: "wiki_read", args: { page: "doc-y.md" } },
|
|
908
|
+
],
|
|
909
|
+
});
|
|
910
|
+
oldCalls++;
|
|
911
|
+
// New: 1 request
|
|
912
|
+
let newCalls = 0;
|
|
913
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "neural network", readTopN: 5 });
|
|
914
|
+
newCalls++;
|
|
915
|
+
const parsed = JSON.parse(result);
|
|
916
|
+
expect(parsed.pagesRead).toBe(2);
|
|
917
|
+
expect(parsed.pages.every((p) => p.content !== null)).toBe(true);
|
|
918
|
+
expect(oldCalls - newCalls).toBe(1);
|
|
919
|
+
});
|
|
920
|
+
});
|
|
921
|
+
describe("server tool: wiki_lint", () => {
|
|
922
|
+
beforeEach(cleanUp);
|
|
923
|
+
afterEach(cleanUp);
|
|
924
|
+
it("returns a valid report", () => {
|
|
925
|
+
const wiki = freshWiki();
|
|
926
|
+
wiki.write("lint-target.md", "---\ntitle: Lint Target\ntype: concept\n---\nSee [[broken-link]].");
|
|
927
|
+
const report = wiki.lint();
|
|
928
|
+
expect(report.pagesChecked).toBeGreaterThan(0);
|
|
929
|
+
expect(report.issues.some(i => i.category === "broken-link")).toBe(true);
|
|
930
|
+
});
|
|
931
|
+
it("sequential lints produce valid cache file", () => {
|
|
932
|
+
const wiki = freshWiki();
|
|
933
|
+
wiki.rawAdd("cache-check.txt", { content: "test" });
|
|
934
|
+
wiki.lint();
|
|
935
|
+
wiki.lint();
|
|
936
|
+
const cachePath = join(wiki.config.workspace, ".lint-cache.json");
|
|
937
|
+
expect(existsSync(cachePath)).toBe(true);
|
|
938
|
+
const cache = JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
939
|
+
expect(cache.version).toBe(1);
|
|
940
|
+
expect(cache.entries["cache-check.txt"]).toBeDefined();
|
|
941
|
+
expect(cache.entries["cache-check.txt"].status).toBe("ok");
|
|
942
|
+
});
|
|
943
|
+
});
|
|
944
|
+
describe("server tool: wiki_classify", () => {
|
|
945
|
+
beforeEach(cleanUp);
|
|
946
|
+
afterEach(cleanUp);
|
|
947
|
+
it("classifies content", () => {
|
|
948
|
+
const wiki = freshWiki();
|
|
949
|
+
const result = wiki.classify("---\ntitle: Step by Step Guide\n---\nStep 1: Do this. Step 2: Do that. Install the software.");
|
|
950
|
+
expect(result.type).toBe("how-to");
|
|
951
|
+
expect(result.confidence).toBeGreaterThan(0);
|
|
952
|
+
});
|
|
953
|
+
});
|
|
954
|
+
describe("server tool: wiki_synthesize", () => {
|
|
955
|
+
beforeEach(cleanUp);
|
|
956
|
+
afterEach(cleanUp);
|
|
957
|
+
it("synthesizes context from pages", () => {
|
|
958
|
+
const wiki = freshWiki();
|
|
959
|
+
wiki.write("src-1.md", "---\ntitle: Source 1\ntags: [ai]\n---\nInsight one.");
|
|
960
|
+
wiki.write("src-2.md", "---\ntitle: Source 2\ntags: [ml]\n---\nInsight two.");
|
|
961
|
+
const ctx = wiki.synthesizeContext(["src-1.md", "src-2.md"]);
|
|
962
|
+
expect(ctx.pages).toHaveLength(2);
|
|
963
|
+
expect(ctx.suggestions.length).toBeGreaterThan(0);
|
|
964
|
+
});
|
|
965
|
+
});
|
|
966
|
+
describe("server tool: wiki_schemas", () => {
|
|
967
|
+
beforeEach(cleanUp);
|
|
968
|
+
afterEach(cleanUp);
|
|
969
|
+
it("returns all schemas", () => {
|
|
970
|
+
const wiki = freshWiki();
|
|
971
|
+
const schemas = wiki.schemas();
|
|
972
|
+
expect(schemas.length).toBe(10);
|
|
973
|
+
});
|
|
974
|
+
});
|
|
975
|
+
describe("server tool: wiki_log", () => {
|
|
976
|
+
beforeEach(cleanUp);
|
|
977
|
+
afterEach(cleanUp);
|
|
978
|
+
it("returns log entries", () => {
|
|
979
|
+
const wiki = freshWiki();
|
|
980
|
+
wiki.write("log-page.md", "---\ntitle: L\n---\nBody");
|
|
981
|
+
const log = wiki.getLog(5);
|
|
982
|
+
expect(log.length).toBeGreaterThan(0);
|
|
983
|
+
});
|
|
984
|
+
});
|
|
985
|
+
describe("server tool: wiki_rebuild_index + wiki_rebuild_timeline", () => {
|
|
986
|
+
beforeEach(cleanUp);
|
|
987
|
+
afterEach(cleanUp);
|
|
988
|
+
it("rebuilds without error", () => {
|
|
989
|
+
const wiki = freshWiki();
|
|
990
|
+
wiki.write("p1.md", "---\ntitle: P1\ntype: concept\n---\nX");
|
|
991
|
+
expect(() => wiki.rebuildIndex()).not.toThrow();
|
|
992
|
+
expect(() => wiki.rebuildTimeline()).not.toThrow();
|
|
993
|
+
});
|
|
994
|
+
});
|
|
995
|
+
describe("server tool: knowledge_ingest_batch", () => {
|
|
996
|
+
const SOURCE_DIR = join(TEST_ROOT, "__ingest_source__");
|
|
997
|
+
beforeEach(() => {
|
|
998
|
+
cleanUp();
|
|
999
|
+
mkdirSync(SOURCE_DIR, { recursive: true });
|
|
1000
|
+
});
|
|
1001
|
+
afterEach(cleanUp);
|
|
1002
|
+
it("ingests a single text file", async () => {
|
|
1003
|
+
const wiki = freshWiki();
|
|
1004
|
+
writeFileSync(join(SOURCE_DIR, "readme.txt"), "Hello world\nLine two\nLine three");
|
|
1005
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1006
|
+
source_path: SOURCE_DIR,
|
|
1007
|
+
topic: "test-topic",
|
|
1008
|
+
});
|
|
1009
|
+
const parsed = JSON.parse(result);
|
|
1010
|
+
expect(parsed.ok).toBe(true);
|
|
1011
|
+
expect(parsed.matched).toBe(1);
|
|
1012
|
+
expect(parsed.imported).toBe(1);
|
|
1013
|
+
expect(parsed.extracted).toBe(1);
|
|
1014
|
+
expect(parsed.packs).toBeGreaterThan(0);
|
|
1015
|
+
expect(parsed.packPaths[0]).toContain("digest-packs/test-topic/pack-001.md");
|
|
1016
|
+
// Pack file exists in raw/
|
|
1017
|
+
expect(existsSync(join(wiki.config.rawDir, "digest-packs/test-topic/pack-001.md"))).toBe(true);
|
|
1018
|
+
});
|
|
1019
|
+
it("ingests a directory with multiple files", async () => {
|
|
1020
|
+
const wiki = freshWiki();
|
|
1021
|
+
writeFileSync(join(SOURCE_DIR, "doc1.md"), "# Doc 1\nContent one.");
|
|
1022
|
+
writeFileSync(join(SOURCE_DIR, "doc2.md"), "# Doc 2\nContent two.");
|
|
1023
|
+
writeFileSync(join(SOURCE_DIR, "doc3.txt"), "Plain text three.");
|
|
1024
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1025
|
+
source_path: SOURCE_DIR,
|
|
1026
|
+
topic: "multi",
|
|
1027
|
+
});
|
|
1028
|
+
const parsed = JSON.parse(result);
|
|
1029
|
+
expect(parsed.matched).toBe(3);
|
|
1030
|
+
expect(parsed.imported).toBe(3);
|
|
1031
|
+
expect(parsed.extracted).toBe(3);
|
|
1032
|
+
// Verify pack contains provenance headers
|
|
1033
|
+
const packContent = readFileSync(join(wiki.config.rawDir, "digest-packs/multi/pack-001.md"), "utf-8");
|
|
1034
|
+
expect(packContent).toContain("## raw/multi/");
|
|
1035
|
+
});
|
|
1036
|
+
it("skips already-imported files", async () => {
|
|
1037
|
+
const wiki = freshWiki();
|
|
1038
|
+
writeFileSync(join(SOURCE_DIR, "exists.txt"), "Already here.");
|
|
1039
|
+
// Pre-import with same raw path
|
|
1040
|
+
wiki.rawAdd("skip-topic/exists.txt", { content: "Already here." });
|
|
1041
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1042
|
+
source_path: SOURCE_DIR,
|
|
1043
|
+
topic: "skip-topic",
|
|
1044
|
+
});
|
|
1045
|
+
const parsed = JSON.parse(result);
|
|
1046
|
+
expect(parsed.matched).toBe(1);
|
|
1047
|
+
expect(parsed.skipped).toBe(1);
|
|
1048
|
+
expect(parsed.imported).toBe(0);
|
|
1049
|
+
});
|
|
1050
|
+
it("filters by pattern", async () => {
|
|
1051
|
+
const wiki = freshWiki();
|
|
1052
|
+
writeFileSync(join(SOURCE_DIR, "keep.md"), "# Keep\nContent.");
|
|
1053
|
+
writeFileSync(join(SOURCE_DIR, "skip.txt"), "Skip this.");
|
|
1054
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1055
|
+
source_path: SOURCE_DIR,
|
|
1056
|
+
topic: "pattern",
|
|
1057
|
+
pattern: "*.md",
|
|
1058
|
+
});
|
|
1059
|
+
const parsed = JSON.parse(result);
|
|
1060
|
+
expect(parsed.matched).toBe(1);
|
|
1061
|
+
expect(parsed.imported).toBe(1);
|
|
1062
|
+
});
|
|
1063
|
+
it("respects maxFiles limit", async () => {
|
|
1064
|
+
const wiki = freshWiki();
|
|
1065
|
+
for (let i = 0; i < 10; i++) {
|
|
1066
|
+
writeFileSync(join(SOURCE_DIR, `file${i}.txt`), `Content ${i}`);
|
|
1067
|
+
}
|
|
1068
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1069
|
+
source_path: SOURCE_DIR,
|
|
1070
|
+
maxFiles: 3,
|
|
1071
|
+
topic: "limited",
|
|
1072
|
+
});
|
|
1073
|
+
const parsed = JSON.parse(result);
|
|
1074
|
+
expect(parsed.matched).toBe(3);
|
|
1075
|
+
});
|
|
1076
|
+
it("continues on error with continueOnError", async () => {
|
|
1077
|
+
const wiki = freshWiki();
|
|
1078
|
+
writeFileSync(join(SOURCE_DIR, "good.txt"), "Good content.");
|
|
1079
|
+
// Create a file with .pdf extension but invalid content
|
|
1080
|
+
writeFileSync(join(SOURCE_DIR, "bad.pdf"), "not a real pdf");
|
|
1081
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1082
|
+
source_path: SOURCE_DIR,
|
|
1083
|
+
topic: "errors",
|
|
1084
|
+
continueOnError: true,
|
|
1085
|
+
});
|
|
1086
|
+
const parsed = JSON.parse(result);
|
|
1087
|
+
// Good file should be processed
|
|
1088
|
+
expect(parsed.extracted).toBeGreaterThan(0);
|
|
1089
|
+
// Bad file should be in errors
|
|
1090
|
+
expect(parsed.failed).toBeGreaterThan(0);
|
|
1091
|
+
expect(parsed.errors[0].file).toContain("bad.pdf");
|
|
1092
|
+
});
|
|
1093
|
+
it("splits into multiple packs when content exceeds packLines", async () => {
|
|
1094
|
+
const wiki = freshWiki();
|
|
1095
|
+
// Create files with enough content to exceed packLines
|
|
1096
|
+
for (let i = 0; i < 3; i++) {
|
|
1097
|
+
const body = Array.from({ length: 100 }, (_, j) => `Line ${j} of file ${i}`).join("\n");
|
|
1098
|
+
writeFileSync(join(SOURCE_DIR, `big${i}.txt`), body);
|
|
1099
|
+
}
|
|
1100
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1101
|
+
source_path: SOURCE_DIR,
|
|
1102
|
+
topic: "split",
|
|
1103
|
+
packLines: 120, // 3 files x 100 lines > 120, should create multiple packs
|
|
1104
|
+
chunkLines: 50,
|
|
1105
|
+
});
|
|
1106
|
+
const parsed = JSON.parse(result);
|
|
1107
|
+
expect(parsed.packs).toBeGreaterThan(1);
|
|
1108
|
+
expect(parsed.packPaths.length).toBe(parsed.packs);
|
|
1109
|
+
});
|
|
1110
|
+
it("cleans stale packs from previous runs", async () => {
|
|
1111
|
+
const wiki = freshWiki();
|
|
1112
|
+
// First run: create enough content for 3 packs
|
|
1113
|
+
for (let i = 0; i < 3; i++) {
|
|
1114
|
+
const body = Array.from({ length: 100 }, (_, j) => `Run1 file${i} line ${j}`).join("\n");
|
|
1115
|
+
writeFileSync(join(SOURCE_DIR, `run1-${i}.txt`), body);
|
|
1116
|
+
}
|
|
1117
|
+
const result1 = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1118
|
+
source_path: SOURCE_DIR,
|
|
1119
|
+
topic: "stale",
|
|
1120
|
+
packLines: 120,
|
|
1121
|
+
chunkLines: 50,
|
|
1122
|
+
});
|
|
1123
|
+
const parsed1 = JSON.parse(result1);
|
|
1124
|
+
expect(parsed1.packs).toBeGreaterThanOrEqual(3);
|
|
1125
|
+
// Clean source dir and create less content for second run
|
|
1126
|
+
for (const f of ["run1-0.txt", "run1-1.txt", "run1-2.txt"])
|
|
1127
|
+
rmSync(join(SOURCE_DIR, f));
|
|
1128
|
+
writeFileSync(join(SOURCE_DIR, "run2.txt"), "Short content for run 2.");
|
|
1129
|
+
const result2 = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1130
|
+
source_path: SOURCE_DIR,
|
|
1131
|
+
topic: "stale",
|
|
1132
|
+
});
|
|
1133
|
+
const parsed2 = JSON.parse(result2);
|
|
1134
|
+
expect(parsed2.packs).toBe(1);
|
|
1135
|
+
// Old pack-002, pack-003 should be cleaned
|
|
1136
|
+
const packsDir = join(wiki.config.rawDir, "digest-packs", "stale");
|
|
1137
|
+
expect(existsSync(join(packsDir, "pack-001.md"))).toBe(true);
|
|
1138
|
+
expect(existsSync(join(packsDir, "pack-002.md"))).toBe(false);
|
|
1139
|
+
expect(existsSync(join(packsDir, "pack-003.md"))).toBe(false);
|
|
1140
|
+
});
|
|
1141
|
+
it("clamps chunkLines to packLines so no pack exceeds limit", async () => {
|
|
1142
|
+
const wiki = freshWiki();
|
|
1143
|
+
// 200 lines of content, packLines=80, chunkLines=200 (would exceed pack)
|
|
1144
|
+
const body = Array.from({ length: 200 }, (_, i) => `Line ${i}`).join("\n");
|
|
1145
|
+
writeFileSync(join(SOURCE_DIR, "big.txt"), body);
|
|
1146
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1147
|
+
source_path: SOURCE_DIR,
|
|
1148
|
+
topic: "clamp",
|
|
1149
|
+
packLines: 80,
|
|
1150
|
+
chunkLines: 200, // should be clamped to 80
|
|
1151
|
+
});
|
|
1152
|
+
const parsed = JSON.parse(result);
|
|
1153
|
+
// With chunkLines clamped to 80, each chunk ≤ 80 lines, each pack ≤ 80 lines
|
|
1154
|
+
// 200 lines / 80 = 3 chunks → at least 3 packs
|
|
1155
|
+
expect(parsed.packs).toBeGreaterThanOrEqual(3);
|
|
1156
|
+
// Verify no pack exceeds packLines (80) by much
|
|
1157
|
+
for (const packPath of parsed.packPaths) {
|
|
1158
|
+
const rawPath = packPath.replace(/^raw\//, "");
|
|
1159
|
+
const content = readFileSync(join(wiki.config.rawDir, rawPath), "utf-8");
|
|
1160
|
+
// Pack includes frontmatter + headers, so body lines should be ≤ chunkLines
|
|
1161
|
+
const bodyLines = content.split("\n").filter((l) => !l.startsWith("---") && !l.startsWith("title:") && !l.startsWith("topic:") && !l.startsWith("sources:") && !l.startsWith("totalChunks:"));
|
|
1162
|
+
// Each pack's content portion (excluding frontmatter/headers) should not vastly exceed packLines
|
|
1163
|
+
expect(bodyLines.length).toBeLessThan(packPath === parsed.packPaths[0] ? 120 : 120);
|
|
1164
|
+
}
|
|
1165
|
+
});
|
|
1166
|
+
it("ingests HTML with provenance", async () => {
|
|
1167
|
+
const wiki = freshWiki();
|
|
1168
|
+
writeFileSync(join(SOURCE_DIR, "page.html"), "<html><body><p>Hello HTML</p></body></html>");
|
|
1169
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1170
|
+
source_path: SOURCE_DIR,
|
|
1171
|
+
topic: "html-test",
|
|
1172
|
+
});
|
|
1173
|
+
const parsed = JSON.parse(result);
|
|
1174
|
+
expect(parsed.extracted).toBe(1);
|
|
1175
|
+
const packContent = readFileSync(join(wiki.config.rawDir, "digest-packs/html-test/pack-001.md"), "utf-8");
|
|
1176
|
+
expect(packContent).toContain("Hello HTML");
|
|
1177
|
+
expect(packContent).toContain("## raw/html-test/page.html");
|
|
1178
|
+
});
|
|
1179
|
+
it("rejects topic with path traversal", async () => {
|
|
1180
|
+
const wiki = freshWiki();
|
|
1181
|
+
writeFileSync(join(SOURCE_DIR, "ok.txt"), "content");
|
|
1182
|
+
await expect(handleTool(wiki, "knowledge_ingest_batch", {
|
|
1183
|
+
source_path: SOURCE_DIR,
|
|
1184
|
+
topic: "../../wiki",
|
|
1185
|
+
})).rejects.toThrow(/invalid topic/i);
|
|
1186
|
+
});
|
|
1187
|
+
it("sanitizes topic to safe slug", async () => {
|
|
1188
|
+
const wiki = freshWiki();
|
|
1189
|
+
writeFileSync(join(SOURCE_DIR, "ok.txt"), "content");
|
|
1190
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1191
|
+
source_path: SOURCE_DIR,
|
|
1192
|
+
topic: "my topic (v2)",
|
|
1193
|
+
});
|
|
1194
|
+
const parsed = JSON.parse(result);
|
|
1195
|
+
// Spaces and parens should be replaced with hyphens
|
|
1196
|
+
expect(parsed.packPaths[0]).toMatch(/my-topic--v2-/);
|
|
1197
|
+
expect(parsed.packPaths[0]).not.toContain(" ");
|
|
1198
|
+
expect(parsed.packPaths[0]).not.toContain("(");
|
|
1199
|
+
});
|
|
1200
|
+
});
|
|
1201
|
+
describe("server tool: knowledge_digest_write", () => {
|
|
1202
|
+
beforeEach(cleanUp);
|
|
1203
|
+
afterEach(cleanUp);
|
|
1204
|
+
it("writes a single digest page with provenance", async () => {
|
|
1205
|
+
const wiki = freshWiki();
|
|
1206
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1207
|
+
pages: [{
|
|
1208
|
+
page: "summary-transformers.md",
|
|
1209
|
+
title: "Transformer Architecture Summary",
|
|
1210
|
+
body: "# Transformer Architecture\n\nAttention is all you need.\n\n## Key Concepts\n\nSelf-attention, multi-head attention.",
|
|
1211
|
+
type: "summary",
|
|
1212
|
+
tags: ["ai", "transformers"],
|
|
1213
|
+
sources: ["raw/paper.pdf"],
|
|
1214
|
+
sourcePacks: ["raw/digest-packs/ai/pack-001.md"],
|
|
1215
|
+
}],
|
|
1216
|
+
});
|
|
1217
|
+
const parsed = JSON.parse(result);
|
|
1218
|
+
expect(parsed.written).toBe(1);
|
|
1219
|
+
expect(parsed.results[0].ok).toBe(true);
|
|
1220
|
+
// Verify the page exists and has correct frontmatter
|
|
1221
|
+
const page = wiki.read(parsed.results[0].page);
|
|
1222
|
+
expect(page).not.toBeNull();
|
|
1223
|
+
expect(page.title).toBe("Transformer Architecture Summary");
|
|
1224
|
+
expect(page.type).toBe("summary");
|
|
1225
|
+
expect(page.tags).toContain("ai");
|
|
1226
|
+
// Read raw content to check provenance fields
|
|
1227
|
+
const rawContent = readFileSync(join(wiki.config.wikiDir, parsed.results[0].page), "utf-8");
|
|
1228
|
+
expect(rawContent).toContain("raw/paper.pdf");
|
|
1229
|
+
expect(rawContent).toContain("raw/digest-packs/ai/pack-001.md");
|
|
1230
|
+
});
|
|
1231
|
+
it("writes multiple pages with one rebuild", async () => {
|
|
1232
|
+
const wiki = freshWiki();
|
|
1233
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1234
|
+
pages: [
|
|
1235
|
+
{ page: "concept-a.md", title: "Concept A", body: "Body A." },
|
|
1236
|
+
{ page: "concept-b.md", title: "Concept B", body: "Body B." },
|
|
1237
|
+
{ page: "concept-c.md", title: "Concept C", body: "Body C." },
|
|
1238
|
+
],
|
|
1239
|
+
});
|
|
1240
|
+
const parsed = JSON.parse(result);
|
|
1241
|
+
expect(parsed.written).toBe(3);
|
|
1242
|
+
expect(parsed.count).toBe(3);
|
|
1243
|
+
// All pages should be in the index (rebuilt once at end)
|
|
1244
|
+
const idx = readFileSync(join(wiki.config.wikiDir, "index.md"), "utf-8");
|
|
1245
|
+
expect(idx).toContain("concept-a");
|
|
1246
|
+
expect(idx).toContain("concept-b");
|
|
1247
|
+
expect(idx).toContain("concept-c");
|
|
1248
|
+
});
|
|
1249
|
+
it("auto-classifies when type is omitted", async () => {
|
|
1250
|
+
const wiki = freshWiki();
|
|
1251
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1252
|
+
pages: [{
|
|
1253
|
+
page: "guide.md",
|
|
1254
|
+
title: "How to Install",
|
|
1255
|
+
body: "Step 1: Download. Step 2: Install. Step 3: Configure. Step 4: Run.",
|
|
1256
|
+
}],
|
|
1257
|
+
});
|
|
1258
|
+
const parsed = JSON.parse(result);
|
|
1259
|
+
expect(parsed.results[0].autoClassified.type).toBe("how-to");
|
|
1260
|
+
});
|
|
1261
|
+
it("auto-routes to topic subdirectory", async () => {
|
|
1262
|
+
const wiki = freshWiki();
|
|
1263
|
+
// Create a topic directory first
|
|
1264
|
+
wiki.write("ai/concept-existing.md", "---\ntitle: Existing\ntype: concept\ntopic: ai\n---\nExisting.");
|
|
1265
|
+
wiki.rebuildIndex();
|
|
1266
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1267
|
+
pages: [{
|
|
1268
|
+
page: "concept-new.md",
|
|
1269
|
+
title: "New AI Concept",
|
|
1270
|
+
body: "Something new about AI.",
|
|
1271
|
+
topic: "ai",
|
|
1272
|
+
}],
|
|
1273
|
+
});
|
|
1274
|
+
const parsed = JSON.parse(result);
|
|
1275
|
+
expect(parsed.results[0].routed).toBe(true);
|
|
1276
|
+
expect(parsed.results[0].page).toContain("ai/");
|
|
1277
|
+
});
|
|
1278
|
+
it("continues on individual write errors", async () => {
|
|
1279
|
+
const wiki = freshWiki();
|
|
1280
|
+
wiki.write("lang/js/concept-js.md", "---\ntitle: JS\ntype: concept\n---\nJS.");
|
|
1281
|
+
wiki.rebuildIndex();
|
|
1282
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1283
|
+
pages: [
|
|
1284
|
+
{ page: "ok-page.md", title: "OK", body: "Fine." },
|
|
1285
|
+
{ page: "lang/js/index.md", title: "Bad", body: "Reserved." },
|
|
1286
|
+
],
|
|
1287
|
+
});
|
|
1288
|
+
const parsed = JSON.parse(result);
|
|
1289
|
+
expect(parsed.written).toBe(1);
|
|
1290
|
+
expect(parsed.results[0].ok).toBe(true);
|
|
1291
|
+
expect(parsed.results[1].ok).toBe(false);
|
|
1292
|
+
expect(parsed.results[1].error).toMatch(/reserved/i);
|
|
1293
|
+
});
|
|
1294
|
+
it("rejects topic with path traversal", async () => {
|
|
1295
|
+
const wiki = freshWiki();
|
|
1296
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1297
|
+
pages: [{
|
|
1298
|
+
page: "evil.md",
|
|
1299
|
+
title: "Evil",
|
|
1300
|
+
body: "Content.",
|
|
1301
|
+
topic: "../../etc",
|
|
1302
|
+
}],
|
|
1303
|
+
});
|
|
1304
|
+
const parsed = JSON.parse(result);
|
|
1305
|
+
expect(parsed.results[0].ok).toBe(false);
|
|
1306
|
+
expect(parsed.results[0].error).toMatch(/invalid topic/i);
|
|
1307
|
+
});
|
|
1308
|
+
it("rejects empty pages array", async () => {
|
|
1309
|
+
const wiki = freshWiki();
|
|
1310
|
+
await expect(handleTool(wiki, "knowledge_digest_write", { pages: [] })).rejects.toThrow(/non-empty/);
|
|
1311
|
+
});
|
|
1312
|
+
it("handles malformed page items without crashing the batch", async () => {
|
|
1313
|
+
const wiki = freshWiki();
|
|
1314
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1315
|
+
pages: [
|
|
1316
|
+
null,
|
|
1317
|
+
{ page: "ok.md", title: "OK", body: "Fine." },
|
|
1318
|
+
{ page: 123, title: "Bad", body: "Missing string page." },
|
|
1319
|
+
],
|
|
1320
|
+
});
|
|
1321
|
+
const parsed = JSON.parse(result);
|
|
1322
|
+
expect(parsed.count).toBe(3);
|
|
1323
|
+
expect(parsed.written).toBe(1);
|
|
1324
|
+
// Malformed items get per-item errors, not a whole-tool crash
|
|
1325
|
+
expect(parsed.results[0].ok).toBe(false);
|
|
1326
|
+
expect(parsed.results[0].page).toBe("(item 0)");
|
|
1327
|
+
expect(parsed.results[1].ok).toBe(true);
|
|
1328
|
+
expect(parsed.results[2].ok).toBe(false);
|
|
1329
|
+
expect(parsed.results[2].page).toBe("(item 2)");
|
|
1330
|
+
});
|
|
1331
|
+
it("rejects pages exceeding limit", async () => {
|
|
1332
|
+
const wiki = freshWiki();
|
|
1333
|
+
const pages = Array.from({ length: 101 }, (_, i) => ({
|
|
1334
|
+
page: `p${i}.md`, title: `P${i}`, body: `Body ${i}`,
|
|
1335
|
+
}));
|
|
1336
|
+
await expect(handleTool(wiki, "knowledge_digest_write", { pages })).rejects.toThrow(/limit/i);
|
|
1337
|
+
});
|
|
1338
|
+
it("handles YAML special characters in title safely", async () => {
|
|
1339
|
+
const wiki = freshWiki();
|
|
1340
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
1341
|
+
pages: [{
|
|
1342
|
+
page: "special.md",
|
|
1343
|
+
title: 'Title with: colon, "quotes", and #hash',
|
|
1344
|
+
body: "Body content.",
|
|
1345
|
+
}],
|
|
1346
|
+
});
|
|
1347
|
+
const parsed = JSON.parse(result);
|
|
1348
|
+
expect(parsed.written).toBe(1);
|
|
1349
|
+
const page = wiki.read(parsed.results[0].page);
|
|
1350
|
+
expect(page).not.toBeNull();
|
|
1351
|
+
expect(page.title).toBe('Title with: colon, "quotes", and #hash');
|
|
1352
|
+
});
|
|
1353
|
+
});
|
|
1354
|
+
describe("server tool: wiki_config", () => {
|
|
1355
|
+
beforeEach(cleanUp);
|
|
1356
|
+
afterEach(cleanUp);
|
|
1357
|
+
it("returns config object", () => {
|
|
1358
|
+
const wiki = freshWiki();
|
|
1359
|
+
const cfg = wiki.config;
|
|
1360
|
+
expect(cfg.configRoot).toBe(TEST_ROOT);
|
|
1361
|
+
expect(cfg.wikiDir).toContain("wiki");
|
|
1362
|
+
expect(cfg.rawDir).toContain("raw");
|
|
1363
|
+
});
|
|
1364
|
+
it("includes search config with hybrid=false by default", async () => {
|
|
1365
|
+
const wiki = freshWiki();
|
|
1366
|
+
const result = await handleTool(wiki, "wiki_config", {});
|
|
1367
|
+
expect(typeof result).toBe("string");
|
|
1368
|
+
const parsed = JSON.parse(result);
|
|
1369
|
+
expect(parsed.search).toBeDefined();
|
|
1370
|
+
expect(parsed.search.hybrid).toBe(false);
|
|
1371
|
+
expect(typeof parsed.search.bm25Weight).toBe("number");
|
|
1372
|
+
expect(typeof parsed.search.vectorWeight).toBe("number");
|
|
1373
|
+
expect(typeof parsed.search.model).toBe("string");
|
|
1374
|
+
});
|
|
1375
|
+
});
|
|
1376
|
+
// ═══ Image ContentBlock tests (server layer) ═══════════════════
|
|
1377
|
+
describe("tryImageBlock", () => {
|
|
1378
|
+
beforeEach(cleanUp);
|
|
1379
|
+
afterEach(cleanUp);
|
|
1380
|
+
it("returns image block for supported image file", () => {
|
|
1381
|
+
const wiki = freshWiki();
|
|
1382
|
+
const filePath = join(wiki.config.rawDir, "test.png");
|
|
1383
|
+
const bytes = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
|
1384
|
+
writeFileSync(filePath, bytes);
|
|
1385
|
+
const block = tryImageBlock(filePath, "image/png");
|
|
1386
|
+
expect(block).not.toBeNull();
|
|
1387
|
+
expect(block.type).toBe("image");
|
|
1388
|
+
expect(block.data).toBe(bytes.toString("base64"));
|
|
1389
|
+
expect(block.mimeType).toBe("image/png");
|
|
1390
|
+
});
|
|
1391
|
+
it("returns null for non-image mime types", () => {
|
|
1392
|
+
const wiki = freshWiki();
|
|
1393
|
+
const filePath = join(wiki.config.rawDir, "doc.pdf");
|
|
1394
|
+
writeFileSync(filePath, "fake pdf");
|
|
1395
|
+
expect(tryImageBlock(filePath, "application/pdf")).toBeNull();
|
|
1396
|
+
});
|
|
1397
|
+
it("returns null for non-existent file", () => {
|
|
1398
|
+
expect(tryImageBlock("/does/not/exist.png", "image/png")).toBeNull();
|
|
1399
|
+
});
|
|
1400
|
+
it("returns null for oversized image", () => {
|
|
1401
|
+
const wiki = freshWiki();
|
|
1402
|
+
const filePath = join(wiki.config.rawDir, "huge.png");
|
|
1403
|
+
writeFileSync(filePath, Buffer.alloc(11 * 1024 * 1024));
|
|
1404
|
+
expect(tryImageBlock(filePath, "image/png")).toBeNull();
|
|
1405
|
+
});
|
|
1406
|
+
});
|
|
1407
|
+
describe("handleTool: raw_add image ContentBlock[]", () => {
|
|
1408
|
+
beforeEach(cleanUp);
|
|
1409
|
+
afterEach(cleanUp);
|
|
1410
|
+
it("returns mixed [text, image] blocks for a single image file", async () => {
|
|
1411
|
+
const wiki = freshWiki();
|
|
1412
|
+
const pngBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
|
1413
|
+
const srcPath = join(TEST_ROOT, "input.png");
|
|
1414
|
+
writeFileSync(srcPath, pngBytes);
|
|
1415
|
+
const result = await handleTool(wiki, "raw_add", {
|
|
1416
|
+
filename: "diagram.png",
|
|
1417
|
+
source_path: srcPath,
|
|
1418
|
+
});
|
|
1419
|
+
expect(Array.isArray(result)).toBe(true);
|
|
1420
|
+
const blocks = result;
|
|
1421
|
+
expect(blocks).toHaveLength(2);
|
|
1422
|
+
expect(blocks[0].type).toBe("text");
|
|
1423
|
+
expect(blocks[1].type).toBe("image");
|
|
1424
|
+
const imgBlock = blocks[1];
|
|
1425
|
+
expect(imgBlock.mimeType).toBe("image/png");
|
|
1426
|
+
expect(imgBlock.data).toBe(pngBytes.toString("base64"));
|
|
1427
|
+
});
|
|
1428
|
+
it("returns plain text string for non-image files", async () => {
|
|
1429
|
+
const wiki = freshWiki();
|
|
1430
|
+
const result = await handleTool(wiki, "raw_add", {
|
|
1431
|
+
filename: "notes.md",
|
|
1432
|
+
content: "# Notes\nSome text.",
|
|
1433
|
+
});
|
|
1434
|
+
expect(typeof result).toBe("string");
|
|
1435
|
+
const parsed = JSON.parse(result);
|
|
1436
|
+
expect(parsed.ok).toBe(true);
|
|
1437
|
+
expect(parsed.document.path).toBe("notes.md");
|
|
1438
|
+
});
|
|
1439
|
+
});
|
|
1440
|
+
describe("handleTool: raw_read image ContentBlock[]", () => {
|
|
1441
|
+
beforeEach(cleanUp);
|
|
1442
|
+
afterEach(cleanUp);
|
|
1443
|
+
it("returns [text, image] blocks for displayable image", async () => {
|
|
1444
|
+
const wiki = freshWiki();
|
|
1445
|
+
const pngBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
|
1446
|
+
const rawPath = join(wiki.config.rawDir, "photo.png");
|
|
1447
|
+
writeFileSync(rawPath, pngBytes);
|
|
1448
|
+
writeFileSync(rawPath + ".meta.yaml", 'path: photo.png\ndownloadedAt: "2024-01-01"\nsha256: abcd\nsize: 4\nmimeType: image/png\n');
|
|
1449
|
+
const result = await handleTool(wiki, "raw_read", { filename: "photo.png" });
|
|
1450
|
+
expect(Array.isArray(result)).toBe(true);
|
|
1451
|
+
const blocks = result;
|
|
1452
|
+
expect(blocks).toHaveLength(2);
|
|
1453
|
+
// text first, image second — consistent with raw_add/raw_fetch
|
|
1454
|
+
expect(blocks[0].type).toBe("text");
|
|
1455
|
+
expect(blocks[1].type).toBe("image");
|
|
1456
|
+
const textBlock = blocks[0];
|
|
1457
|
+
const parsed = JSON.parse(textBlock.text);
|
|
1458
|
+
expect(parsed.binary).toBe(true);
|
|
1459
|
+
expect(parsed.meta.mimeType).toBe("image/png");
|
|
1460
|
+
const imgBlock = blocks[1];
|
|
1461
|
+
expect(imgBlock.mimeType).toBe("image/png");
|
|
1462
|
+
expect(imgBlock.data).toBe(pngBytes.toString("base64"));
|
|
1463
|
+
});
|
|
1464
|
+
it("returns text-only JSON for oversized image", async () => {
|
|
1465
|
+
const wiki = freshWiki();
|
|
1466
|
+
const bigSize = 11 * 1024 * 1024;
|
|
1467
|
+
const rawPath = join(wiki.config.rawDir, "big.jpg");
|
|
1468
|
+
writeFileSync(rawPath, Buffer.alloc(bigSize));
|
|
1469
|
+
writeFileSync(rawPath + ".meta.yaml", `path: big.jpg\ndownloadedAt: "2024-01-01"\nsha256: abcd\nsize: ${bigSize}\nmimeType: image/jpeg\n`);
|
|
1470
|
+
const result = await handleTool(wiki, "raw_read", { filename: "big.jpg" });
|
|
1471
|
+
expect(typeof result).toBe("string");
|
|
1472
|
+
const parsed = JSON.parse(result);
|
|
1473
|
+
expect(parsed.binary).toBe(true);
|
|
1474
|
+
expect(parsed.note).toMatch(/too large/i);
|
|
1475
|
+
});
|
|
1476
|
+
it("returns text content for non-image raw files", async () => {
|
|
1477
|
+
const wiki = freshWiki();
|
|
1478
|
+
wiki.rawAdd("readme.txt", { content: "Hello world" });
|
|
1479
|
+
const result = await handleTool(wiki, "raw_read", { filename: "readme.txt" });
|
|
1480
|
+
expect(typeof result).toBe("string");
|
|
1481
|
+
const parsed = JSON.parse(result);
|
|
1482
|
+
expect(parsed.binary).toBe(false);
|
|
1483
|
+
expect(parsed.content).toBe("Hello world");
|
|
1484
|
+
});
|
|
1485
|
+
it("throws for missing file", async () => {
|
|
1486
|
+
const wiki = freshWiki();
|
|
1487
|
+
await expect(handleTool(wiki, "raw_read", { filename: "nope.txt" }))
|
|
1488
|
+
.rejects.toThrow(/not found/i);
|
|
1489
|
+
});
|
|
1490
|
+
});
|
|
1491
|
+
describe("handleTool: raw_fetch image ContentBlock[] (mocked network)", () => {
|
|
1492
|
+
beforeEach(cleanUp);
|
|
1493
|
+
afterEach(() => {
|
|
1494
|
+
cleanUp();
|
|
1495
|
+
vi.restoreAllMocks();
|
|
1496
|
+
});
|
|
1497
|
+
function mockFetch(body, contentType, status = 200) {
|
|
1498
|
+
const readable = new ReadableStream({
|
|
1499
|
+
start(controller) {
|
|
1500
|
+
controller.enqueue(new Uint8Array(body));
|
|
1501
|
+
controller.close();
|
|
1502
|
+
},
|
|
1503
|
+
});
|
|
1504
|
+
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(readable, {
|
|
1505
|
+
status,
|
|
1506
|
+
headers: { "content-type": contentType },
|
|
1507
|
+
}));
|
|
1508
|
+
}
|
|
1509
|
+
it("returns [text, image] blocks when fetching an image", async () => {
|
|
1510
|
+
const wiki = freshWiki();
|
|
1511
|
+
const pngBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
|
1512
|
+
mockFetch(pngBytes, "image/png");
|
|
1513
|
+
const result = await handleTool(wiki, "raw_fetch", {
|
|
1514
|
+
url: "https://example.com/photo.png",
|
|
1515
|
+
filename: "fetched.png",
|
|
1516
|
+
});
|
|
1517
|
+
expect(Array.isArray(result)).toBe(true);
|
|
1518
|
+
const blocks = result;
|
|
1519
|
+
expect(blocks).toHaveLength(2);
|
|
1520
|
+
expect(blocks[0].type).toBe("text");
|
|
1521
|
+
expect(blocks[1].type).toBe("image");
|
|
1522
|
+
// text block has document metadata
|
|
1523
|
+
const textBlock = blocks[0];
|
|
1524
|
+
const parsed = JSON.parse(textBlock.text);
|
|
1525
|
+
expect(parsed.ok).toBe(true);
|
|
1526
|
+
expect(parsed.document.path).toBe("fetched.png");
|
|
1527
|
+
expect(parsed.document.mimeType).toBe("image/png");
|
|
1528
|
+
// image block has correct base64 data
|
|
1529
|
+
const imgBlock = blocks[1];
|
|
1530
|
+
expect(imgBlock.mimeType).toBe("image/png");
|
|
1531
|
+
expect(imgBlock.data).toBe(pngBytes.toString("base64"));
|
|
1532
|
+
});
|
|
1533
|
+
it("returns plain text string when fetching a non-image file", async () => {
|
|
1534
|
+
const wiki = freshWiki();
|
|
1535
|
+
const htmlBytes = Buffer.from("<html><body>Hello</body></html>");
|
|
1536
|
+
mockFetch(htmlBytes, "text/html");
|
|
1537
|
+
const result = await handleTool(wiki, "raw_fetch", {
|
|
1538
|
+
url: "https://example.com/page.html",
|
|
1539
|
+
filename: "page.html",
|
|
1540
|
+
});
|
|
1541
|
+
expect(typeof result).toBe("string");
|
|
1542
|
+
const parsed = JSON.parse(result);
|
|
1543
|
+
expect(parsed.ok).toBe(true);
|
|
1544
|
+
expect(parsed.document.mimeType).toBe("text/html");
|
|
1545
|
+
});
|
|
1546
|
+
it("returns plain text string when fetched image exceeds 10 MB", async () => {
|
|
1547
|
+
const wiki = freshWiki();
|
|
1548
|
+
const bigImage = Buffer.alloc(11 * 1024 * 1024);
|
|
1549
|
+
mockFetch(bigImage, "image/jpeg");
|
|
1550
|
+
const result = await handleTool(wiki, "raw_fetch", {
|
|
1551
|
+
url: "https://example.com/huge.jpg",
|
|
1552
|
+
filename: "huge.jpg",
|
|
1553
|
+
});
|
|
1554
|
+
// tryImageBlock returns null for oversized → plain string, no image block
|
|
1555
|
+
expect(typeof result).toBe("string");
|
|
1556
|
+
const parsed = JSON.parse(result);
|
|
1557
|
+
expect(parsed.ok).toBe(true);
|
|
1558
|
+
expect(parsed.document.mimeType).toBe("image/jpeg");
|
|
1559
|
+
});
|
|
1560
|
+
});
|
|
1561
|
+
// ── knowledge_ingest_batch: semantic chunking ─────────────────────
|
|
1562
|
+
describe("knowledge_ingest_batch: semantic chunking", () => {
|
|
1563
|
+
const TEST_SRC = join(TEST_ROOT, "src-docs");
|
|
1564
|
+
beforeEach(() => {
|
|
1565
|
+
cleanUp();
|
|
1566
|
+
mkdirSync(TEST_SRC, { recursive: true });
|
|
1567
|
+
});
|
|
1568
|
+
afterEach(cleanUp);
|
|
1569
|
+
function freshWikiWithSrc() {
|
|
1570
|
+
const wiki = Wiki.init(TEST_ROOT);
|
|
1571
|
+
// Allow TEST_SRC as a source directory
|
|
1572
|
+
wiki.config.allowedSourceDirs = [TEST_SRC];
|
|
1573
|
+
return wiki;
|
|
1574
|
+
}
|
|
1575
|
+
it("single-section markdown is one chunk", async () => {
|
|
1576
|
+
writeFileSync(join(TEST_SRC, "simple.md"), "# Title\n\nSome content here.\n");
|
|
1577
|
+
const wiki = freshWikiWithSrc();
|
|
1578
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1579
|
+
source_path: TEST_SRC,
|
|
1580
|
+
topic: "test",
|
|
1581
|
+
chunkLines: 200,
|
|
1582
|
+
});
|
|
1583
|
+
const parsed = JSON.parse(result);
|
|
1584
|
+
expect(parsed.ok).toBe(true);
|
|
1585
|
+
expect(parsed.extracted).toBe(1);
|
|
1586
|
+
// One section → one chunk
|
|
1587
|
+
expect(parsed.chunks).toBe(1);
|
|
1588
|
+
});
|
|
1589
|
+
it("multi-section markdown produces one chunk per section (not one chunk total)", async () => {
|
|
1590
|
+
const md = [
|
|
1591
|
+
"# Overview",
|
|
1592
|
+
"Introduction paragraph.",
|
|
1593
|
+
"",
|
|
1594
|
+
"## Section A",
|
|
1595
|
+
"Content for section A.",
|
|
1596
|
+
"",
|
|
1597
|
+
"## Section B",
|
|
1598
|
+
"Content for section B.",
|
|
1599
|
+
].join("\n");
|
|
1600
|
+
writeFileSync(join(TEST_SRC, "multi.md"), md);
|
|
1601
|
+
const wiki = freshWikiWithSrc();
|
|
1602
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1603
|
+
source_path: TEST_SRC,
|
|
1604
|
+
topic: "test",
|
|
1605
|
+
chunkLines: 200, // high limit so we don't split sections further
|
|
1606
|
+
});
|
|
1607
|
+
const parsed = JSON.parse(result);
|
|
1608
|
+
expect(parsed.ok).toBe(true);
|
|
1609
|
+
// 3 sections (preamble + Section A + Section B) → 3 chunks
|
|
1610
|
+
expect(parsed.chunks).toBeGreaterThanOrEqual(2);
|
|
1611
|
+
});
|
|
1612
|
+
it("plain text without headings falls back to single segment", async () => {
|
|
1613
|
+
writeFileSync(join(TEST_SRC, "data.txt"), "line1\nline2\nline3\n");
|
|
1614
|
+
const wiki = freshWikiWithSrc();
|
|
1615
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
1616
|
+
source_path: TEST_SRC,
|
|
1617
|
+
topic: "test",
|
|
1618
|
+
chunkLines: 200,
|
|
1619
|
+
});
|
|
1620
|
+
const parsed = JSON.parse(result);
|
|
1621
|
+
expect(parsed.ok).toBe(true);
|
|
1622
|
+
expect(parsed.chunks).toBe(1);
|
|
1623
|
+
});
|
|
1624
|
+
});
|
|
1625
|
+
// ── hybrid search: SearchEngine vector API ────────────────────────
|
|
1626
|
+
import { SearchEngine, cosineSimilarity } from "./search.js";
|
|
1627
|
+
describe("cosineSimilarity", () => {
|
|
1628
|
+
it("returns 1 for identical vectors", () => {
|
|
1629
|
+
expect(cosineSimilarity([1, 0, 0], [1, 0, 0])).toBeCloseTo(1);
|
|
1630
|
+
});
|
|
1631
|
+
it("returns 0 for orthogonal vectors", () => {
|
|
1632
|
+
expect(cosineSimilarity([1, 0], [0, 1])).toBeCloseTo(0);
|
|
1633
|
+
});
|
|
1634
|
+
it("returns 0 for dimension mismatch", () => {
|
|
1635
|
+
expect(cosineSimilarity([1, 2], [1])).toBe(0);
|
|
1636
|
+
});
|
|
1637
|
+
it("returns 0 for zero-length vectors", () => {
|
|
1638
|
+
expect(cosineSimilarity([], [])).toBe(0);
|
|
1639
|
+
});
|
|
1640
|
+
});
|
|
1641
|
+
describe("SearchEngine hybrid vector API", () => {
|
|
1642
|
+
it("updateVector / getVectors round-trips correctly", () => {
|
|
1643
|
+
const engine = new SearchEngine();
|
|
1644
|
+
engine.updateVector("foo.md", [0.1, 0.2, 0.3]);
|
|
1645
|
+
engine.updateVector("bar.md", [0.4, 0.5, 0.6]);
|
|
1646
|
+
const vecs = engine.getVectors();
|
|
1647
|
+
expect(vecs.size).toBe(2);
|
|
1648
|
+
expect(vecs.get("foo.md")).toEqual([0.1, 0.2, 0.3]);
|
|
1649
|
+
});
|
|
1650
|
+
it("removeVector deletes the entry", () => {
|
|
1651
|
+
const engine = new SearchEngine();
|
|
1652
|
+
engine.updateVector("foo.md", [1, 0]);
|
|
1653
|
+
engine.removeVector("foo.md");
|
|
1654
|
+
expect(engine.getVectors().size).toBe(0);
|
|
1655
|
+
});
|
|
1656
|
+
it("setVectors replaces the whole map", () => {
|
|
1657
|
+
const engine = new SearchEngine();
|
|
1658
|
+
engine.updateVector("old.md", [1, 0]);
|
|
1659
|
+
const fresh = new Map([["new.md", [0, 1]]]);
|
|
1660
|
+
engine.setVectors(fresh);
|
|
1661
|
+
expect(engine.getVectors().size).toBe(1);
|
|
1662
|
+
expect(engine.getVectors().has("new.md")).toBe(true);
|
|
1663
|
+
});
|
|
1664
|
+
it("searchHybrid re-ranks via vectors when available", async () => {
|
|
1665
|
+
// Two pages: "alpha" and "beta". We set vectors so "beta" is much closer
|
|
1666
|
+
// to the query embedding than "alpha", then verify beta wins.
|
|
1667
|
+
const pages = [
|
|
1668
|
+
{
|
|
1669
|
+
path: "alpha.md", title: "Alpha Topic", type: "concept",
|
|
1670
|
+
tags: ["alpha"], content: "Alpha alpha alpha alpha alpha",
|
|
1671
|
+
sources: [], links: [], frontmatter: {},
|
|
1672
|
+
},
|
|
1673
|
+
{
|
|
1674
|
+
path: "beta.md", title: "Beta Topic", type: "concept",
|
|
1675
|
+
tags: ["beta"], content: "Beta beta beta beta beta",
|
|
1676
|
+
sources: [], links: [], frontmatter: {},
|
|
1677
|
+
},
|
|
1678
|
+
];
|
|
1679
|
+
const engine = new SearchEngine();
|
|
1680
|
+
engine.setLoader(() => pages);
|
|
1681
|
+
engine.setConfig({ hybrid: true, bm25Weight: 0.0, vectorWeight: 1.0, model: "test-model" });
|
|
1682
|
+
// Set vectors: query will get [1, 0]; beta has [0.99, 0.01] ≈ cosine 0.99
|
|
1683
|
+
// alpha has [0, 1] ≈ cosine 0
|
|
1684
|
+
engine.updateVector("alpha.md", [0, 1]);
|
|
1685
|
+
engine.updateVector("beta.md", [0.99, Math.sqrt(1 - 0.99 ** 2)]);
|
|
1686
|
+
// Mock embedText to return [1, 0] for any query
|
|
1687
|
+
engine.embedPipeline = {
|
|
1688
|
+
// The pipeline call returns an object with .data
|
|
1689
|
+
};
|
|
1690
|
+
vi.spyOn(engine, "embedText").mockResolvedValue([1, 0]);
|
|
1691
|
+
// BM25 should find both pages if we search "topic" (in titles)
|
|
1692
|
+
const results = await engine.searchHybrid("alpha topic", 2);
|
|
1693
|
+
expect(results.length).toBeGreaterThan(0);
|
|
1694
|
+
// beta should rank higher because its vector is closer to [1, 0]
|
|
1695
|
+
expect(results[0].path).toBe("beta.md");
|
|
1696
|
+
});
|
|
1697
|
+
});
|
|
1698
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1699
|
+
// wiki_read multi-page
|
|
1700
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1701
|
+
describe("wiki_read: multi-page (pages[])", () => {
|
|
1702
|
+
beforeEach(cleanUp);
|
|
1703
|
+
afterEach(cleanUp);
|
|
1704
|
+
it("reads multiple pages in one call", async () => {
|
|
1705
|
+
const wiki = freshWiki();
|
|
1706
|
+
wiki.write("alpha.md", "---\ntitle: Alpha\ntype: concept\n---\nAlpha content.");
|
|
1707
|
+
wiki.write("beta.md", "---\ntitle: Beta\ntype: concept\n---\nBeta content.");
|
|
1708
|
+
const result = await handleTool(wiki, "wiki_read", { pages: ["alpha.md", "beta.md"] });
|
|
1709
|
+
const parsed = JSON.parse(result);
|
|
1710
|
+
expect(parsed.count).toBe(2);
|
|
1711
|
+
expect(parsed.pages).toHaveLength(2);
|
|
1712
|
+
expect(parsed.pages[0].content).toContain("Alpha content.");
|
|
1713
|
+
expect(parsed.pages[1].content).toContain("Beta content.");
|
|
1714
|
+
});
|
|
1715
|
+
it("marks missing pages as not_found", async () => {
|
|
1716
|
+
const wiki = freshWiki();
|
|
1717
|
+
wiki.write("exists.md", "---\ntitle: Exists\ntype: note\n---\nBody.");
|
|
1718
|
+
const result = await handleTool(wiki, "wiki_read", { pages: ["exists.md", "missing.md"] });
|
|
1719
|
+
const parsed = JSON.parse(result);
|
|
1720
|
+
expect(parsed.count).toBe(2);
|
|
1721
|
+
const found = parsed.pages.find((p) => p.page === "exists.md");
|
|
1722
|
+
const missing = parsed.pages.find((p) => p.page === "missing.md");
|
|
1723
|
+
expect(found.content).toContain("Body.");
|
|
1724
|
+
expect(missing.not_found).toBe(true);
|
|
1725
|
+
});
|
|
1726
|
+
it("single page path still works as before (backward compat)", async () => {
|
|
1727
|
+
const wiki = freshWiki();
|
|
1728
|
+
wiki.write("solo.md", "---\ntitle: Solo\ntype: note\n---\nSolo body.");
|
|
1729
|
+
const result = await handleTool(wiki, "wiki_read", { page: "solo.md" });
|
|
1730
|
+
// Small page → plain text, not JSON array
|
|
1731
|
+
expect(typeof result).toBe("string");
|
|
1732
|
+
expect(result).toContain("Solo body.");
|
|
1733
|
+
});
|
|
1734
|
+
it("throws when neither page nor pages is supplied", async () => {
|
|
1735
|
+
const wiki = freshWiki();
|
|
1736
|
+
await expect(handleTool(wiki, "wiki_read", {})).rejects.toThrow(/page.*pages/i);
|
|
1737
|
+
});
|
|
1738
|
+
});
|
|
1739
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1740
|
+
// wiki_write return_content
|
|
1741
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1742
|
+
describe("wiki_write: return_content", () => {
|
|
1743
|
+
beforeEach(cleanUp);
|
|
1744
|
+
afterEach(cleanUp);
|
|
1745
|
+
it("returns content when return_content is true", async () => {
|
|
1746
|
+
const wiki = freshWiki();
|
|
1747
|
+
const result = await handleTool(wiki, "wiki_write", {
|
|
1748
|
+
page: "note-rc.md",
|
|
1749
|
+
content: "---\ntitle: Return Content\ntype: note\n---\nInline body.",
|
|
1750
|
+
return_content: true,
|
|
1751
|
+
});
|
|
1752
|
+
const parsed = JSON.parse(result);
|
|
1753
|
+
expect(parsed.ok).toBe(true);
|
|
1754
|
+
expect(parsed.content).toBeDefined();
|
|
1755
|
+
expect(parsed.content).toContain("Inline body.");
|
|
1756
|
+
});
|
|
1757
|
+
it("return_content includes created and updated timestamps", async () => {
|
|
1758
|
+
const wiki = freshWiki();
|
|
1759
|
+
const result = await handleTool(wiki, "wiki_write", {
|
|
1760
|
+
page: "note-ts.md",
|
|
1761
|
+
content: "---\ntitle: Timestamps\ntype: note\n---\nBody.",
|
|
1762
|
+
return_content: true,
|
|
1763
|
+
});
|
|
1764
|
+
const parsed = JSON.parse(result);
|
|
1765
|
+
// write() injects timestamps — returned content must include them
|
|
1766
|
+
expect(parsed.content).toContain("created:");
|
|
1767
|
+
expect(parsed.content).toContain("updated:");
|
|
1768
|
+
});
|
|
1769
|
+
it("does not return content by default", async () => {
|
|
1770
|
+
const wiki = freshWiki();
|
|
1771
|
+
const result = await handleTool(wiki, "wiki_write", {
|
|
1772
|
+
page: "note-no-rc.md",
|
|
1773
|
+
content: "---\ntitle: No Return\ntype: note\n---\nBody.",
|
|
1774
|
+
});
|
|
1775
|
+
const parsed = JSON.parse(result);
|
|
1776
|
+
expect(parsed.ok).toBe(true);
|
|
1777
|
+
expect(parsed.content).toBeUndefined();
|
|
1778
|
+
});
|
|
1779
|
+
it("eliminates follow-up wiki_read in write-then-reference workflow", async () => {
|
|
1780
|
+
const wiki = freshWiki();
|
|
1781
|
+
// Before: write (1) + read (1) = 2 requests
|
|
1782
|
+
// After: write with return_content (1) = 1 request
|
|
1783
|
+
const result = await handleTool(wiki, "wiki_write", {
|
|
1784
|
+
page: "chain.md",
|
|
1785
|
+
content: "---\ntitle: Chain\ntype: note\n---\nChained content.",
|
|
1786
|
+
return_content: true,
|
|
1787
|
+
});
|
|
1788
|
+
const parsed = JSON.parse(result);
|
|
1789
|
+
expect(parsed.content).toContain("Chained content.");
|
|
1790
|
+
// page path available for further reference — no separate wiki_read needed
|
|
1791
|
+
expect(parsed.page).toBe("chain.md");
|
|
1792
|
+
});
|
|
1793
|
+
});
|
|
1794
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1795
|
+
// wiki_write auto-link
|
|
1796
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1797
|
+
describe("wiki_write: auto-link", () => {
|
|
1798
|
+
beforeEach(cleanUp);
|
|
1799
|
+
afterEach(cleanUp);
|
|
1800
|
+
it("injects links and reports autoLinked count", async () => {
|
|
1801
|
+
const wiki = freshWiki();
|
|
1802
|
+
wiki.write("concept-rust.md", "---\ntitle: Rust Language\ntype: concept\ntags: [rust]\n---\nSystems language.");
|
|
1803
|
+
const result = await handleTool(wiki, "wiki_write", {
|
|
1804
|
+
page: "note-perf.md",
|
|
1805
|
+
content: "---\ntitle: Performance\ntype: note\n---\nRust Language is great for systems code.",
|
|
1806
|
+
return_content: true,
|
|
1807
|
+
});
|
|
1808
|
+
const parsed = JSON.parse(result);
|
|
1809
|
+
expect(parsed.autoLinked).toBe(1);
|
|
1810
|
+
expect(parsed.content).toContain("[[concept-rust|Rust Language]]");
|
|
1811
|
+
});
|
|
1812
|
+
it("skips auto-link when config.autoLink.enabled is false", async () => {
|
|
1813
|
+
const wiki = freshWiki();
|
|
1814
|
+
wiki.write("concept-rust.md", "---\ntitle: Rust Language\ntype: concept\ntags: [rust]\n---\nSystems language.");
|
|
1815
|
+
wiki.config.autoLink.enabled = false;
|
|
1816
|
+
const result = await handleTool(wiki, "wiki_write", {
|
|
1817
|
+
page: "note-perf.md",
|
|
1818
|
+
content: "---\ntitle: Performance\ntype: note\n---\nRust Language is great.",
|
|
1819
|
+
return_content: true,
|
|
1820
|
+
});
|
|
1821
|
+
const parsed = JSON.parse(result);
|
|
1822
|
+
expect(parsed.autoLinked).toBe(0);
|
|
1823
|
+
expect(parsed.content).not.toContain("[[");
|
|
1824
|
+
});
|
|
1825
|
+
});
|
|
1826
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1827
|
+
// wiki_search type / tags filter
|
|
1828
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1829
|
+
describe("wiki_search: type and tags filter", () => {
|
|
1830
|
+
beforeEach(cleanUp);
|
|
1831
|
+
afterEach(cleanUp);
|
|
1832
|
+
it("filters by type", async () => {
|
|
1833
|
+
const wiki = freshWiki();
|
|
1834
|
+
wiki.write("c1.md", "---\ntitle: Concept One\ntype: concept\ntags: [ml]\n---\nNeural nets.");
|
|
1835
|
+
wiki.write("n1.md", "---\ntitle: Note One\ntype: note\ntags: [ml]\n---\nNeural note.");
|
|
1836
|
+
const result = await handleTool(wiki, "wiki_search", { query: "neural", type: "concept" });
|
|
1837
|
+
const parsed = JSON.parse(result);
|
|
1838
|
+
expect(parsed.results.every((r) => !r.path.startsWith("n1"))).toBe(true);
|
|
1839
|
+
expect(parsed.results.some((r) => r.path === "c1.md")).toBe(true);
|
|
1840
|
+
});
|
|
1841
|
+
it("filters by tags (any match)", async () => {
|
|
1842
|
+
const wiki = freshWiki();
|
|
1843
|
+
wiki.write("p1.md", "---\ntitle: PyTorch\ntype: artifact\ntags: [python, ml]\n---\nDeep learning.");
|
|
1844
|
+
wiki.write("p2.md", "---\ntitle: TensorFlow\ntype: artifact\ntags: [python, ml]\n---\nDeep learning.");
|
|
1845
|
+
wiki.write("p3.md", "---\ntitle: Java Deep\ntype: artifact\ntags: [java]\n---\nDeep learning.");
|
|
1846
|
+
const result = await handleTool(wiki, "wiki_search", { query: "deep learning", tags: ["python"] });
|
|
1847
|
+
const parsed = JSON.parse(result);
|
|
1848
|
+
const paths = parsed.results.map((r) => r.path);
|
|
1849
|
+
expect(paths).toContain("p1.md");
|
|
1850
|
+
expect(paths).toContain("p2.md");
|
|
1851
|
+
expect(paths).not.toContain("p3.md");
|
|
1852
|
+
});
|
|
1853
|
+
it("no filter returns all matching pages", async () => {
|
|
1854
|
+
const wiki = freshWiki();
|
|
1855
|
+
wiki.write("a.md", "---\ntitle: A\ntype: concept\n---\nKeyword here.");
|
|
1856
|
+
wiki.write("b.md", "---\ntitle: B\ntype: note\n---\nKeyword here.");
|
|
1857
|
+
const result = await handleTool(wiki, "wiki_search", { query: "keyword" });
|
|
1858
|
+
const parsed = JSON.parse(result);
|
|
1859
|
+
expect(parsed.results.length).toBe(2);
|
|
1860
|
+
});
|
|
1861
|
+
it("returns empty when no results match filter", async () => {
|
|
1862
|
+
const wiki = freshWiki();
|
|
1863
|
+
wiki.write("a.md", "---\ntitle: A\ntype: note\n---\nFoo content.");
|
|
1864
|
+
const result = await handleTool(wiki, "wiki_search", { query: "foo", type: "concept" });
|
|
1865
|
+
const parsed = JSON.parse(result);
|
|
1866
|
+
expect(parsed.results).toHaveLength(0);
|
|
1867
|
+
});
|
|
1868
|
+
});
|
|
1869
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1870
|
+
// wiki_search knowledge_gap
|
|
1871
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1872
|
+
describe("wiki_search: knowledge_gap", () => {
|
|
1873
|
+
beforeEach(cleanUp);
|
|
1874
|
+
afterEach(cleanUp);
|
|
1875
|
+
it("returns knowledge_gap when no results found", async () => {
|
|
1876
|
+
const wiki = freshWiki();
|
|
1877
|
+
const result = await handleTool(wiki, "wiki_search", { query: "quantum computing" });
|
|
1878
|
+
const parsed = JSON.parse(result);
|
|
1879
|
+
expect(parsed.count).toBe(0);
|
|
1880
|
+
expect(parsed.knowledge_gap).toBeDefined();
|
|
1881
|
+
expect(parsed.knowledge_gap.query).toBe("quantum computing");
|
|
1882
|
+
expect(parsed.knowledge_gap.suggested_page).toContain("quantum");
|
|
1883
|
+
expect(parsed.knowledge_gap.suggested_title).toBe("Quantum Computing");
|
|
1884
|
+
expect(parsed.knowledge_gap.suggested_type).toBeDefined();
|
|
1885
|
+
expect(Array.isArray(parsed.knowledge_gap.suggested_tags)).toBe(true);
|
|
1886
|
+
expect(parsed.knowledge_gap.hint).toContain("wiki_write");
|
|
1887
|
+
});
|
|
1888
|
+
it("does not return knowledge_gap when results exist", async () => {
|
|
1889
|
+
const wiki = freshWiki();
|
|
1890
|
+
wiki.write("concept-qc.md", "---\ntitle: Quantum Computing\ntype: concept\n---\nQubits and superposition.");
|
|
1891
|
+
const result = await handleTool(wiki, "wiki_search", { query: "quantum computing" });
|
|
1892
|
+
const parsed = JSON.parse(result);
|
|
1893
|
+
expect(parsed.count).toBeGreaterThan(0);
|
|
1894
|
+
expect(parsed.knowledge_gap).toBeUndefined();
|
|
1895
|
+
});
|
|
1896
|
+
it("knowledge_gap suggested_page uses type prefix", async () => {
|
|
1897
|
+
const wiki = freshWiki();
|
|
1898
|
+
const result = await handleTool(wiki, "wiki_search", { query: "docker deployment guide" });
|
|
1899
|
+
const parsed = JSON.parse(result);
|
|
1900
|
+
expect(parsed.knowledge_gap.suggested_page).toMatch(/^(how-to|concept|note|artifact)-/);
|
|
1901
|
+
});
|
|
1902
|
+
it("wiki_search_read also returns knowledge_gap on empty results", async () => {
|
|
1903
|
+
const wiki = freshWiki();
|
|
1904
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "nonexistent topic xyz" });
|
|
1905
|
+
const parsed = JSON.parse(result);
|
|
1906
|
+
expect(parsed.count).toBe(0);
|
|
1907
|
+
expect(parsed.knowledge_gap).toBeDefined();
|
|
1908
|
+
expect(parsed.knowledge_gap.hint).toContain("wiki_write");
|
|
1909
|
+
});
|
|
1910
|
+
it("CJK query produces non-empty suggested_page", async () => {
|
|
1911
|
+
const wiki = freshWiki();
|
|
1912
|
+
const result = await handleTool(wiki, "wiki_search", { query: "机器学习" });
|
|
1913
|
+
const parsed = JSON.parse(result);
|
|
1914
|
+
expect(parsed.knowledge_gap.suggested_page).not.toContain("-.md");
|
|
1915
|
+
expect(parsed.knowledge_gap.suggested_page.length).toBeGreaterThan(5);
|
|
1916
|
+
});
|
|
1917
|
+
it("filterType is reflected in knowledge_gap.suggested_type", async () => {
|
|
1918
|
+
const wiki = freshWiki();
|
|
1919
|
+
const result = await handleTool(wiki, "wiki_search", { query: "nonexistent xyz", type: "artifact" });
|
|
1920
|
+
const parsed = JSON.parse(result);
|
|
1921
|
+
expect(parsed.knowledge_gap.suggested_type).toBe("artifact");
|
|
1922
|
+
expect(parsed.knowledge_gap.suggested_page).toMatch(/^artifact-/);
|
|
1923
|
+
});
|
|
1924
|
+
});
|
|
1925
|
+
// ═══════════════════════════════════════════════════════
|
|
1926
|
+
// CONSOLIDATED PUBLIC TOOLS — routing and backward compat
|
|
1927
|
+
// ═══════════════════════════════════════════════════════
|
|
1928
|
+
describe("consolidated tool: raw_ingest", () => {
|
|
1929
|
+
beforeEach(cleanUp);
|
|
1930
|
+
afterEach(cleanUp);
|
|
1931
|
+
it("mode:add delegates to raw_add", async () => {
|
|
1932
|
+
const wiki = freshWiki();
|
|
1933
|
+
const result = await handleTool(wiki, "raw_ingest", {
|
|
1934
|
+
mode: "add",
|
|
1935
|
+
filename: "ingest-test.md",
|
|
1936
|
+
content: "hello from raw_ingest",
|
|
1937
|
+
});
|
|
1938
|
+
const parsed = JSON.parse(result);
|
|
1939
|
+
expect(parsed.ok).toBe(true);
|
|
1940
|
+
expect(parsed.document.path).toBe("ingest-test.md");
|
|
1941
|
+
// verify file actually exists in raw
|
|
1942
|
+
const docs = wiki.rawList();
|
|
1943
|
+
expect(docs.some((d) => d.path === "ingest-test.md")).toBe(true);
|
|
1944
|
+
});
|
|
1945
|
+
it("mode:fetch delegates to raw_fetch", async () => {
|
|
1946
|
+
const wiki = freshWiki();
|
|
1947
|
+
const readable = new ReadableStream({
|
|
1948
|
+
start(controller) {
|
|
1949
|
+
controller.enqueue(new Uint8Array(Buffer.from("fetched content")));
|
|
1950
|
+
controller.close();
|
|
1951
|
+
},
|
|
1952
|
+
});
|
|
1953
|
+
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(readable, { status: 200, headers: { "content-type": "text/plain" } }));
|
|
1954
|
+
try {
|
|
1955
|
+
const result = await handleTool(wiki, "raw_ingest", {
|
|
1956
|
+
mode: "fetch",
|
|
1957
|
+
url: "https://example.com/doc.txt",
|
|
1958
|
+
filename: "fetched-via-ingest.txt",
|
|
1959
|
+
});
|
|
1960
|
+
const parsed = JSON.parse(result);
|
|
1961
|
+
expect(parsed.ok).toBe(true);
|
|
1962
|
+
expect(parsed.document.path).toBe("fetched-via-ingest.txt");
|
|
1963
|
+
}
|
|
1964
|
+
finally {
|
|
1965
|
+
vi.restoreAllMocks();
|
|
1966
|
+
}
|
|
1967
|
+
});
|
|
1968
|
+
it("throws on unknown mode", async () => {
|
|
1969
|
+
const wiki = freshWiki();
|
|
1970
|
+
await expect(handleTool(wiki, "raw_ingest", { mode: "invalid_mode" })).rejects.toThrow(/Unknown raw_ingest mode/);
|
|
1971
|
+
});
|
|
1972
|
+
it("throws when mode is missing", async () => {
|
|
1973
|
+
const wiki = freshWiki();
|
|
1974
|
+
await expect(handleTool(wiki, "raw_ingest", {})).rejects.toThrow(/Unknown raw_ingest mode/);
|
|
1975
|
+
});
|
|
1976
|
+
});
|
|
1977
|
+
describe("consolidated tool: wiki_admin", () => {
|
|
1978
|
+
beforeEach(cleanUp);
|
|
1979
|
+
afterEach(cleanUp);
|
|
1980
|
+
it("action:config returns current config", async () => {
|
|
1981
|
+
const wiki = freshWiki();
|
|
1982
|
+
const result = await handleTool(wiki, "wiki_admin", { action: "config" });
|
|
1983
|
+
const parsed = JSON.parse(result);
|
|
1984
|
+
expect(parsed).toHaveProperty("wikiDir");
|
|
1985
|
+
expect(parsed).toHaveProperty("rawDir");
|
|
1986
|
+
expect(parsed).toHaveProperty("search");
|
|
1987
|
+
});
|
|
1988
|
+
it("action:rebuild runs without error", async () => {
|
|
1989
|
+
const wiki = freshWiki();
|
|
1990
|
+
const result = await handleTool(wiki, "wiki_admin", { action: "rebuild" });
|
|
1991
|
+
const parsed = JSON.parse(result);
|
|
1992
|
+
expect(parsed.ok).toBe(true);
|
|
1993
|
+
expect(parsed.message).toMatch(/rebuilt/i);
|
|
1994
|
+
});
|
|
1995
|
+
it("action:lint returns a lint report", async () => {
|
|
1996
|
+
const wiki = freshWiki();
|
|
1997
|
+
const result = await handleTool(wiki, "wiki_admin", { action: "lint" });
|
|
1998
|
+
const parsed = JSON.parse(result);
|
|
1999
|
+
expect(parsed).toHaveProperty("issues");
|
|
2000
|
+
expect(parsed).toHaveProperty("pagesChecked");
|
|
2001
|
+
});
|
|
2002
|
+
it("action:init initializes a new knowledge base", async () => {
|
|
2003
|
+
// Use a fresh subdirectory to avoid conflicts
|
|
2004
|
+
const initPath = join(TEST_ROOT, "fresh-kb");
|
|
2005
|
+
mkdirSync(initPath, { recursive: true });
|
|
2006
|
+
const wiki = freshWiki(); // need a wiki instance for handleTool signature
|
|
2007
|
+
const result = await handleTool(wiki, "wiki_admin", { action: "init", path: initPath });
|
|
2008
|
+
const parsed = JSON.parse(result);
|
|
2009
|
+
expect(parsed.ok).toBe(true);
|
|
2010
|
+
expect(existsSync(join(initPath, "wiki"))).toBe(true);
|
|
2011
|
+
expect(existsSync(join(initPath, "raw"))).toBe(true);
|
|
2012
|
+
});
|
|
2013
|
+
it("throws on unknown action", async () => {
|
|
2014
|
+
const wiki = freshWiki();
|
|
2015
|
+
await expect(handleTool(wiki, "wiki_admin", { action: "unknown_action" })).rejects.toThrow(/Unknown wiki_admin action/);
|
|
2016
|
+
});
|
|
2017
|
+
it("throws when action is missing", async () => {
|
|
2018
|
+
const wiki = freshWiki();
|
|
2019
|
+
await expect(handleTool(wiki, "wiki_admin", {})).rejects.toThrow(/Unknown wiki_admin action/);
|
|
2020
|
+
});
|
|
2021
|
+
});
|
|
2022
|
+
describe("consolidated tool: code_query", () => {
|
|
2023
|
+
beforeEach(cleanUp);
|
|
2024
|
+
afterEach(cleanUp);
|
|
2025
|
+
it("query_type:trace_variable traces a variable in parsed COBOL", async () => {
|
|
2026
|
+
const wiki = freshWiki();
|
|
2027
|
+
const cobol = ` IDENTIFICATION DIVISION.
|
|
2028
|
+
PROGRAM-ID. SAMPLE.
|
|
2029
|
+
DATA DIVISION.
|
|
2030
|
+
WORKING-STORAGE SECTION.
|
|
2031
|
+
01 WS-COUNTER PIC 9(4) VALUE 0.
|
|
2032
|
+
PROCEDURE DIVISION.
|
|
2033
|
+
MOVE 1 TO WS-COUNTER.
|
|
2034
|
+
STOP RUN.`;
|
|
2035
|
+
wiki.rawAdd("SAMPLE.cbl", { content: cobol });
|
|
2036
|
+
// parse first
|
|
2037
|
+
await handleTool(wiki, "code_parse", { path: "SAMPLE.cbl" });
|
|
2038
|
+
const result = await handleTool(wiki, "code_query", {
|
|
2039
|
+
query_type: "trace_variable",
|
|
2040
|
+
path: "SAMPLE.cbl",
|
|
2041
|
+
variable: "WS-COUNTER",
|
|
2042
|
+
});
|
|
2043
|
+
const parsed = JSON.parse(result);
|
|
2044
|
+
expect(parsed.variable).toBe("WS-COUNTER");
|
|
2045
|
+
expect(parsed.file).toBe("SAMPLE.cbl");
|
|
2046
|
+
expect(Array.isArray(parsed.references)).toBe(true);
|
|
2047
|
+
});
|
|
2048
|
+
it("query_type:procedure_flow returns PERFORM flow for parsed COBOL", async () => {
|
|
2049
|
+
const wiki = freshWiki();
|
|
2050
|
+
const cobol = ` IDENTIFICATION DIVISION.
|
|
2051
|
+
PROGRAM-ID. SAMPLE.
|
|
2052
|
+
PROCEDURE DIVISION.
|
|
2053
|
+
A000-MAIN SECTION.
|
|
2054
|
+
A100-START.
|
|
2055
|
+
PERFORM B000-WORK.
|
|
2056
|
+
STOP RUN.
|
|
2057
|
+
B000-WORK SECTION.
|
|
2058
|
+
B100-STEP.
|
|
2059
|
+
EXIT.`;
|
|
2060
|
+
wiki.rawAdd("SAMPLE.cbl", { content: cobol });
|
|
2061
|
+
await handleTool(wiki, "code_parse", { path: "SAMPLE.cbl" });
|
|
2062
|
+
const result = await handleTool(wiki, "code_query", {
|
|
2063
|
+
query_type: "procedure_flow",
|
|
2064
|
+
path: "SAMPLE.cbl",
|
|
2065
|
+
procedure: "A100-START",
|
|
2066
|
+
});
|
|
2067
|
+
const parsed = JSON.parse(result);
|
|
2068
|
+
expect(parsed.query.type).toBe("procedure_flow");
|
|
2069
|
+
expect(parsed.sectionCalls).toHaveLength(1);
|
|
2070
|
+
expect(parsed.sectionCalls[0].fromSection).toBe("A000-MAIN");
|
|
2071
|
+
expect(parsed.sectionCalls[0].toSection).toBe("B000-WORK");
|
|
2072
|
+
});
|
|
2073
|
+
it("query_type:field_lineage returns deterministic lineage matches", async () => {
|
|
2074
|
+
const wiki = freshWiki();
|
|
2075
|
+
const copybook = ` 01 CUSTOMER-REC.
|
|
2076
|
+
05 CUSTOMER-ID PIC X(10).`;
|
|
2077
|
+
const orderA = ` IDENTIFICATION DIVISION.
|
|
2078
|
+
PROGRAM-ID. ORDERA.
|
|
2079
|
+
DATA DIVISION.
|
|
2080
|
+
WORKING-STORAGE SECTION.
|
|
2081
|
+
COPY CUSTOMER-REC.
|
|
2082
|
+
PROCEDURE DIVISION.
|
|
2083
|
+
STOP RUN.`;
|
|
2084
|
+
const orderB = ` IDENTIFICATION DIVISION.
|
|
2085
|
+
PROGRAM-ID. ORDERB.
|
|
2086
|
+
DATA DIVISION.
|
|
2087
|
+
WORKING-STORAGE SECTION.
|
|
2088
|
+
COPY CUSTOMER-REC.
|
|
2089
|
+
PROCEDURE DIVISION.
|
|
2090
|
+
STOP RUN.`;
|
|
2091
|
+
wiki.rawAdd("CUSTOMER-REC.cpy", { content: copybook });
|
|
2092
|
+
wiki.rawAdd("ORDERA.cbl", { content: orderA });
|
|
2093
|
+
wiki.rawAdd("ORDERB.cbl", { content: orderB });
|
|
2094
|
+
await handleTool(wiki, "code_parse", { path: "ORDERA.cbl" });
|
|
2095
|
+
await handleTool(wiki, "code_parse", { path: "ORDERB.cbl" });
|
|
2096
|
+
await handleTool(wiki, "code_parse", { path: "CUSTOMER-REC.cpy" });
|
|
2097
|
+
const result = await handleTool(wiki, "code_query", {
|
|
2098
|
+
query_type: "field_lineage",
|
|
2099
|
+
field_name: "CUSTOMER-ID",
|
|
2100
|
+
copybook: "CUSTOMER-REC",
|
|
2101
|
+
});
|
|
2102
|
+
const parsed = JSON.parse(result);
|
|
2103
|
+
expect(parsed.query.type).toBe("field_lineage");
|
|
2104
|
+
expect(parsed.summary.deterministicMatches).toBe(1);
|
|
2105
|
+
expect(parsed.deterministic[0].fieldName).toBe("CUSTOMER-ID");
|
|
2106
|
+
});
|
|
2107
|
+
it("query_type:impact throws when no compiled graph exists", async () => {
|
|
2108
|
+
const wiki = freshWiki();
|
|
2109
|
+
await expect(handleTool(wiki, "code_query", { query_type: "impact", node_id: "program:MISSING" })).rejects.toThrow(/not found/i);
|
|
2110
|
+
});
|
|
2111
|
+
it("throws on unknown query_type", async () => {
|
|
2112
|
+
const wiki = freshWiki();
|
|
2113
|
+
await expect(handleTool(wiki, "code_query", { query_type: "unknown" })).rejects.toThrow(/Unknown code_query query_type/);
|
|
2114
|
+
});
|
|
2115
|
+
it("throws when query_type is missing", async () => {
|
|
2116
|
+
const wiki = freshWiki();
|
|
2117
|
+
await expect(handleTool(wiki, "code_query", {})).rejects.toThrow(/Unknown code_query query_type/);
|
|
2118
|
+
});
|
|
2119
|
+
it("trace_variable: throws when path is missing", async () => {
|
|
2120
|
+
const wiki = freshWiki();
|
|
2121
|
+
await expect(handleTool(wiki, "code_query", { query_type: "trace_variable", variable: "WS-FOO" })).rejects.toThrow("code_trace_variable requires 'path'");
|
|
2122
|
+
});
|
|
2123
|
+
it("trace_variable: throws when variable is missing", async () => {
|
|
2124
|
+
const wiki = freshWiki();
|
|
2125
|
+
await expect(handleTool(wiki, "code_query", { query_type: "trace_variable", path: "SAMPLE.cbl" })).rejects.toThrow("code_trace_variable requires 'variable'");
|
|
2126
|
+
});
|
|
2127
|
+
it("procedure_flow: throws when path is missing", async () => {
|
|
2128
|
+
const wiki = freshWiki();
|
|
2129
|
+
await expect(handleTool(wiki, "code_query", { query_type: "procedure_flow" })).rejects.toThrow("code_procedure_flow requires 'path'");
|
|
2130
|
+
});
|
|
2131
|
+
it("dataflow_edges: throws when path is missing", async () => {
|
|
2132
|
+
const wiki = freshWiki();
|
|
2133
|
+
await expect(handleTool(wiki, "code_query", { query_type: "dataflow_edges" })).rejects.toThrow("code_dataflow_edges requires 'path'");
|
|
2134
|
+
});
|
|
2135
|
+
it("impact: throws when node_id is missing", async () => {
|
|
2136
|
+
const wiki = freshWiki();
|
|
2137
|
+
await expect(handleTool(wiki, "code_query", { query_type: "impact" })).rejects.toThrow("code_impact requires 'node_id'");
|
|
2138
|
+
});
|
|
2139
|
+
});
|
|
2140
|
+
describe("consolidated tool: knowledge_ingest", () => {
|
|
2141
|
+
beforeEach(cleanUp);
|
|
2142
|
+
afterEach(cleanUp);
|
|
2143
|
+
it("mode:batch delegates to knowledge_ingest_batch", async () => {
|
|
2144
|
+
const wiki = freshWiki();
|
|
2145
|
+
const srcDir = join(TEST_ROOT, "sources");
|
|
2146
|
+
mkdirSync(srcDir, { recursive: true });
|
|
2147
|
+
writeFileSync(join(srcDir, "doc.txt"), "content of doc");
|
|
2148
|
+
// Add allowed source dir to config
|
|
2149
|
+
wiki.config.allowedSourceDirs = [srcDir];
|
|
2150
|
+
const result = await handleTool(wiki, "knowledge_ingest", {
|
|
2151
|
+
mode: "batch",
|
|
2152
|
+
source_path: srcDir,
|
|
2153
|
+
topic: "test-topic",
|
|
2154
|
+
});
|
|
2155
|
+
const parsed = JSON.parse(result);
|
|
2156
|
+
expect(parsed.ok).toBe(true);
|
|
2157
|
+
expect(parsed.matched).toBeGreaterThan(0);
|
|
2158
|
+
});
|
|
2159
|
+
it("mode:digest_write delegates to knowledge_digest_write", async () => {
|
|
2160
|
+
const wiki = freshWiki();
|
|
2161
|
+
const result = await handleTool(wiki, "knowledge_ingest", {
|
|
2162
|
+
mode: "digest_write",
|
|
2163
|
+
pages: [
|
|
2164
|
+
{ page: "concept-test.md", title: "Test Concept", body: "Some content about testing." },
|
|
2165
|
+
],
|
|
2166
|
+
});
|
|
2167
|
+
const parsed = JSON.parse(result);
|
|
2168
|
+
expect(parsed.count).toBe(1);
|
|
2169
|
+
expect(parsed.written).toBe(1);
|
|
2170
|
+
});
|
|
2171
|
+
it("throws on unknown mode", async () => {
|
|
2172
|
+
const wiki = freshWiki();
|
|
2173
|
+
await expect(handleTool(wiki, "knowledge_ingest", { mode: "unknown" })).rejects.toThrow(/Unknown knowledge_ingest mode/);
|
|
2174
|
+
});
|
|
2175
|
+
it("throws when mode is missing", async () => {
|
|
2176
|
+
const wiki = freshWiki();
|
|
2177
|
+
await expect(handleTool(wiki, "knowledge_ingest", {})).rejects.toThrow(/Unknown knowledge_ingest mode/);
|
|
2178
|
+
});
|
|
2179
|
+
});
|
|
2180
|
+
describe("wiki_search: read_top_n combined search+read mode", () => {
|
|
2181
|
+
beforeEach(cleanUp);
|
|
2182
|
+
afterEach(cleanUp);
|
|
2183
|
+
it("returns pages array when read_top_n is set", async () => {
|
|
2184
|
+
const wiki = freshWiki();
|
|
2185
|
+
wiki.write("concept-alpha.md", `---
|
|
2186
|
+
title: Alpha Concept
|
|
2187
|
+
type: concept
|
|
2188
|
+
tags: [alpha]
|
|
2189
|
+
---
|
|
2190
|
+
Alpha is a concept about beginnings.`);
|
|
2191
|
+
wiki.write("concept-beta.md", `---
|
|
2192
|
+
title: Beta Concept
|
|
2193
|
+
type: concept
|
|
2194
|
+
tags: [beta]
|
|
2195
|
+
---
|
|
2196
|
+
Beta is a concept about second iterations.`);
|
|
2197
|
+
const result = await handleTool(wiki, "wiki_search", { query: "concept", read_top_n: 2 });
|
|
2198
|
+
const parsed = JSON.parse(result);
|
|
2199
|
+
expect(parsed).toHaveProperty("results");
|
|
2200
|
+
expect(parsed).toHaveProperty("pages");
|
|
2201
|
+
expect(parsed).toHaveProperty("nextReads");
|
|
2202
|
+
expect(Array.isArray(parsed.pages)).toBe(true);
|
|
2203
|
+
expect(parsed.pagesRead).toBeGreaterThan(0);
|
|
2204
|
+
});
|
|
2205
|
+
it("deduplicates multiple hits on the same page", async () => {
|
|
2206
|
+
const wiki = freshWiki();
|
|
2207
|
+
wiki.write("concept-dup.md", `---
|
|
2208
|
+
title: Dup Concept
|
|
2209
|
+
type: concept
|
|
2210
|
+
tags: [dup, alpha, beta]
|
|
2211
|
+
---
|
|
2212
|
+
Alpha beta gamma delta epsilon. Alpha again.`);
|
|
2213
|
+
const result = await handleTool(wiki, "wiki_search", { query: "alpha beta", read_top_n: 3, limit: 5 });
|
|
2214
|
+
const parsed = JSON.parse(result);
|
|
2215
|
+
const pagePaths = parsed.pages.map((p) => p.path);
|
|
2216
|
+
const uniquePaths = [...new Set(pagePaths)];
|
|
2217
|
+
expect(pagePaths.length).toBe(uniquePaths.length);
|
|
2218
|
+
});
|
|
2219
|
+
it("nextReads contains pages beyond read_top_n", async () => {
|
|
2220
|
+
const wiki = freshWiki();
|
|
2221
|
+
for (let i = 1; i <= 5; i++) {
|
|
2222
|
+
wiki.write(`concept-item${i}.md`, `---
|
|
2223
|
+
title: Item ${i}
|
|
2224
|
+
type: concept
|
|
2225
|
+
tags: [searchable]
|
|
2226
|
+
---
|
|
2227
|
+
Searchable content item number ${i}.`);
|
|
2228
|
+
}
|
|
2229
|
+
const result = await handleTool(wiki, "wiki_search", { query: "searchable", read_top_n: 2, limit: 10 });
|
|
2230
|
+
const parsed = JSON.parse(result);
|
|
2231
|
+
expect(parsed.pagesRead).toBeLessThanOrEqual(2);
|
|
2232
|
+
expect(Array.isArray(parsed.nextReads)).toBe(true);
|
|
2233
|
+
});
|
|
2234
|
+
it("read_top_n is capped at 10", async () => {
|
|
2235
|
+
const wiki = freshWiki();
|
|
2236
|
+
wiki.write("concept-cap.md", `---
|
|
2237
|
+
title: Cap Test
|
|
2238
|
+
type: concept
|
|
2239
|
+
tags: [cap]
|
|
2240
|
+
---
|
|
2241
|
+
Capped at ten.`);
|
|
2242
|
+
// read_top_n=99 should be clamped to 10 internally
|
|
2243
|
+
const result = await handleTool(wiki, "wiki_search", { query: "capped", read_top_n: 99 });
|
|
2244
|
+
const parsed = JSON.parse(result);
|
|
2245
|
+
// Should not throw and should return valid structure
|
|
2246
|
+
expect(parsed).toHaveProperty("pages");
|
|
2247
|
+
expect(parsed.pagesRead).toBeLessThanOrEqual(10);
|
|
2248
|
+
});
|
|
2249
|
+
it("section filter applies to read pages when read_top_n is set", async () => {
|
|
2250
|
+
const wiki = freshWiki();
|
|
2251
|
+
wiki.write("concept-sections.md", `---
|
|
2252
|
+
title: Section Test
|
|
2253
|
+
type: concept
|
|
2254
|
+
tags: [sections]
|
|
2255
|
+
---
|
|
2256
|
+
## Introduction
|
|
2257
|
+
Intro content here.
|
|
2258
|
+
|
|
2259
|
+
## Details
|
|
2260
|
+
Detail content here.`);
|
|
2261
|
+
const result = await handleTool(wiki, "wiki_search", {
|
|
2262
|
+
query: "sections",
|
|
2263
|
+
read_top_n: 1,
|
|
2264
|
+
section: "## Details",
|
|
2265
|
+
});
|
|
2266
|
+
const parsed = JSON.parse(result);
|
|
2267
|
+
expect(parsed.pages.length).toBeGreaterThan(0);
|
|
2268
|
+
if (parsed.pages[0].content) {
|
|
2269
|
+
expect(parsed.pages[0].content).toContain("Detail content");
|
|
2270
|
+
}
|
|
2271
|
+
});
|
|
2272
|
+
it("knowledge_gap is returned when read_top_n is set but no results found", async () => {
|
|
2273
|
+
const wiki = freshWiki();
|
|
2274
|
+
const result = await handleTool(wiki, "wiki_search", { query: "nonexistent-xyz-123", read_top_n: 3 });
|
|
2275
|
+
const parsed = JSON.parse(result);
|
|
2276
|
+
expect(parsed.results).toHaveLength(0);
|
|
2277
|
+
expect(parsed).toHaveProperty("knowledge_gap");
|
|
2278
|
+
});
|
|
2279
|
+
});
|
|
2280
|
+
describe("backward compat: legacy tool names still work via handleTool", () => {
|
|
2281
|
+
beforeEach(cleanUp);
|
|
2282
|
+
afterEach(cleanUp);
|
|
2283
|
+
it("raw_add still works", async () => {
|
|
2284
|
+
const wiki = freshWiki();
|
|
2285
|
+
const result = await handleTool(wiki, "raw_add", {
|
|
2286
|
+
filename: "compat-test.md",
|
|
2287
|
+
content: "backward compat content",
|
|
2288
|
+
});
|
|
2289
|
+
const parsed = JSON.parse(result);
|
|
2290
|
+
expect(parsed.ok).toBe(true);
|
|
2291
|
+
});
|
|
2292
|
+
it("wiki_config still works", async () => {
|
|
2293
|
+
const wiki = freshWiki();
|
|
2294
|
+
const result = await handleTool(wiki, "wiki_config", {});
|
|
2295
|
+
const parsed = JSON.parse(result);
|
|
2296
|
+
expect(parsed).toHaveProperty("wikiDir");
|
|
2297
|
+
});
|
|
2298
|
+
it("wiki_lint still works", async () => {
|
|
2299
|
+
const wiki = freshWiki();
|
|
2300
|
+
const result = await handleTool(wiki, "wiki_lint", {});
|
|
2301
|
+
const parsed = JSON.parse(result);
|
|
2302
|
+
expect(parsed).toHaveProperty("issues");
|
|
2303
|
+
});
|
|
2304
|
+
it("wiki_rebuild still works", async () => {
|
|
2305
|
+
const wiki = freshWiki();
|
|
2306
|
+
const result = await handleTool(wiki, "wiki_rebuild", {});
|
|
2307
|
+
const parsed = JSON.parse(result);
|
|
2308
|
+
expect(parsed.ok).toBe(true);
|
|
2309
|
+
});
|
|
2310
|
+
it("wiki_search_read still works", async () => {
|
|
2311
|
+
const wiki = freshWiki();
|
|
2312
|
+
wiki.write("concept-compat.md", `---
|
|
2313
|
+
title: Compat Page
|
|
2314
|
+
type: concept
|
|
2315
|
+
tags: [compat]
|
|
2316
|
+
---
|
|
2317
|
+
Content for backward compatibility test.`);
|
|
2318
|
+
const result = await handleTool(wiki, "wiki_search_read", { query: "compat" });
|
|
2319
|
+
const parsed = JSON.parse(result);
|
|
2320
|
+
expect(parsed).toHaveProperty("results");
|
|
2321
|
+
expect(parsed).toHaveProperty("pages");
|
|
2322
|
+
});
|
|
2323
|
+
it("knowledge_ingest_batch still works", async () => {
|
|
2324
|
+
const wiki = freshWiki();
|
|
2325
|
+
const srcDir = join(TEST_ROOT, "compat-sources");
|
|
2326
|
+
mkdirSync(srcDir, { recursive: true });
|
|
2327
|
+
writeFileSync(join(srcDir, "compat.txt"), "compat content");
|
|
2328
|
+
wiki.config.allowedSourceDirs = [srcDir];
|
|
2329
|
+
const result = await handleTool(wiki, "knowledge_ingest_batch", {
|
|
2330
|
+
source_path: srcDir,
|
|
2331
|
+
topic: "compat-topic",
|
|
2332
|
+
});
|
|
2333
|
+
const parsed = JSON.parse(result);
|
|
2334
|
+
expect(parsed.ok).toBe(true);
|
|
2335
|
+
});
|
|
2336
|
+
it("knowledge_digest_write still works", async () => {
|
|
2337
|
+
const wiki = freshWiki();
|
|
2338
|
+
const result = await handleTool(wiki, "knowledge_digest_write", {
|
|
2339
|
+
pages: [{ page: "concept-compat-write.md", title: "Compat Write", body: "body text" }],
|
|
2340
|
+
});
|
|
2341
|
+
const parsed = JSON.parse(result);
|
|
2342
|
+
expect(parsed.written).toBe(1);
|
|
2343
|
+
});
|
|
2344
|
+
it("code_trace_variable still works", async () => {
|
|
2345
|
+
const wiki = freshWiki();
|
|
2346
|
+
const cobol = ` IDENTIFICATION DIVISION.
|
|
2347
|
+
PROGRAM-ID. COMPAT.
|
|
2348
|
+
DATA DIVISION.
|
|
2349
|
+
WORKING-STORAGE SECTION.
|
|
2350
|
+
01 WS-X PIC 9(4) VALUE 0.
|
|
2351
|
+
PROCEDURE DIVISION.
|
|
2352
|
+
MOVE 5 TO WS-X.
|
|
2353
|
+
STOP RUN.`;
|
|
2354
|
+
wiki.rawAdd("COMPAT.cbl", { content: cobol });
|
|
2355
|
+
await handleTool(wiki, "code_parse", { path: "COMPAT.cbl" });
|
|
2356
|
+
const result = await handleTool(wiki, "code_trace_variable", {
|
|
2357
|
+
path: "COMPAT.cbl",
|
|
2358
|
+
variable: "WS-X",
|
|
2359
|
+
});
|
|
2360
|
+
const parsed = JSON.parse(result);
|
|
2361
|
+
expect(parsed.variable).toBe("WS-X");
|
|
2362
|
+
});
|
|
2363
|
+
it("code_impact still throws when no compiled graph exists", async () => {
|
|
2364
|
+
const wiki = freshWiki();
|
|
2365
|
+
await expect(handleTool(wiki, "code_impact", { node_id: "program:MISSING" })).rejects.toThrow(/not found/i);
|
|
2366
|
+
});
|
|
2367
|
+
});
|
|
2368
|
+
describe("batch: wiki_admin action:rebuild is deduplicated", () => {
|
|
2369
|
+
beforeEach(cleanUp);
|
|
2370
|
+
afterEach(cleanUp);
|
|
2371
|
+
it("wiki_admin action:rebuild is deferred and merged with end-of-batch rebuild", async () => {
|
|
2372
|
+
const wiki = freshWiki();
|
|
2373
|
+
wiki.write("concept-batch-rebuild.md", `---
|
|
2374
|
+
title: Batch Rebuild Test
|
|
2375
|
+
type: concept
|
|
2376
|
+
tags: [batch]
|
|
2377
|
+
---
|
|
2378
|
+
Content for batch rebuild test.`);
|
|
2379
|
+
const result = await handleTool(wiki, "batch", {
|
|
2380
|
+
operations: [
|
|
2381
|
+
{ tool: "wiki_write", args: { page: "concept-extra.md", content: `---
|
|
2382
|
+
title: Extra
|
|
2383
|
+
type: concept
|
|
2384
|
+
tags: [extra]
|
|
2385
|
+
---
|
|
2386
|
+
Extra content.` } },
|
|
2387
|
+
{ tool: "wiki_admin", args: { action: "rebuild" } },
|
|
2388
|
+
],
|
|
2389
|
+
});
|
|
2390
|
+
const parsed = JSON.parse(result);
|
|
2391
|
+
expect(parsed.count).toBe(2);
|
|
2392
|
+
// wiki_admin action:rebuild should be deferred
|
|
2393
|
+
const rebuildResult = parsed.results.find((r) => r.tool === "wiki_admin");
|
|
2394
|
+
expect(rebuildResult?.result?.deferred).toMatch(/rebuild/);
|
|
2395
|
+
});
|
|
2396
|
+
});
|
|
2397
|
+
//# sourceMappingURL=server.test.js.map
|