test: give every case its own mock state under a shuffled order

Cases leaned on what an earlier case set up or left behind: a once-value
the code under test never consumed, an implementation swapped in and
kept by clearAllMocks, a payload only another block's setup provided,
the zone resolver's per-user cache, module state in a hoisted object,
and a scope set one case discovered for the next. Each now resets or
states what it reads.
This commit is contained in:
Marc-André Bombeck committed 2026-11-03 20:58:38 +01:00
1 parent 4f7802ba28
commit 227c80878c
11 files changed
+90 -37

No files matched your search

@@ -60,6 +60,14 @@ import { GET } from "../route";
import { prisma } from "@/lib/db";
import { requireAdmin } from "@/lib/api-handler";
// Reset, not clear, before every case: cases replace the paused rows, the
// settings row and the account groups, and a cleared mock would hand the
// next case whatever the last one left there. A reset mock goes back to the
// empty answer it was created with.
beforeEach(() => {
vi.resetAllMocks();
});
const groupBy = vi.mocked(prisma.providerHealth.groupBy);
const usageRows = vi.mocked(prisma.coachUsage.findMany);
@@ -101,7 +109,6 @@ function group(
describe("GET /api/admin/provider-health", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(requireAdmin).mockResolvedValue({
user: { id: "admin1" },
} as never);
@@ -302,7 +309,6 @@ describe("GET /api/admin/provider-health — pause and model check (v1.42)", ()
const settings = vi.mocked(prisma.appSettings.findUnique);
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(requireAdmin).mockResolvedValue({
user: { id: "admin" },
} as never);
@@ -131,7 +131,10 @@ function putReq(
}
beforeEach(() => {
vi.clearAllMocks();
// Reset, not clear: cases replace the profile reads and the question store
// with their own implementations, and a cleared mock keeps them. A reset
// mock returns to the implementation it was created with.
vi.resetAllMocks();
requireModuleEnabled.mockResolvedValue({ enabled: true });
});
@@ -174,6 +174,16 @@ beforeEach(() => {
vi.mocked(isModuleEnabled).mockResolvedValue(true);
vi.mocked(requireModuleEnabled).mockResolvedValue({ enabled: true } as never);
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
// A session that signed in just now. Reset, not only cleared: a case below
// swaps in a stale session, and `clearAllMocks` keeps the implementation
// it left behind for whichever case runs next.
vi.mocked(prisma.session.findUnique)
.mockReset()
.mockImplementation((async () => ({
createdAt: new Date(),
mfaVerifiedAt: null,
reproofAt: null,
})) as never);
vi.mocked(checkRateLimit).mockResolvedValue({
allowed: true,
limit: 60,
@@ -87,6 +87,15 @@ beforeEach(() => {
vi.clearAllMocks();
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(validateAttachmentCandidate).mockResolvedValue({ ok: true });
// Open again for every case: the detach case closes every capability, and
// `clearAllMocks` would carry that refusal into whichever case runs next.
vi.mocked(requireAiCapability)
.mockReset()
.mockResolvedValue({
available: true,
reason: null,
onDeviceAllowed: true,
} as never);
});
describe("POST attach", () => {
@@ -276,7 +276,10 @@ function chatReq(body: Record<string, unknown>): Request {
describe("coach chat — tool-mode routing (F1)", () => {
beforeEach(() => {
vi.clearAllMocks();
// Reset, not clear: cases swap in their own tool loop and parsers, and a
// cleared mock would hand those to the next case. Every mock here goes
// back to the implementation it was created with.
vi.resetAllMocks();
reserveBudget.mockResolvedValue({
allowed: true,
reserved: 3000,