|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; |
| 2 | +import { mkdirSync, rmSync, utimesSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { CACHE_TTL_MS, getCacheDir, getCacheKey, readCache, writeCache } from "./cache.ts"; |
| 6 | + |
| 7 | +// Use env-var override to redirect the cache to an isolated temp directory |
| 8 | +const TEST_CACHE_DIR = join(tmpdir(), `gcs-cache-test-${process.pid}`); |
| 9 | + |
| 10 | +beforeEach(() => { |
| 11 | + process.env.GITHUB_CODE_SEARCH_CACHE_DIR = TEST_CACHE_DIR; |
| 12 | + mkdirSync(TEST_CACHE_DIR, { recursive: true }); |
| 13 | +}); |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR; |
| 17 | + try { |
| 18 | + rmSync(TEST_CACHE_DIR, { recursive: true, force: true }); |
| 19 | + } catch { |
| 20 | + // Best-effort cleanup |
| 21 | + } |
| 22 | +}); |
| 23 | + |
| 24 | +// ─── getCacheDir ────────────────────────────────────────────────────────────── |
| 25 | + |
| 26 | +describe("getCacheDir", () => { |
| 27 | + it("returns the override when GITHUB_CODE_SEARCH_CACHE_DIR is set", () => { |
| 28 | + process.env.GITHUB_CODE_SEARCH_CACHE_DIR = "/custom/cache/path"; |
| 29 | + expect(getCacheDir()).toBe("/custom/cache/path"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("ignores the override when it is an empty string", () => { |
| 33 | + process.env.GITHUB_CODE_SEARCH_CACHE_DIR = " "; |
| 34 | + // On any platform the result must be a non-empty path that doesn't include |
| 35 | + // the blank override string — we just test it doesn't blow up and returns something. |
| 36 | + const dir = getCacheDir(); |
| 37 | + expect(dir.trim()).not.toBe(""); |
| 38 | + }); |
| 39 | + |
| 40 | + it("contains 'github-code-search' in the path on all platforms", () => { |
| 41 | + delete process.env.GITHUB_CODE_SEARCH_CACHE_DIR; |
| 42 | + expect(getCacheDir()).toContain("github-code-search"); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +// ─── getCacheKey ───────────────────────────────────────────────────────────── |
| 47 | + |
| 48 | +describe("getCacheKey", () => { |
| 49 | + it("includes the org name", () => { |
| 50 | + expect(getCacheKey("myorg", [])).toContain("myorg"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("includes each prefix", () => { |
| 54 | + const key = getCacheKey("myorg", ["squad-", "chapter-"]); |
| 55 | + expect(key).toContain("squad-"); |
| 56 | + expect(key).toContain("chapter-"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("produces the same key regardless of prefix order", () => { |
| 60 | + const key1 = getCacheKey("acme", ["squad-", "chapter-"]); |
| 61 | + const key2 = getCacheKey("acme", ["chapter-", "squad-"]); |
| 62 | + expect(key1).toBe(key2); |
| 63 | + }); |
| 64 | + |
| 65 | + it("produces different keys for different orgs", () => { |
| 66 | + const key1 = getCacheKey("org-a", ["squad-"]); |
| 67 | + const key2 = getCacheKey("org-b", ["squad-"]); |
| 68 | + expect(key1).not.toBe(key2); |
| 69 | + }); |
| 70 | + |
| 71 | + it("produces different keys for different prefixes", () => { |
| 72 | + const key1 = getCacheKey("myorg", ["squad-"]); |
| 73 | + const key2 = getCacheKey("myorg", ["chapter-"]); |
| 74 | + expect(key1).not.toBe(key2); |
| 75 | + }); |
| 76 | + |
| 77 | + it("ends with .json", () => { |
| 78 | + expect(getCacheKey("myorg", ["squad-"])).toMatch(/\.json$/); |
| 79 | + }); |
| 80 | + |
| 81 | + it("replaces special characters with underscores to keep the filename filesystem-safe", () => { |
| 82 | + const key = getCacheKey("my/org", ["prefix:"]); |
| 83 | + expect(key).not.toContain("/"); |
| 84 | + expect(key).not.toContain(":"); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +// ─── writeCache / readCache ─────────────────────────────────────────────────── |
| 89 | + |
| 90 | +describe("writeCache / readCache round-trip", () => { |
| 91 | + it("stores and retrieves a Map serialised as an array of entries", () => { |
| 92 | + // Maps are not directly JSON-serialisable; callers are responsible for |
| 93 | + // the serialisation format — here we test with a plain object. |
| 94 | + const data = { repo: "org/repo", teams: ["squad-alpha"] }; |
| 95 | + const key = getCacheKey("myorg", ["squad-"]); |
| 96 | + writeCache(key, data); |
| 97 | + expect(readCache(key)).toEqual(data); |
| 98 | + }); |
| 99 | + |
| 100 | + it("returns null when the key does not exist", () => { |
| 101 | + expect(readCache("nonexistent.json")).toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("returns null when the cached file is corrupted JSON", () => { |
| 105 | + const key = "corrupted.json"; |
| 106 | + writeFileSync(join(TEST_CACHE_DIR, key), "not valid json", "utf8"); |
| 107 | + expect(readCache(key)).toBeNull(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("returns null when the cache entry is older than CACHE_TTL_MS", () => { |
| 111 | + const key = getCacheKey("myorg", ["old-"]); |
| 112 | + writeCache(key, { cached: true }); |
| 113 | + // Back-date the file's mtime to simulate TTL expiry |
| 114 | + const expired = new Date(Date.now() - CACHE_TTL_MS - 1_000); |
| 115 | + const filePath = join(TEST_CACHE_DIR, key); |
| 116 | + utimesSync(filePath, expired, expired); |
| 117 | + expect(readCache(key)).toBeNull(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("returns data when the cache entry is within TTL", () => { |
| 121 | + const key = getCacheKey("myorg", ["fresh-"]); |
| 122 | + const payload = [{ full_name: "org/repo" }]; |
| 123 | + writeCache(key, payload); |
| 124 | + expect(readCache(key)).toEqual(payload); |
| 125 | + }); |
| 126 | + |
| 127 | + it("creates the cache directory if it does not exist", () => { |
| 128 | + // Point to a subdirectory that doesn't exist yet |
| 129 | + const subDir = join(TEST_CACHE_DIR, "subdir", "nested"); |
| 130 | + process.env.GITHUB_CODE_SEARCH_CACHE_DIR = subDir; |
| 131 | + const key = getCacheKey("myorg", ["squad-"]); |
| 132 | + // Should not throw |
| 133 | + expect(() => writeCache(key, { ok: true })).not.toThrow(); |
| 134 | + expect(readCache(key)).toEqual({ ok: true }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("silently ignores write errors on read-only paths", () => { |
| 138 | + process.env.GITHUB_CODE_SEARCH_CACHE_DIR = "/nonexistent/readonly/path"; |
| 139 | + // writeCache must not throw even on filesystem errors |
| 140 | + expect(() => writeCache("key.json", { data: 1 })).not.toThrow(); |
| 141 | + }); |
| 142 | +}); |
0 commit comments