|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { accountNeedsReauth } from "../src/oauth-health-display"; |
| 3 | +import { buildActiveAccountNeedsReauthMap } from "../src/hooks/useProviderAccountPools"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Aggregate-path regressions: health-only `reauth_required` must demote Providers |
| 7 | + * overview / rail attention the same way row CTAs already do. Fixtures intentionally |
| 8 | + * omit emails/tokens — never assert raw account identifiers into attention state. |
| 9 | + */ |
| 10 | + |
| 11 | +describe("Codex callback aggregate path", () => { |
| 12 | + test("activePoolNeedsReauth (health-aware) is what the callback propagates", async () => { |
| 13 | + const pool = await Bun.file(new URL("../src/components/CodexAccountPool.tsx", import.meta.url)).text(); |
| 14 | + expect(pool).toContain("accountNeedsReauth(activePoolAccount)"); |
| 15 | + expect(pool).toContain("onActiveNeedsReauthChange?.(activePoolNeedsReauth)"); |
| 16 | + expect(pool).toContain("[activePoolNeedsReauth, onActiveNeedsReauthChange]"); |
| 17 | + // Legacy-only controller flag must not be the callback dependency. |
| 18 | + expect(pool).not.toContain("onActiveNeedsReauthChange?.(activeNeedsReauth)"); |
| 19 | + expect(pool).not.toMatch(/\[activeNeedsReauth,\s*onActiveNeedsReauthChange\]/); |
| 20 | + }); |
| 21 | + |
| 22 | + test("shared Codex controller derives activeNeedsReauth from health as well", async () => { |
| 23 | + const hook = await Bun.file(new URL("../src/hooks/useCodexAccountPool.ts", import.meta.url)).text(); |
| 24 | + expect(hook).toContain("accountNeedsReauth(activePoolAccount ?? mainAccount)"); |
| 25 | + expect(hook).not.toMatch(/activePoolAccount\s*\?\s*Boolean\(activePoolAccount\.needsReauth\)/); |
| 26 | + }); |
| 27 | + |
| 28 | + test("health-only reauth_required marks the active Codex account", () => { |
| 29 | + // Mirrors CodexAccountPool's activePoolNeedsReauth / controller activeNeedsReauth. |
| 30 | + expect(accountNeedsReauth({ |
| 31 | + needsReauth: undefined, |
| 32 | + health: { status: "reauth_required", reason: "refresh_failed" }, |
| 33 | + })).toBe(true); |
| 34 | + expect(accountNeedsReauth({ |
| 35 | + needsReauth: undefined, |
| 36 | + health: { status: "healthy" }, |
| 37 | + })).toBe(false); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("generic provider map aggregate path", () => { |
| 42 | + test("health-only reauth_required on the active account demotes the provider", () => { |
| 43 | + const map = buildActiveAccountNeedsReauthMap({ |
| 44 | + anthropic: { |
| 45 | + activeAccountId: "acct_active", |
| 46 | + accounts: [ |
| 47 | + { |
| 48 | + id: "acct_active", |
| 49 | + active: true, |
| 50 | + health: { status: "reauth_required", reason: "refresh_failed" }, |
| 51 | + }, |
| 52 | + { |
| 53 | + id: "acct_other", |
| 54 | + active: false, |
| 55 | + needsReauth: true, |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + xai: { |
| 60 | + activeAccountId: "xai_ok", |
| 61 | + accounts: [ |
| 62 | + { |
| 63 | + id: "xai_ok", |
| 64 | + active: true, |
| 65 | + health: { status: "healthy" }, |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + }); |
| 70 | + expect(map).toEqual({ anthropic: true }); |
| 71 | + }); |
| 72 | + |
| 73 | + test("inactive-only reauth does not demote the provider", () => { |
| 74 | + const map = buildActiveAccountNeedsReauthMap({ |
| 75 | + anthropic: { |
| 76 | + activeAccountId: "acct_active", |
| 77 | + accounts: [ |
| 78 | + { id: "acct_active", active: true, health: { status: "healthy" } }, |
| 79 | + { id: "acct_stale", active: false, health: { status: "reauth_required" } }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + }); |
| 83 | + expect(map).toEqual({}); |
| 84 | + }); |
| 85 | + |
| 86 | + test("legacy needsReauth and Codex overlay still mark providers", () => { |
| 87 | + const map = buildActiveAccountNeedsReauthMap( |
| 88 | + { |
| 89 | + kimi: { |
| 90 | + activeAccountId: "kimi_1", |
| 91 | + accounts: [{ id: "kimi_1", active: true, needsReauth: true }], |
| 92 | + }, |
| 93 | + }, |
| 94 | + true, |
| 95 | + ); |
| 96 | + expect(map).toEqual({ kimi: true, openai: true }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments