refactor: dedupe auth-profile failure marking and rotation test setup

This commit is contained in:
Peter Steinberger
2026-02-22 15:43:57 +01:00
parent 44dfbd23df
commit d0b59270a7
2 changed files with 54 additions and 52 deletions

View File

@@ -188,16 +188,33 @@ async function readUsageStats(agentDir: string) {
return stored.usageStats ?? {};
}
async function expectProfileP2UsageUpdated(agentDir: string) {
const usageStats = await readUsageStats(agentDir);
expect(typeof usageStats["openai:p2"]?.lastUsed).toBe("number");
}
async function expectProfileP2UsageUnchanged(agentDir: string) {
const usageStats = await readUsageStats(agentDir);
expect(usageStats["openai:p2"]?.lastUsed).toBe(2);
}
async function runAutoPinnedRotationCase(params: {
errorMessage: string;
sessionKey: string;
runId: string;
}) {
runEmbeddedAttemptMock.mockClear();
return withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
await writeAuthStore(agentDir);
mockFailedThenSuccessfulAttempt(params.errorMessage);
await runAutoPinnedOpenAiTurn({
agentDir,
workspaceDir,
sessionKey: params.sessionKey,
runId: params.runId,
});
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(2);
const usageStats = await readUsageStats(agentDir);
return { usageStats };
});
}
function mockSingleSuccessfulAttempt() {
runEmbeddedAttemptMock.mockResolvedValueOnce(
makeAttempt({
@@ -314,40 +331,19 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
] as const;
for (const testCase of cases) {
runEmbeddedAttemptMock.mockClear();
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
await writeAuthStore(agentDir);
mockFailedThenSuccessfulAttempt(testCase.errorMessage);
await runAutoPinnedOpenAiTurn({
agentDir,
workspaceDir,
sessionKey: testCase.sessionKey,
runId: testCase.runId,
});
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(2);
await expectProfileP2UsageUpdated(agentDir);
});
const { usageStats } = await runAutoPinnedRotationCase(testCase);
expect(typeof usageStats["openai:p2"]?.lastUsed).toBe("number");
}
});
it("rotates on timeout without cooling down the timed-out profile", async () => {
await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
await writeAuthStore(agentDir);
mockFailedThenSuccessfulAttempt("request ended without sending any chunks");
await runAutoPinnedOpenAiTurn({
agentDir,
workspaceDir,
sessionKey: "agent:test:timeout-no-cooldown",
runId: "run:timeout-no-cooldown",
});
expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(2);
const usageStats = await readUsageStats(agentDir);
expect(typeof usageStats["openai:p2"]?.lastUsed).toBe("number");
expect(usageStats["openai:p1"]?.cooldownUntil).toBeUndefined();
const { usageStats } = await runAutoPinnedRotationCase({
errorMessage: "request ended without sending any chunks",
sessionKey: "agent:test:timeout-no-cooldown",
runId: "run:timeout-no-cooldown",
});
expect(typeof usageStats["openai:p2"]?.lastUsed).toBe("number");
expect(usageStats["openai:p1"]?.cooldownUntil).toBeUndefined();
});
it("does not rotate for compaction timeouts", async () => {