diff --git a/packages/cli/src/__tests__/digitalocean-token.test.ts b/packages/cli/src/__tests__/digitalocean-token.test.ts index e9f1a36b..004cf2ef 100644 --- a/packages/cli/src/__tests__/digitalocean-token.test.ts +++ b/packages/cli/src/__tests__/digitalocean-token.test.ts @@ -88,23 +88,17 @@ describe("doApi 401 OAuth recovery", () => { it("attempts OAuth recovery on 401 before throwing", async () => { state.token = "expired-token"; - let callCount = 0; + let apiCalls = 0; + let oauthConnChecks = 0; globalThis.fetch = mock((url: string | URL | Request) => { - callCount++; const urlStr = String(url); - // First call: the actual API call returning 401 - if (callCount === 1) { - return Promise.resolve( - new Response("Unauthorized", { - status: 401, - }), - ); - } - // Second call: OAuth connectivity check — fail it so tryDoOAuth returns null quickly - // (avoids starting a real Bun.serve OAuth server) + // OAuth connectivity check — fail it so tryDoOAuth returns null quickly if (urlStr.includes("cloud.digitalocean.com")) { + oauthConnChecks++; return Promise.reject(new Error("network unavailable")); } + // API calls to DigitalOcean — return 401 + apiCalls++; return Promise.resolve( new Response("Unauthorized", { status: 401, @@ -114,8 +108,9 @@ describe("doApi 401 OAuth recovery", () => { // OAuth recovery fails (connectivity check fails), so doApi throws the 401 await expect(doApi("GET", "/account", undefined, 1)).rejects.toThrow("DigitalOcean API error 401"); - // Verify recovery was attempted (API call + OAuth connectivity check + retries) - expect(callCount).toBeGreaterThanOrEqual(2); + // Verify recovery was attempted: at least 1 API call + 1 connectivity check + expect(apiCalls).toBeGreaterThanOrEqual(1); + expect(oauthConnChecks).toBeGreaterThanOrEqual(1); }); it("succeeds after OAuth recovery provides a new token", async () => {