test(workouts): count the source-priority read by shape, not by call total

The batch route now also resolves the user's timezone and module map through
the arrival seam, so a raw call count on user.findUnique no longer isolates
the read this guard is about. Filtering by the SELECT shape keeps the
once-per-batch contract pinned while letting the unrelated cached lookups
through.
This commit is contained in:
Marc-André Bombeck committed 2026-07-19 13:40:18 +02:00
1 parent ccd3c5e68f
commit f9112925d2
3 files changed
+18 -4

No files matched your search

@@ -524,7 +524,19 @@ describe("POST /api/workouts/batch — user source-priority (v1.4.43 W9)", () =>
}),
);
expect(res.status).toBe(200);
expect(prisma.user.findUnique).toHaveBeenCalledTimes(1);
// Counted by SELECT shape rather than by raw call count: the route also
// resolves the user's timezone and module map through the arrival seam,
// which are separate cached lookups. The guard here is that the
// source-priority blob is read ONCE PER BATCH and never once per workout —
// two workouts must still produce exactly one read.
const sourcePriorityReads = vi
.mocked(prisma.user.findUnique)
.mock.calls.filter(
(call) =>
(call[0] as { select?: Record<string, unknown> })?.select
?.sourcePriorityJson === true,
);
expect(sourcePriorityReads).toHaveLength(1);
expect(prisma.user.findUnique).toHaveBeenCalledWith({
where: { id: "user-1" },
select: { sourcePriorityJson: true },
@@ -25,7 +25,9 @@
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
const sendMock = vi.fn(async () => "job-id");
const sendMock = vi.fn(
async (_queue: string, _payload: unknown, _opts: unknown) => "job-id",
);
const annotateMock = vi.fn();
let moduleMap: Record<string, boolean> = {};
let timezone = "Europe/Berlin";
@@ -29,8 +29,8 @@ vi.mock("@/lib/jobs/reminder/shared", () => ({
workerLog: vi.fn(),
}));
const createMany = vi.fn(async () => ({ count: 1 }));
const updateMany = vi.fn(async () => ({ count: 1 }));
const createMany = vi.fn(async (_args: unknown) => ({ count: 1 }));
const updateMany = vi.fn(async (_args: unknown) => ({ count: 1 }));
const fakePrisma = { arrivalReaction: { createMany, updateMany } };
const { runDataArrival } = await import("../data-arrival");