fix(tests): make hetzner and digitalocean tests URL-aware to fix full-suite flakiness

The hetzner createServer resource-limit test and digitalocean OAuth
recovery test used callCount-based mocks that broke when module state
persisted across the full test suite. Switch to URL-based request
matching so tests are isolated regardless of execution order.

Agent: code-health
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
B 2026-05-08 08:39:46 +00:00
parent de83873110
commit 97d85bba2d

View file

@ -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 () => {