Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions packages/cli/src/__tests__/digitalocean-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,34 +88,37 @@ describe("doApi 401 OAuth recovery", () => {

it("attempts OAuth recovery on 401 before throwing", async () => {
state.token = "expired-token";
let callCount = 0;
let apiCallCount = 0;
let oauthCheckCount = 0;
globalThis.fetch = mock((url: string | URL | Request) => {
callCount++;
const urlStr = String(url);
// First call: the actual API call returning 401
if (callCount === 1) {
const urlStr = String(url instanceof Request ? url.url : url);
// OAuth connectivity check — fail it so tryDoOAuth returns null quickly
if (urlStr.includes("cloud.digitalocean.com")) {
oauthCheckCount++;
return Promise.reject(new Error("network unavailable"));
}
// DO API call — return 401
if (urlStr.includes("api.digitalocean.com")) {
apiCallCount++;
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)
if (urlStr.includes("cloud.digitalocean.com")) {
return Promise.reject(new Error("network unavailable"));
}
// Fallback for any phantom fetches (e.g. telemetry from other test files)
return Promise.resolve(
new Response("Unauthorized", {
status: 401,
new Response("", {
status: 200,
}),
);
});

// 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: 1 API call + 1 connectivity check = 2
expect(callCount).toBe(2);
// Verify recovery was attempted: 1 API call + 1 connectivity check
expect(apiCallCount).toBe(1);
expect(oauthCheckCount).toBe(1);
});

it("succeeds after OAuth recovery provides a new token", async () => {
Expand Down
64 changes: 36 additions & 28 deletions packages/cli/src/__tests__/hetzner-cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,13 @@ describe("hetzner/createServer", () => {
},
},
};
let callCount = 0;
global.fetch = mock(() => {
callCount++;
if (callCount <= 1) {
let serverPostCount = 0;
global.fetch = mock((url: string | URL | Request, opts?: RequestInit) => {
const urlStr = String(url instanceof Request ? url.url : url);
const method = (opts?.method ?? "GET").toUpperCase();

// Route by URL+method to avoid callCount sensitivity to phantom fetches
if (method === "GET" && urlStr.includes("/servers")) {
// Token validation
return Promise.resolve(
new Response(
Expand All @@ -598,8 +601,7 @@ describe("hetzner/createServer", () => {
),
);
}
if (callCount <= 2) {
// SSH keys
if (method === "GET" && urlStr.includes("/ssh_keys")) {
return Promise.resolve(
new Response(
JSON.stringify({
Expand All @@ -608,24 +610,28 @@ describe("hetzner/createServer", () => {
),
);
}
if (callCount <= 3) {
// First create attempt — resource_limit_exceeded (HTTP 403)
return Promise.resolve(
new Response(
JSON.stringify({
error: {
code: "resource_limit_exceeded",
message: "primary_ip_limit",
if (method === "POST" && urlStr.includes("/servers")) {
serverPostCount++;
if (serverPostCount === 1) {
// First create attempt — resource_limit_exceeded (HTTP 403)
return Promise.resolve(
new Response(
JSON.stringify({
error: {
code: "resource_limit_exceeded",
message: "primary_ip_limit",
},
}),
{
status: 403,
},
}),
{
status: 403,
},
),
);
),
);
}
// Retry create — success
return Promise.resolve(new Response(JSON.stringify(serverResp)));
}
if (callCount <= 4) {
// List primary IPs for cleanup
if (method === "GET" && urlStr.includes("/primary_ips")) {
return Promise.resolve(
new Response(
JSON.stringify({
Expand All @@ -645,23 +651,25 @@ describe("hetzner/createServer", () => {
),
);
}
if (callCount <= 5) {
// Delete orphaned IP 100
if (method === "DELETE" && urlStr.includes("/primary_ips/")) {
return Promise.resolve(
new Response("", {
status: 204,
}),
);
}
// Retry create — success
return Promise.resolve(new Response(JSON.stringify(serverResp)));
// Fallback for any unexpected/phantom fetches (e.g. telemetry)
return Promise.resolve(
new Response("", {
status: 200,
}),
);
});
const { ensureHcloudToken, createServer } = await import("../hetzner/hetzner");
await ensureHcloudToken();
const conn = await createServer("test-retry", "cx23", "fsn1");
expect(conn.ip).toBe("10.0.0.5");
// Should have called: token(1), ssh_keys(2), create-fail(3), list-ips(4), delete-ip(5), create-ok(6)
expect(callCount).toBeGreaterThanOrEqual(6);
expect(serverPostCount).toBe(2);
});

it("throws with guidance when resource limit hit and no orphaned IPs to clean", async () => {
Expand Down
Loading