mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 05:47:39 +00:00
test(agents): dedupe auth profile rotation fixture setup
This commit is contained in:
@@ -12,6 +12,14 @@ vi.mock("./pi-embedded-runner/run/attempt.js", () => ({
|
|||||||
runEmbeddedAttempt: (params: unknown) => runEmbeddedAttemptMock(params),
|
runEmbeddedAttempt: (params: unknown) => runEmbeddedAttemptMock(params),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
vi.mock("./models-config.js", async (importOriginal) => {
|
||||||
|
const mod = await importOriginal<typeof import("./models-config.js")>();
|
||||||
|
return {
|
||||||
|
...mod,
|
||||||
|
ensureOpenClawModelsJson: vi.fn(async () => ({ wrote: false })),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
let runEmbeddedPiAgent: typeof import("./pi-embedded-runner.js").runEmbeddedPiAgent;
|
let runEmbeddedPiAgent: typeof import("./pi-embedded-runner.js").runEmbeddedPiAgent;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
@@ -235,6 +243,19 @@ async function withTimedAgentWorkspace<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function withAgentWorkspace<T>(
|
||||||
|
run: (ctx: { agentDir: string; workspaceDir: string }) => Promise<T>,
|
||||||
|
) {
|
||||||
|
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
||||||
|
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
||||||
|
try {
|
||||||
|
return await run({ agentDir, workspaceDir });
|
||||||
|
} finally {
|
||||||
|
await fs.rm(agentDir, { recursive: true, force: true });
|
||||||
|
await fs.rm(workspaceDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function runTurnWithCooldownSeed(params: {
|
async function runTurnWithCooldownSeed(params: {
|
||||||
sessionKey: string;
|
sessionKey: string;
|
||||||
runId: string;
|
runId: string;
|
||||||
@@ -288,9 +309,7 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
|
|
||||||
for (const testCase of cases) {
|
for (const testCase of cases) {
|
||||||
runEmbeddedAttemptMock.mockClear();
|
runEmbeddedAttemptMock.mockClear();
|
||||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
|
||||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
|
||||||
try {
|
|
||||||
await writeAuthStore(agentDir);
|
await writeAuthStore(agentDir);
|
||||||
mockFailedThenSuccessfulAttempt(testCase.errorMessage);
|
mockFailedThenSuccessfulAttempt(testCase.errorMessage);
|
||||||
await runAutoPinnedOpenAiTurn({
|
await runAutoPinnedOpenAiTurn({
|
||||||
@@ -302,17 +321,12 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
|
|
||||||
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(2);
|
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(2);
|
||||||
await expectProfileP2UsageUpdated(agentDir);
|
await expectProfileP2UsageUpdated(agentDir);
|
||||||
} finally {
|
});
|
||||||
await fs.rm(agentDir, { recursive: true, force: true });
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not rotate for compaction timeouts", async () => {
|
it("does not rotate for compaction timeouts", async () => {
|
||||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
|
||||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
|
||||||
try {
|
|
||||||
await writeAuthStore(agentDir);
|
await writeAuthStore(agentDir);
|
||||||
|
|
||||||
runEmbeddedAttemptMock.mockResolvedValueOnce(
|
runEmbeddedAttemptMock.mockResolvedValueOnce(
|
||||||
@@ -348,16 +362,11 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
expect(result.meta.aborted).toBe(true);
|
expect(result.meta.aborted).toBe(true);
|
||||||
|
|
||||||
await expectProfileP2UsageUnchanged(agentDir);
|
await expectProfileP2UsageUnchanged(agentDir);
|
||||||
} finally {
|
});
|
||||||
await fs.rm(agentDir, { recursive: true, force: true });
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not rotate for user-pinned profiles", async () => {
|
it("does not rotate for user-pinned profiles", async () => {
|
||||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
|
||||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
|
||||||
try {
|
|
||||||
await writeAuthStore(agentDir);
|
await writeAuthStore(agentDir);
|
||||||
|
|
||||||
mockSingleErrorAttempt({ errorMessage: "rate limit" });
|
mockSingleErrorAttempt({ errorMessage: "rate limit" });
|
||||||
@@ -380,10 +389,7 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
|
|
||||||
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(1);
|
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(1);
|
||||||
await expectProfileP2UsageUnchanged(agentDir);
|
await expectProfileP2UsageUnchanged(agentDir);
|
||||||
} finally {
|
});
|
||||||
await fs.rm(agentDir, { recursive: true, force: true });
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("honors user-pinned profiles even when in cooldown", async () => {
|
it("honors user-pinned profiles even when in cooldown", async () => {
|
||||||
@@ -400,9 +406,7 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("ignores user-locked profile when provider mismatches", async () => {
|
it("ignores user-locked profile when provider mismatches", async () => {
|
||||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
|
||||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
|
||||||
try {
|
|
||||||
await writeAuthStore(agentDir, { includeAnthropic: true });
|
await writeAuthStore(agentDir, { includeAnthropic: true });
|
||||||
|
|
||||||
runEmbeddedAttemptMock.mockResolvedValueOnce(
|
runEmbeddedAttemptMock.mockResolvedValueOnce(
|
||||||
@@ -432,10 +436,7 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(1);
|
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(1);
|
||||||
} finally {
|
});
|
||||||
await fs.rm(agentDir, { recursive: true, force: true });
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips profiles in cooldown during initial selection", async () => {
|
it("skips profiles in cooldown during initial selection", async () => {
|
||||||
@@ -486,11 +487,10 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("fails over when auth is unavailable and fallbacks are configured", async () => {
|
it("fails over when auth is unavailable and fallbacks are configured", async () => {
|
||||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
|
||||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
|
||||||
const previousOpenAiKey = process.env.OPENAI_API_KEY;
|
const previousOpenAiKey = process.env.OPENAI_API_KEY;
|
||||||
delete process.env.OPENAI_API_KEY;
|
delete process.env.OPENAI_API_KEY;
|
||||||
try {
|
try {
|
||||||
|
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
|
||||||
const authPath = path.join(agentDir, "auth-profiles.json");
|
const authPath = path.join(agentDir, "auth-profiles.json");
|
||||||
await fs.writeFile(authPath, JSON.stringify({ version: 1, profiles: {}, usageStats: {} }));
|
await fs.writeFile(authPath, JSON.stringify({ version: 1, profiles: {}, usageStats: {} }));
|
||||||
|
|
||||||
@@ -512,21 +512,18 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
).rejects.toMatchObject({ name: "FailoverError", reason: "auth" });
|
).rejects.toMatchObject({ name: "FailoverError", reason: "auth" });
|
||||||
|
|
||||||
expect(runEmbeddedAttemptMock).not.toHaveBeenCalled();
|
expect(runEmbeddedAttemptMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
} finally {
|
} finally {
|
||||||
if (previousOpenAiKey === undefined) {
|
if (previousOpenAiKey === undefined) {
|
||||||
delete process.env.OPENAI_API_KEY;
|
delete process.env.OPENAI_API_KEY;
|
||||||
} else {
|
} else {
|
||||||
process.env.OPENAI_API_KEY = previousOpenAiKey;
|
process.env.OPENAI_API_KEY = previousOpenAiKey;
|
||||||
}
|
}
|
||||||
await fs.rm(agentDir, { recursive: true, force: true });
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses the active erroring model in billing failover errors", async () => {
|
it("uses the active erroring model in billing failover errors", async () => {
|
||||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
|
||||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
|
||||||
try {
|
|
||||||
await writeAuthStore(agentDir);
|
await writeAuthStore(agentDir);
|
||||||
mockSingleErrorAttempt({
|
mockSingleErrorAttempt({
|
||||||
errorMessage: "insufficient credits",
|
errorMessage: "insufficient credits",
|
||||||
@@ -564,21 +561,11 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
expect(thrown).toBeInstanceOf(Error);
|
expect(thrown).toBeInstanceOf(Error);
|
||||||
expect((thrown as Error).message).toContain("openai (mock-rotated) returned a billing error");
|
expect((thrown as Error).message).toContain("openai (mock-rotated) returned a billing error");
|
||||||
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(1);
|
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(1);
|
||||||
} finally {
|
});
|
||||||
await fs.rm(agentDir, { recursive: true, force: true });
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips profiles in cooldown when rotating after failure", async () => {
|
it("skips profiles in cooldown when rotating after failure", async () => {
|
||||||
vi.useFakeTimers();
|
await withTimedAgentWorkspace(async ({ agentDir, workspaceDir, now }) => {
|
||||||
try {
|
|
||||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-"));
|
|
||||||
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-workspace-"));
|
|
||||||
const now = Date.now();
|
|
||||||
vi.setSystemTime(now);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const authPath = path.join(agentDir, "auth-profiles.json");
|
const authPath = path.join(agentDir, "auth-profiles.json");
|
||||||
const payload = {
|
const payload = {
|
||||||
version: 1,
|
version: 1,
|
||||||
@@ -608,12 +595,6 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
|
|||||||
expect(typeof usageStats["openai:p1"]?.lastUsed).toBe("number");
|
expect(typeof usageStats["openai:p1"]?.lastUsed).toBe("number");
|
||||||
expect(typeof usageStats["openai:p3"]?.lastUsed).toBe("number");
|
expect(typeof usageStats["openai:p3"]?.lastUsed).toBe("number");
|
||||||
expect(usageStats["openai:p2"]?.cooldownUntil).toBe(now + 60 * 60 * 1000);
|
expect(usageStats["openai:p2"]?.cooldownUntil).toBe(now + 60 * 60 * 1000);
|
||||||
} finally {
|
});
|
||||||
await fs.rm(agentDir, { recursive: true, force: true });
|
|
||||||
await fs.rm(workspaceDir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
vi.useRealTimers();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user