mcp.test.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /**
  2. * MCP Server Tests
  3. *
  4. * Tests all MCP tools, resources, and prompts.
  5. * Uses mocked Ollama responses and a test database.
  6. */
  7. import { describe, test, expect, beforeAll, afterAll, beforeEach, afterEach } from "bun:test";
  8. import { Database } from "bun:sqlite";
  9. import * as sqliteVec from "sqlite-vec";
  10. import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
  11. import { z } from "zod";
  12. import { setDefaultOllama, Ollama } from "./llm";
  13. // =============================================================================
  14. // Mock Ollama
  15. // =============================================================================
  16. const OLLAMA_URL = "http://localhost:11434";
  17. const originalFetch = globalThis.fetch;
  18. const mockOllamaResponses: Record<string, (body: unknown) => Response> = {
  19. "/api/embed": () => {
  20. const embedding = Array(768).fill(0).map(() => Math.random());
  21. return new Response(JSON.stringify({ embeddings: [embedding] }), {
  22. status: 200,
  23. headers: { "Content-Type": "application/json" },
  24. });
  25. },
  26. "/api/generate": (body: unknown) => {
  27. const reqBody = body as { prompt?: string };
  28. if (reqBody.prompt?.includes("Judge") || reqBody.prompt?.includes("Document")) {
  29. return new Response(JSON.stringify({
  30. response: "yes",
  31. done: true,
  32. logprobs: { tokens: ["yes"], token_logprobs: [-0.1] },
  33. }), { status: 200, headers: { "Content-Type": "application/json" } });
  34. } else {
  35. return new Response(JSON.stringify({
  36. response: "expanded query variation 1\nexpanded query variation 2",
  37. done: true,
  38. }), { status: 200, headers: { "Content-Type": "application/json" } });
  39. }
  40. },
  41. "/api/show": () => {
  42. return new Response(JSON.stringify({ size: 1000000 }), {
  43. status: 200,
  44. headers: { "Content-Type": "application/json" },
  45. });
  46. },
  47. };
  48. function mockFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
  49. const url = typeof input === "string" ? input : input.toString();
  50. if (url.startsWith(OLLAMA_URL)) {
  51. const path = url.replace(OLLAMA_URL, "");
  52. const handler = mockOllamaResponses[path];
  53. if (handler) {
  54. const body = init?.body ? JSON.parse(init.body as string) : {};
  55. return Promise.resolve(handler(body));
  56. }
  57. throw new Error(`Unmocked Ollama endpoint: ${path}`);
  58. }
  59. throw new Error(`Unexpected fetch call to: ${url}`);
  60. }
  61. // =============================================================================
  62. // Test Database Setup
  63. // =============================================================================
  64. let testDb: Database;
  65. let testDbPath: string;
  66. function initTestDatabase(db: Database): void {
  67. sqliteVec.load(db);
  68. db.exec("PRAGMA journal_mode = WAL");
  69. db.exec(`
  70. CREATE TABLE IF NOT EXISTS collections (
  71. id INTEGER PRIMARY KEY AUTOINCREMENT,
  72. pwd TEXT NOT NULL,
  73. glob_pattern TEXT NOT NULL,
  74. created_at TEXT NOT NULL,
  75. context TEXT,
  76. UNIQUE(pwd, glob_pattern)
  77. )
  78. `);
  79. db.exec(`
  80. CREATE TABLE IF NOT EXISTS path_contexts (
  81. id INTEGER PRIMARY KEY AUTOINCREMENT,
  82. path_prefix TEXT NOT NULL UNIQUE,
  83. context TEXT NOT NULL,
  84. created_at TEXT NOT NULL
  85. )
  86. `);
  87. db.exec(`
  88. CREATE TABLE IF NOT EXISTS ollama_cache (
  89. hash TEXT PRIMARY KEY,
  90. result TEXT NOT NULL,
  91. created_at TEXT NOT NULL
  92. )
  93. `);
  94. db.exec(`
  95. CREATE TABLE IF NOT EXISTS documents (
  96. id INTEGER PRIMARY KEY AUTOINCREMENT,
  97. collection_id INTEGER NOT NULL,
  98. name TEXT NOT NULL,
  99. title TEXT NOT NULL,
  100. hash TEXT NOT NULL,
  101. filepath TEXT NOT NULL,
  102. display_path TEXT NOT NULL DEFAULT '',
  103. body TEXT NOT NULL,
  104. created_at TEXT NOT NULL,
  105. modified_at TEXT NOT NULL,
  106. active INTEGER NOT NULL DEFAULT 1,
  107. FOREIGN KEY (collection_id) REFERENCES collections(id)
  108. )
  109. `);
  110. db.exec(`
  111. CREATE TABLE IF NOT EXISTS content_vectors (
  112. hash TEXT NOT NULL,
  113. seq INTEGER NOT NULL DEFAULT 0,
  114. pos INTEGER NOT NULL DEFAULT 0,
  115. model TEXT NOT NULL,
  116. embedded_at TEXT NOT NULL,
  117. PRIMARY KEY (hash, seq)
  118. )
  119. `);
  120. db.exec(`
  121. CREATE VIRTUAL TABLE IF NOT EXISTS documents_fts USING fts5(
  122. name, body,
  123. content='documents',
  124. content_rowid='id',
  125. tokenize='porter unicode61'
  126. )
  127. `);
  128. db.exec(`
  129. CREATE TRIGGER IF NOT EXISTS documents_ai AFTER INSERT ON documents BEGIN
  130. INSERT INTO documents_fts(rowid, name, body) VALUES (new.id, new.name, new.body);
  131. END
  132. `);
  133. // Create vector table
  134. db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS vectors_vec USING vec0(hash_seq TEXT PRIMARY KEY, embedding float[768])`);
  135. }
  136. function seedTestData(db: Database): void {
  137. const now = new Date().toISOString();
  138. // Create a collection
  139. db.prepare(`INSERT INTO collections (pwd, glob_pattern, created_at, context) VALUES (?, ?, ?, ?)`).run(
  140. "/test/docs",
  141. "**/*.md",
  142. now,
  143. "Test documentation collection"
  144. );
  145. // Add path context
  146. db.prepare(`INSERT INTO path_contexts (path_prefix, context, created_at) VALUES (?, ?, ?)`).run(
  147. "/test/docs/meetings",
  148. "Meeting notes and transcripts",
  149. now
  150. );
  151. // Add test documents
  152. const docs = [
  153. {
  154. name: "readme.md",
  155. title: "Project README",
  156. hash: "hash1",
  157. filepath: "/test/docs/readme.md",
  158. display_path: "readme.md",
  159. body: "# Project README\n\nThis is the main readme file for the project.\n\nIt contains important information about setup and usage.",
  160. },
  161. {
  162. name: "api.md",
  163. title: "API Documentation",
  164. hash: "hash2",
  165. filepath: "/test/docs/api.md",
  166. display_path: "api.md",
  167. body: "# API Documentation\n\nThis document describes the REST API endpoints.\n\n## Authentication\n\nUse Bearer tokens for auth.",
  168. },
  169. {
  170. name: "meeting-2024-01.md",
  171. title: "January Meeting Notes",
  172. hash: "hash3",
  173. filepath: "/test/docs/meetings/meeting-2024-01.md",
  174. display_path: "meetings/meeting-2024-01.md",
  175. body: "# January Meeting Notes\n\nDiscussed Q1 goals and roadmap.\n\n## Action Items\n\n- Review budget\n- Hire new team members",
  176. },
  177. {
  178. name: "meeting-2024-02.md",
  179. title: "February Meeting Notes",
  180. hash: "hash4",
  181. filepath: "/test/docs/meetings/meeting-2024-02.md",
  182. display_path: "meetings/meeting-2024-02.md",
  183. body: "# February Meeting Notes\n\nFollowed up on Q1 progress.\n\n## Updates\n\n- Budget approved\n- Two candidates interviewed",
  184. },
  185. {
  186. name: "large-file.md",
  187. title: "Large Document",
  188. hash: "hash5",
  189. filepath: "/test/docs/large-file.md",
  190. display_path: "large-file.md",
  191. body: "# Large Document\n\n" + "Lorem ipsum ".repeat(2000), // ~24KB
  192. },
  193. ];
  194. for (const doc of docs) {
  195. db.prepare(`
  196. INSERT INTO documents (collection_id, name, title, hash, filepath, display_path, body, created_at, modified_at, active)
  197. VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, 1)
  198. `).run(doc.name, doc.title, doc.hash, doc.filepath, doc.display_path, doc.body, now, now);
  199. }
  200. // Add embeddings for vector search
  201. const embedding = new Float32Array(768);
  202. for (let i = 0; i < 768; i++) embedding[i] = Math.random();
  203. for (const doc of docs.slice(0, 4)) { // Skip large file for embeddings
  204. db.prepare(`INSERT INTO content_vectors (hash, seq, pos, model, embedded_at) VALUES (?, 0, 0, 'embeddinggemma', ?)`).run(doc.hash, now);
  205. db.prepare(`INSERT INTO vectors_vec (hash_seq, embedding) VALUES (?, ?)`).run(`${doc.hash}_0`, embedding);
  206. }
  207. }
  208. // =============================================================================
  209. // MCP Server Test Helpers
  210. // =============================================================================
  211. // We need to create a testable version of the MCP handlers
  212. // Since McpServer uses internal routing, we'll test the handler functions directly
  213. import {
  214. searchFTS,
  215. searchVec,
  216. expandQuery,
  217. rerank,
  218. reciprocalRankFusion,
  219. extractSnippet,
  220. getContextForFile,
  221. getCollectionIdByName,
  222. getDocument,
  223. getMultipleDocuments,
  224. getStatus,
  225. DEFAULT_EMBED_MODEL,
  226. DEFAULT_QUERY_MODEL,
  227. DEFAULT_RERANK_MODEL,
  228. DEFAULT_MULTI_GET_MAX_BYTES,
  229. } from "./store";
  230. import type { RankedResult } from "./store";
  231. import { searchResultsToMcpCsv } from "./formatter";
  232. // =============================================================================
  233. // Tests
  234. // =============================================================================
  235. describe("MCP Server", () => {
  236. beforeAll(() => {
  237. globalThis.fetch = mockFetch as typeof fetch;
  238. setDefaultOllama(new Ollama({ baseUrl: OLLAMA_URL }));
  239. testDbPath = `/tmp/qmd-mcp-test-${Date.now()}.sqlite`;
  240. testDb = new Database(testDbPath);
  241. initTestDatabase(testDb);
  242. seedTestData(testDb);
  243. });
  244. afterAll(() => {
  245. globalThis.fetch = originalFetch;
  246. setDefaultOllama(null);
  247. testDb.close();
  248. try {
  249. require("fs").unlinkSync(testDbPath);
  250. } catch {}
  251. });
  252. // ===========================================================================
  253. // Tool: qmd_search (BM25)
  254. // ===========================================================================
  255. describe("qmd_search tool", () => {
  256. test("returns results for matching query", () => {
  257. const results = searchFTS(testDb, "readme", 10);
  258. expect(results.length).toBeGreaterThan(0);
  259. expect(results[0].displayPath).toBe("readme.md");
  260. });
  261. test("returns empty for non-matching query", () => {
  262. const results = searchFTS(testDb, "xyznonexistent", 10);
  263. expect(results.length).toBe(0);
  264. });
  265. test("respects limit parameter", () => {
  266. const results = searchFTS(testDb, "meeting", 1);
  267. expect(results.length).toBe(1);
  268. });
  269. test("filters by collection", () => {
  270. const collectionId = getCollectionIdByName(testDb, "docs");
  271. expect(collectionId).toBe(1);
  272. const results = searchFTS(testDb, "meeting", 10, collectionId!);
  273. expect(results.length).toBeGreaterThan(0);
  274. });
  275. test("returns null for non-existent collection", () => {
  276. const collectionId = getCollectionIdByName(testDb, "nonexistent");
  277. expect(collectionId).toBeNull();
  278. });
  279. test("formats results as CSV", () => {
  280. const results = searchFTS(testDb, "api", 10);
  281. const filtered = results.map(r => ({
  282. file: r.displayPath,
  283. title: r.title,
  284. score: Math.round(r.score * 100) / 100,
  285. context: getContextForFile(testDb, r.file),
  286. snippet: extractSnippet(r.body, "api", 300, r.chunkPos).snippet,
  287. }));
  288. const csv = searchResultsToMcpCsv(filtered);
  289. expect(csv).toContain("file,title,score,context,snippet");
  290. expect(csv).toContain("api.md");
  291. });
  292. });
  293. // ===========================================================================
  294. // Tool: qmd_vsearch (Vector)
  295. // ===========================================================================
  296. describe("qmd_vsearch tool", () => {
  297. test("returns results for semantic query", async () => {
  298. const results = await searchVec(testDb, "project documentation", DEFAULT_EMBED_MODEL, 10);
  299. expect(results.length).toBeGreaterThan(0);
  300. });
  301. test("respects limit parameter", async () => {
  302. const results = await searchVec(testDb, "documentation", DEFAULT_EMBED_MODEL, 2);
  303. expect(results.length).toBeLessThanOrEqual(2);
  304. });
  305. test("returns empty when no vector table exists", async () => {
  306. const emptyDb = new Database(":memory:");
  307. initTestDatabase(emptyDb);
  308. emptyDb.exec("DROP TABLE IF EXISTS vectors_vec");
  309. const results = await searchVec(emptyDb, "test", DEFAULT_EMBED_MODEL, 10);
  310. expect(results.length).toBe(0);
  311. emptyDb.close();
  312. });
  313. });
  314. // ===========================================================================
  315. // Tool: qmd_query (Hybrid)
  316. // ===========================================================================
  317. describe("qmd_query tool", () => {
  318. test("expands query with variations", async () => {
  319. const queries = await expandQuery("api documentation", DEFAULT_QUERY_MODEL, testDb);
  320. expect(queries.length).toBeGreaterThan(1);
  321. expect(queries[0]).toBe("api documentation");
  322. });
  323. test("performs RRF fusion on multiple result lists", () => {
  324. const list1: RankedResult[] = [
  325. { file: "/a", displayPath: "a.md", title: "A", body: "body", score: 1 },
  326. { file: "/b", displayPath: "b.md", title: "B", body: "body", score: 0.8 },
  327. ];
  328. const list2: RankedResult[] = [
  329. { file: "/b", displayPath: "b.md", title: "B", body: "body", score: 1 },
  330. { file: "/c", displayPath: "c.md", title: "C", body: "body", score: 0.9 },
  331. ];
  332. const fused = reciprocalRankFusion([list1, list2]);
  333. expect(fused.length).toBe(3);
  334. // B appears in both lists, should have higher score
  335. const bResult = fused.find(r => r.file === "/b");
  336. expect(bResult).toBeDefined();
  337. });
  338. test("reranks documents with LLM", async () => {
  339. const docs = [
  340. { file: "/test/docs/readme.md", text: "Project readme" },
  341. { file: "/test/docs/api.md", text: "API documentation" },
  342. ];
  343. const reranked = await rerank("readme", docs, DEFAULT_RERANK_MODEL, testDb);
  344. expect(reranked.length).toBe(2);
  345. expect(reranked[0].score).toBeGreaterThan(0);
  346. });
  347. test("full hybrid search pipeline", async () => {
  348. // Simulate full qmd_query flow
  349. const query = "meeting notes";
  350. const queries = await expandQuery(query, DEFAULT_QUERY_MODEL, testDb);
  351. const rankedLists: RankedResult[][] = [];
  352. for (const q of queries) {
  353. const ftsResults = searchFTS(testDb, q, 20);
  354. if (ftsResults.length > 0) {
  355. rankedLists.push(ftsResults.map(r => ({
  356. file: r.file,
  357. displayPath: r.displayPath,
  358. title: r.title,
  359. body: r.body,
  360. score: r.score,
  361. })));
  362. }
  363. }
  364. expect(rankedLists.length).toBeGreaterThan(0);
  365. const fused = reciprocalRankFusion(rankedLists);
  366. expect(fused.length).toBeGreaterThan(0);
  367. const candidates = fused.slice(0, 10);
  368. const reranked = await rerank(
  369. query,
  370. candidates.map(c => ({ file: c.file, text: c.body })),
  371. DEFAULT_RERANK_MODEL,
  372. testDb
  373. );
  374. expect(reranked.length).toBeGreaterThan(0);
  375. });
  376. });
  377. // ===========================================================================
  378. // Tool: qmd_get (Get Document)
  379. // ===========================================================================
  380. describe("qmd_get tool", () => {
  381. test("retrieves document by display_path", () => {
  382. const result = getDocument(testDb, "readme.md");
  383. expect("error" in result).toBe(false);
  384. if (!("error" in result)) {
  385. expect(result.displayPath).toBe("readme.md");
  386. expect(result.body).toContain("Project README");
  387. }
  388. });
  389. test("retrieves document by filepath", () => {
  390. const result = getDocument(testDb, "/test/docs/api.md");
  391. expect("error" in result).toBe(false);
  392. if (!("error" in result)) {
  393. expect(result.title).toBe("API Documentation");
  394. }
  395. });
  396. test("retrieves document by partial path", () => {
  397. const result = getDocument(testDb, "api.md");
  398. expect("error" in result).toBe(false);
  399. });
  400. test("returns not found for missing document", () => {
  401. const result = getDocument(testDb, "nonexistent.md");
  402. expect("error" in result).toBe(true);
  403. if ("error" in result) {
  404. expect(result.error).toBe("not_found");
  405. }
  406. });
  407. test("suggests similar files when not found", () => {
  408. const result = getDocument(testDb, "readm.md"); // typo
  409. expect("error" in result).toBe(true);
  410. if ("error" in result) {
  411. expect(result.similarFiles.length).toBeGreaterThanOrEqual(0);
  412. }
  413. });
  414. test("supports line range with :line suffix", () => {
  415. const result = getDocument(testDb, "readme.md:2", undefined, 2);
  416. expect("error" in result).toBe(false);
  417. if (!("error" in result)) {
  418. const lines = result.body.split("\n");
  419. expect(lines.length).toBeLessThanOrEqual(2);
  420. }
  421. });
  422. test("supports fromLine parameter", () => {
  423. const result = getDocument(testDb, "readme.md", 3);
  424. expect("error" in result).toBe(false);
  425. if (!("error" in result)) {
  426. expect(result.body).not.toContain("# Project README");
  427. }
  428. });
  429. test("supports maxLines parameter", () => {
  430. const result = getDocument(testDb, "api.md", 1, 3);
  431. expect("error" in result).toBe(false);
  432. if (!("error" in result)) {
  433. const lines = result.body.split("\n");
  434. expect(lines.length).toBeLessThanOrEqual(3);
  435. }
  436. });
  437. test("includes context for documents in context path", () => {
  438. const result = getDocument(testDb, "meetings/meeting-2024-01.md");
  439. expect("error" in result).toBe(false);
  440. if (!("error" in result)) {
  441. expect(result.context).toBe("Meeting notes and transcripts");
  442. }
  443. });
  444. });
  445. // ===========================================================================
  446. // Tool: qmd_multi_get (Multi Get)
  447. // ===========================================================================
  448. describe("qmd_multi_get tool", () => {
  449. test("retrieves multiple documents by glob pattern", () => {
  450. const { files, errors } = getMultipleDocuments(testDb, "meetings/*.md");
  451. expect(errors.length).toBe(0);
  452. expect(files.length).toBe(2);
  453. expect(files.some(f => f.displayPath === "meetings/meeting-2024-01.md")).toBe(true);
  454. expect(files.some(f => f.displayPath === "meetings/meeting-2024-02.md")).toBe(true);
  455. });
  456. test("retrieves documents by comma-separated list", () => {
  457. const { files, errors } = getMultipleDocuments(testDb, "readme.md, api.md");
  458. expect(errors.length).toBe(0);
  459. expect(files.length).toBe(2);
  460. });
  461. test("returns errors for missing files in comma list", () => {
  462. const { files, errors } = getMultipleDocuments(testDb, "readme.md, nonexistent.md");
  463. expect(files.length).toBe(1);
  464. expect(errors.length).toBe(1);
  465. expect(errors[0]).toContain("not found");
  466. });
  467. test("skips files larger than maxBytes", () => {
  468. const { files } = getMultipleDocuments(testDb, "*.md", undefined, 1000); // 1KB limit
  469. const largeFile = files.find(f => f.displayPath === "large-file.md");
  470. expect(largeFile).toBeDefined();
  471. expect(largeFile?.skipped).toBe(true);
  472. if (largeFile?.skipped) {
  473. expect(largeFile.skipReason).toContain("too large");
  474. }
  475. });
  476. test("respects maxLines parameter", () => {
  477. const { files } = getMultipleDocuments(testDb, "readme.md", 2);
  478. expect(files.length).toBe(1);
  479. if (!files[0].skipped) {
  480. const lines = files[0].body.split("\n");
  481. // maxLines + truncation message
  482. expect(lines.length).toBeLessThanOrEqual(4);
  483. }
  484. });
  485. test("returns error for non-matching glob", () => {
  486. const { files, errors } = getMultipleDocuments(testDb, "nonexistent/*.md");
  487. expect(files.length).toBe(0);
  488. expect(errors.length).toBe(1);
  489. expect(errors[0]).toContain("No files matched");
  490. });
  491. test("includes context in results", () => {
  492. const { files } = getMultipleDocuments(testDb, "meetings/meeting-2024-01.md");
  493. expect(files.length).toBe(1);
  494. if (!files[0].skipped) {
  495. expect(files[0].context).toBe("Meeting notes and transcripts");
  496. }
  497. });
  498. });
  499. // ===========================================================================
  500. // Tool: qmd_status
  501. // ===========================================================================
  502. describe("qmd_status tool", () => {
  503. test("returns index status", () => {
  504. const status = getStatus(testDb);
  505. expect(status.totalDocuments).toBe(5);
  506. expect(status.hasVectorIndex).toBe(true);
  507. expect(status.collections.length).toBe(1);
  508. expect(status.collections[0].path).toBe("/test/docs");
  509. });
  510. test("shows documents needing embedding", () => {
  511. const status = getStatus(testDb);
  512. // large-file.md doesn't have embeddings
  513. expect(status.needsEmbedding).toBe(1);
  514. });
  515. });
  516. // ===========================================================================
  517. // Resource: qmd://{path}
  518. // ===========================================================================
  519. describe("qmd:// resource", () => {
  520. test("lists all documents", () => {
  521. const docs = testDb.prepare(`
  522. SELECT display_path, title
  523. FROM documents
  524. WHERE active = 1
  525. ORDER BY modified_at DESC
  526. LIMIT 1000
  527. `).all() as { display_path: string; title: string }[];
  528. expect(docs.length).toBe(5);
  529. expect(docs.map(d => d.display_path)).toContain("readme.md");
  530. });
  531. test("reads document by display_path", () => {
  532. const path = "readme.md";
  533. const doc = testDb.prepare(`
  534. SELECT filepath, display_path, body
  535. FROM documents
  536. WHERE display_path = ? AND active = 1
  537. `).get(path) as { filepath: string; display_path: string; body: string } | null;
  538. expect(doc).not.toBeNull();
  539. expect(doc?.body).toContain("Project README");
  540. });
  541. test("reads document by URL-encoded path", () => {
  542. // Simulate URL encoding that MCP clients may send
  543. const encodedPath = "meetings%2Fmeeting-2024-01.md";
  544. const decodedPath = decodeURIComponent(encodedPath);
  545. const doc = testDb.prepare(`
  546. SELECT filepath, display_path, body
  547. FROM documents
  548. WHERE display_path = ? AND active = 1
  549. `).get(decodedPath) as { filepath: string; display_path: string; body: string } | null;
  550. expect(doc).not.toBeNull();
  551. expect(doc?.display_path).toBe("meetings/meeting-2024-01.md");
  552. });
  553. test("reads document by suffix match", () => {
  554. const path = "meeting-2024-01.md"; // without meetings/ prefix
  555. let doc = testDb.prepare(`
  556. SELECT filepath, display_path, body
  557. FROM documents
  558. WHERE display_path = ? AND active = 1
  559. `).get(path) as { filepath: string; display_path: string; body: string } | null;
  560. if (!doc) {
  561. doc = testDb.prepare(`
  562. SELECT filepath, display_path, body
  563. FROM documents
  564. WHERE display_path LIKE ? AND active = 1
  565. LIMIT 1
  566. `).get(`%${path}`) as { filepath: string; display_path: string; body: string } | null;
  567. }
  568. expect(doc).not.toBeNull();
  569. expect(doc?.display_path).toBe("meetings/meeting-2024-01.md");
  570. });
  571. test("returns not found for missing document", () => {
  572. const path = "nonexistent.md";
  573. const doc = testDb.prepare(`
  574. SELECT filepath, display_path, body
  575. FROM documents
  576. WHERE display_path = ? AND active = 1
  577. `).get(path) as { filepath: string; display_path: string; body: string } | null;
  578. expect(doc).toBeNull();
  579. });
  580. test("includes context in document body", () => {
  581. const path = "meetings/meeting-2024-01.md";
  582. const doc = testDb.prepare(`
  583. SELECT filepath, display_path, body
  584. FROM documents
  585. WHERE display_path = ? AND active = 1
  586. `).get(path) as { filepath: string; display_path: string; body: string } | null;
  587. expect(doc).not.toBeNull();
  588. const context = getContextForFile(testDb, doc!.filepath);
  589. expect(context).toBe("Meeting notes and transcripts");
  590. // Verify context would be prepended
  591. let text = doc!.body;
  592. if (context) {
  593. text = `<!-- Context: ${context} -->\n\n` + text;
  594. }
  595. expect(text).toContain("<!-- Context: Meeting notes and transcripts -->");
  596. });
  597. test("handles URL-encoded special characters", () => {
  598. // Test various URL encodings
  599. const testCases = [
  600. { encoded: "readme.md", decoded: "readme.md" },
  601. { encoded: "meetings%2Fmeeting-2024-01.md", decoded: "meetings/meeting-2024-01.md" },
  602. { encoded: "api.md%3A10", decoded: "api.md:10" }, // with line number
  603. ];
  604. for (const { encoded, decoded } of testCases) {
  605. expect(decodeURIComponent(encoded)).toBe(decoded);
  606. }
  607. });
  608. test("handles double-encoded URLs", () => {
  609. // Some clients may double-encode
  610. const doubleEncoded = "meetings%252Fmeeting-2024-01.md";
  611. const singleDecoded = decodeURIComponent(doubleEncoded);
  612. expect(singleDecoded).toBe("meetings%2Fmeeting-2024-01.md");
  613. const fullyDecoded = decodeURIComponent(singleDecoded);
  614. expect(fullyDecoded).toBe("meetings/meeting-2024-01.md");
  615. });
  616. test("handles URL-encoded paths with spaces", () => {
  617. // Add a document with spaces in the path
  618. const now = new Date().toISOString();
  619. testDb.prepare(`
  620. INSERT INTO documents (collection_id, name, title, hash, filepath, display_path, body, created_at, modified_at, active)
  621. VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, 1)
  622. `).run(
  623. "podcast with spaces.md",
  624. "Podcast Episode",
  625. "hash_spaces",
  626. "/test/docs/External Podcast/2023 April - Interview.md",
  627. "External Podcast/2023 April - Interview.md",
  628. "# Podcast Episode\n\nInterview content here.",
  629. now,
  630. now
  631. );
  632. // Simulate URL-encoded path from MCP client
  633. const encodedPath = "External%20Podcast%2F2023%20April%20-%20Interview.md";
  634. const decodedPath = decodeURIComponent(encodedPath);
  635. expect(decodedPath).toBe("External Podcast/2023 April - Interview.md");
  636. const doc = testDb.prepare(`
  637. SELECT filepath, display_path, body
  638. FROM documents
  639. WHERE display_path = ? AND active = 1
  640. `).get(decodedPath) as { filepath: string; display_path: string; body: string } | null;
  641. expect(doc).not.toBeNull();
  642. expect(doc?.display_path).toBe("External Podcast/2023 April - Interview.md");
  643. expect(doc?.body).toContain("Podcast Episode");
  644. });
  645. });
  646. // ===========================================================================
  647. // Prompt: query
  648. // ===========================================================================
  649. describe("query prompt", () => {
  650. test("returns usage guide", () => {
  651. // The prompt content is static, just verify the structure
  652. const promptContent = `# QMD - Quick Markdown Search
  653. QMD is your on-device search engine for markdown knowledge bases.`;
  654. expect(promptContent).toContain("QMD");
  655. expect(promptContent).toContain("search");
  656. });
  657. test("describes all available tools", () => {
  658. const toolNames = [
  659. "qmd_search",
  660. "qmd_vsearch",
  661. "qmd_query",
  662. "qmd_get",
  663. "qmd_multi_get",
  664. "qmd_status",
  665. ];
  666. // Verify these are documented in the prompt
  667. const promptGuide = `
  668. ### 1. qmd_search (Fast keyword search)
  669. ### 2. qmd_vsearch (Semantic search)
  670. ### 3. qmd_query (Hybrid search - highest quality)
  671. ### 4. qmd_get (Retrieve document)
  672. ### 5. qmd_multi_get (Retrieve multiple documents)
  673. ### 6. qmd_status (Index info)
  674. `;
  675. for (const tool of toolNames) {
  676. expect(promptGuide).toContain(tool);
  677. }
  678. });
  679. });
  680. // ===========================================================================
  681. // Edge Cases
  682. // ===========================================================================
  683. describe("edge cases", () => {
  684. test("handles empty query", () => {
  685. const results = searchFTS(testDb, "", 10);
  686. expect(results.length).toBe(0);
  687. });
  688. test("handles special characters in query", () => {
  689. const results = searchFTS(testDb, "project's", 10);
  690. // Should not throw
  691. expect(Array.isArray(results)).toBe(true);
  692. });
  693. test("handles unicode in query", () => {
  694. const results = searchFTS(testDb, "文档", 10);
  695. expect(Array.isArray(results)).toBe(true);
  696. });
  697. test("handles very long query", () => {
  698. const longQuery = "documentation ".repeat(100);
  699. const results = searchFTS(testDb, longQuery, 10);
  700. expect(Array.isArray(results)).toBe(true);
  701. });
  702. test("handles query with only stopwords", () => {
  703. const results = searchFTS(testDb, "the and or", 10);
  704. expect(Array.isArray(results)).toBe(true);
  705. });
  706. test("extracts snippet around matching text", () => {
  707. const body = "Line 1\nLine 2\nThis is the important line with the keyword\nLine 4\nLine 5";
  708. const { line, snippet } = extractSnippet(body, "keyword", 200);
  709. expect(snippet).toContain("keyword");
  710. expect(line).toBe(3);
  711. });
  712. test("handles snippet extraction with chunkPos", () => {
  713. const body = "A".repeat(1000) + "KEYWORD" + "B".repeat(1000);
  714. const chunkPos = 1000; // Position of KEYWORD
  715. const { snippet } = extractSnippet(body, "keyword", 200, chunkPos);
  716. expect(snippet).toContain("KEYWORD");
  717. });
  718. });
  719. // ===========================================================================
  720. // CSV Formatting
  721. // ===========================================================================
  722. describe("CSV formatting", () => {
  723. test("escapes quotes in CSV", () => {
  724. const results = [{
  725. file: 'test.md',
  726. title: 'Test "quoted" title',
  727. score: 0.9,
  728. context: null,
  729. snippet: 'Some "quoted" text',
  730. }];
  731. const csv = searchResultsToMcpCsv(results);
  732. expect(csv).toContain('""quoted""');
  733. });
  734. test("escapes newlines in CSV", () => {
  735. const results = [{
  736. file: 'test.md',
  737. title: 'Test title',
  738. score: 0.9,
  739. context: null,
  740. snippet: 'Line 1\nLine 2',
  741. }];
  742. const csv = searchResultsToMcpCsv(results);
  743. expect(csv).not.toContain('\n\n'); // Should be escaped within quotes
  744. });
  745. test("handles empty results", () => {
  746. const csv = searchResultsToMcpCsv([]);
  747. expect(csv).toBe("file,title,score,context,snippet");
  748. });
  749. });
  750. });