import { afterEach, beforeEach, describe, expect, test } from "bun:test"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { DEFAULT_LANGFUSE_CACHE_DIR, LANGFUSE_CACHE_DIR_ENV, TraceCache } from "./cache.js"; import type { CachedTrace } from "./types.js"; const project = { baseUrl: "https://langfuse.test", projectId: "test-project" }; const originalCwd = process.cwd(); const originalCacheDir = process.env[LANGFUSE_CACHE_DIR_ENV]; let dir: string; beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "seal-trace-cache-test-")); process.env[LANGFUSE_CACHE_DIR_ENV] = join(dir, "user-cache"); }); afterEach(() => { process.chdir(originalCwd); if (originalCacheDir === undefined) delete process.env[LANGFUSE_CACHE_DIR_ENV]; else process.env[LANGFUSE_CACHE_DIR_ENV] = originalCacheDir; rmSync(dir, { recursive: true, force: true }); }); function legacyEntry(overrides: Record = {}): string { const cached: CachedTrace = { cacheVersion: 1, cachedAt: "2026-10-01T00:00:00Z", project: { key: "default", ...project }, trace: { id: "trace", projectId: project.projectId, observations: [] } }; const file = join(dir, ".cache", "langfuse", "traces", "default", "trace.json"); mkdirSync(join(dir, ".cache", "langfuse", "traces", "default"), { recursive: true }); writeFileSync(file, JSON.stringify({ ...cached, ...overrides })); process.chdir(dir); return file; } describe("portable Langfuse cache", () => { test("the default path is independent of cwd without writing to the real user's cache", () => { delete process.env[LANGFUSE_CACHE_DIR_ENV]; const first = new TraceCache(project).path("trace"); process.chdir(dir); const second = new TraceCache(project).path("trace"); expect(first).toBe(join(DEFAULT_LANGFUSE_CACHE_DIR, "default", "trace.json")); expect(second).toBe(first); }); test("a newly created service can read a trace after changing directories", async () => { const first = new TraceCache(project); await first.write("trace", { id: "trace", projectId: project.projectId, observations: [] }); const elsewhere = join(dir, "elsewhere"); mkdirSync(elsewhere); process.chdir(elsewhere); const second = new TraceCache(project); expect(second.path("trace")).toBe(first.path("trace")); expect((await second.read("trace"))?.trace.id).toBe("trace"); expect(statSync(first.path("trace")).mode & 0o777).toBe(0o600); }); test("legacy cache is copied once with its timestamp and original file retained", async () => { const legacy = legacyEntry(); const original = readFileSync(legacy, "utf8"); const imported = await new TraceCache(project).read("trace"); expect(imported?.cachedAt).toBe("2026-10-01T00:00:00Z"); expect(readFileSync(legacy, "utf8")).toBe(original); process.chdir(originalCwd); expect(await new TraceCache(project).read("trace")).toEqual(imported); }); test("a different project's legacy trace is not imported", async () => { legacyEntry({ project: { key: "default", baseUrl: project.baseUrl, projectId: "other-project" } }); const cache = new TraceCache(project); expect(await cache.read("trace")).toBeUndefined(); expect(existsSync(cache.path("trace"))).toBe(false); }); test("wrong trace IDs and unsupported cache versions are not accepted", async () => { for (const entry of [{ cacheVersion: 2 }, { trace: { id: "another-trace", projectId: project.projectId } }]) { legacyEntry(entry); expect(await new TraceCache(project).read("trace")).toBeUndefined(); } }); });