cli.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. beforeEach(async () => {
  245. // Ensure we have indexed files
  246. await runQmd(["collection", "add", "."]);
  247. });
  248. test("retrieves multiple documents by pattern", async () => {
  249. const { stdout, exitCode } = await runQmd(["multi-get", "notes/*.md"]);
  250. expect(exitCode).toBe(0);
  251. // Should contain content from both notes files
  252. expect(stdout).toContain("Meeting");
  253. expect(stdout).toContain("Ideas");
  254. });
  255. test("retrieves documents by comma-separated paths", async () => {
  256. const { stdout, exitCode } = await runQmd([
  257. "multi-get",
  258. "README.md,notes/meeting.md",
  259. ]);
  260. expect(exitCode).toBe(0);
  261. expect(stdout).toContain("Test Project");
  262. expect(stdout).toContain("Team Meeting");
  263. });
  264. });
  265. describe("CLI Update Command", () => {
  266. let localDbPath: string;
  267. beforeEach(async () => {
  268. // Use a fresh database for this test suite
  269. localDbPath = getFreshDbPath();
  270. // Ensure we have indexed files
  271. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  272. });
  273. test("updates all collections", async () => {
  274. const { stdout, exitCode } = await runQmd(["update"], { dbPath: localDbPath });
  275. expect(exitCode).toBe(0);
  276. expect(stdout).toContain("Updating");
  277. });
  278. });
  279. describe("CLI Add-Context Command", () => {
  280. beforeEach(async () => {
  281. // Ensure we have indexed files
  282. await runQmd(["collection", "add", "."]);
  283. });
  284. test("adds context to a path", async () => {
  285. // First add a collection to get its name
  286. const listResult = await runQmd(["collection", "list"]);
  287. // Parse collection name - it's on the 3rd line (after header and blank line)
  288. const lines = listResult.stdout.split('\n').filter(l => l.trim());
  289. const collectionName = lines[1]; // "fixtures" is on line 2
  290. // Add context to the collection root using virtual path
  291. const { stdout, exitCode } = await runQmd([
  292. "context",
  293. "add",
  294. `qmd://${collectionName}/`,
  295. "Personal notes and meeting logs",
  296. ]);
  297. expect(exitCode).toBe(0);
  298. expect(stdout).toContain("✓ Added context");
  299. });
  300. test("requires path and text arguments", async () => {
  301. const { stderr, exitCode } = await runQmd(["add-context"]);
  302. expect(exitCode).toBe(1);
  303. // Error message goes to stderr
  304. expect(stderr).toContain("Usage:");
  305. });
  306. });
  307. describe("CLI Cleanup Command", () => {
  308. beforeEach(async () => {
  309. // Ensure we have indexed files
  310. await runQmd(["collection", "add", "."]);
  311. });
  312. test("cleans up orphaned entries", async () => {
  313. const { stdout, exitCode } = await runQmd(["cleanup"]);
  314. expect(exitCode).toBe(0);
  315. });
  316. });
  317. describe("CLI Error Handling", () => {
  318. test("handles unknown command", async () => {
  319. const { stderr, exitCode } = await runQmd(["unknowncommand"]);
  320. expect(exitCode).toBe(1);
  321. // Should indicate unknown command
  322. expect(stderr).toContain("Unknown command");
  323. });
  324. test("uses INDEX_PATH environment variable", async () => {
  325. // Verify the test DB path is being used by creating a separate index
  326. const customDbPath = join(testDir, "custom.sqlite");
  327. const { exitCode } = await runQmd(["collection", "add", "."], {
  328. env: { INDEX_PATH: customDbPath },
  329. });
  330. expect(exitCode).toBe(0);
  331. // The custom database should exist
  332. const file = Bun.file(customDbPath);
  333. expect(await file.exists()).toBe(true);
  334. });
  335. });
  336. describe("CLI Output Formats", () => {
  337. beforeEach(async () => {
  338. await runQmd(["collection", "add", "."]);
  339. });
  340. test("search with --json flag outputs JSON", async () => {
  341. const { stdout, exitCode } = await runQmd(["search", "--json", "test"]);
  342. expect(exitCode).toBe(0);
  343. // Should be valid JSON
  344. const parsed = JSON.parse(stdout);
  345. expect(Array.isArray(parsed)).toBe(true);
  346. });
  347. test("search with --files flag outputs file paths", async () => {
  348. const { stdout, exitCode } = await runQmd(["search", "--files", "meeting"]);
  349. expect(exitCode).toBe(0);
  350. expect(stdout).toContain(".md");
  351. });
  352. test("search output includes snippets by default", async () => {
  353. const { stdout, exitCode } = await runQmd(["search", "API"]);
  354. expect(exitCode).toBe(0);
  355. // If results found, should have snippet content
  356. if (!stdout.includes("No results")) {
  357. expect(stdout.toLowerCase()).toContain("api");
  358. }
  359. });
  360. });
  361. describe("CLI Search with Collection Filter", () => {
  362. let localDbPath: string;
  363. beforeEach(async () => {
  364. // Use a fresh database for this test suite
  365. localDbPath = getFreshDbPath();
  366. // Create multiple collections with explicit names
  367. await runQmd(["collection", "add", ".", "--name", "notes", "--mask", "notes/*.md"], { dbPath: localDbPath });
  368. await runQmd(["collection", "add", ".", "--name", "docs", "--mask", "docs/*.md"], { dbPath: localDbPath });
  369. });
  370. test("filters search by collection name", async () => {
  371. const { stdout, stderr, exitCode } = await runQmd([
  372. "search",
  373. "-c",
  374. "notes",
  375. "meeting",
  376. ], { dbPath: localDbPath });
  377. if (exitCode !== 0) {
  378. console.log("Collection filter search failed:");
  379. console.log("stdout:", stdout);
  380. console.log("stderr:", stderr);
  381. }
  382. expect(exitCode).toBe(0);
  383. });
  384. });
  385. describe("CLI Context Management", () => {
  386. let localDbPath: string;
  387. beforeEach(async () => {
  388. // Use a fresh database for this test suite
  389. localDbPath = getFreshDbPath();
  390. // Index some files first
  391. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  392. });
  393. test("add global context with /", async () => {
  394. const { stdout, exitCode } = await runQmd([
  395. "context",
  396. "add",
  397. "/",
  398. "Global system context",
  399. ], { dbPath: localDbPath });
  400. expect(exitCode).toBe(0);
  401. expect(stdout).toContain("✓ Set global context");
  402. expect(stdout).toContain("Global system context");
  403. });
  404. test("list contexts", async () => {
  405. // Add a global context first
  406. await runQmd([
  407. "context",
  408. "add",
  409. "/",
  410. "Test context",
  411. ], { dbPath: localDbPath });
  412. const { stdout, exitCode } = await runQmd([
  413. "context",
  414. "list",
  415. ], { dbPath: localDbPath });
  416. expect(exitCode).toBe(0);
  417. expect(stdout).toContain("Configured Contexts");
  418. expect(stdout).toContain("Test context");
  419. });
  420. test("add context to virtual path", async () => {
  421. // Collection name should be "fixtures" (basename of the fixtures directory)
  422. const { stdout, exitCode } = await runQmd([
  423. "context",
  424. "add",
  425. "qmd://fixtures/notes",
  426. "Context for notes subdirectory",
  427. ], { dbPath: localDbPath });
  428. expect(exitCode).toBe(0);
  429. expect(stdout).toContain("✓ Added context for: qmd://fixtures/notes");
  430. });
  431. test("remove global context", async () => {
  432. // Add a global context first
  433. await runQmd([
  434. "context",
  435. "add",
  436. "/",
  437. "Global context to remove",
  438. ], { dbPath: localDbPath });
  439. const { stdout, exitCode } = await runQmd([
  440. "context",
  441. "rm",
  442. "/",
  443. ], { dbPath: localDbPath });
  444. expect(exitCode).toBe(0);
  445. expect(stdout).toContain("✓ Removed");
  446. });
  447. test("remove virtual path context", async () => {
  448. // Add a context first
  449. await runQmd([
  450. "context",
  451. "add",
  452. "qmd://fixtures/notes",
  453. "Context to remove",
  454. ], { dbPath: localDbPath });
  455. const { stdout, exitCode } = await runQmd([
  456. "context",
  457. "rm",
  458. "qmd://fixtures/notes",
  459. ], { dbPath: localDbPath });
  460. expect(exitCode).toBe(0);
  461. expect(stdout).toContain("✓ Removed context for: qmd://fixtures/notes");
  462. });
  463. test("fails to remove non-existent context", async () => {
  464. const { stdout, stderr, exitCode } = await runQmd([
  465. "context",
  466. "rm",
  467. "qmd://nonexistent/path",
  468. ], { dbPath: localDbPath });
  469. expect(exitCode).toBe(1);
  470. expect(stderr || stdout).toContain("not found");
  471. });
  472. });
  473. describe("CLI ls Command", () => {
  474. let localDbPath: string;
  475. beforeEach(async () => {
  476. // Use a fresh database for this test suite
  477. localDbPath = getFreshDbPath();
  478. // Index some files first
  479. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  480. });
  481. test("lists all collections", async () => {
  482. const { stdout, exitCode } = await runQmd(["ls"], { dbPath: localDbPath });
  483. expect(exitCode).toBe(0);
  484. expect(stdout).toContain("Collections:");
  485. expect(stdout).toContain("qmd://fixtures/");
  486. });
  487. test("lists files in a collection", async () => {
  488. const { stdout, exitCode } = await runQmd(["ls", "fixtures"], { dbPath: localDbPath });
  489. expect(exitCode).toBe(0);
  490. expect(stdout).toContain("qmd://fixtures/README.md");
  491. expect(stdout).toContain("qmd://fixtures/notes/meeting.md");
  492. });
  493. test("lists files with path prefix", async () => {
  494. const { stdout, exitCode } = await runQmd(["ls", "fixtures/notes"], { dbPath: localDbPath });
  495. expect(exitCode).toBe(0);
  496. expect(stdout).toContain("qmd://fixtures/notes/meeting.md");
  497. expect(stdout).toContain("qmd://fixtures/notes/ideas.md");
  498. // Should not include files outside the prefix
  499. expect(stdout).not.toContain("qmd://fixtures/README.md");
  500. });
  501. test("lists files with virtual path", async () => {
  502. const { stdout, exitCode } = await runQmd(["ls", "qmd://fixtures/docs"], { dbPath: localDbPath });
  503. expect(exitCode).toBe(0);
  504. expect(stdout).toContain("qmd://fixtures/docs/api.md");
  505. });
  506. test("handles non-existent collection", async () => {
  507. const { stderr, exitCode } = await runQmd(["ls", "nonexistent"], { dbPath: localDbPath });
  508. expect(exitCode).toBe(1);
  509. expect(stderr).toContain("Collection not found");
  510. });
  511. });
  512. describe("CLI Collection Commands", () => {
  513. let localDbPath: string;
  514. beforeEach(async () => {
  515. // Use a fresh database for this test suite
  516. localDbPath = getFreshDbPath();
  517. // Index some files first to create a collection
  518. await runQmd(["collection", "add", "."], { dbPath: localDbPath });
  519. });
  520. test("lists collections", async () => {
  521. const { stdout, exitCode } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  522. expect(exitCode).toBe(0);
  523. expect(stdout).toContain("Collections");
  524. expect(stdout).toContain("fixtures");
  525. expect(stdout).toContain("Path:");
  526. expect(stdout).toContain("Pattern:");
  527. expect(stdout).toContain("Files:");
  528. });
  529. test("removes a collection", async () => {
  530. // First verify the collection exists
  531. const { stdout: listBefore } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  532. expect(listBefore).toContain("fixtures");
  533. // Remove it
  534. const { stdout, exitCode } = await runQmd(["collection", "remove", "fixtures"], { dbPath: localDbPath });
  535. expect(exitCode).toBe(0);
  536. expect(stdout).toContain("✓ Removed collection 'fixtures'");
  537. expect(stdout).toContain("Deleted");
  538. // Verify it's gone
  539. const { stdout: listAfter } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  540. expect(listAfter).not.toContain("fixtures");
  541. });
  542. test("handles removing non-existent collection", async () => {
  543. const { stderr, exitCode } = await runQmd(["collection", "remove", "nonexistent"], { dbPath: localDbPath });
  544. expect(exitCode).toBe(1);
  545. expect(stderr).toContain("Collection not found");
  546. });
  547. test("handles missing remove argument", async () => {
  548. const { stderr, exitCode } = await runQmd(["collection", "remove"], { dbPath: localDbPath });
  549. expect(exitCode).toBe(1);
  550. expect(stderr).toContain("Usage:");
  551. });
  552. test("handles unknown subcommand", async () => {
  553. const { stderr, exitCode } = await runQmd(["collection", "invalid"], { dbPath: localDbPath });
  554. expect(exitCode).toBe(1);
  555. expect(stderr).toContain("Unknown subcommand");
  556. });
  557. test("renames a collection", async () => {
  558. // First verify the collection exists
  559. const { stdout: listBefore } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  560. expect(listBefore).toMatch(/^fixtures$/m); // Collection name on its own line
  561. // Rename it
  562. const { stdout, exitCode } = await runQmd(["collection", "rename", "fixtures", "my-fixtures"], { dbPath: localDbPath });
  563. expect(exitCode).toBe(0);
  564. expect(stdout).toContain("✓ Renamed collection 'fixtures' to 'my-fixtures'");
  565. expect(stdout).toContain("qmd://fixtures/");
  566. expect(stdout).toContain("qmd://my-fixtures/");
  567. // Verify the new name exists and old name is gone
  568. const { stdout: listAfter } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  569. expect(listAfter).toMatch(/^my-fixtures$/m); // Collection name on its own line
  570. expect(listAfter).not.toMatch(/^fixtures$/m); // Old name should not appear as collection name
  571. });
  572. test("handles renaming non-existent collection", async () => {
  573. const { stderr, exitCode } = await runQmd(["collection", "rename", "nonexistent", "newname"], { dbPath: localDbPath });
  574. expect(exitCode).toBe(1);
  575. expect(stderr).toContain("Collection not found");
  576. });
  577. test("handles renaming to existing collection name", async () => {
  578. // Create a second collection in a temp directory
  579. const tempDir = await mkdtemp(join(tmpdir(), "qmd-second-"));
  580. await writeFile(join(tempDir, "test.md"), "# Test");
  581. const addResult = await runQmd(["collection", "add", tempDir, "--name", "second"], { dbPath: localDbPath });
  582. if (addResult.exitCode !== 0) {
  583. console.error("Failed to add second collection:", addResult.stderr);
  584. }
  585. expect(addResult.exitCode).toBe(0);
  586. // Verify both collections exist
  587. const { stdout: listBoth } = await runQmd(["collection", "list"], { dbPath: localDbPath });
  588. expect(listBoth).toMatch(/^fixtures$/m);
  589. expect(listBoth).toMatch(/^second$/m);
  590. // Try to rename fixtures to second (which already exists)
  591. const { stderr, exitCode } = await runQmd(["collection", "rename", "fixtures", "second"], { dbPath: localDbPath });
  592. expect(exitCode).toBe(1);
  593. expect(stderr).toContain("Collection name already exists");
  594. });
  595. test("handles missing rename arguments", async () => {
  596. const { stderr: stderr1, exitCode: exitCode1 } = await runQmd(["collection", "rename"], { dbPath: localDbPath });
  597. expect(exitCode1).toBe(1);
  598. expect(stderr1).toContain("Usage:");
  599. const { stderr: stderr2, exitCode: exitCode2 } = await runQmd(["collection", "rename", "fixtures"], { dbPath: localDbPath });
  600. expect(exitCode2).toBe(1);
  601. expect(stderr2).toContain("Usage:");
  602. });
  603. });