mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 07:20:23 +00:00
refactor(test): dedupe agent and discord test fixtures
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { AuthProfileStore } from "./auth-profiles.js";
|
||||
import { makeModelFallbackCfg } from "./test-helpers/model-fallback-config-fixture.js";
|
||||
|
||||
// Mock auth-profiles module — must be before importing model-fallback
|
||||
vi.mock("./auth-profiles.js", () => ({
|
||||
@@ -23,19 +24,7 @@ const mockedGetSoonestCooldownExpiry = vi.mocked(getSoonestCooldownExpiry);
|
||||
const mockedIsProfileInCooldown = vi.mocked(isProfileInCooldown);
|
||||
const mockedResolveAuthProfileOrder = vi.mocked(resolveAuthProfileOrder);
|
||||
|
||||
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 expectFallbackUsed(
|
||||
result: { result: unknown; attempts: Array<{ reason?: string }> },
|
||||
@@ -50,10 +39,34 @@ function expectFallbackUsed(
|
||||
expect(result.attempts[0]?.reason).toBe("rate_limit");
|
||||
}
|
||||
|
||||
function expectPrimaryProbeSuccess(
|
||||
result: { result: unknown },
|
||||
run: {
|
||||
(...args: unknown[]): unknown;
|
||||
mock: { calls: unknown[][] };
|
||||
},
|
||||
expectedResult: unknown,
|
||||
) {
|
||||
expect(result.result).toBe(expectedResult);
|
||||
expect(run).toHaveBeenCalledTimes(1);
|
||||
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
||||
}
|
||||
|
||||
describe("runWithModelFallback – probe logic", () => {
|
||||
let realDateNow: () => number;
|
||||
const NOW = 1_700_000_000_000;
|
||||
|
||||
const runPrimaryCandidate = (
|
||||
cfg: OpenClawConfig,
|
||||
run: (provider: string, model: string) => Promise<unknown>,
|
||||
) =>
|
||||
runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
realDateNow = Date.now;
|
||||
Date.now = vi.fn(() => NOW);
|
||||
@@ -100,12 +113,7 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("ok");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
|
||||
// Should skip primary and use fallback
|
||||
expectFallbackUsed(result, run);
|
||||
@@ -119,17 +127,8 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("probed-ok");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
// Should probe primary and succeed
|
||||
expect(result.result).toBe("probed-ok");
|
||||
expect(run).toHaveBeenCalledTimes(1);
|
||||
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
expectPrimaryProbeSuccess(result, run, "probed-ok");
|
||||
});
|
||||
|
||||
it("probes primary model when cooldown already expired", async () => {
|
||||
@@ -140,16 +139,8 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("recovered");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
expect(result.result).toBe("recovered");
|
||||
expect(run).toHaveBeenCalledTimes(1);
|
||||
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
expectPrimaryProbeSuccess(result, run, "recovered");
|
||||
});
|
||||
|
||||
it("does NOT probe non-primary candidates during cooldown", async () => {
|
||||
@@ -203,12 +194,7 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("ok");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
|
||||
// Should be throttled → skip primary, use fallback
|
||||
expectFallbackUsed(result, run);
|
||||
@@ -224,16 +210,8 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("probed-ok");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
expect(result.result).toBe("probed-ok");
|
||||
expect(run).toHaveBeenCalledTimes(1);
|
||||
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
expectPrimaryProbeSuccess(result, run, "probed-ok");
|
||||
});
|
||||
|
||||
it("handles non-finite soonest safely (treats as probe-worthy)", async () => {
|
||||
@@ -244,15 +222,8 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("ok-infinity");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
expect(result.result).toBe("ok-infinity");
|
||||
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
expectPrimaryProbeSuccess(result, run, "ok-infinity");
|
||||
});
|
||||
|
||||
it("handles NaN soonest safely (treats as probe-worthy)", async () => {
|
||||
@@ -262,15 +233,8 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("ok-nan");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
expect(result.result).toBe("ok-nan");
|
||||
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
expectPrimaryProbeSuccess(result, run, "ok-nan");
|
||||
});
|
||||
|
||||
it("handles null soonest safely (treats as probe-worthy)", async () => {
|
||||
@@ -280,15 +244,8 @@ describe("runWithModelFallback – probe logic", () => {
|
||||
|
||||
const run = vi.fn().mockResolvedValue("ok-null");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
expect(result.result).toBe("ok-null");
|
||||
expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini");
|
||||
const result = await runPrimaryCandidate(cfg, run);
|
||||
expectPrimaryProbeSuccess(result, run, "ok-null");
|
||||
});
|
||||
|
||||
it("single candidate skips with rate_limit and exhausts candidates", async () => {
|
||||
|
||||
Reference in New Issue
Block a user