|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +import { |
| 7 | + _resetRecipesCacheForTests, |
| 8 | + getQueryRecipeSql, |
| 9 | + listQueryRecipeCatalog, |
| 10 | + listQueryRecipeIds, |
| 11 | + setQueryRecipesProjectRoot, |
| 12 | +} from "./query-recipes"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Regression — `parseQueryRest` validates `--recipe <id>` / `--recipes-json` / |
| 16 | + * `--print-sql <id>` BEFORE `runQueryCmd` calls `bootstrapCodemap`, so the |
| 17 | + * registry has historically seen `getProjectRoot()` throw and silently |
| 18 | + * fallen back to bundled-only. `main.ts` now plumbs the resolved `--root` |
| 19 | + * (from `parseBootstrapArgs`) into `setQueryRecipesProjectRoot` so the |
| 20 | + * parser-phase discovery sees the project's recipes. |
| 21 | + * |
| 22 | + * The tests below deliberately DO NOT call `initCodemap()` — they exercise |
| 23 | + * the override-only path the CLI parser hits. |
| 24 | + */ |
| 25 | +describe("setQueryRecipesProjectRoot — pre-bootstrap CLI parse-phase path", () => { |
| 26 | + let projectRoot: string; |
| 27 | + // Per-test unique ids so the assertions can't collide with a future bundled |
| 28 | + // recipe of the same name. |
| 29 | + let primaryId: string; |
| 30 | + let otherId: string; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + const suffix = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`; |
| 34 | + primaryId = `team-fixture-${suffix}`; |
| 35 | + otherId = `other-fixture-${suffix}`; |
| 36 | + projectRoot = mkdtempSync(join(tmpdir(), "codemap-pre-bootstrap-")); |
| 37 | + mkdirSync(join(projectRoot, ".codemap", "recipes"), { recursive: true }); |
| 38 | + writeFileSync( |
| 39 | + join(projectRoot, ".codemap", "recipes", `${primaryId}.sql`), |
| 40 | + "SELECT 1 AS ok\n", |
| 41 | + ); |
| 42 | + _resetRecipesCacheForTests(); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + rmSync(projectRoot, { recursive: true, force: true }); |
| 47 | + _resetRecipesCacheForTests(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("loads project recipes when only the override is set (no initCodemap)", () => { |
| 51 | + setQueryRecipesProjectRoot(projectRoot); |
| 52 | + const catalog = listQueryRecipeCatalog(); |
| 53 | + const projectIds = catalog |
| 54 | + .filter((c) => c.source === "project") |
| 55 | + .map((c) => c.id); |
| 56 | + expect(projectIds).toContain(primaryId); |
| 57 | + expect(getQueryRecipeSql(primaryId)).toContain("SELECT 1"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("clears project recipes when override is reset to undefined", () => { |
| 61 | + setQueryRecipesProjectRoot(projectRoot); |
| 62 | + expect(listQueryRecipeIds()).toContain(primaryId); |
| 63 | + setQueryRecipesProjectRoot(undefined); |
| 64 | + expect(listQueryRecipeIds()).not.toContain(primaryId); |
| 65 | + }); |
| 66 | + |
| 67 | + it("re-setting the override to a new root invalidates the cache", () => { |
| 68 | + setQueryRecipesProjectRoot(projectRoot); |
| 69 | + expect(listQueryRecipeIds()).toContain(primaryId); |
| 70 | + |
| 71 | + const otherRoot = mkdtempSync(join(tmpdir(), "codemap-pre-bootstrap-")); |
| 72 | + try { |
| 73 | + mkdirSync(join(otherRoot, ".codemap", "recipes"), { recursive: true }); |
| 74 | + writeFileSync( |
| 75 | + join(otherRoot, ".codemap", "recipes", `${otherId}.sql`), |
| 76 | + "SELECT 2\n", |
| 77 | + ); |
| 78 | + setQueryRecipesProjectRoot(otherRoot); |
| 79 | + const ids = listQueryRecipeIds(); |
| 80 | + expect(ids).toContain(otherId); |
| 81 | + expect(ids).not.toContain(primaryId); |
| 82 | + } finally { |
| 83 | + rmSync(otherRoot, { recursive: true, force: true }); |
| 84 | + } |
| 85 | + }); |
| 86 | +}); |
0 commit comments