test(auth-profiles): cover self-heal hook firing + survives hook errors

This commit is contained in:
Sarah Fortune
2026-05-21 19:16:28 -07:00
parent ab265dbce9
commit a1bdffc212

View File

@@ -17,7 +17,11 @@ import {
clearRuntimeAuthProfileStoreSnapshots,
ensureAuthProfileStore,
} from "./auth-profiles/store.js";
import { calculateAuthProfileCooldownMs, markAuthProfileFailure } from "./auth-profiles/usage.js";
import {
calculateAuthProfileCooldownMs,
markAuthProfileFailure,
setAuthProfileFailureHook,
} from "./auth-profiles/usage.js";
type AuthProfileStore = ReturnType<typeof ensureAuthProfileStore>;
@@ -374,6 +378,46 @@ describe("markAuthProfileFailure", () => {
expect(reloaded.usageStats?.["openrouter:default"]).toBeUndefined();
});
});
it("fires the auth profile failure hook so callers can self-heal", async () => {
await withAuthProfileStore(async ({ agentDir, store }) => {
const hook = vi.fn();
setAuthProfileFailureHook(hook);
try {
await markAuthProfileFailure({
store,
profileId: "anthropic:default",
reason: "auth",
agentDir,
});
expect(hook).toHaveBeenCalledTimes(1);
} finally {
setAuthProfileFailureHook(undefined);
}
});
});
it("does not break failure recording when the hook throws", async () => {
await withAuthProfileStore(async ({ agentDir, store }) => {
const throwingHook = vi.fn(() => {
throw new Error("boom");
});
setAuthProfileFailureHook(throwingHook);
try {
await markAuthProfileFailure({
store,
profileId: "anthropic:default",
reason: "auth",
agentDir,
});
expect(throwingHook).toHaveBeenCalledTimes(1);
// Failure still got recorded despite the hook throwing.
expect(store.usageStats?.["anthropic:default"]?.errorCount ?? 0).toBeGreaterThan(0);
} finally {
setAuthProfileFailureHook(undefined);
}
});
});
});
describe("calculateAuthProfileCooldownMs", () => {