diff --git a/src/qmd.ts b/src/qmd.ts index 367d5f5..b50847c 100755 --- a/src/qmd.ts +++ b/src/qmd.ts @@ -1428,11 +1428,11 @@ async function indexFiles(pwd?: string, globPattern: string = DEFAULT_GLOB, coll }); const total = files.length; - if (total === 0) { + const hasNoFiles = total === 0; + if (hasNoFiles) { progress.clear(); console.log("No files found matching pattern."); - closeDb(); - return; + // Continue so the deactivation pass can mark previously indexed docs as inactive. } let indexed = 0, updated = 0, unchanged = 0, processed = 0; diff --git a/test/cli.test.ts b/test/cli.test.ts index 8c96107..15ec5ee 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -395,6 +395,43 @@ describe("CLI Update Command", () => { expect(exitCode).toBe(0); expect(stdout).toContain("Updating"); }); + + test("deactivates stale docs when collection has zero matching files", async () => { + const { dbPath, configDir } = await createIsolatedTestEnv("update-empty"); + const collectionDir = join(testDir, `update-empty-${Date.now()}`); + await mkdir(collectionDir, { recursive: true }); + + const docPath = join(collectionDir, "only.md"); + const token = `stale-proof-${Date.now()}`; + await writeFile( + docPath, + `--- +date: 2026-03-06 +--- +# Empty Collection Deactivation +${token} +` + ); + + const add = await runQmd( + ["collection", "add", collectionDir, "--name", "empty-check"], + { dbPath, configDir } + ); + expect(add.exitCode).toBe(0); + + const before = await runQmd(["get", "qmd://empty-check/only.md"], { dbPath, configDir }); + expect(before.exitCode).toBe(0); + expect(before.stdout).toContain(token); + + unlinkSync(docPath); + + const update = await runQmd(["update"], { dbPath, configDir }); + expect(update.exitCode).toBe(0); + expect(update.stdout).toContain("No files found matching pattern."); + + const after = await runQmd(["get", "qmd://empty-check/only.md"], { dbPath, configDir }); + expect(after.exitCode).toBe(1); + }); }); describe("CLI Add-Context Command", () => {