test(modules): stub the module gate in doctor-report and export suites

The v1.18.0 per-user module gate reads the database to resolve a user's module map. Three pre-existing aggregator/route test suites — the doctor-report PDF route, the health-record export route, and the export soft-delete filter — exercise paths that now call resolveModuleMap / requireModuleEnabled but never mocked the gate, so its DB read threw and the route returned 500. Stub the gate to all-modules-enabled (an empty map is default-on) in each file, re-asserting the return values in the mock-resetting beforeEach, mirroring the snapshot suite's existing pattern.
This commit is contained in:
Marc-André Bombeck committed 2026-06-15 22:04:07 +02:00
1 parent 6bde45ce0a
commit dceac65ab8
3 files changed
+69

No files matched your search

@@ -39,11 +39,36 @@ vi.mock("@/lib/logging/transports", () => ({
emitIfSampled: vi.fn(),
}));
// v1.18.0 — the doctor-report aggregator resolves the per-user module map
// so a disabled data-domain module never reaches the report. Stub the gate
// to "all modules enabled" (an empty map ⇒ default-on) so these pre-existing
// route tests don't stand up the real gate's DB reads.
vi.mock("@/lib/modules/gate", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/modules/gate")>();
return {
...actual,
resolveModuleMap: vi.fn(),
isModuleEnabled: vi.fn(),
requireModuleEnabled: vi.fn(),
};
});
import { POST } from "../pdf/route";
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { checkRateLimit } from "@/lib/rate-limit";
import { auditLog } from "@/lib/auth/audit";
import {
resolveModuleMap,
isModuleEnabled,
requireModuleEnabled,
} from "@/lib/modules/gate";
function setModuleGateAllEnabled() {
vi.mocked(resolveModuleMap).mockResolvedValue({} as never);
vi.mocked(isModuleEnabled).mockResolvedValue(true);
vi.mocked(requireModuleEnabled).mockResolvedValue({ enabled: true } as never);
}
function makeRequest(
body?: Record<string, unknown>,
@@ -86,6 +111,7 @@ function setEmptyDataMocks() {
describe("POST /api/doctor-report/pdf", () => {
beforeEach(() => {
vi.resetAllMocks();
setModuleGateAllEnabled();
});
it("returns 401 when not authenticated", async () => {
@@ -51,9 +51,27 @@ vi.mock("@/lib/tz/resolver", () => ({
resolveUserTimezone: vi.fn().mockResolvedValue("Europe/Berlin"),
}));
// v1.18.0 — the achievements route gates on `requireModuleEnabled`. Stub
// the gate to "all modules enabled" (an empty map ⇒ default-on) so this
// pre-existing soft-delete test doesn't stand up the real gate's DB reads.
vi.mock("@/lib/modules/gate", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/modules/gate")>();
return {
...actual,
resolveModuleMap: vi.fn(),
isModuleEnabled: vi.fn(),
requireModuleEnabled: vi.fn(),
};
});
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { checkRateLimit } from "@/lib/rate-limit";
import {
resolveModuleMap,
isModuleEnabled,
requireModuleEnabled,
} from "@/lib/modules/gate";
const SESSION_OK = {
user: {
@@ -69,6 +87,9 @@ function mkReq(url: string): NextRequest {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(resolveModuleMap).mockResolvedValue({} as never);
vi.mocked(isModuleEnabled).mockResolvedValue(true);
vi.mocked(requireModuleEnabled).mockResolvedValue({ enabled: true } as never);
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(checkRateLimit).mockResolvedValue({
allowed: true,
@@ -32,9 +32,28 @@ vi.mock("@/lib/tz/resolver", () => ({
resolveUserTimezone: vi.fn().mockResolvedValue("Europe/Berlin"),
}));
// v1.18.0 — the health-record aggregator resolves the per-user module map
// so a disabled data-domain module never reaches the export. Stub the gate
// to "all modules enabled" (an empty map ⇒ default-on) so these pre-existing
// route tests don't stand up the real gate's DB reads.
vi.mock("@/lib/modules/gate", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/modules/gate")>();
return {
...actual,
resolveModuleMap: vi.fn(),
isModuleEnabled: vi.fn(),
requireModuleEnabled: vi.fn(),
};
});
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { checkRateLimit } from "@/lib/rate-limit";
import {
resolveModuleMap,
isModuleEnabled,
requireModuleEnabled,
} from "@/lib/modules/gate";
const SESSION_OK = {
user: { id: "user-1", email: "test@example.com", role: "USER" },
@@ -63,6 +82,9 @@ async function pdfText(res: Response): Promise<string> {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(resolveModuleMap).mockResolvedValue({} as never);
vi.mocked(isModuleEnabled).mockResolvedValue(true);
vi.mocked(requireModuleEnabled).mockResolvedValue({ enabled: true } as never);
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(checkRateLimit).mockResolvedValue({
allowed: true,