rebreak-monorepo/backend/tests/profile/demographics.zod.test.ts
chahinebrini cddc4d0f26 feat(profile): DiGA-Demographics + Pro-Trial-Reward + 7 Profile-Endpoints
Schema:
- 8 neue Profile-Felder fuer DiGA-Demographics (birthYear/gender/maritalStatus/
  profession/bundesland/city + 2 consent-stamps demographicsConsentAt/
  demographicsWithdrawnAt)
- 4 Pro-Trial-Felder (proTrialStartedAt/ExpiresAt/Source/UsedAt) — Free-User
  bekommen 1 Woche Pro als Reward fuer DiGA-Daten-Pflege (siehe
  project_demographic_pro_trial_reward.md)
- lyra_voice_id (Legend-only Voice-Picker)
- diga_banner_dismissed_at (server-side persistence ueber Re-Install)
- last_install_at (Streak-Logic survives Re-Install)
- Migration 20260507_profile_demographics_and_trial: alle Felder optional,
  keine Backfill-Logik notwendig

Endpoints (alle auth-protected, scope=me):
- GET /api/profile/me/sos-insights
- GET /api/profile/me/cooldown-history
- GET /api/profile/me/approved-domains
- POST /api/profile/me/install-event (track app re-installs)
- POST /api/profile/me/diga-banner-dismiss
- PATCH /api/profile/me/demographics (consent-stamp + re-grant-after-withdrawal in tx)
- DELETE /api/profile/me/demographics (DSGVO right-to-be-forgotten)

Plugin:
- pro-trial-expiry-cron: 6h-Interval, conservative-fallback (revoke nur wenn
  kein stripeSubId), 60s initial-delay damit Server-boot nicht blockiert wird

Tests:
- vitest config + erste Test-Files (test-infrastructure setup)

Memory:
- feedback_demographics_user_initiated.md (Lyra darf NIE extrahieren)
- project_demographic_pro_trial_reward.md (Pro-Trial-Reward-Mechanik)
- project_profile_page_design.md (UI-Showpiece, eigene/fremde-Ansicht streng getrennt)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 21:14:06 +02:00

94 lines
3.1 KiB
TypeScript

/**
* Tests for the zod-validation in PATCH /api/profile/me/demographics.
* Replicates the schema from the endpoint to test the validation
* boundaries (anti-Cascade-Bug: invalid input must 422, not 500).
*/
import { describe, expect, it } from "vitest";
import { z } from "zod";
const BUNDESLAND_REGEX = /^DE-(BW|BY|BE|BB|HB|HH|HE|MV|NI|NW|RP|SL|SN|ST|SH|TH)$/;
const GENDER_VALUES = ["male", "female", "diverse", "no_answer"] as const;
const MARITAL_VALUES = [
"single",
"partnered",
"married",
"divorced",
"widowed",
"no_answer",
] as const;
const Schema = z.object({
birthYear: z.number().int().min(1920).max(2010).nullable().optional(),
gender: z.enum(GENDER_VALUES).nullable().optional(),
maritalStatus: z.enum(MARITAL_VALUES).nullable().optional(),
profession: z.string().trim().max(80).nullable().optional(),
bundesland: z.string().regex(BUNDESLAND_REGEX).nullable().optional(),
city: z.string().trim().max(80).nullable().optional(),
});
describe("demographics zod schema — happy", () => {
it("accepts a complete valid demographic payload", () => {
const r = Schema.safeParse({
birthYear: 1989,
gender: "diverse",
maritalStatus: "partnered",
profession: "Pflege",
bundesland: "DE-BY",
city: "München",
});
expect(r.success).toBe(true);
});
it("accepts a partial subset (1 field at a time)", () => {
expect(Schema.safeParse({ birthYear: 1989 }).success).toBe(true);
expect(Schema.safeParse({ city: "Berlin" }).success).toBe(true);
});
it("accepts explicit nulls (= clear field)", () => {
expect(Schema.safeParse({ city: null, birthYear: null }).success).toBe(true);
});
});
describe("demographics zod schema — invalid input must reject (not 500)", () => {
it("rejects birthYear out of range (< 1920)", () => {
const r = Schema.safeParse({ birthYear: 1800 });
expect(r.success).toBe(false);
});
it("rejects birthYear out of range (> 2010 = under-13 protection)", () => {
const r = Schema.safeParse({ birthYear: 2024 });
expect(r.success).toBe(false);
});
it("rejects unknown gender enum", () => {
const r = Schema.safeParse({ gender: "alien" });
expect(r.success).toBe(false);
});
it("rejects bundesland with foreign-country code", () => {
expect(Schema.safeParse({ bundesland: "AT-9" }).success).toBe(false);
expect(Schema.safeParse({ bundesland: "BY" }).success).toBe(false);
expect(Schema.safeParse({ bundesland: "DE-XX" }).success).toBe(false);
});
it("accepts all 16 valid Bundesländer codes", () => {
const codes = [
"DE-BW","DE-BY","DE-BE","DE-BB","DE-HB","DE-HH","DE-HE","DE-MV",
"DE-NI","DE-NW","DE-RP","DE-SL","DE-SN","DE-ST","DE-SH","DE-TH",
];
for (const c of codes) {
expect(Schema.safeParse({ bundesland: c }).success).toBe(true);
}
});
it("rejects profession longer than 80 chars", () => {
const r = Schema.safeParse({ profession: "x".repeat(81) });
expect(r.success).toBe(false);
});
it("rejects city longer than 80 chars", () => {
const r = Schema.safeParse({ city: "x".repeat(81) });
expect(r.success).toBe(false);
});
});