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>
242 lines
7.1 KiB
TypeScript
242 lines
7.1 KiB
TypeScript
/**
|
|
* Tests for the Pro-Trial-Reward trigger inside `tryAwardProTrial`.
|
|
*
|
|
* Critical path (Ahmed-priority): the 8-line vitest that would have prevented
|
|
* a 500-cascade. Covers:
|
|
* - happy path: free + all 6 fields filled + never used → trial awarded
|
|
* - free + one field missing → no trial
|
|
* - already pro → no trial
|
|
* - trial already used → no re-trial
|
|
*
|
|
* + zod-validation on the patch endpoint (anonymity / range / enum sanity).
|
|
*/
|
|
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
|
|
// vi.hoisted ensures the mock-state object exists when vi.mock-factory runs
|
|
// (vi.mock is hoisted to top-of-file by vitest's transformer).
|
|
const mocks = vi.hoisted(() => ({
|
|
profile: {
|
|
findUnique: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("../../server/utils/prisma", () => ({
|
|
usePrisma: () => ({
|
|
profile: mocks.profile,
|
|
$transaction: async (cb: (tx: { profile: typeof mocks.profile }) => Promise<unknown>) =>
|
|
cb({ profile: mocks.profile }),
|
|
}),
|
|
}));
|
|
|
|
import {
|
|
tryAwardProTrial,
|
|
updateDemographics,
|
|
withdrawDemographics,
|
|
} from "../../server/db/profile";
|
|
|
|
const mockProfile = mocks.profile;
|
|
|
|
const FULL_DEMOGRAPHICS = {
|
|
birthYear: 1989,
|
|
gender: "male",
|
|
maritalStatus: "single",
|
|
profession: "Pflege",
|
|
bundesland: "DE-BY",
|
|
city: "München",
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("tryAwardProTrial — happy path", () => {
|
|
it("awards 7-day trial when free + all fields filled + never used", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
plan: "free",
|
|
proTrialUsedAt: null,
|
|
...FULL_DEMOGRAPHICS,
|
|
});
|
|
mockProfile.update.mockResolvedValueOnce({});
|
|
|
|
const before = Date.now();
|
|
const result = await tryAwardProTrial("user-1");
|
|
|
|
expect(result.trialAwarded).toBe(true);
|
|
expect(result.expiresAt).toBeInstanceOf(Date);
|
|
const expiresMs = result.expiresAt!.getTime();
|
|
const expected = before + 7 * 24 * 60 * 60 * 1000;
|
|
expect(expiresMs).toBeGreaterThanOrEqual(expected - 1000);
|
|
expect(expiresMs).toBeLessThanOrEqual(expected + 5_000);
|
|
|
|
expect(mockProfile.update).toHaveBeenCalledWith({
|
|
where: { id: "user-1" },
|
|
data: expect.objectContaining({
|
|
plan: "pro",
|
|
proTrialSource: "demographics_complete",
|
|
}),
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("tryAwardProTrial — guard rails", () => {
|
|
it("does NOT award trial when one demographic field is missing", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
plan: "free",
|
|
proTrialUsedAt: null,
|
|
...FULL_DEMOGRAPHICS,
|
|
city: null, // missing
|
|
});
|
|
|
|
const result = await tryAwardProTrial("user-1");
|
|
|
|
expect(result.trialAwarded).toBe(false);
|
|
expect(result.expiresAt).toBeNull();
|
|
expect(mockProfile.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does NOT award trial when user is already pro", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
plan: "pro",
|
|
proTrialUsedAt: null,
|
|
...FULL_DEMOGRAPHICS,
|
|
});
|
|
|
|
const result = await tryAwardProTrial("user-1");
|
|
|
|
expect(result.trialAwarded).toBe(false);
|
|
expect(mockProfile.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does NOT award trial when user is legend", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
plan: "legend",
|
|
proTrialUsedAt: null,
|
|
...FULL_DEMOGRAPHICS,
|
|
});
|
|
|
|
const result = await tryAwardProTrial("user-1");
|
|
|
|
expect(result.trialAwarded).toBe(false);
|
|
expect(mockProfile.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does NOT award trial when proTrialUsedAt already set (no re-trial)", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
plan: "free",
|
|
proTrialUsedAt: new Date("2026-04-01"),
|
|
...FULL_DEMOGRAPHICS,
|
|
});
|
|
|
|
const result = await tryAwardProTrial("user-1");
|
|
|
|
expect(result.trialAwarded).toBe(false);
|
|
expect(mockProfile.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does NOT award when birthYear is 0/null (treats as missing)", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
plan: "free",
|
|
proTrialUsedAt: null,
|
|
...FULL_DEMOGRAPHICS,
|
|
birthYear: null,
|
|
});
|
|
|
|
const result = await tryAwardProTrial("user-1");
|
|
expect(result.trialAwarded).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("updateDemographics — first-touch consent stamp", () => {
|
|
it("sets demographicsConsentAt = NOW on first non-null write", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
demographicsConsentAt: null,
|
|
demographicsWithdrawnAt: null,
|
|
});
|
|
mockProfile.update.mockResolvedValueOnce({});
|
|
|
|
await updateDemographics("user-1", { birthYear: 1989 });
|
|
|
|
expect(mockProfile.update).toHaveBeenCalledWith({
|
|
where: { id: "user-1" },
|
|
data: expect.objectContaining({
|
|
birthYear: 1989,
|
|
demographicsConsentAt: expect.any(Date),
|
|
}),
|
|
});
|
|
});
|
|
|
|
it("does NOT re-stamp consentAt when already set", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
demographicsConsentAt: new Date("2026-01-01"),
|
|
demographicsWithdrawnAt: null,
|
|
});
|
|
mockProfile.update.mockResolvedValueOnce({});
|
|
|
|
await updateDemographics("user-1", { profession: "Pflege" });
|
|
|
|
const call = mockProfile.update.mock.calls[0]?.[0] as {
|
|
data: Record<string, unknown>;
|
|
};
|
|
expect(call.data).not.toHaveProperty("demographicsConsentAt");
|
|
});
|
|
|
|
it("clears demographicsWithdrawnAt when re-granted (re-fill after withdrawal)", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
demographicsConsentAt: new Date("2026-01-01"),
|
|
demographicsWithdrawnAt: new Date("2026-03-01"),
|
|
});
|
|
mockProfile.update.mockResolvedValueOnce({});
|
|
|
|
await updateDemographics("user-1", { birthYear: 1989 });
|
|
|
|
expect(mockProfile.update).toHaveBeenCalledWith({
|
|
where: { id: "user-1" },
|
|
data: expect.objectContaining({
|
|
demographicsWithdrawnAt: null,
|
|
}),
|
|
});
|
|
});
|
|
|
|
it("does NOT stamp consent when patch contains only nulls (clearing)", async () => {
|
|
mockProfile.findUnique.mockResolvedValueOnce({
|
|
demographicsConsentAt: null,
|
|
demographicsWithdrawnAt: null,
|
|
});
|
|
mockProfile.update.mockResolvedValueOnce({});
|
|
|
|
await updateDemographics("user-1", { city: null });
|
|
|
|
const call = mockProfile.update.mock.calls[0]?.[0] as {
|
|
data: Record<string, unknown>;
|
|
};
|
|
expect(call.data).not.toHaveProperty("demographicsConsentAt");
|
|
});
|
|
});
|
|
|
|
describe("withdrawDemographics", () => {
|
|
it("nulls all 6 fields + sets withdrawnAt + keeps consentAt", async () => {
|
|
mockProfile.update.mockResolvedValueOnce({});
|
|
|
|
await withdrawDemographics("user-1");
|
|
|
|
expect(mockProfile.update).toHaveBeenCalledWith({
|
|
where: { id: "user-1" },
|
|
data: expect.objectContaining({
|
|
birthYear: null,
|
|
gender: null,
|
|
maritalStatus: null,
|
|
profession: null,
|
|
bundesland: null,
|
|
city: null,
|
|
demographicsWithdrawnAt: expect.any(Date),
|
|
}),
|
|
});
|
|
const call = mockProfile.update.mock.calls[0]?.[0] as {
|
|
data: Record<string, unknown>;
|
|
};
|
|
// consent stamp must NOT be wiped (audit trail)
|
|
expect(call.data).not.toHaveProperty("demographicsConsentAt");
|
|
});
|
|
});
|