Files
alpenwerk-hr/tests/unit/db-type-parsers.test.ts
Maximilian Stubhan 4d8e3f7154 Restore the shapes the application was written against
The app runs against a real database for the first time since the port, and
two things were broken. Both were invisible to typecheck, lint, 192 tests
and the build.

Sign-in looped. Auth.js created the session and app_upsert_user() adopted
the existing profiles id correctly — the row was in app_users, right id and
all — but the proxy builds its own Auth.js instance from lib/auth/config.ts
alone, and the session callback that copies token.uid onto session.user.id
lived in auth.ts. So the proxy saw a session without an id, treated every
signed-in user as signed out, and sent them back to /login. Click, flash,
login page: from the outside it looked like the button did nothing.

The callback moves to the config both instances share. auth.ts now spreads
the base callbacks instead of replacing them, which is the mistake that
would reintroduce this.

The proxy test did not catch it because its fixture hands the handler a
session that already has user.id — it tested the routing, not the shape
Auth.js actually produces.

Then the dashboard crashed on a.date.localeCompare. PostgREST returned JSON:
a `date` arrived as "2026-08-03", a `numeric` as a number, and that is what
lib/supabase/types.ts declares and what every sort, every date comparison
and every status derivation assumes. The pg driver does the opposite — Date
object and string respectively. The declarations stayed true to what the
code believes; only the runtime value changed, which is why nothing flagged
it.

The driver is configured back to the declared shapes in lib/db/pool.ts,
rather than rewriting 49 call sites. That also removes a timezone hazard:
`date` is a calendar day, and as a Date object it acquires midnight in the
server's zone — a birth date would shift by a day in Austria, always. The
same class of bug as in the seed.

int8 stays a string on purpose: it only comes from count() and is read
through Number() everywhere; parsed as a number it would quietly lose
precision past 2^53.

A missing sign-in error now reaches the server log. Auth.js was failing
silently — a 302 back to /login and nothing to read. That was its own
defect, and it is the reason the first diagnosis took as long as it did.

Verified against the live database: all six pages render, 797 active of
852 records, 744.4 FTE, and a detail page shows birth date 15.08.1968
against SV number 7960 150868 — the digits agree, so no day has shifted.
Both new tests were checked by mutation: remove the fix and they fail.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 10:11:34 +02:00

69 lines
3.0 KiB
TypeScript

import { types } from "pg";
import { beforeAll, describe, expect, it, vi } from "vitest";
// Warum es diesen Test gibt.
//
// lib/supabase/types.ts sagt für 16 Spalten `string` und für zwei `number`.
// Der `pg`-Treiber liefert von Haus aus das Gegenteil: aus `date` wird ein
// Date-Objekt, aus `numeric` eine Zeichenkette.
//
// Diese Abweichung ist für Typprüfer und Tests unsichtbar — die
// Deklarationen beschreiben, was der Code glaubt, nicht was ankommt. tsc war
// sauber, 187 Tests waren grün, und die Anwendung stürzte auf der ersten
// Seite ab („a.date.localeCompare is not a function"). Der Absturz war noch
// der freundliche Fall; ein Vergleich zwischen Date und Zeichenkette wirft
// nicht, er liefert bloss das falsche Ergebnis.
//
// Geprüft wird deshalb die einzige Stelle, an der beides zusammenkommt: die
// Parser, die lib/db/pool.ts beim Import registriert.
vi.mock("server-only", () => ({}));
beforeAll(async () => {
// Der Import allein registriert die Parser — deshalb reicht er als
// Vorbereitung, und deshalb würde ein Entfernen der Zeilen hier auffallen.
await import("@/lib/db/pool");
});
const parse = (oid: number, raw: string) => types.getTypeParser(oid)(raw);
describe("Typumwandlung des Treibers", () => {
it("gibt `date` unverändert als Kalendertag zurück", () => {
// Nicht `new Date(...)`: ein Kalendertag hat keine Zeitzone. Als
// Date-Objekt bekäme er Mitternacht in der Zone des Servers, und ein
// Geburtsdatum verschöbe sich beim Formatieren um einen Tag — in
// Österreich immer, weil MEZ östlich von UTC liegt.
expect(parse(1082, "1968-08-15")).toBe("1968-08-15");
});
it("gibt `numeric` als Zahl zurück", () => {
// Die FTE-Kachel rechnet damit. Als Zeichenkette ergäbe die Summe
// "744.4" + "0.5" = "744.40.5" statt 744.9 — ohne jede Fehlermeldung.
expect(parse(1700, "744.4")).toBe(744.4);
expect(parse(1700, "38.5")).toBe(38.5);
});
it("gibt `timestamptz` als ISO-8601 mit Z zurück", () => {
const v = parse(1184, "2026-08-03 07:55:46.659+00");
expect(v).toBe("2026-08-03T07:55:46.659Z");
// Muss für Date verwertbar bleiben — die Protokollansicht formatiert
// damit. Die Rohform des Treibers („… +00") ist kein Format, das der
// Standard kennt.
expect(Number.isNaN(new Date(v as string).getTime())).toBe(false);
});
it("lässt `int8` als Zeichenkette", () => {
// Kommt nur aus count() und wird überall mit Number() gelesen. Als Zahl
// geparst verlöre es jenseits von 2^53 stillschweigend an Genauigkeit.
expect(parse(20, "852")).toBe("852");
});
it("sortiert ISO-Zeitstempel als Text chronologisch", () => {
// Darauf beruht jedes orderBy im Anwendungscode, das auf Zeichenketten
// arbeitet. Gilt nur, weil das Format feste Breite hat und in UTC steht.
const frueher = parse(1184, "2026-08-03 07:55:46.659+00") as string;
const spaeter = parse(1184, "2026-08-03 09:12:01.000+00") as string;
expect(frueher.localeCompare(spaeter)).toBeLessThan(0);
});
});