test: dedupe fixtures and test harness setup

This commit is contained in:
Peter Steinberger
2026-02-23 05:43:30 +00:00
parent 8af19ddc5b
commit 1c753ea786
75 changed files with 1886 additions and 2136 deletions

View File

@@ -107,6 +107,60 @@ function createOverrideFailureRun(params: {
});
}
function makeSingleProviderStore(params: {
provider: string;
usageStat: NonNullable<AuthProfileStore["usageStats"]>[string];
}): AuthProfileStore {
const profileId = `${params.provider}:default`;
return {
version: AUTH_STORE_VERSION,
profiles: {
[profileId]: {
type: "api_key",
provider: params.provider,
key: "test-key",
},
},
usageStats: {
[profileId]: params.usageStat,
},
};
}
function createFallbackOnlyRun() {
return vi.fn().mockImplementation(async (providerId, modelId) => {
if (providerId === "fallback") {
return "ok";
}
throw new Error(`unexpected provider: ${providerId}/${modelId}`);
});
}
async function expectSkippedUnavailableProvider(params: {
providerPrefix: string;
usageStat: NonNullable<AuthProfileStore["usageStats"]>[string];
expectedReason: string;
}) {
const provider = `${params.providerPrefix}-${crypto.randomUUID()}`;
const cfg = makeProviderFallbackCfg(provider);
const store = makeSingleProviderStore({
provider,
usageStat: params.usageStat,
});
const run = createFallbackOnlyRun();
const result = await runWithStoredAuth({
cfg,
store,
provider,
run,
});
expect(result.result).toBe("ok");
expect(run.mock.calls).toEqual([["fallback", "ok-model"]]);
expect(result.attempts[0]?.reason).toBe(params.expectedReason);
}
describe("runWithModelFallback", () => {
it("normalizes openai gpt-5.3 codex to openai-codex before running", async () => {
const cfg = makeCfg();
@@ -310,86 +364,26 @@ describe("runWithModelFallback", () => {
});
it("skips providers when all profiles are in cooldown", async () => {
const provider = `cooldown-test-${crypto.randomUUID()}`;
const profileId = `${provider}:default`;
const store: AuthProfileStore = {
version: AUTH_STORE_VERSION,
profiles: {
[profileId]: {
type: "api_key",
provider,
key: "test-key",
},
await expectSkippedUnavailableProvider({
providerPrefix: "cooldown-test",
usageStat: {
cooldownUntil: Date.now() + 5 * 60_000,
},
usageStats: {
[profileId]: {
cooldownUntil: Date.now() + 5 * 60_000,
},
},
};
const cfg = makeProviderFallbackCfg(provider);
const run = vi.fn().mockImplementation(async (providerId, modelId) => {
if (providerId === "fallback") {
return "ok";
}
throw new Error(`unexpected provider: ${providerId}/${modelId}`);
expectedReason: "rate_limit",
});
const result = await runWithStoredAuth({
cfg,
store,
provider,
run,
});
expect(result.result).toBe("ok");
expect(run.mock.calls).toEqual([["fallback", "ok-model"]]);
expect(result.attempts[0]?.reason).toBe("rate_limit");
});
it("propagates disabled reason when all profiles are unavailable", async () => {
const provider = `disabled-test-${crypto.randomUUID()}`;
const profileId = `${provider}:default`;
const now = Date.now();
const store: AuthProfileStore = {
version: AUTH_STORE_VERSION,
profiles: {
[profileId]: {
type: "api_key",
provider,
key: "test-key",
},
await expectSkippedUnavailableProvider({
providerPrefix: "disabled-test",
usageStat: {
disabledUntil: now + 5 * 60_000,
disabledReason: "billing",
failureCounts: { rate_limit: 4 },
},
usageStats: {
[profileId]: {
disabledUntil: now + 5 * 60_000,
disabledReason: "billing",
failureCounts: { rate_limit: 4 },
},
},
};
const cfg = makeProviderFallbackCfg(provider);
const run = vi.fn().mockImplementation(async (providerId, modelId) => {
if (providerId === "fallback") {
return "ok";
}
throw new Error(`unexpected provider: ${providerId}/${modelId}`);
expectedReason: "billing",
});
const result = await runWithStoredAuth({
cfg,
store,
provider,
run,
});
expect(result.result).toBe("ok");
expect(run.mock.calls).toEqual([["fallback", "ok-model"]]);
expect(result.attempts[0]?.reason).toBe("billing");
});
it("does not skip when any profile is available", async () => {