From 95db1515653c4a043762165f4ac2e5da49793dec Mon Sep 17 00:00:00 2001 From: npeter83 Date: Tue, 28 Jul 2026 23:04:39 +0200 Subject: [PATCH] =?UTF-8?q?test(api):=20guard=20the=20spread-only=20fa?= =?UTF-8?q?=C3=A7ade=20against=20interleave=20regrowth=20(R7.5=20S3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add facade.test.ts asserting the two invariants that keep lib/api.ts safe: no method name is defined in two slices (a silent last-wins spread override), and the `api` object is exactly the union of the slices (nothing added inline in the barrel, nothing lost). Verified to have teeth — an injected duplicate key fails the collision test with a named message. Closes R7.5: the api layer is now per-domain modules behind a mechanically guarded spread-only façade. --- frontend/src/lib/api/facade.test.ts | 85 +++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 frontend/src/lib/api/facade.test.ts diff --git a/frontend/src/lib/api/facade.test.ts b/frontend/src/lib/api/facade.test.ts new file mode 100644 index 0000000..64deace --- /dev/null +++ b/frontend/src/lib/api/facade.test.ts @@ -0,0 +1,85 @@ +import { describe, it, expect, beforeAll } from "vitest"; + +// The R7.5 guard: lib/api.ts is a spread-only façade over per-domain slice modules. These tests +// mechanise the two invariants that make that structure safe — so a future edit that reintroduces +// the interleave (a method dropped into two slices → a silent last-wins spread override) or bloats +// the barrel (a method defined inline instead of in a domain module) fails CI instead of shipping. + +type Slice = Record; + +// Names line up with the modules below; kept as a list so the wiring stays a single source. +const SLICE_MODULES = [ + ["accountApi", () => import("./account")], + ["authApi", () => import("./auth")], + ["setupApi", () => import("./setup")], + ["feedApi", () => import("./feed")], + ["channelsApi", () => import("./channels")], + ["syncApi", () => import("./sync")], + ["playlistsApi", () => import("./playlists")], + ["tagsApi", () => import("./tags")], + ["notificationsApi", () => import("./notifications")], + ["savedViewsApi", () => import("./savedViews")], + ["quotaApi", () => import("./quota")], + ["adminApi", () => import("./admin")], + ["plexApi", () => import("./plex")], + ["messagingApi", () => import("./messaging")], + ["downloadsApi", () => import("./downloads")], +] as const; + +let api: Slice; +const slices: Record = {}; + +beforeAll(async () => { + // The runtime api graph pulls i18n at module-load, which reads localStorage/navigator. Under + // vitest's node env (no jsdom — see vitest.config.ts) stub the minimum so the import succeeds. + if (typeof globalThis.localStorage === "undefined") { + const store = new Map(); + globalThis.localStorage = { + getItem: (k: string) => store.get(k) ?? null, + setItem: (k: string, v: string) => void store.set(k, String(v)), + removeItem: (k: string) => void store.delete(k), + clear: () => store.clear(), + key: (i: number) => [...store.keys()][i] ?? null, + get length() { + return store.size; + }, + } as Storage; + } + if (typeof globalThis.navigator === "undefined") { + globalThis.navigator = { language: "en" } as Navigator; + } + if (typeof globalThis.document === "undefined") { + globalThis.document = { documentElement: {} } as Document; + } + + for (const [name, load] of SLICE_MODULES) { + const mod = (await load()) as Record; + const slice = mod[name]; + if (!slice) throw new Error(`slice module did not export ${name}`); + slices[name] = slice; + } + api = ((await import("../api")) as { api: Slice }).api; +}); + +describe("api façade is a collision-free spread of domain slices", () => { + it("defines no method name in more than one slice (object spread would silently drop one)", () => { + const owner = new Map(); + const collisions: string[] = []; + for (const [sliceName, slice] of Object.entries(slices)) { + for (const key of Object.keys(slice)) { + const prev = owner.get(key); + if (prev) collisions.push(`"${key}" is defined in both ${prev} and ${sliceName}`); + else owner.set(key, sliceName); + } + } + expect(collisions).toEqual([]); + }); + + it("exposes exactly the union of the slices — nothing added inline in the barrel, nothing lost", () => { + const union = new Set(); + for (const slice of Object.values(slices)) { + for (const key of Object.keys(slice)) union.add(key); + } + expect(new Set(Object.keys(api))).toEqual(union); + }); +});