rebreak-monorepo/backend/tests/profile/cooldown-history.get.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

97 lines
2.7 KiB
TypeScript

/**
* Tests for cooldown-history shape — status-derivation + pagination cursor.
*/
import { describe, expect, it } from "vitest";
type CooldownRow = {
id: string;
reason: string | null;
cooldownStartedAt: Date;
cooldownEndsAt: Date;
resolvedAt: Date | null;
cancelledAt: Date | null;
};
// Inline-replication of the endpoint's status-derivation logic — keeps test
// fast and independent of full nitro-event-handler boot.
function deriveStatus(r: CooldownRow): "active" | "resolved" | "cancelled" {
if (r.cancelledAt) return "cancelled";
if (r.resolvedAt || r.cooldownEndsAt <= new Date()) return "resolved";
return "active";
}
describe("cooldown-history status derivation", () => {
it("returns 'cancelled' if cancelledAt is set", () => {
expect(
deriveStatus({
id: "1",
reason: null,
cooldownStartedAt: new Date("2026-04-01"),
cooldownEndsAt: new Date("2026-04-02"),
resolvedAt: null,
cancelledAt: new Date("2026-04-01T12:00"),
}),
).toBe("cancelled");
});
it("returns 'resolved' if resolvedAt is set", () => {
expect(
deriveStatus({
id: "1",
reason: null,
cooldownStartedAt: new Date("2026-04-01"),
cooldownEndsAt: new Date("2026-04-02"),
resolvedAt: new Date("2026-04-02"),
cancelledAt: null,
}),
).toBe("resolved");
});
it("returns 'resolved' if cooldownEndsAt is in the past (auto-resolved)", () => {
expect(
deriveStatus({
id: "1",
reason: null,
cooldownStartedAt: new Date("2026-01-01"),
cooldownEndsAt: new Date("2026-01-02"),
resolvedAt: null,
cancelledAt: null,
}),
).toBe("resolved");
});
it("returns 'active' if cooldownEndsAt is in the future + nothing set", () => {
const future = new Date(Date.now() + 24 * 60 * 60 * 1000);
expect(
deriveStatus({
id: "1",
reason: null,
cooldownStartedAt: new Date(),
cooldownEndsAt: future,
resolvedAt: null,
cancelledAt: null,
}),
).toBe("active");
});
});
describe("cooldown-history pagination math", () => {
it("computes durationMinutes correctly", () => {
const start = new Date("2026-04-01T10:00:00Z");
const end = new Date("2026-04-01T22:00:00Z");
const minutes = Math.round((end.getTime() - start.getTime()) / 60_000);
expect(minutes).toBe(720); // 12h
});
it("clamps limit to MAX_LIMIT=50", () => {
const requested = 9999;
const limit = Math.min(50, Math.max(1, requested));
expect(limit).toBe(50);
});
it("falls back to default 20 when limit is missing/invalid", () => {
const limit = Math.min(50, Math.max(1, parseInt("" as string, 10) || 20));
expect(limit).toBe(20);
});
});