CI (SIAX Cloud) / security (push) Successful in 14s
Security Scan (SIAX Cloud) / security (push) Successful in 21s
CI (SIAX Cloud) / security (pull_request) Successful in 15s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 15s
CI (SIAX Cloud) / quality (push) Successful in 44s
CI (SIAX Cloud) / integration (push) Successful in 45s
CI (SIAX Cloud) / integration (pull_request) Successful in 27s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m45s
The `resolveStoreMode` kill switch (Master-DoD §3/§10) covered the newer `src/repositories/*` stores but not two older paths, both of which are live production execution paths today. 1. LEGACY NEXT.JS STORES. `lib/orders-store.ts` and `lib/subscriptions-store.ts` predate `src/platform/dependency-mode.ts` and called `getSql()` directly, silently falling back to module-global `Map`s when no database is configured. For orders, payments and subscriptions that is not a degraded mode, it is invisible loss of money-bearing state: the API returns 201 and the row evaporates on the next restart or on any other replica. The live 0pt deployment (Coolify app 54) runs NODE_ENV=production with DATABASE_URL, POSTGRES_URL and POSTGRES_PRISMA_URL all unset — so this is the behavior in production right now, across 7 API routes. These now go through `getSqlForDurableStore()`, which reuses the SAME `resolveStoreMode` decision every `src/repositories/*` store already uses rather than re-implementing a second production check. The reuse is load-bearing: `isProductionRuntime` deliberately exempts `NEXT_PHASE=phase-production-build`, because `next build` runs page-data collection under NODE_ENV=production while serving nothing. A naive `process.env.NODE_ENV === "production"` test here would have failed the BUILD instead of protecting a request path. 2. LOCALBLOBSTORE. It writes document bytes to a container-local disk while `document-capture-service` registers an artifact pointer claiming the bytes are stored — durable object storage silently swapped for ephemeral disk. It is the default blob store for both the capture and extraction services. In production it now raises DEPENDENCY_UNAVAILABLE (capability blocked: no ST0RE bytes path exists yet) instead of fabricating success. Asserted per-operation, not in the constructor, so merely wiring the services is not fatal — only actually persisting or reading bytes is. Evidence: full suite 956 passed / 184 skipped, typecheck clean, dependency-cruiser 0 errors, lint identical to main's pre-existing baseline (17 problems, none in changed files). Both guards asserted in both directions and mutation-tested: bypassing resolveStoreMode -> 2 failed; dropping the write assert -> 2 failed; dropping the read assert -> 1 failed; all restored -> 8 passed. 7 non-test call sites. DEPLOYMENT NOTE: merging changes no runtime (no Gitea webhook exists on any repo; deploys are manual). Deploying this build REQUIRES provisioning DATABASE_URL for 0pt first, or the order/subscription routes will correctly return DEPENDENCY_UNAVAILABLE instead of silently accepting orders into memory. That provisioning is a Simon action. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
159 lines
6.1 KiB
TypeScript
159 lines
6.1 KiB
TypeScript
/**
|
|
* 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<T>(vars: Record<string, string | undefined>, fn: () => T): T {
|
|
const restore = applyEnv(vars);
|
|
try {
|
|
return fn();
|
|
} finally {
|
|
restore();
|
|
}
|
|
}
|
|
|
|
async function withEnvAsync<T>(
|
|
vars: Record<string, string | undefined>,
|
|
fn: () => Promise<T>
|
|
): Promise<T> {
|
|
const restore = applyEnv(vars);
|
|
try {
|
|
return await fn();
|
|
} finally {
|
|
restore();
|
|
}
|
|
}
|
|
|
|
function applyEnv(vars: Record<string, string | undefined>): () => void {
|
|
const prev: Record<string, string | undefined> = {};
|
|
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");
|
|
});
|
|
});
|