cli.test.ts 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /**
  2. * CLI Integration Tests
  3. *
  4. * Tests all qmd CLI commands using a temporary test database via INDEX_PATH.
  5. * These tests spawn actual qmd processes to verify end-to-end functionality.
  6. */
  7. import { describe, test, expect, beforeAll, afterAll, beforeEach } from "vitest";
  8. import { mkdtemp, rm, writeFile, mkdir } from "fs/promises";
  9. import { existsSync, lstatSync, readFileSync, symlinkSync, writeFileSync, unlinkSync } from "fs";
  10. import { tmpdir } from "os";
  11. import { join, dirname } from "path";
  12. import { fileURLToPath } from "url";
  13. import { spawn } from "child_process";
  14. import { setTimeout as sleep } from "timers/promises";
  15. // Test fixtures directory and database path
  16. let testDir: string;
  17. let testDbPath: string;
  18. let testConfigDir: string;
  19. let fixturesDir: string;
  20. let testCounter = 0; // Unique counter for each test run
  21. // Get the directory where this test file lives
  22. const thisDir = dirname(fileURLToPath(import.meta.url));
  23. const projectRoot = join(thisDir, "..");
  24. const qmdScript = join(projectRoot, "src", "cli", "qmd.ts");
  25. // Resolve tsx binary from project's node_modules (not cwd-dependent)
  26. const tsxBin = (() => {
  27. const candidate = join(projectRoot, "node_modules", ".bin", "tsx");
  28. if (existsSync(candidate)) {
  29. return candidate;
  30. }
  31. return join(process.cwd(), "node_modules", ".bin", "tsx");
  32. })();
  33. // Helper to run qmd command with test database
  34. async function runQmd(
  35. args: string[],
  36. options: { cwd?: string; env?: Record<string, string>; dbPath?: string; configDir?: string } = {}
  37. ): Promise<{ stdout: string; stderr: string; exitCode: number }> {
  38. const workingDir = options.cwd || fixturesDir;
  39. const dbPath = options.dbPath || testDbPath;
  40. const configDir = options.configDir || testConfigDir;
  41. const proc = spawn(tsxBin, [qmdScript, ...args], {
  42. cwd: workingDir,
  43. env: {
  44. ...process.env,
  45. INDEX_PATH: dbPath,
  46. QMD_CONFIG_DIR: configDir, // Use test config directory
  47. PWD: workingDir, // Must explicitly set PWD since getPwd() checks this
  48. ...options.env,
  49. },
  50. stdio: ["ignore", "pipe", "pipe"],
  51. });
  52. const stdoutPromise = new Promise<string>((resolve, reject) => {
  53. let data = "";
  54. proc.stdout?.on("data", (chunk: Buffer) => { data += chunk.toString(); });
  55. proc.once("error", reject);
  56. proc.stdout?.once("end", () => resolve(data));
  57. });
  58. const stderrPromise = new Promise<string>((resolve, reject) => {
  59. let data = "";
  60. proc.stderr?.on("data", (chunk: Buffer) => { data += chunk.toString(); });
  61. proc.once("error", reject);
  62. proc.stderr?.once("end", () => resolve(data));
  63. });
  64. const exitCode = await new Promise<number>((resolve, reject) => {
  65. proc.once("error", reject);
  66. proc.on("close", (code) => resolve(code ?? 1));
  67. });
  68. const stdout = await stdoutPromise;
  69. const stderr = await stderrPromise;
  70. return { stdout, stderr, exitCode };
  71. }
  72. // Get a fresh database path for isolated tests
  73. function getFreshDbPath(): string {
  74. testCounter++;
  75. return join(testDir, `test-${testCounter}.sqlite`);
  76. }
  77. // Create an isolated test environment (db + config dir)
  78. async function createIsolatedTestEnv(prefix: string): Promise<{ dbPath: string; configDir: string }> {
  79. testCounter++;
  80. const dbPath = join(testDir, `${prefix}-${testCounter}.sqlite`);
  81. const configDir = join(testDir, `${prefix}-config-${testCounter}`);
  82. await mkdir(configDir, { recursive: true });
  83. await writeFile(join(configDir, "index.yml"), "collections: {}\n");
  84. return { dbPath, configDir };
  85. }
  86. // Setup test fixtures
  87. beforeAll(async () => {
  88. // Create temp directory structure
  89. testDir = await mkdtemp(join(tmpdir(), "qmd-test-"));
  90. testDbPath = join(testDir, "test.sqlite");
  91. testConfigDir = join(testDir, "config");
  92. fixturesDir = join(testDir, "fixtures");
  93. await mkdir(testConfigDir, { recursive: true });
  94. await mkdir(fixturesDir, { recursive: true });
  95. await mkdir(join(fixturesDir, "notes"), { recursive: true });
  96. await mkdir(join(fixturesDir, "docs"), { recursive: true });
  97. // Create empty YAML config for tests
  98. await writeFile(
  99. join(testConfigDir, "index.yml"),
  100. "collections: {}\n"
  101. );
  102. // Create test markdown files
  103. await writeFile(
  104. join(fixturesDir, "README.md"),
  105. `# Test Project
  106. This is a test project for QMD CLI testing.
  107. ## Features
  108. - Full-text search with BM25
  109. - Vector similarity search
  110. - Hybrid search with reranking
  111. `
  112. );
  113. await writeFile(
  114. join(fixturesDir, "notes", "meeting.md"),
  115. `# Team Meeting Notes
  116. Date: 2024-01-15
  117. ## Attendees
  118. - Alice
  119. - Bob
  120. - Charlie
  121. ## Discussion Topics
  122. - Project timeline review
  123. - Resource allocation
  124. - Technical debt prioritization
  125. ## Action Items
  126. 1. Alice to update documentation
  127. 2. Bob to fix authentication bug
  128. 3. Charlie to review pull requests
  129. `
  130. );
  131. await writeFile(
  132. join(fixturesDir, "notes", "ideas.md"),
  133. `# Product Ideas
  134. ## Feature Requests
  135. - Dark mode support
  136. - Keyboard shortcuts
  137. - Export to PDF
  138. ## Technical Improvements
  139. - Improve search performance
  140. - Add caching layer
  141. - Optimize database queries
  142. `
  143. );
  144. await writeFile(
  145. join(fixturesDir, "docs", "api.md"),
  146. `# API Documentation
  147. ## Endpoints
  148. ### GET /search
  149. Search for documents.
  150. Parameters:
  151. - q: Search query (required)
  152. - limit: Max results (default: 10)
  153. ### GET /document/:id
  154. Retrieve a specific document.
  155. ### POST /index
  156. Index new documents.
  157. `
  158. );
  159. // Create test files for path normalization tests
  160. await writeFile(
  161. join(fixturesDir, "test1.md"),
  162. `# Test Document 1
  163. This is the first test document.
  164. It has multiple lines for testing line numbers.
  165. Line 6 is here.
  166. Line 7 is here.
  167. `
  168. );
  169. await writeFile(
  170. join(fixturesDir, "test2.md"),
  171. `# Test Document 2
  172. This is the second test document.
  173. `
  174. );
  175. });
  176. // Cleanup after all tests
  177. afterAll(async () => {
  178. if (testDir) {
  179. await rm(testDir, { recursive: true, force: true });
  180. }
  181. });
  182. // Reset YAML config before each test to ensure isolation
  183. beforeEach(async () => {
  184. // Reset to empty collections config
  185. await writeFile(
  186. join(testConfigDir, "index.yml"),
  187. "collections: {}\n"
  188. );
  189. });
  190. describe("CLI Help", () => {
  191. test("shows help with --help flag", async () => {
  192. const { stdout, exitCode } = await runQmd(["--help"]);
  193. expect(exitCode).toBe(0);
  194. expect(stdout).toContain("Usage:");
  195. expect(stdout).toContain("qmd collection add");
  196. expect(stdout).toContain("qmd search");
  197. expect(stdout).toContain("qmd skill show/install");
  198. });
  199. test("shows help with no arguments", async () => {
  200. const { stdout, exitCode } = await runQmd([]);
  201. expect(exitCode).toBe(1);
  202. expect(stdout).toContain("Usage:");
  203. });
  204. });
  205. describe("CLI Embed", () => {
  206. test("rejects invalid --max-docs-per-batch", async () => {
  207. const { stderr, exitCode } = await runQmd(["embed", "--max-docs-per-batch", "0"]);
  208. expect(exitCode).toBe(1);
  209. expect(stderr).toContain("maxDocsPerBatch");
  210. });
  211. test("rejects invalid --max-batch-mb", async () => {
  212. const { stderr, exitCode } = await runQmd(["embed", "--max-batch-mb", "0"]);
  213. expect(exitCode).toBe(1);
  214. expect(stderr).toContain("maxBatchBytes");
  215. });
  216. });
  217. describe("CLI Skill Commands", () => {
  218. test("shows embedded skill with --skill alias", async () => {
  219. const { stdout, exitCode } = await runQmd(["--skill"]);
  220. expect(exitCode).toBe(0);
  221. expect(stdout).toContain("QMD Skill (embedded)");
  222. expect(stdout).toContain("name: qmd");
  223. expect(stdout).toContain("allowed-tools: Bash(qmd:*), mcp__qmd__*");
  224. });
  225. test("shows skill help with -h", async () => {
  226. const { stdout, exitCode } = await runQmd(["skill", "-h"]);
  227. expect(exitCode).toBe(0);
  228. expect(stdout).toContain("Usage: qmd skill <show|install> [options]");
  229. expect(stdout).toContain("install");
  230. expect(stdout).toContain("--global");
  231. });
  232. test("installs the skill into the current project", async () => {
  233. const projectDir = join(testDir, "skill-project");
  234. await mkdir(projectDir, { recursive: true });
  235. const { stdout, exitCode } = await runQmd(["skill", "install"], { cwd: projectDir });
  236. expect(exitCode).toBe(0);
  237. const skillDir = join(projectDir, ".agents", "skills", "qmd");
  238. expect(readFileSync(join(skillDir, "SKILL.md"), "utf-8")).toContain("name: qmd");
  239. expect(readFileSync(join(skillDir, "references", "mcp-setup.md"), "utf-8")).toContain("Claude Code");
  240. expect(existsSync(join(projectDir, ".claude", "skills", "qmd"))).toBe(false);
  241. expect(stdout).toContain(`✓ Installed QMD skill to ${skillDir}`);
  242. expect(stdout).toContain("Tip: create a Claude symlink manually");
  243. });
  244. test("installs globally and creates the Claude symlink with --yes", async () => {
  245. const fakeHome = join(testDir, "skill-home");
  246. await mkdir(fakeHome, { recursive: true });
  247. const { stdout, exitCode } = await runQmd(["skill", "install", "--global", "--yes"], {
  248. env: { HOME: fakeHome },
  249. });
  250. expect(exitCode).toBe(0);
  251. const skillDir = join(fakeHome, ".agents", "skills", "qmd");
  252. const claudeLink = join(fakeHome, ".claude", "skills", "qmd");
  253. expect(readFileSync(join(skillDir, "SKILL.md"), "utf-8")).toContain("name: qmd");
  254. expect(lstatSync(claudeLink).isSymbolicLink()).toBe(true);
  255. expect(readFileSync(join(claudeLink, "SKILL.md"), "utf-8")).toContain("name: qmd");
  256. expect(stdout).toContain(`✓ Installed QMD skill to ${skillDir}`);
  257. expect(stdout).toContain(`✓ Linked Claude skill at ${claudeLink}`);
  258. });
  259. test("skips Claude qmd symlink when .claude/skills already points to .agents/skills", async () => {
  260. const fakeHome = join(testDir, "skill-home-shared");
  261. await mkdir(join(fakeHome, ".agents"), { recursive: true });
  262. await mkdir(join(fakeHome, ".claude"), { recursive: true });
  263. symlinkSync(join(fakeHome, ".agents", "skills"), join(fakeHome, ".claude", "skills"), "dir");
  264. const { stdout, exitCode } = await runQmd(["skill", "install", "--global", "--yes"], {
  265. env: { HOME: fakeHome },
  266. });
  267. expect(exitCode).toBe(0);
  268. const skillDir = join(fakeHome, ".agents", "skills", "qmd");
  269. expect(lstatSync(skillDir).isSymbolicLink()).toBe(false);
  270. expect(readFileSync(join(skillDir, "SKILL.md"), "utf-8")).toContain("name: qmd");
  271. expect(stdout).toContain(`✓ Claude already sees the skill via ${join(fakeHome, ".claude", "skills")}`);
  272. });
  273. test("refuses to overwrite an existing install without --force", async () => {
  274. const projectDir = join(testDir, "skill-project-force");
  275. await mkdir(projectDir, { recursive: true });
  276. const first = await runQmd(["skill", "install"], { cwd: projectDir });
  277. expect(first.exitCode).toBe(0);
  278. const second = await runQmd(["skill", "install"], { cwd: projectDir });
  279. expect(second.exitCode).toBe(1);
  280. expect(second.stderr).toContain("Skill already exists");
  281. expect(second.stderr).toContain("--force");
  282. });
  283. });
  284. describe("CLI Add Command", () => {
  285. test("adds files from current directory", async () => {
  286. const { stdout, exitCode } = await runQmd(["collection", "add", "."]);
  287. expect(exitCode).toBe(0);
  288. expect(stdout).toContain("Collection:");
  289. expect(stdout).toContain("Indexed:");
  290. });
  291. test("adds files with custom glob pattern", async () => {
  292. const { stdout, stderr, exitCode } = await runQmd(["collection", "add", ".", "--mask", "notes/*.md"]);
  293. if (exitCode !== 0) {
  294. console.error("Command failed:", stderr);
  295. }
  296. expect(exitCode).toBe(0);
  297. expect(stdout).toContain("Collection:");
  298. // Should find meeting.md and ideas.md in notes/
  299. expect(stdout).toContain("notes/*.md");
  300. });
  301. test("can recreate collection with remove and add", async () => {
  302. // First add
  303. await runQmd(["collection", "add", "."]);
  304. // Remove it
  305. await runQmd(["collection", "remove", "fixtures"]);
  306. // Re-add
  307. const { stdout, exitCode } = await runQmd(["collection", "add", "."]);
  308. expect(exitCode).toBe(0);
  309. expect(stdout).toContain("Collection 'fixtures' created successfully");
  310. });
  311. });
  312. describe("CLI Status Command", () => {
  313. beforeEach(async () => {
  314. // Ensure we have indexed files
  315. await runQmd(["collection", "add", "."]);
  316. });
  317. test("shows index status", async () => {
  318. const { stdout, exitCode } = await runQmd(["status"]);
  319. expect(exitCode).toBe(0);
  320. // Should show collection info
  321. expect(stdout).toContain("Collection");
  322. });
  323. });
  324. describe("CLI Search Command", () => {
  325. beforeEach(async () => {
  326. // Ensure we have indexed files
  327. await runQmd(["collection", "add", "."]);
  328. });
  329. test("searches for documents with BM25", async () => {
  330. const { stdout, exitCode } = await runQmd(["search", "meeting"]);
  331. expect(exitCode).toBe(0);
  332. // Should find meeting.md
  333. expect(stdout.toLowerCase()).toContain("meeting");
  334. });
  335. test("searches with limit option", async () => {
  336. const { stdout, exitCode } = await runQmd(["search", "-n", "1", "test"]);
  337. expect(exitCode).toBe(0);
  338. });
  339. test("searches with all results option", async () => {
  340. const { stdout, exitCode } = await runQmd(["search", "--all", "the"]);
  341. expect(exitCode).toBe(0);
  342. });
  343. test("returns no results message for non-matching query", async () => {
  344. const { stdout, exitCode } = await runQmd(["search", "xyznonexistent123"]);
  345. expect(exitCode).toBe(0);
  346. expect(stdout).toContain("No results");
  347. });
  348. test("returns empty JSON array for non-matching query with --json", async () => {
  349. const { stdout, exitCode } = await runQmd(["search", "xyznonexistent123", "--json"]);
  350. expect(exitCode).toBe(0);
  351. expect(JSON.parse(stdout)).toEqual([]);
  352. });
  353. test("returns CSV header only for non-matching query with --csv", async () => {
  354. const { stdout, exitCode } = await runQmd(["search", "xyznonexistent123", "--csv"]);
  355. expect(exitCode).toBe(0);
  356. expect(stdout.trim()).toBe("docid,score,file,title,context,line,snippet");
  357. });
  358. test("returns empty XML container for non-matching query with --xml", async () => {
  359. const { stdout, exitCode } = await runQmd(["search", "xyznonexistent123", "--xml"]);
  360. expect(exitCode).toBe(0);
  361. expect(stdout.trim()).toBe("<results></results>");
  362. });
  363. test("returns empty output for non-matching query with --md", async () => {
  364. const { stdout, exitCode } = await runQmd(["search", "xyznonexistent123", "--md"]);
  365. expect(exitCode).toBe(0);
  366. expect(stdout.trim()).toBe("");
  367. });
  368. test("returns empty output for non-matching query with --files", async () => {
  369. const { stdout, exitCode } = await runQmd(["search", "xyznonexistent123", "--files"]);
  370. expect(exitCode).toBe(0);
  371. expect(stdout.trim()).toBe("");
  372. });
  373. test("returns min-score threshold message for default CLI output", async () => {
  374. const { stdout, exitCode } = await runQmd(["search", "test", "--min-score", "2"]);
  375. expect(exitCode).toBe(0);
  376. expect(stdout).toContain("No results found above minimum score threshold.");
  377. });
  378. test("returns format-safe empty output when --min-score filters all results", async () => {
  379. const json = await runQmd(["search", "test", "--json", "--min-score", "2"]);
  380. expect(json.exitCode).toBe(0);
  381. expect(JSON.parse(json.stdout)).toEqual([]);
  382. const csv = await runQmd(["search", "test", "--csv", "--min-score", "2"]);
  383. expect(csv.exitCode).toBe(0);
  384. expect(csv.stdout.trim()).toBe("docid,score,file,title,context,line,snippet");
  385. const xml = await runQmd(["search", "test", "--xml", "--min-score", "2"]);
  386. expect(xml.exitCode).toBe(0);
  387. expect(xml.stdout.trim()).toBe("<results></results>");
  388. const md = await runQmd(["search", "test", "--md", "--min-score", "2"]);
  389. expect(md.exitCode).toBe(0);
  390. expect(md.stdout.trim()).toBe("");
  391. const files = await runQmd(["search", "test", "--files", "--min-score", "2"]);
  392. expect(files.exitCode).toBe(0);
  393. expect(files.stdout.trim()).toBe("");
  394. });
  395. test("requires query argument", async () => {
  396. const { stdout, stderr, exitCode } = await runQmd(["search"]);
  397. expect(exitCode).toBe(1);
  398. // Error message goes to stderr
  399. expect(stderr).toContain("Usage:");
  400. });
  401. });
  402. describe("CLI Get Command", () => {
  403. beforeEach(async () => {
  404. // Ensure we have indexed files
  405. await runQmd(["collection", "add", "."]);
  406. });
  407. test("retrieves document content by path", async () => {
  408. const { stdout, exitCode } = await runQmd(["get", "README.md"]);
  409. expect(exitCode).toBe(0);
  410. expect(stdout).toContain("Test Project");
  411. });
  412. test("retrieves document from subdirectory", async () => {
  413. const { stdout, exitCode } = await runQmd(["get", "notes/meeting.md"]);
  414. expect(exitCode).toBe(0);
  415. expect(stdout).toContain("Team Meeting");
  416. });
  417. test("handles non-existent file", async () => {
  418. const { stdout, exitCode } = await runQmd(["get", "nonexistent.md"]);
  419. // Should indicate file not found
  420. expect(exitCode).toBe(1);
  421. });
  422. });
  423. describe("CLI Multi-Get Command", () => {
  424. let localDbPath: string;
  425. beforeEach(async () => {
  426. // Use fresh database for each test
  427. localDbPath = getFreshDbPath();
  428. // Ensure we have indexed files
  429. const addResult = await runQmd(["collection", "add", ".", "--name", "fixtures"], { dbPath: localDbPath });
  430. if (addResult.exitCode !== 0) {
  431. throw new Error(`Failed to add collection: ${addResult.stderr}`);
  432. }
  433. });
  434. test("retrieves multiple documents by pattern", async () => {
  435. // Test glob pattern matching
  436. const { stdout, stderr, exitCode } = await runQmd(["multi-get", "notes/*.md"], { dbPath: localDbPath });
  437. expect(exitCode).toBe(0);
  438. // Should contain content from both notes files
  439. expect(stdout).toContain("Meeting");
  440. expect(stdout).toContain("Ideas");
  441. });
  442. test("retrieves documents by comma-separated paths", async () => {
  443. const { stdout, exitCode } = await runQmd([
  444. "multi-get",
  445. "README.md,notes/meeting.md",
  446. ], { dbPath: localDbPath });
  447. expect(exitCode).toBe(0);
  448. expect(stdout).toContain("Test Project");
  449. expect(stdout).toContain("Team Meeting");
  450. });
  451. });
  452. describe("CLI Update Command", () => {
  453. let localDbPath: string;
  454. beforeEach(async () => {
  455. // Use a fresh database for this test suite
  456. localDbPath = getFreshDbPath();
  457. // Ensure we have indexed files
  458. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  459. });
  460. test("updates all collections", async () => {
  461. const { stdout, exitCode } = await runQmd(["update"], { dbPath: localDbPath });
  462. expect(exitCode).toBe(0);
  463. expect(stdout).toContain("Updating");
  464. });
  465. test("deactivates stale docs when collection has zero matching files", async () => {
  466. const { dbPath, configDir } = await createIsolatedTestEnv("update-empty");
  467. const collectionDir = join(testDir, `update-empty-${Date.now()}`);
  468. await mkdir(collectionDir, { recursive: true });
  469. const docPath = join(collectionDir, "only.md");
  470. const token = `stale-proof-${Date.now()}`;
  471. await writeFile(
  472. docPath,
  473. `---
  474. date: 2026-03-06
  475. ---
  476. # Empty Collection Deactivation
  477. ${token}
  478. `
  479. );
  480. const add = await runQmd(
  481. ["collection", "add", collectionDir, "--name", "empty-check"],
  482. { dbPath, configDir }
  483. );
  484. expect(add.exitCode).toBe(0);
  485. const before = await runQmd(["get", "qmd://empty-check/only.md"], { dbPath, configDir });
  486. expect(before.exitCode).toBe(0);
  487. expect(before.stdout).toContain(token);
  488. unlinkSync(docPath);
  489. const update = await runQmd(["update"], { dbPath, configDir });
  490. expect(update.exitCode).toBe(0);
  491. expect(update.stdout).toContain("0 new, 0 updated, 0 unchanged, 1 removed");
  492. const after = await runQmd(["get", "qmd://empty-check/only.md"], { dbPath, configDir });
  493. expect(after.exitCode).toBe(1);
  494. });
  495. });
  496. describe("CLI Add-Context Command", () => {
  497. let localDbPath: string;
  498. let localConfigDir: string;
  499. const collName = "fixtures";
  500. beforeAll(async () => {
  501. const env = await createIsolatedTestEnv("context-cmd");
  502. localDbPath = env.dbPath;
  503. localConfigDir = env.configDir;
  504. // Add collection with known name
  505. const { exitCode, stderr } = await runQmd(
  506. ["collection", "add", fixturesDir, "--name", collName],
  507. { dbPath: localDbPath, configDir: localConfigDir }
  508. );
  509. if (exitCode !== 0) console.error("collection add failed:", stderr);
  510. expect(exitCode).toBe(0);
  511. });
  512. test("adds context to a path", async () => {
  513. // Add context to the collection root using virtual path
  514. const { stdout, exitCode } = await runQmd([
  515. "context",
  516. "add",
  517. `qmd://${collName}/`,
  518. "Personal notes and meeting logs",
  519. ], { dbPath: localDbPath, configDir: localConfigDir });
  520. expect(exitCode).toBe(0);
  521. expect(stdout).toContain("✓ Added context");
  522. });
  523. test("requires path and text arguments", async () => {
  524. const { stderr, exitCode } = await runQmd(["context", "add"], { dbPath: localDbPath, configDir: localConfigDir });
  525. expect(exitCode).toBe(1);
  526. // Error message goes to stderr
  527. expect(stderr).toContain("Usage:");
  528. });
  529. });
  530. describe("CLI Cleanup Command", () => {
  531. beforeEach(async () => {
  532. // Ensure we have indexed files
  533. await runQmd(["collection", "add", "."]);
  534. });
  535. test("cleans up orphaned entries", async () => {
  536. const { stdout, exitCode } = await runQmd(["cleanup"]);
  537. expect(exitCode).toBe(0);
  538. });
  539. });
  540. describe("CLI Error Handling", () => {
  541. test("handles unknown command", async () => {
  542. const { stderr, exitCode } = await runQmd(["unknowncommand"]);
  543. expect(exitCode).toBe(1);
  544. // Should indicate unknown command
  545. expect(stderr).toContain("Unknown command");
  546. });
  547. test("uses INDEX_PATH environment variable", async () => {
  548. // Verify the test DB path is being used by creating a separate index
  549. const customDbPath = join(testDir, "custom.sqlite");
  550. const { exitCode } = await runQmd(["collection", "add", "."], {
  551. env: { INDEX_PATH: customDbPath },
  552. });
  553. expect(exitCode).toBe(0);
  554. // The custom database should exist
  555. expect(existsSync(customDbPath)).toBe(true);
  556. });
  557. });
  558. describe("CLI Output Formats", () => {
  559. beforeEach(async () => {
  560. await runQmd(["collection", "add", "."]);
  561. });
  562. test("search with --json flag outputs JSON", async () => {
  563. const { stdout, exitCode } = await runQmd(["search", "--json", "test"]);
  564. expect(exitCode).toBe(0);
  565. // Should be valid JSON
  566. const parsed = JSON.parse(stdout);
  567. expect(Array.isArray(parsed)).toBe(true);
  568. });
  569. test("search with --files flag outputs file paths", async () => {
  570. const { stdout, exitCode } = await runQmd(["search", "--files", "meeting"]);
  571. expect(exitCode).toBe(0);
  572. expect(stdout).toContain(".md");
  573. });
  574. test("search output includes snippets by default", async () => {
  575. const { stdout, exitCode } = await runQmd(["search", "API"]);
  576. expect(exitCode).toBe(0);
  577. // If results found, should have snippet content
  578. if (!stdout.includes("No results")) {
  579. expect(stdout.toLowerCase()).toContain("api");
  580. }
  581. });
  582. });
  583. describe("CLI Search with Collection Filter", () => {
  584. let localDbPath: string;
  585. beforeEach(async () => {
  586. // Use a fresh database for this test suite
  587. localDbPath = getFreshDbPath();
  588. // Create multiple collections with explicit names
  589. await runQmd(["collection", "add", ".", "--name", "notes", "--mask", "notes/*.md"], { dbPath: localDbPath });
  590. await runQmd(["collection", "add", ".", "--name", "docs", "--mask", "docs/*.md"], { dbPath: localDbPath });
  591. });
  592. test("filters search by collection name", async () => {
  593. const { stdout, stderr, exitCode } = await runQmd([
  594. "search",
  595. "-c",
  596. "notes",
  597. "meeting",
  598. ], { dbPath: localDbPath });
  599. if (exitCode !== 0) {
  600. console.log("Collection filter search failed:");
  601. console.log("stdout:", stdout);
  602. console.log("stderr:", stderr);
  603. }
  604. expect(exitCode).toBe(0);
  605. });
  606. });
  607. describe("CLI Context Management", () => {
  608. let localDbPath: string;
  609. beforeEach(async () => {
  610. // Use a fresh database for this test suite
  611. localDbPath = getFreshDbPath();
  612. // Index some files first
  613. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  614. });
  615. test("add global context with /", async () => {
  616. const { stdout, exitCode } = await runQmd([
  617. "context",
  618. "add",
  619. "/",
  620. "Global system context",
  621. ], { dbPath: localDbPath });
  622. expect(exitCode).toBe(0);
  623. expect(stdout).toContain("✓ Set global context");
  624. expect(stdout).toContain("Global system context");
  625. });
  626. test("list contexts", async () => {
  627. // Add a global context first
  628. await runQmd([
  629. "context",
  630. "add",
  631. "/",
  632. "Test context",
  633. ], { dbPath: localDbPath });
  634. const { stdout, exitCode } = await runQmd([
  635. "context",
  636. "list",
  637. ], { dbPath: localDbPath });
  638. expect(exitCode).toBe(0);
  639. expect(stdout).toContain("Configured Contexts");
  640. expect(stdout).toContain("Test context");
  641. });
  642. test("add context to virtual path", async () => {
  643. // Collection name should be "fixtures" (basename of the fixtures directory)
  644. const { stdout, exitCode } = await runQmd([
  645. "context",
  646. "add",
  647. "qmd://fixtures/notes",
  648. "Context for notes subdirectory",
  649. ], { dbPath: localDbPath });
  650. expect(exitCode).toBe(0);
  651. expect(stdout).toContain("✓ Added context for: qmd://fixtures/notes");
  652. });
  653. test("remove global context", async () => {
  654. // Add a global context first
  655. await runQmd([
  656. "context",
  657. "add",
  658. "/",
  659. "Global context to remove",
  660. ], { dbPath: localDbPath });
  661. const { stdout, exitCode } = await runQmd([
  662. "context",
  663. "rm",
  664. "/",
  665. ], { dbPath: localDbPath });
  666. expect(exitCode).toBe(0);
  667. expect(stdout).toContain("✓ Removed");
  668. });
  669. test("remove virtual path context", async () => {
  670. // Add a context first
  671. await runQmd([
  672. "context",
  673. "add",
  674. "qmd://fixtures/notes",
  675. "Context to remove",
  676. ], { dbPath: localDbPath });
  677. const { stdout, exitCode } = await runQmd([
  678. "context",
  679. "rm",
  680. "qmd://fixtures/notes",
  681. ], { dbPath: localDbPath });
  682. expect(exitCode).toBe(0);
  683. expect(stdout).toContain("✓ Removed context for: qmd://fixtures/notes");
  684. });
  685. test("fails to remove non-existent context", async () => {
  686. const { stdout, stderr, exitCode } = await runQmd([
  687. "context",
  688. "rm",
  689. "qmd://nonexistent/path",
  690. ], { dbPath: localDbPath });
  691. expect(exitCode).toBe(1);
  692. expect(stderr || stdout).toContain("not found");
  693. });
  694. });
  695. describe("CLI ls Command", () => {
  696. let localDbPath: string;
  697. beforeEach(async () => {
  698. // Use a fresh database for this test suite
  699. localDbPath = getFreshDbPath();
  700. // Index some files first
  701. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  702. });
  703. test("lists all collections", async () => {
  704. const { stdout, exitCode } = await runQmd(["ls"], { dbPath: localDbPath });
  705. expect(exitCode).toBe(0);
  706. expect(stdout).toContain("Collections:");
  707. expect(stdout).toContain("qmd://fixtures/");
  708. });
  709. test("lists files in a collection", async () => {
  710. const { stdout, exitCode } = await runQmd(["ls", "fixtures"], { dbPath: localDbPath });
  711. expect(exitCode).toBe(0);
  712. // handelize converts to lowercase
  713. expect(stdout).toContain("qmd://fixtures/readme.md");
  714. expect(stdout).toContain("qmd://fixtures/notes/meeting.md");
  715. });
  716. test("lists files with path prefix", async () => {
  717. const { stdout, exitCode } = await runQmd(["ls", "fixtures/notes"], { dbPath: localDbPath });
  718. expect(exitCode).toBe(0);
  719. expect(stdout).toContain("qmd://fixtures/notes/meeting.md");
  720. expect(stdout).toContain("qmd://fixtures/notes/ideas.md");
  721. // Should not include files outside the prefix (handelize converts to lowercase)
  722. expect(stdout).not.toContain("qmd://fixtures/readme.md");
  723. });
  724. test("lists files with virtual path", async () => {
  725. const { stdout, exitCode } = await runQmd(["ls", "qmd://fixtures/docs"], { dbPath: localDbPath });
  726. expect(exitCode).toBe(0);
  727. expect(stdout).toContain("qmd://fixtures/docs/api.md");
  728. });
  729. test("handles non-existent collection", async () => {
  730. const { stderr, exitCode } = await runQmd(["ls", "nonexistent"], { dbPath: localDbPath });
  731. expect(exitCode).toBe(1);
  732. expect(stderr).toContain("Collection not found");
  733. });
  734. });
  735. describe("CLI Collection Commands", () => {
  736. let localDbPath: string;
  737. beforeEach(async () => {
  738. // Use a fresh database for this test suite
  739. localDbPath = getFreshDbPath();
  740. // Index some files first to create a collection
  741. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  742. });
  743. test("lists collections", async () => {
  744. const { stdout, exitCode } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  745. expect(exitCode).toBe(0);
  746. expect(stdout).toContain("Collections");
  747. expect(stdout).toContain("fixtures");
  748. expect(stdout).toContain("qmd://fixtures/");
  749. expect(stdout).toContain("Pattern:");
  750. expect(stdout).toContain("Files:");
  751. });
  752. test("removes a collection", async () => {
  753. // First verify the collection exists
  754. const { stdout: listBefore } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  755. expect(listBefore).toContain("fixtures");
  756. // Remove it
  757. const { stdout, exitCode } = await runQmd(["collection", "remove", "fixtures"], { dbPath: localDbPath });
  758. expect(exitCode).toBe(0);
  759. expect(stdout).toContain("✓ Removed collection 'fixtures'");
  760. expect(stdout).toContain("Deleted");
  761. // Verify it's gone
  762. const { stdout: listAfter } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  763. expect(listAfter).not.toContain("fixtures");
  764. });
  765. test("handles removing non-existent collection", async () => {
  766. const { stderr, exitCode } = await runQmd(["collection", "remove", "nonexistent"], { dbPath: localDbPath });
  767. expect(exitCode).toBe(1);
  768. expect(stderr).toContain("Collection not found");
  769. });
  770. test("handles missing remove argument", async () => {
  771. const { stderr, exitCode } = await runQmd(["collection", "remove"], { dbPath: localDbPath });
  772. expect(exitCode).toBe(1);
  773. expect(stderr).toContain("Usage:");
  774. });
  775. test("handles unknown subcommand", async () => {
  776. const { stderr, exitCode } = await runQmd(["collection", "invalid"], { dbPath: localDbPath });
  777. expect(exitCode).toBe(1);
  778. expect(stderr).toContain("Unknown subcommand");
  779. });
  780. test("renames a collection", async () => {
  781. // First verify the collection exists
  782. const { stdout: listBefore } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  783. expect(listBefore).toContain("qmd://fixtures/");
  784. // Rename it
  785. const { stdout, exitCode } = await runQmd(["collection", "rename", "fixtures", "my-fixtures"], { dbPath: localDbPath });
  786. expect(exitCode).toBe(0);
  787. expect(stdout).toContain("✓ Renamed collection 'fixtures' to 'my-fixtures'");
  788. expect(stdout).toContain("qmd://fixtures/");
  789. expect(stdout).toContain("qmd://my-fixtures/");
  790. // Verify the new name exists and old name is gone
  791. const { stdout: listAfter } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  792. expect(listAfter).toContain("qmd://my-fixtures/");
  793. expect(listAfter).not.toContain("qmd://fixtures/"); // Old collection should not appear
  794. });
  795. test("handles renaming non-existent collection", async () => {
  796. const { stderr, exitCode } = await runQmd(["collection", "rename", "nonexistent", "newname"], { dbPath: localDbPath });
  797. expect(exitCode).toBe(1);
  798. expect(stderr).toContain("Collection not found");
  799. });
  800. test("handles renaming to existing collection name", async () => {
  801. // Create a second collection in a temp directory
  802. const tempDir = await mkdtemp(join(tmpdir(), "qmd-second-"));
  803. await writeFile(join(tempDir, "test.md"), "# Test");
  804. const addResult = await runQmd(["collection", "add", tempDir, "--name", "second"], { dbPath: localDbPath });
  805. if (addResult.exitCode !== 0) {
  806. console.error("Failed to add second collection:", addResult.stderr);
  807. }
  808. expect(addResult.exitCode).toBe(0);
  809. // Verify both collections exist
  810. const { stdout: listBoth } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  811. expect(listBoth).toContain("qmd://fixtures/");
  812. expect(listBoth).toContain("qmd://second/");
  813. // Try to rename fixtures to second (which already exists)
  814. const { stderr, exitCode } = await runQmd(["collection", "rename", "fixtures", "second"], { dbPath: localDbPath });
  815. expect(exitCode).toBe(1);
  816. expect(stderr).toContain("Collection name already exists");
  817. });
  818. test("handles missing rename arguments", async () => {
  819. const { stderr: stderr1, exitCode: exitCode1 } = await runQmd(["collection", "rename"], { dbPath: localDbPath });
  820. expect(exitCode1).toBe(1);
  821. expect(stderr1).toContain("Usage:");
  822. const { stderr: stderr2, exitCode: exitCode2 } = await runQmd(["collection", "rename", "fixtures"], { dbPath: localDbPath });
  823. expect(exitCode2).toBe(1);
  824. expect(stderr2).toContain("Usage:");
  825. });
  826. });
  827. // =============================================================================
  828. // Collection Ignore Patterns
  829. // =============================================================================
  830. describe("collection ignore patterns", () => {
  831. let localDbPath: string;
  832. let localConfigDir: string;
  833. let ignoreTestDir: string;
  834. beforeAll(async () => {
  835. const env = await createIsolatedTestEnv("ignore-patterns");
  836. localDbPath = env.dbPath;
  837. localConfigDir = env.configDir;
  838. // Create directory structure with subdirectories to ignore
  839. ignoreTestDir = join(testDir, "ignore-fixtures");
  840. await mkdir(join(ignoreTestDir, "notes"), { recursive: true });
  841. await mkdir(join(ignoreTestDir, "sessions"), { recursive: true });
  842. await mkdir(join(ignoreTestDir, "sessions", "2026-03"), { recursive: true });
  843. await mkdir(join(ignoreTestDir, "archive"), { recursive: true });
  844. // Files that should be indexed
  845. await writeFile(join(ignoreTestDir, "readme.md"), "# Main readme\nThis should be indexed.");
  846. await writeFile(join(ignoreTestDir, "notes", "note1.md"), "# Note 1\nThis is a personal note.");
  847. // Files that should be ignored
  848. await writeFile(join(ignoreTestDir, "sessions", "session1.md"), "# Session 1\nThis session should be ignored.");
  849. await writeFile(join(ignoreTestDir, "sessions", "2026-03", "session2.md"), "# Session 2\nNested session should also be ignored.");
  850. await writeFile(join(ignoreTestDir, "archive", "old.md"), "# Old stuff\nThis archive file should be ignored.");
  851. });
  852. test("ignore patterns exclude matching files from indexing", async () => {
  853. // Write YAML config with ignore patterns
  854. await writeFile(
  855. join(localConfigDir, "index.yml"),
  856. `collections:
  857. ignoretst:
  858. path: ${ignoreTestDir}
  859. pattern: "**/*.md"
  860. ignore:
  861. - "sessions/**"
  862. - "archive/**"
  863. `
  864. );
  865. const { stdout, exitCode } = await runQmd(["update"], {
  866. cwd: ignoreTestDir,
  867. dbPath: localDbPath,
  868. configDir: localConfigDir,
  869. });
  870. expect(exitCode).toBe(0);
  871. // Should index 2 files (readme.md + notes/note1.md), not 5
  872. expect(stdout).toContain("2 new");
  873. });
  874. test("ignored files are not searchable", async () => {
  875. const { stdout, exitCode } = await runQmd(["search", "session", "-n", "10"], {
  876. cwd: ignoreTestDir,
  877. dbPath: localDbPath,
  878. configDir: localConfigDir,
  879. });
  880. // Should find no results since sessions/ was ignored
  881. if (exitCode === 0) {
  882. expect(stdout).not.toContain("session1");
  883. expect(stdout).not.toContain("session2");
  884. }
  885. });
  886. test("non-ignored files are searchable", async () => {
  887. const { stdout, exitCode } = await runQmd(["search", "personal note", "-n", "10"], {
  888. cwd: ignoreTestDir,
  889. dbPath: localDbPath,
  890. configDir: localConfigDir,
  891. });
  892. expect(exitCode).toBe(0);
  893. expect(stdout).toContain("note1");
  894. });
  895. test("status shows ignore patterns", async () => {
  896. const { stdout, exitCode } = await runQmd(["collection", "list"], {
  897. cwd: ignoreTestDir,
  898. dbPath: localDbPath,
  899. configDir: localConfigDir,
  900. });
  901. expect(exitCode).toBe(0);
  902. expect(stdout).toContain("Ignore:");
  903. expect(stdout).toContain("sessions/**");
  904. expect(stdout).toContain("archive/**");
  905. });
  906. test("collection without ignore indexes all files", async () => {
  907. // Create a second collection without ignore
  908. const env2 = await createIsolatedTestEnv("no-ignore");
  909. await writeFile(
  910. join(env2.configDir, "index.yml"),
  911. `collections:
  912. allfiles:
  913. path: ${ignoreTestDir}
  914. pattern: "**/*.md"
  915. `
  916. );
  917. const { stdout, exitCode } = await runQmd(["update"], {
  918. cwd: ignoreTestDir,
  919. dbPath: env2.dbPath,
  920. configDir: env2.configDir,
  921. });
  922. expect(exitCode).toBe(0);
  923. // Should index all 5 files
  924. expect(stdout).toContain("5 new");
  925. });
  926. });
  927. // =============================================================================
  928. // Output Format Tests - qmd:// URIs, context, and docid
  929. // =============================================================================
  930. describe("search output formats", () => {
  931. let localDbPath: string;
  932. let localConfigDir: string;
  933. const collName = "fixtures";
  934. beforeAll(async () => {
  935. const env = await createIsolatedTestEnv("output-format");
  936. localDbPath = env.dbPath;
  937. localConfigDir = env.configDir;
  938. // Add collection
  939. const { exitCode, stderr } = await runQmd(
  940. ["collection", "add", fixturesDir, "--name", collName],
  941. { dbPath: localDbPath, configDir: localConfigDir }
  942. );
  943. if (exitCode !== 0) console.error("collection add failed:", stderr);
  944. expect(exitCode).toBe(0);
  945. // Add context
  946. await runQmd(["context", "add", `qmd://${collName}/`, "Test fixtures for QMD"], { dbPath: localDbPath, configDir: localConfigDir });
  947. });
  948. test("search --json includes qmd:// path, docid, and context", async () => {
  949. const { stdout, exitCode } = await runQmd(["search", "test", "--json", "-n", "1"], { dbPath: localDbPath, configDir: localConfigDir });
  950. expect(exitCode).toBe(0);
  951. const results = JSON.parse(stdout);
  952. expect(results.length).toBeGreaterThan(0);
  953. const result = results[0];
  954. expect(result.file).toMatch(new RegExp(`^qmd://${collName}/`));
  955. expect(result.docid).toMatch(/^#[a-f0-9]{6}$/);
  956. expect(result.context).toBe("Test fixtures for QMD");
  957. // Ensure no full filesystem paths
  958. expect(result.file).not.toMatch(/^\/Users\//);
  959. expect(result.file).not.toMatch(/^\/home\//);
  960. });
  961. test("search --files includes qmd:// path, docid, and context", async () => {
  962. const { stdout, exitCode } = await runQmd(["search", "test", "--files", "-n", "1"], { dbPath: localDbPath, configDir: localConfigDir });
  963. expect(exitCode).toBe(0);
  964. // Format: #docid,score,qmd://collection/path,"context"
  965. expect(stdout).toMatch(new RegExp(`^#[a-f0-9]{6},[\\d.]+,qmd://${collName}/`, "m"));
  966. expect(stdout).toContain("Test fixtures for QMD");
  967. // Ensure no full filesystem paths
  968. expect(stdout).not.toMatch(/\/Users\//);
  969. expect(stdout).not.toMatch(/\/home\//);
  970. });
  971. test("search --csv includes qmd:// path, docid, and context", async () => {
  972. const { stdout, exitCode } = await runQmd(["search", "test", "--csv", "-n", "1"], { dbPath: localDbPath, configDir: localConfigDir });
  973. expect(exitCode).toBe(0);
  974. // Header should include context
  975. expect(stdout).toMatch(/^docid,score,file,title,context,line,snippet$/m);
  976. // Data rows should have qmd:// paths and context
  977. expect(stdout).toMatch(new RegExp(`#[a-f0-9]{6},[\\d.]+,qmd://${collName}/`));
  978. expect(stdout).toContain("Test fixtures for QMD");
  979. // Ensure no full filesystem paths
  980. expect(stdout).not.toMatch(/\/Users\//);
  981. expect(stdout).not.toMatch(/\/home\//);
  982. });
  983. test("search --md includes docid and context", async () => {
  984. const { stdout, exitCode } = await runQmd(["search", "test", "--md", "-n", "1"], { dbPath: localDbPath, configDir: localConfigDir });
  985. expect(exitCode).toBe(0);
  986. expect(stdout).toMatch(/\*\*docid:\*\* `#[a-f0-9]{6}`/);
  987. expect(stdout).toContain("**context:** Test fixtures for QMD");
  988. });
  989. test("search --xml includes qmd:// path, docid, and context", async () => {
  990. const { stdout, exitCode } = await runQmd(["search", "test", "--xml", "-n", "1"], { dbPath: localDbPath, configDir: localConfigDir });
  991. expect(exitCode).toBe(0);
  992. expect(stdout).toMatch(new RegExp(`<file docid="#[a-f0-9]{6}" name="qmd://${collName}/`));
  993. expect(stdout).toContain('context="Test fixtures for QMD"');
  994. // Ensure no full filesystem paths
  995. expect(stdout).not.toMatch(/\/Users\//);
  996. expect(stdout).not.toMatch(/\/home\//);
  997. });
  998. test("search default CLI format includes qmd:// path, docid, and context", async () => {
  999. const { stdout, exitCode } = await runQmd(["search", "test", "-n", "1"], { dbPath: localDbPath, configDir: localConfigDir });
  1000. expect(exitCode).toBe(0);
  1001. // First line should have qmd:// path and docid
  1002. expect(stdout).toMatch(new RegExp(`^qmd://${collName}/.*#[a-f0-9]{6}`, "m"));
  1003. expect(stdout).toContain("Context: Test fixtures for QMD");
  1004. // Ensure no full filesystem paths
  1005. expect(stdout).not.toMatch(/\/Users\//);
  1006. expect(stdout).not.toMatch(/\/home\//);
  1007. });
  1008. });
  1009. // =============================================================================
  1010. // Get Command Path Normalization Tests
  1011. // =============================================================================
  1012. describe("get command path normalization", () => {
  1013. let localDbPath: string;
  1014. let localConfigDir: string;
  1015. const collName = "fixtures";
  1016. beforeAll(async () => {
  1017. const env = await createIsolatedTestEnv("get-paths");
  1018. localDbPath = env.dbPath;
  1019. localConfigDir = env.configDir;
  1020. const { exitCode, stderr } = await runQmd(
  1021. ["collection", "add", fixturesDir, "--name", collName],
  1022. { dbPath: localDbPath, configDir: localConfigDir }
  1023. );
  1024. if (exitCode !== 0) console.error("collection add failed:", stderr);
  1025. expect(exitCode).toBe(0);
  1026. });
  1027. test("get with qmd://collection/path format", async () => {
  1028. const { stdout, exitCode } = await runQmd(["get", `qmd://${collName}/test1.md`, "-l", "3"], { dbPath: localDbPath, configDir: localConfigDir });
  1029. expect(exitCode).toBe(0);
  1030. expect(stdout).toContain("Test Document 1");
  1031. });
  1032. test("get with collection/path format (no scheme)", async () => {
  1033. const { stdout, exitCode } = await runQmd(["get", `${collName}/test1.md`, "-l", "3"], { dbPath: localDbPath, configDir: localConfigDir });
  1034. expect(exitCode).toBe(0);
  1035. expect(stdout).toContain("Test Document 1");
  1036. });
  1037. test("get with //collection/path format", async () => {
  1038. const { stdout, exitCode } = await runQmd(["get", `//${collName}/test1.md`, "-l", "3"], { dbPath: localDbPath, configDir: localConfigDir });
  1039. expect(exitCode).toBe(0);
  1040. expect(stdout).toContain("Test Document 1");
  1041. });
  1042. test("get with qmd:////collection/path format (extra slashes)", async () => {
  1043. const { stdout, exitCode } = await runQmd(["get", `qmd:////${collName}/test1.md`, "-l", "3"], { dbPath: localDbPath, configDir: localConfigDir });
  1044. expect(exitCode).toBe(0);
  1045. expect(stdout).toContain("Test Document 1");
  1046. });
  1047. test("get with path:line format", async () => {
  1048. const { stdout, exitCode } = await runQmd(["get", `${collName}/test1.md:3`, "-l", "2"], { dbPath: localDbPath, configDir: localConfigDir });
  1049. expect(exitCode).toBe(0);
  1050. // Should start from line 3, not line 1
  1051. expect(stdout).not.toMatch(/^# Test Document 1$/m);
  1052. });
  1053. test("get with qmd://path:line format", async () => {
  1054. const { stdout, exitCode } = await runQmd(["get", `qmd://${collName}/test1.md:3`, "-l", "2"], { dbPath: localDbPath, configDir: localConfigDir });
  1055. expect(exitCode).toBe(0);
  1056. // Should start from line 3, not line 1
  1057. expect(stdout).not.toMatch(/^# Test Document 1$/m);
  1058. });
  1059. });
  1060. // =============================================================================
  1061. // Status and Collection List - No Full Paths
  1062. // =============================================================================
  1063. describe("status and collection list hide filesystem paths", () => {
  1064. let localDbPath: string;
  1065. let localConfigDir: string;
  1066. const collName = "fixtures";
  1067. beforeAll(async () => {
  1068. const env = await createIsolatedTestEnv("status-paths");
  1069. localDbPath = env.dbPath;
  1070. localConfigDir = env.configDir;
  1071. const { exitCode, stderr } = await runQmd(
  1072. ["collection", "add", fixturesDir, "--name", collName],
  1073. { dbPath: localDbPath, configDir: localConfigDir }
  1074. );
  1075. if (exitCode !== 0) console.error("collection add failed:", stderr);
  1076. expect(exitCode).toBe(0);
  1077. });
  1078. test("status does not show full filesystem paths", async () => {
  1079. const { stdout, exitCode } = await runQmd(["status"], { dbPath: localDbPath, configDir: localConfigDir });
  1080. expect(exitCode).toBe(0);
  1081. // Should show qmd:// URIs
  1082. expect(stdout).toContain(`qmd://${collName}/`);
  1083. // Should NOT show full filesystem paths (except for the index location which is ok)
  1084. const lines = stdout.split('\n').filter(l => !l.includes('Index:'));
  1085. const pathLines = lines.filter(l => l.includes('/Users/') || l.includes('/home/') || l.includes('/tmp/'));
  1086. expect(pathLines.length).toBe(0);
  1087. });
  1088. test("collection list does not show full filesystem paths", async () => {
  1089. const { stdout, exitCode } = await runQmd(["collection", "list"], { dbPath: localDbPath, configDir: localConfigDir });
  1090. expect(exitCode).toBe(0);
  1091. // Should show qmd:// URIs
  1092. expect(stdout).toContain(`qmd://${collName}/`);
  1093. // Should NOT show Path: lines with filesystem paths
  1094. expect(stdout).not.toMatch(/Path:\s+\//);
  1095. });
  1096. });
  1097. // =============================================================================
  1098. // MCP HTTP Daemon Lifecycle
  1099. // =============================================================================
  1100. describe("mcp http daemon", () => {
  1101. let daemonTestDir: string;
  1102. let daemonCacheDir: string; // XDG_CACHE_HOME value (the qmd/ subdir is created automatically)
  1103. let daemonDbPath: string;
  1104. let daemonConfigDir: string;
  1105. // Track spawned PIDs for cleanup
  1106. const spawnedPids: number[] = [];
  1107. /** Get path to PID file inside the test cache dir */
  1108. function pidPath(): string {
  1109. return join(daemonCacheDir, "qmd", "mcp.pid");
  1110. }
  1111. /** Run qmd with test-isolated env (cache, db, config) */
  1112. async function runDaemonQmd(
  1113. args: string[],
  1114. ): Promise<{ stdout: string; stderr: string; exitCode: number }> {
  1115. return runQmd(args, {
  1116. dbPath: daemonDbPath,
  1117. configDir: daemonConfigDir,
  1118. env: { XDG_CACHE_HOME: daemonCacheDir },
  1119. });
  1120. }
  1121. /** Spawn a foreground HTTP server (non-blocking) and return the process */
  1122. function spawnHttpServer(port: number): import("child_process").ChildProcess {
  1123. const proc = spawn(tsxBin, [qmdScript, "mcp", "--http", "--port", String(port)], {
  1124. cwd: fixturesDir,
  1125. env: {
  1126. ...process.env,
  1127. INDEX_PATH: daemonDbPath,
  1128. QMD_CONFIG_DIR: daemonConfigDir,
  1129. },
  1130. stdio: ["ignore", "pipe", "pipe"],
  1131. });
  1132. if (proc.pid) spawnedPids.push(proc.pid);
  1133. return proc;
  1134. }
  1135. /** Wait for HTTP server to become ready */
  1136. async function waitForServer(port: number, timeoutMs = 5000): Promise<boolean> {
  1137. const deadline = Date.now() + timeoutMs;
  1138. while (Date.now() < deadline) {
  1139. try {
  1140. const res = await fetch(`http://localhost:${port}/health`);
  1141. if (res.ok) return true;
  1142. } catch { /* not ready yet */ }
  1143. await sleep(200);
  1144. }
  1145. return false;
  1146. }
  1147. /** Pick a random high port unlikely to conflict */
  1148. function randomPort(): number {
  1149. return 10000 + Math.floor(Math.random() * 50000);
  1150. }
  1151. beforeAll(async () => {
  1152. daemonTestDir = await mkdtemp(join(tmpdir(), "qmd-daemon-test-"));
  1153. daemonCacheDir = join(daemonTestDir, "cache");
  1154. daemonDbPath = join(daemonTestDir, "test.sqlite");
  1155. daemonConfigDir = join(daemonTestDir, "config");
  1156. await mkdir(join(daemonCacheDir, "qmd"), { recursive: true });
  1157. await mkdir(daemonConfigDir, { recursive: true });
  1158. await writeFile(join(daemonConfigDir, "index.yml"), "collections: {}\n");
  1159. });
  1160. afterAll(async () => {
  1161. // Kill any leftover spawned processes
  1162. for (const pid of spawnedPids) {
  1163. try { process.kill(pid, "SIGTERM"); } catch { /* already dead */ }
  1164. }
  1165. // Also clean up via PID file if present
  1166. try {
  1167. const pf = pidPath();
  1168. if (existsSync(pf)) {
  1169. const pid = parseInt(readFileSync(pf, "utf-8").trim());
  1170. try { process.kill(pid, "SIGTERM"); } catch {}
  1171. unlinkSync(pf);
  1172. }
  1173. } catch {}
  1174. await rm(daemonTestDir, { recursive: true, force: true });
  1175. });
  1176. // -------------------------------------------------------------------------
  1177. // Foreground HTTP
  1178. // -------------------------------------------------------------------------
  1179. test("foreground HTTP server starts and responds to health check", async () => {
  1180. const port = randomPort();
  1181. const proc = spawnHttpServer(port);
  1182. try {
  1183. const ready = await waitForServer(port);
  1184. expect(ready).toBe(true);
  1185. const res = await fetch(`http://localhost:${port}/health`);
  1186. expect(res.status).toBe(200);
  1187. const body = await res.json();
  1188. expect(body.status).toBe("ok");
  1189. } finally {
  1190. proc.kill("SIGTERM");
  1191. await new Promise(r => proc.on("close", r));
  1192. }
  1193. });
  1194. // -------------------------------------------------------------------------
  1195. // Daemon lifecycle
  1196. // -------------------------------------------------------------------------
  1197. test("--daemon writes PID file and starts server", async () => {
  1198. const port = randomPort();
  1199. const { stdout, exitCode } = await runDaemonQmd([
  1200. "mcp", "--http", "--daemon", "--port", String(port),
  1201. ]);
  1202. expect(exitCode).toBe(0);
  1203. expect(stdout).toContain(`http://localhost:${port}/mcp`);
  1204. // PID file should exist
  1205. expect(existsSync(pidPath())).toBe(true);
  1206. const pid = parseInt(readFileSync(pidPath(), "utf-8").trim());
  1207. spawnedPids.push(pid);
  1208. // Server should be reachable
  1209. const ready = await waitForServer(port);
  1210. expect(ready).toBe(true);
  1211. // Clean up
  1212. process.kill(pid, "SIGTERM");
  1213. await sleep(500);
  1214. try { unlinkSync(pidPath()); } catch {}
  1215. });
  1216. test("stop kills daemon and removes PID file", async () => {
  1217. const port = randomPort();
  1218. // Start daemon
  1219. const { exitCode: startCode } = await runDaemonQmd([
  1220. "mcp", "--http", "--daemon", "--port", String(port),
  1221. ]);
  1222. expect(startCode).toBe(0);
  1223. const pid = parseInt(readFileSync(pidPath(), "utf-8").trim());
  1224. spawnedPids.push(pid);
  1225. await waitForServer(port);
  1226. // Stop it
  1227. const { stdout: stopOut, exitCode: stopCode } = await runDaemonQmd(["mcp", "stop"]);
  1228. expect(stopCode).toBe(0);
  1229. expect(stopOut).toContain("Stopped");
  1230. // PID file should be gone
  1231. expect(existsSync(pidPath())).toBe(false);
  1232. // Process should be dead
  1233. await sleep(500);
  1234. expect(() => process.kill(pid, 0)).toThrow();
  1235. });
  1236. test("stop handles dead PID gracefully (cleans stale file)", async () => {
  1237. // Write a PID file pointing to a dead process
  1238. writeFileSync(pidPath(), "999999999");
  1239. const { stdout, exitCode } = await runDaemonQmd(["mcp", "stop"]);
  1240. expect(exitCode).toBe(0);
  1241. expect(stdout).toContain("stale");
  1242. // PID file should be cleaned up
  1243. expect(existsSync(pidPath())).toBe(false);
  1244. });
  1245. test("--daemon rejects if already running", async () => {
  1246. const port = randomPort();
  1247. // Start first daemon
  1248. const { exitCode: firstCode } = await runDaemonQmd([
  1249. "mcp", "--http", "--daemon", "--port", String(port),
  1250. ]);
  1251. expect(firstCode).toBe(0);
  1252. const pid = parseInt(readFileSync(pidPath(), "utf-8").trim());
  1253. spawnedPids.push(pid);
  1254. await waitForServer(port);
  1255. // Try to start second daemon — should fail
  1256. const { stderr, exitCode } = await runDaemonQmd([
  1257. "mcp", "--http", "--daemon", "--port", String(port + 1),
  1258. ]);
  1259. expect(exitCode).toBe(1);
  1260. expect(stderr).toContain("Already running");
  1261. // Clean up first daemon
  1262. process.kill(pid, "SIGTERM");
  1263. await sleep(500);
  1264. try { unlinkSync(pidPath()); } catch {}
  1265. });
  1266. test("--daemon cleans stale PID file and starts fresh", async () => {
  1267. // Write a stale PID file
  1268. writeFileSync(pidPath(), "999999999");
  1269. const port = randomPort();
  1270. const { exitCode, stdout } = await runDaemonQmd([
  1271. "mcp", "--http", "--daemon", "--port", String(port),
  1272. ]);
  1273. expect(exitCode).toBe(0);
  1274. expect(stdout).toContain(`http://localhost:${port}/mcp`);
  1275. const pid = parseInt(readFileSync(pidPath(), "utf-8").trim());
  1276. spawnedPids.push(pid);
  1277. expect(pid).not.toBe(999999999);
  1278. // Clean up
  1279. const ready = await waitForServer(port);
  1280. expect(ready).toBe(true);
  1281. process.kill(pid, "SIGTERM");
  1282. await sleep(500);
  1283. try { unlinkSync(pidPath()); } catch {}
  1284. });
  1285. });