cli.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 "bun:test";
  8. import { mkdtemp, rm, writeFile, mkdir } from "fs/promises";
  9. import { tmpdir } from "os";
  10. import { join } from "path";
  11. // Test fixtures directory and database path
  12. let testDir: string;
  13. let testDbPath: string;
  14. let testConfigDir: string;
  15. let fixturesDir: string;
  16. let testCounter = 0; // Unique counter for each test run
  17. // Get the directory where this test file lives (same as qmd.ts)
  18. const qmdDir = import.meta.dir;
  19. const qmdScript = join(qmdDir, "qmd.ts");
  20. // Helper to run qmd command with test database
  21. async function runQmd(
  22. args: string[],
  23. options: { cwd?: string; env?: Record<string, string>; dbPath?: string } = {}
  24. ): Promise<{ stdout: string; stderr: string; exitCode: number }> {
  25. const workingDir = options.cwd || fixturesDir;
  26. const dbPath = options.dbPath || testDbPath;
  27. const proc = Bun.spawn(["bun", qmdScript, ...args], {
  28. cwd: workingDir,
  29. env: {
  30. ...process.env,
  31. INDEX_PATH: dbPath,
  32. QMD_CONFIG_DIR: testConfigDir, // Use test config directory
  33. PWD: workingDir, // Must explicitly set PWD since getPwd() checks this
  34. ...options.env,
  35. },
  36. stdout: "pipe",
  37. stderr: "pipe",
  38. });
  39. const stdout = await new Response(proc.stdout).text();
  40. const stderr = await new Response(proc.stderr).text();
  41. const exitCode = await proc.exited;
  42. return { stdout, stderr, exitCode };
  43. }
  44. // Get a fresh database path for isolated tests
  45. function getFreshDbPath(): string {
  46. testCounter++;
  47. return join(testDir, `test-${testCounter}.sqlite`);
  48. }
  49. // Setup test fixtures
  50. beforeAll(async () => {
  51. // Create temp directory structure
  52. testDir = await mkdtemp(join(tmpdir(), "qmd-test-"));
  53. testDbPath = join(testDir, "test.sqlite");
  54. testConfigDir = join(testDir, "config");
  55. fixturesDir = join(testDir, "fixtures");
  56. await mkdir(testConfigDir, { recursive: true });
  57. await mkdir(fixturesDir, { recursive: true });
  58. await mkdir(join(fixturesDir, "notes"), { recursive: true });
  59. await mkdir(join(fixturesDir, "docs"), { recursive: true });
  60. // Create empty YAML config for tests
  61. await writeFile(
  62. join(testConfigDir, "index.yml"),
  63. "collections: {}\n"
  64. );
  65. // Create test markdown files
  66. await writeFile(
  67. join(fixturesDir, "README.md"),
  68. `# Test Project
  69. This is a test project for QMD CLI testing.
  70. ## Features
  71. - Full-text search with BM25
  72. - Vector similarity search
  73. - Hybrid search with reranking
  74. `
  75. );
  76. await writeFile(
  77. join(fixturesDir, "notes", "meeting.md"),
  78. `# Team Meeting Notes
  79. Date: 2024-01-15
  80. ## Attendees
  81. - Alice
  82. - Bob
  83. - Charlie
  84. ## Discussion Topics
  85. - Project timeline review
  86. - Resource allocation
  87. - Technical debt prioritization
  88. ## Action Items
  89. 1. Alice to update documentation
  90. 2. Bob to fix authentication bug
  91. 3. Charlie to review pull requests
  92. `
  93. );
  94. await writeFile(
  95. join(fixturesDir, "notes", "ideas.md"),
  96. `# Product Ideas
  97. ## Feature Requests
  98. - Dark mode support
  99. - Keyboard shortcuts
  100. - Export to PDF
  101. ## Technical Improvements
  102. - Improve search performance
  103. - Add caching layer
  104. - Optimize database queries
  105. `
  106. );
  107. await writeFile(
  108. join(fixturesDir, "docs", "api.md"),
  109. `# API Documentation
  110. ## Endpoints
  111. ### GET /search
  112. Search for documents.
  113. Parameters:
  114. - q: Search query (required)
  115. - limit: Max results (default: 10)
  116. ### GET /document/:id
  117. Retrieve a specific document.
  118. ### POST /index
  119. Index new documents.
  120. `
  121. );
  122. });
  123. // Cleanup after all tests
  124. afterAll(async () => {
  125. if (testDir) {
  126. await rm(testDir, { recursive: true, force: true });
  127. }
  128. });
  129. // Reset YAML config before each test to ensure isolation
  130. beforeEach(async () => {
  131. // Reset to empty collections config
  132. await writeFile(
  133. join(testConfigDir, "index.yml"),
  134. "collections: {}\n"
  135. );
  136. });
  137. describe("CLI Help", () => {
  138. test("shows help with --help flag", async () => {
  139. const { stdout, exitCode } = await runQmd(["--help"]);
  140. expect(exitCode).toBe(0);
  141. expect(stdout).toContain("Usage:");
  142. expect(stdout).toContain("qmd collection add");
  143. expect(stdout).toContain("qmd search");
  144. });
  145. test("shows help with no arguments", async () => {
  146. const { stdout, exitCode } = await runQmd([]);
  147. expect(exitCode).toBe(1);
  148. expect(stdout).toContain("Usage:");
  149. });
  150. });
  151. describe("CLI Add Command", () => {
  152. test("adds files from current directory", async () => {
  153. const { stdout, exitCode } = await runQmd(["collection", "add", "."]);
  154. expect(exitCode).toBe(0);
  155. expect(stdout).toContain("Collection:");
  156. expect(stdout).toContain("Indexed:");
  157. });
  158. test("adds files with custom glob pattern", async () => {
  159. const { stdout, stderr, exitCode } = await runQmd(["collection", "add", ".", "--mask", "notes/*.md"]);
  160. if (exitCode !== 0) {
  161. console.error("Command failed:", stderr);
  162. }
  163. expect(exitCode).toBe(0);
  164. expect(stdout).toContain("Collection:");
  165. // Should find meeting.md and ideas.md in notes/
  166. expect(stdout).toContain("notes/*.md");
  167. });
  168. test("can recreate collection with remove and add", async () => {
  169. // First add
  170. await runQmd(["collection", "add", "."]);
  171. // Remove it
  172. await runQmd(["collection", "remove", "fixtures"]);
  173. // Re-add
  174. const { stdout, exitCode } = await runQmd(["collection", "add", "."]);
  175. expect(exitCode).toBe(0);
  176. expect(stdout).toContain("Collection 'fixtures' created successfully");
  177. });
  178. });
  179. describe("CLI Status Command", () => {
  180. beforeEach(async () => {
  181. // Ensure we have indexed files
  182. await runQmd(["collection", "add", "."]);
  183. });
  184. test("shows index status", async () => {
  185. const { stdout, exitCode } = await runQmd(["status"]);
  186. expect(exitCode).toBe(0);
  187. // Should show collection info
  188. expect(stdout).toContain("Collection");
  189. });
  190. });
  191. describe("CLI Search Command", () => {
  192. beforeEach(async () => {
  193. // Ensure we have indexed files
  194. await runQmd(["collection", "add", "."]);
  195. });
  196. test("searches for documents with BM25", async () => {
  197. const { stdout, exitCode } = await runQmd(["search", "meeting"]);
  198. expect(exitCode).toBe(0);
  199. // Should find meeting.md
  200. expect(stdout.toLowerCase()).toContain("meeting");
  201. });
  202. test("searches with limit option", async () => {
  203. const { stdout, exitCode } = await runQmd(["search", "-n", "1", "test"]);
  204. expect(exitCode).toBe(0);
  205. });
  206. test("searches with all results option", async () => {
  207. const { stdout, exitCode } = await runQmd(["search", "--all", "the"]);
  208. expect(exitCode).toBe(0);
  209. });
  210. test("returns no results message for non-matching query", async () => {
  211. const { stdout, exitCode } = await runQmd(["search", "xyznonexistent123"]);
  212. expect(exitCode).toBe(0);
  213. expect(stdout).toContain("No results");
  214. });
  215. test("requires query argument", async () => {
  216. const { stdout, stderr, exitCode } = await runQmd(["search"]);
  217. expect(exitCode).toBe(1);
  218. // Error message goes to stderr
  219. expect(stderr).toContain("Usage:");
  220. });
  221. });
  222. describe("CLI Get Command", () => {
  223. beforeEach(async () => {
  224. // Ensure we have indexed files
  225. await runQmd(["collection", "add", "."]);
  226. });
  227. test("retrieves document content by path", async () => {
  228. const { stdout, exitCode } = await runQmd(["get", "README.md"]);
  229. expect(exitCode).toBe(0);
  230. expect(stdout).toContain("Test Project");
  231. });
  232. test("retrieves document from subdirectory", async () => {
  233. const { stdout, exitCode } = await runQmd(["get", "notes/meeting.md"]);
  234. expect(exitCode).toBe(0);
  235. expect(stdout).toContain("Team Meeting");
  236. });
  237. test("handles non-existent file", async () => {
  238. const { stdout, exitCode } = await runQmd(["get", "nonexistent.md"]);
  239. // Should indicate file not found
  240. expect(exitCode).toBe(1);
  241. });
  242. });
  243. describe("CLI Multi-Get Command", () => {
  244. let localDbPath: string;
  245. beforeEach(async () => {
  246. // Use fresh database for each test
  247. localDbPath = getFreshDbPath();
  248. // Ensure we have indexed files
  249. const addResult = await runQmd(["collection", "add", ".", "--name", "fixtures"], { dbPath: localDbPath });
  250. if (addResult.exitCode !== 0) {
  251. throw new Error(`Failed to add collection: ${addResult.stderr}`);
  252. }
  253. });
  254. test("retrieves multiple documents by pattern", async () => {
  255. // Test glob pattern matching
  256. const { stdout, stderr, exitCode } = await runQmd(["multi-get", "notes/*.md"], { dbPath: localDbPath });
  257. expect(exitCode).toBe(0);
  258. // Should contain content from both notes files
  259. expect(stdout).toContain("Meeting");
  260. expect(stdout).toContain("Ideas");
  261. });
  262. test("retrieves documents by comma-separated paths", async () => {
  263. const { stdout, exitCode } = await runQmd([
  264. "multi-get",
  265. "README.md,notes/meeting.md",
  266. ], { dbPath: localDbPath });
  267. expect(exitCode).toBe(0);
  268. expect(stdout).toContain("Test Project");
  269. expect(stdout).toContain("Team Meeting");
  270. });
  271. });
  272. describe("CLI Update Command", () => {
  273. let localDbPath: string;
  274. beforeEach(async () => {
  275. // Use a fresh database for this test suite
  276. localDbPath = getFreshDbPath();
  277. // Ensure we have indexed files
  278. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  279. });
  280. test("updates all collections", async () => {
  281. const { stdout, exitCode } = await runQmd(["update"], { dbPath: localDbPath });
  282. expect(exitCode).toBe(0);
  283. expect(stdout).toContain("Updating");
  284. });
  285. });
  286. describe("CLI Add-Context Command", () => {
  287. beforeEach(async () => {
  288. // Ensure we have indexed files
  289. await runQmd(["collection", "add", "."]);
  290. });
  291. test("adds context to a path", async () => {
  292. // First add a collection to get its name
  293. const listResult = await runQmd(["collection", "list"]);
  294. // Parse collection name - it's on the 3rd line (after header and blank line)
  295. const lines = listResult.stdout.split('\n').filter(l => l.trim());
  296. const collectionName = lines[1]; // "fixtures" is on line 2
  297. // Add context to the collection root using virtual path
  298. const { stdout, exitCode } = await runQmd([
  299. "context",
  300. "add",
  301. `qmd://${collectionName}/`,
  302. "Personal notes and meeting logs",
  303. ]);
  304. expect(exitCode).toBe(0);
  305. expect(stdout).toContain("✓ Added context");
  306. });
  307. test("requires path and text arguments", async () => {
  308. const { stderr, exitCode } = await runQmd(["add-context"]);
  309. expect(exitCode).toBe(1);
  310. // Error message goes to stderr
  311. expect(stderr).toContain("Usage:");
  312. });
  313. });
  314. describe("CLI Cleanup Command", () => {
  315. beforeEach(async () => {
  316. // Ensure we have indexed files
  317. await runQmd(["collection", "add", "."]);
  318. });
  319. test("cleans up orphaned entries", async () => {
  320. const { stdout, exitCode } = await runQmd(["cleanup"]);
  321. expect(exitCode).toBe(0);
  322. });
  323. });
  324. describe("CLI Error Handling", () => {
  325. test("handles unknown command", async () => {
  326. const { stderr, exitCode } = await runQmd(["unknowncommand"]);
  327. expect(exitCode).toBe(1);
  328. // Should indicate unknown command
  329. expect(stderr).toContain("Unknown command");
  330. });
  331. test("uses INDEX_PATH environment variable", async () => {
  332. // Verify the test DB path is being used by creating a separate index
  333. const customDbPath = join(testDir, "custom.sqlite");
  334. const { exitCode } = await runQmd(["collection", "add", "."], {
  335. env: { INDEX_PATH: customDbPath },
  336. });
  337. expect(exitCode).toBe(0);
  338. // The custom database should exist
  339. const file = Bun.file(customDbPath);
  340. expect(await file.exists()).toBe(true);
  341. });
  342. });
  343. describe("CLI Output Formats", () => {
  344. beforeEach(async () => {
  345. await runQmd(["collection", "add", "."]);
  346. });
  347. test("search with --json flag outputs JSON", async () => {
  348. const { stdout, exitCode } = await runQmd(["search", "--json", "test"]);
  349. expect(exitCode).toBe(0);
  350. // Should be valid JSON
  351. const parsed = JSON.parse(stdout);
  352. expect(Array.isArray(parsed)).toBe(true);
  353. });
  354. test("search with --files flag outputs file paths", async () => {
  355. const { stdout, exitCode } = await runQmd(["search", "--files", "meeting"]);
  356. expect(exitCode).toBe(0);
  357. expect(stdout).toContain(".md");
  358. });
  359. test("search output includes snippets by default", async () => {
  360. const { stdout, exitCode } = await runQmd(["search", "API"]);
  361. expect(exitCode).toBe(0);
  362. // If results found, should have snippet content
  363. if (!stdout.includes("No results")) {
  364. expect(stdout.toLowerCase()).toContain("api");
  365. }
  366. });
  367. });
  368. describe("CLI Search with Collection Filter", () => {
  369. let localDbPath: string;
  370. beforeEach(async () => {
  371. // Use a fresh database for this test suite
  372. localDbPath = getFreshDbPath();
  373. // Create multiple collections with explicit names
  374. await runQmd(["collection", "add", ".", "--name", "notes", "--mask", "notes/*.md"], { dbPath: localDbPath });
  375. await runQmd(["collection", "add", ".", "--name", "docs", "--mask", "docs/*.md"], { dbPath: localDbPath });
  376. });
  377. test("filters search by collection name", async () => {
  378. const { stdout, stderr, exitCode } = await runQmd([
  379. "search",
  380. "-c",
  381. "notes",
  382. "meeting",
  383. ], { dbPath: localDbPath });
  384. if (exitCode !== 0) {
  385. console.log("Collection filter search failed:");
  386. console.log("stdout:", stdout);
  387. console.log("stderr:", stderr);
  388. }
  389. expect(exitCode).toBe(0);
  390. });
  391. });
  392. describe("CLI Context Management", () => {
  393. let localDbPath: string;
  394. beforeEach(async () => {
  395. // Use a fresh database for this test suite
  396. localDbPath = getFreshDbPath();
  397. // Index some files first
  398. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  399. });
  400. test("add global context with /", async () => {
  401. const { stdout, exitCode } = await runQmd([
  402. "context",
  403. "add",
  404. "/",
  405. "Global system context",
  406. ], { dbPath: localDbPath });
  407. expect(exitCode).toBe(0);
  408. expect(stdout).toContain("✓ Set global context");
  409. expect(stdout).toContain("Global system context");
  410. });
  411. test("list contexts", async () => {
  412. // Add a global context first
  413. await runQmd([
  414. "context",
  415. "add",
  416. "/",
  417. "Test context",
  418. ], { dbPath: localDbPath });
  419. const { stdout, exitCode } = await runQmd([
  420. "context",
  421. "list",
  422. ], { dbPath: localDbPath });
  423. expect(exitCode).toBe(0);
  424. expect(stdout).toContain("Configured Contexts");
  425. expect(stdout).toContain("Test context");
  426. });
  427. test("add context to virtual path", async () => {
  428. // Collection name should be "fixtures" (basename of the fixtures directory)
  429. const { stdout, exitCode } = await runQmd([
  430. "context",
  431. "add",
  432. "qmd://fixtures/notes",
  433. "Context for notes subdirectory",
  434. ], { dbPath: localDbPath });
  435. expect(exitCode).toBe(0);
  436. expect(stdout).toContain("✓ Added context for: qmd://fixtures/notes");
  437. });
  438. test("remove global context", async () => {
  439. // Add a global context first
  440. await runQmd([
  441. "context",
  442. "add",
  443. "/",
  444. "Global context to remove",
  445. ], { dbPath: localDbPath });
  446. const { stdout, exitCode } = await runQmd([
  447. "context",
  448. "rm",
  449. "/",
  450. ], { dbPath: localDbPath });
  451. expect(exitCode).toBe(0);
  452. expect(stdout).toContain("✓ Removed");
  453. });
  454. test("remove virtual path context", async () => {
  455. // Add a context first
  456. await runQmd([
  457. "context",
  458. "add",
  459. "qmd://fixtures/notes",
  460. "Context to remove",
  461. ], { dbPath: localDbPath });
  462. const { stdout, exitCode } = await runQmd([
  463. "context",
  464. "rm",
  465. "qmd://fixtures/notes",
  466. ], { dbPath: localDbPath });
  467. expect(exitCode).toBe(0);
  468. expect(stdout).toContain("✓ Removed context for: qmd://fixtures/notes");
  469. });
  470. test("fails to remove non-existent context", async () => {
  471. const { stdout, stderr, exitCode } = await runQmd([
  472. "context",
  473. "rm",
  474. "qmd://nonexistent/path",
  475. ], { dbPath: localDbPath });
  476. expect(exitCode).toBe(1);
  477. expect(stderr || stdout).toContain("not found");
  478. });
  479. });
  480. describe("CLI ls Command", () => {
  481. let localDbPath: string;
  482. beforeEach(async () => {
  483. // Use a fresh database for this test suite
  484. localDbPath = getFreshDbPath();
  485. // Index some files first
  486. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  487. });
  488. test("lists all collections", async () => {
  489. const { stdout, exitCode } = await runQmd(["ls"], { dbPath: localDbPath });
  490. expect(exitCode).toBe(0);
  491. expect(stdout).toContain("Collections:");
  492. expect(stdout).toContain("qmd://fixtures/");
  493. });
  494. test("lists files in a collection", async () => {
  495. const { stdout, exitCode } = await runQmd(["ls", "fixtures"], { dbPath: localDbPath });
  496. expect(exitCode).toBe(0);
  497. // handelize converts to lowercase
  498. expect(stdout).toContain("qmd://fixtures/readme.md");
  499. expect(stdout).toContain("qmd://fixtures/notes/meeting.md");
  500. });
  501. test("lists files with path prefix", async () => {
  502. const { stdout, exitCode } = await runQmd(["ls", "fixtures/notes"], { dbPath: localDbPath });
  503. expect(exitCode).toBe(0);
  504. expect(stdout).toContain("qmd://fixtures/notes/meeting.md");
  505. expect(stdout).toContain("qmd://fixtures/notes/ideas.md");
  506. // Should not include files outside the prefix (handelize converts to lowercase)
  507. expect(stdout).not.toContain("qmd://fixtures/readme.md");
  508. });
  509. test("lists files with virtual path", async () => {
  510. const { stdout, exitCode } = await runQmd(["ls", "qmd://fixtures/docs"], { dbPath: localDbPath });
  511. expect(exitCode).toBe(0);
  512. expect(stdout).toContain("qmd://fixtures/docs/api.md");
  513. });
  514. test("handles non-existent collection", async () => {
  515. const { stderr, exitCode } = await runQmd(["ls", "nonexistent"], { dbPath: localDbPath });
  516. expect(exitCode).toBe(1);
  517. expect(stderr).toContain("Collection not found");
  518. });
  519. });
  520. describe("CLI Collection Commands", () => {
  521. let localDbPath: string;
  522. beforeEach(async () => {
  523. // Use a fresh database for this test suite
  524. localDbPath = getFreshDbPath();
  525. // Index some files first to create a collection
  526. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  527. });
  528. test("lists collections", async () => {
  529. const { stdout, exitCode } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  530. expect(exitCode).toBe(0);
  531. expect(stdout).toContain("Collections");
  532. expect(stdout).toContain("fixtures");
  533. expect(stdout).toContain("Path:");
  534. expect(stdout).toContain("Pattern:");
  535. expect(stdout).toContain("Files:");
  536. });
  537. test("removes a collection", async () => {
  538. // First verify the collection exists
  539. const { stdout: listBefore } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  540. expect(listBefore).toContain("fixtures");
  541. // Remove it
  542. const { stdout, exitCode } = await runQmd(["collection", "remove", "fixtures"], { dbPath: localDbPath });
  543. expect(exitCode).toBe(0);
  544. expect(stdout).toContain("✓ Removed collection 'fixtures'");
  545. expect(stdout).toContain("Deleted");
  546. // Verify it's gone
  547. const { stdout: listAfter } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  548. expect(listAfter).not.toContain("fixtures");
  549. });
  550. test("handles removing non-existent collection", async () => {
  551. const { stderr, exitCode } = await runQmd(["collection", "remove", "nonexistent"], { dbPath: localDbPath });
  552. expect(exitCode).toBe(1);
  553. expect(stderr).toContain("Collection not found");
  554. });
  555. test("handles missing remove argument", async () => {
  556. const { stderr, exitCode } = await runQmd(["collection", "remove"], { dbPath: localDbPath });
  557. expect(exitCode).toBe(1);
  558. expect(stderr).toContain("Usage:");
  559. });
  560. test("handles unknown subcommand", async () => {
  561. const { stderr, exitCode } = await runQmd(["collection", "invalid"], { dbPath: localDbPath });
  562. expect(exitCode).toBe(1);
  563. expect(stderr).toContain("Unknown subcommand");
  564. });
  565. test("renames a collection", async () => {
  566. // First verify the collection exists
  567. const { stdout: listBefore } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  568. expect(listBefore).toMatch(/^fixtures$/m); // Collection name on its own line
  569. // Rename it
  570. const { stdout, exitCode } = await runQmd(["collection", "rename", "fixtures", "my-fixtures"], { dbPath: localDbPath });
  571. expect(exitCode).toBe(0);
  572. expect(stdout).toContain("✓ Renamed collection 'fixtures' to 'my-fixtures'");
  573. expect(stdout).toContain("qmd://fixtures/");
  574. expect(stdout).toContain("qmd://my-fixtures/");
  575. // Verify the new name exists and old name is gone
  576. const { stdout: listAfter } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  577. expect(listAfter).toMatch(/^my-fixtures$/m); // Collection name on its own line
  578. expect(listAfter).not.toMatch(/^fixtures$/m); // Old name should not appear as collection name
  579. });
  580. test("handles renaming non-existent collection", async () => {
  581. const { stderr, exitCode } = await runQmd(["collection", "rename", "nonexistent", "newname"], { dbPath: localDbPath });
  582. expect(exitCode).toBe(1);
  583. expect(stderr).toContain("Collection not found");
  584. });
  585. test("handles renaming to existing collection name", async () => {
  586. // Create a second collection in a temp directory
  587. const tempDir = await mkdtemp(join(tmpdir(), "qmd-second-"));
  588. await writeFile(join(tempDir, "test.md"), "# Test");
  589. const addResult = await runQmd(["collection", "add", tempDir, "--name", "second"], { dbPath: localDbPath });
  590. if (addResult.exitCode !== 0) {
  591. console.error("Failed to add second collection:", addResult.stderr);
  592. }
  593. expect(addResult.exitCode).toBe(0);
  594. // Verify both collections exist
  595. const { stdout: listBoth } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  596. expect(listBoth).toMatch(/^fixtures$/m);
  597. expect(listBoth).toMatch(/^second$/m);
  598. // Try to rename fixtures to second (which already exists)
  599. const { stderr, exitCode } = await runQmd(["collection", "rename", "fixtures", "second"], { dbPath: localDbPath });
  600. expect(exitCode).toBe(1);
  601. expect(stderr).toContain("Collection name already exists");
  602. });
  603. test("handles missing rename arguments", async () => {
  604. const { stderr: stderr1, exitCode: exitCode1 } = await runQmd(["collection", "rename"], { dbPath: localDbPath });
  605. expect(exitCode1).toBe(1);
  606. expect(stderr1).toContain("Usage:");
  607. const { stderr: stderr2, exitCode: exitCode2 } = await runQmd(["collection", "rename", "fixtures"], { dbPath: localDbPath });
  608. expect(exitCode2).toBe(1);
  609. expect(stderr2).toContain("Usage:");
  610. });
  611. });