refactor: share fallback skip assertions

This commit is contained in:
Peter Steinberger 2026-03-13 19:07:08 +00:00
parent 0652b885df
commit ad52724d9a
1 changed files with 16 additions and 10 deletions

View File

@ -46,6 +46,20 @@ function expectFallbackUsed(
expect(result.attempts[0]?.reason).toBe("rate_limit"); expect(result.attempts[0]?.reason).toBe("rate_limit");
} }
function expectPrimarySkippedForReason(
result: { result: unknown; attempts: Array<{ reason?: string }> },
run: {
(...args: unknown[]): unknown;
mock: { calls: unknown[][] };
},
reason: string,
) {
expect(result.result).toBe("ok");
expect(run).toHaveBeenCalledTimes(1);
expect(run).toHaveBeenCalledWith("anthropic", "claude-haiku-3-5");
expect(result.attempts[0]?.reason).toBe(reason);
}
function expectPrimaryProbeSuccess( function expectPrimaryProbeSuccess(
result: { result: unknown }, result: { result: unknown },
run: { run: {
@ -183,11 +197,7 @@ describe("runWithModelFallback probe logic", () => {
const run = vi.fn().mockResolvedValue("ok"); const run = vi.fn().mockResolvedValue("ok");
const result = await runPrimaryCandidate(cfg, run); const result = await runPrimaryCandidate(cfg, run);
expectPrimarySkippedForReason(result, run, "billing");
expect(result.result).toBe("ok");
expect(run).toHaveBeenCalledTimes(1);
expect(run).toHaveBeenCalledWith("anthropic", "claude-haiku-3-5");
expect(result.attempts[0]?.reason).toBe("billing");
}); });
it("probes primary model when within 2-min margin of cooldown expiry", async () => { it("probes primary model when within 2-min margin of cooldown expiry", async () => {
@ -540,10 +550,6 @@ describe("runWithModelFallback probe logic", () => {
const run = vi.fn().mockResolvedValue("ok"); const run = vi.fn().mockResolvedValue("ok");
const result = await runPrimaryCandidate(cfg, run); const result = await runPrimaryCandidate(cfg, run);
expectPrimarySkippedForReason(result, run, "billing");
expect(result.result).toBe("ok");
expect(run).toHaveBeenCalledTimes(1);
expect(run).toHaveBeenCalledWith("anthropic", "claude-haiku-3-5");
expect(result.attempts[0]?.reason).toBe("billing");
}); });
}); });