cli.test.ts 52 KB

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