From a1bdffc2129f3ebbf64b57eecdac728555b40175 Mon Sep 17 00:00:00 2001 From: Sarah Fortune Date: Thu, 21 May 2026 19:16:28 -0700 Subject: [PATCH] test(auth-profiles): cover self-heal hook firing + survives hook errors --- ...th-profiles.markauthprofilefailure.test.ts | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/agents/auth-profiles.markauthprofilefailure.test.ts b/src/agents/auth-profiles.markauthprofilefailure.test.ts index 53d7100b7c4..44e7496be8d 100644 --- a/src/agents/auth-profiles.markauthprofilefailure.test.ts +++ b/src/agents/auth-profiles.markauthprofilefailure.test.ts @@ -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; @@ -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", () => {