/** * Master-DoD §3/§10 for the storage paths that `src/platform/dependency-mode.ts` * did NOT yet cover when it was introduced: * * 1. The legacy Next.js stores (`lib/orders-store.ts`, * `lib/subscriptions-store.ts`) predate that module and called `getSql()` * directly, silently falling back to module-global `Map`s. For orders, * payments and subscriptions that is invisible loss of money-bearing * state: 201 returned, row gone on the next restart or on another replica. * * 2. `LocalBlobStore` writes document bytes to a container-local disk while * an artifact pointer claims they are stored — a fallback that silently * swaps durable object storage for ephemeral disk. * * Both are asserted in BOTH directions: production refuses, and the * dev/test seam plus the correctly-configured production path still work. A * guard that had degenerated into a blanket refusal would fail these too. */ import { describe, it, expect, afterEach } from "vitest"; import { getSqlForDurableStore } from "@/lib/db"; import { LocalBlobStore } from "@/src/adapters/local-blob-store"; import { SiaxError } from "@/src/platform/errors"; import { promises as fs } from "fs"; import os from "os"; import path from "path"; /** * Drive the real process env (these read it through `process.env`), restoring * after. * * NOTE the async variant below is not a stylistic duplicate. A synchronous * `try/finally` wrapper around an async callback restores the environment at * the callback's FIRST `await`, not when it finishes — so every assertion * after that await runs with the env already restored and silently measures * the wrong runtime. That produced a false failure while writing these tests * (a `read` that should have been refused returned ENOENT instead, because * NODE_ENV was no longer "production" by the time it ran). */ function withEnv(vars: Record, fn: () => T): T { const restore = applyEnv(vars); try { return fn(); } finally { restore(); } } async function withEnvAsync( vars: Record, fn: () => Promise ): Promise { const restore = applyEnv(vars); try { return await fn(); } finally { restore(); } } function applyEnv(vars: Record): () => void { const prev: Record = {}; for (const [k, v] of Object.entries(vars)) { prev[k] = process.env[k]; if (v === undefined) delete process.env[k]; else process.env[k] = v; } return () => { for (const [k, v] of Object.entries(prev)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } }; } const NO_DB = { DATABASE_URL: undefined, POSTGRES_URL: undefined, POSTGRES_PRISMA_URL: undefined, }; const tmpDirs: string[] = []; afterEach(async () => { while (tmpDirs.length) { const d = tmpDirs.pop()!; await fs.rm(d, { recursive: true, force: true }).catch(() => {}); } }); describe("legacy Next.js durable stores (orders/subscriptions)", () => { it("production + no database => throws DEPENDENCY_UNAVAILABLE instead of an in-memory Map", () => { withEnv({ NODE_ENV: "production", NEXT_PHASE: undefined, ...NO_DB }, () => { try { getSqlForDurableStore("0pt.orders"); expect.unreachable("must throw rather than return the memory seam"); } catch (err) { expect(err).toBeInstanceOf(SiaxError); expect((err as SiaxError).code).toBe("DEPENDENCY_UNAVAILABLE"); expect((err as SiaxError).source).toBe("0pt.orders"); } }); }); it("names the specific store that is unbacked, so the operator knows what is at risk", () => { withEnv({ NODE_ENV: "production", NEXT_PHASE: undefined, ...NO_DB }, () => { expect(() => getSqlForDurableStore("0pt.subscriptions")).toThrowError(/0pt\.subscriptions/); }); }); it("non-production + no database => null, keeping the in-memory seam for tests/dev", () => { withEnv({ NODE_ENV: "test", ...NO_DB }, () => { expect(getSqlForDurableStore("0pt.orders")).toBeNull(); }); }); it("the Next BUILD phase is not a production runtime (page-data collection must compile)", () => { withEnv({ NODE_ENV: "production", NEXT_PHASE: "phase-production-build", ...NO_DB }, () => { expect(getSqlForDurableStore("0pt.orders")).toBeNull(); }); }); }); describe("LocalBlobStore is not production object storage", () => { it("refuses to persist document bytes in a production runtime", async () => { const store = new LocalBlobStore(path.join(os.tmpdir(), "0pt-blob-guard-prod")); await withEnvAsync({ NODE_ENV: "production", NEXT_PHASE: undefined }, async () => { await expect(store.write("docs", "a.pdf", Buffer.from("x"))).rejects.toThrowError(SiaxError); await expect(store.read("docs", "a.pdf")).rejects.toThrowError(/DEPENDENCY_UNAVAILABLE|ST0RE/); }); }); it("does not write anything to disk when it refuses", async () => { const dir = await fs.mkdtemp(path.join(os.tmpdir(), "0pt-blob-noleak-")); tmpDirs.push(dir); const store = new LocalBlobStore(dir); await withEnvAsync({ NODE_ENV: "production", NEXT_PHASE: undefined }, async () => { await expect(store.write("docs", "a.pdf", Buffer.from("x"))).rejects.toThrowError(SiaxError); }); expect(await fs.readdir(dir)).toEqual([]); }); it("constructing the adapter is never fatal — only persisting/reading bytes is", () => { withEnv({ NODE_ENV: "production", NEXT_PHASE: undefined }, () => { expect(() => new LocalBlobStore("/tmp/whatever")).not.toThrow(); }); }); it("still round-trips bytes outside production (dev/test capability stays real)", async () => { const dir = await fs.mkdtemp(path.join(os.tmpdir(), "0pt-blob-roundtrip-")); tmpDirs.push(dir); const store = new LocalBlobStore(dir); const result = await withEnvAsync({ NODE_ENV: "test" }, () => store.write("docs", "a.pdf", Buffer.from("hello")) ); expect(result.sizeBytes).toBe(5); const back = await withEnvAsync({ NODE_ENV: "test" }, () => store.read("docs", "a.pdf")); expect(back.toString()).toBe("hello"); }); });