Files
gerbeur/api/test/setup.ts
khannurien d392be7cb8
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 50s
v3: api/ gets one layout rule and keeps to it, and whatever crosses the wire is defined once, in shared/ — api/model/interfaces.ts and src/model.ts declared the same 40 types by hand and had drifted: the error envelope, email required on one side and optional on the other, Invitee/InviteTreeEntry and LinkPreview/UrlPreviewResponse for one shape each. The validation limits, the role permissions, the chat length cap, the @mention pattern and the Bandcamp and YouTube player responses were copied too, behind "keep in sync" comments. shared/ now holds them once, one file per domain (api, category, chat, comment, dump, follow, mention, notification, permissions, player, playlist, search, user, validation, ws), and imports nothing from either half. The server sends Dates; the client reads Wire<T>, the same type with every Date an ISO string, and turns it back with src/utils/shared/deserialize.ts. APIErrorCode becomes a const object, since the frontend's erasableSyntaxOnly rejects an enum. The shared types showed two real gaps: the avatar upload and GET /api/users/me return the full self user and the frontend read them as public users, and a stored session may predate media tokens, so the client keeps it as its own Session type. The backend rule: each folder holds one kind of module, each file is named after its domain. routes/ parse and answer; services/ hold the logic and every SQL query (lib/auth.ts's token check moves to user-service); remote/ is the only place that fetches from other sites — the guarded fetch and the HTML scraping out of rich-content-service, public-address, the Bandcamp and YouTube resolvers, the link providers, the image proxy (fetch, checks and cache, out of routes/preview.ts) and the Twemoji glyphs (out of lib/site-icons.ts). model/ holds per table a row type, its guard and its mappers; validators/ the request guards; db/connection.ts the handle and transaction(), db/startup.ts the purges and default admin that db.ts ran on import, called at the same moment. Tests mirror the source one file per module, test/<layer>/<module>.test.ts, with shared/'s under shared/test/ (deno task test runs both); the test that covered the bandcamp and soundcloud providers together is two now, and the backend can() has its own, so 202 tests. deno lint src/ is clean too: window.* becomes globalThis.*, and test bodies and act() callbacks that never awaited lose their async. The README states the rule. No behaviour changes; the catalogues only move their source references.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 20:19:48 +00:00

32 lines
1.5 KiB
TypeScript

// Points the app at a throwaway database and uploads directory before any app
// module is evaluated. api/config.ts reads the environment and api/db/connection.ts
// opens the database at import time, so this module must be the first import
// of every test file — directly, or as helpers.ts's first import. It imports
// nothing from the app itself, and it is synchronous on purpose: a module with
// a top-level await doesn't hold back the sibling imports that follow it.
import { DatabaseSync } from "node:sqlite";
export const TEST_DIR = Deno.makeTempDirSync({ prefix: "gerbeur-test-" });
Deno.env.set("GERBEUR_DB_PATH", `${TEST_DIR}/gerbeur.db`);
Deno.env.set("GERBEUR_UPLOADS_DIR", `${TEST_DIR}/uploads`);
Deno.env.set("GERBEUR_JWT_SECRET", "test-secret-test-secret-test-secret");
Deno.env.set("GERBEUR_PUBLIC_URL", "http://gerbeur.test");
Deno.env.set("GERBEUR_ALLOWED_ORIGINS", "http://allowed.test");
// The suite never sends mail; the routes behave as on an instance without SMTP.
Deno.env.delete("GERBEUR_SMTPS_URL");
// The migrations, the error middleware and failed fetches all log; a passing
// run shouldn't drown in them. TEST_VERBOSE=1 brings them back for debugging.
if (!Deno.env.get("TEST_VERBOSE")) {
console.log = () => {};
console.error = () => {};
}
// A fresh database is built from schema.sql, exactly as api/db/init.ts does.
const db = new DatabaseSync(`${TEST_DIR}/gerbeur.db`);
db.exec(
Deno.readTextFileSync(new URL("../db/schema.sql", import.meta.url)),
);
db.close();