refactor(test): dedupe agent and discord test fixtures

This commit is contained in:
Peter Steinberger
2026-02-22 20:01:43 +00:00
parent 5547a2275c
commit 3c75bc0e41
26 changed files with 632 additions and 737 deletions

View File

@@ -8,20 +8,9 @@ import type { AuthProfileStore } from "./auth-profiles.js";
import { saveAuthProfileStore } from "./auth-profiles.js";
import { AUTH_STORE_VERSION } from "./auth-profiles/constants.js";
import { runWithModelFallback } from "./model-fallback.js";
import { makeModelFallbackCfg } from "./test-helpers/model-fallback-config-fixture.js";
function makeCfg(overrides: Partial<OpenClawConfig> = {}): OpenClawConfig {
return {
agents: {
defaults: {
model: {
primary: "openai/gpt-4.1-mini",
fallbacks: ["anthropic/claude-haiku-3-5"],
},
},
},
...overrides,
} as OpenClawConfig;
}
const makeCfg = makeModelFallbackCfg;
function makeFallbacksOnlyCfg(): OpenClawConfig {
return {
@@ -99,6 +88,24 @@ async function expectFallsBackToHaiku(params: {
expect(run.mock.calls[1]?.[1]).toBe("claude-haiku-3-5");
}
function createOverrideFailureRun(params: {
overrideProvider: string;
overrideModel: string;
fallbackProvider: string;
fallbackModel: string;
firstError: Error;
}) {
return vi.fn().mockImplementation(async (provider, model) => {
if (provider === params.overrideProvider && model === params.overrideModel) {
throw params.firstError;
}
if (provider === params.fallbackProvider && model === params.fallbackModel) {
return "ok";
}
throw new Error(`unexpected fallback candidate: ${provider}/${model}`);
});
}
describe("runWithModelFallback", () => {
it("normalizes openai gpt-5.3 codex to openai-codex before running", async () => {
const cfg = makeCfg();
@@ -151,14 +158,12 @@ describe("runWithModelFallback", () => {
},
});
const run = vi.fn().mockImplementation(async (provider, model) => {
if (provider === "anthropic" && model === "claude-opus-4-5") {
throw Object.assign(new Error("unauthorized"), { status: 401 });
}
if (provider === "openai" && model === "gpt-4.1-mini") {
return "ok";
}
throw new Error(`unexpected fallback candidate: ${provider}/${model}`);
const run = createOverrideFailureRun({
overrideProvider: "anthropic",
overrideModel: "claude-opus-4-5",
fallbackProvider: "openai",
fallbackModel: "gpt-4.1-mini",
firstError: Object.assign(new Error("unauthorized"), { status: 401 }),
});
const result = await runWithModelFallback({
@@ -238,14 +243,12 @@ describe("runWithModelFallback", () => {
it("falls back to configured primary for override credential validation errors", async () => {
const cfg = makeCfg();
const run = vi.fn().mockImplementation(async (provider, model) => {
if (provider === "anthropic" && model === "claude-opus-4") {
throw new Error('No credentials found for profile "anthropic:default".');
}
if (provider === "openai" && model === "gpt-4.1-mini") {
return "ok";
}
throw new Error(`unexpected fallback candidate: ${provider}/${model}`);
const run = createOverrideFailureRun({
overrideProvider: "anthropic",
overrideModel: "claude-opus-4",
fallbackProvider: "openai",
fallbackModel: "gpt-4.1-mini",
firstError: new Error('No credentials found for profile "anthropic:default".'),
});
const result = await runWithModelFallback({