Skip to content
Open
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: 15 additions & 16 deletions packages/cli/src/__tests__/digitalocean-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,34 +88,33 @@ describe("doApi 401 OAuth recovery", () => {

it("attempts OAuth recovery on 401 before throwing", async () => {
state.token = "expired-token";
let callCount = 0;
let doApiCalls = 0;
let oauthChecks = 0;
globalThis.fetch = mock((url: string | URL | Request) => {
callCount++;
const urlStr = String(url);
// First call: the actual API call returning 401
if (callCount === 1) {
// OAuth connectivity check — fail it so tryDoOAuth returns null quickly
if (urlStr.includes("cloud.digitalocean.com")) {
oauthChecks++;
return Promise.reject(new Error("network unavailable"));
}
// DigitalOcean API call returning 401
if (urlStr.includes("api.digitalocean.com")) {
doApiCalls++;
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"));
}
return Promise.resolve(
new Response("Unauthorized", {
status: 401,
}),
);
// Other fetches from concurrent tests — return harmless 200
return Promise.resolve(new Response("{}"));
});

// 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 DO API call + 1 connectivity check
expect(doApiCalls).toBe(1);
expect(oauthChecks).toBe(1);
});

it("succeeds after OAuth recovery provides a new token", async () => {
Expand Down
96 changes: 50 additions & 46 deletions packages/cli/src/__tests__/hetzner-cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,47 +585,40 @@ describe("hetzner/createServer", () => {
},
},
};
let callCount = 0;
global.fetch = mock(() => {
callCount++;
if (callCount <= 1) {
// Token validation
return Promise.resolve(
new Response(
JSON.stringify({
servers: [],
}),
),
);
}
if (callCount <= 2) {
// SSH keys
return Promise.resolve(
new Response(
JSON.stringify({
ssh_keys: [],
}),
),
);
let serverPostCount = 0;
global.fetch = mock((url: string | URL | Request, init?: RequestInit) => {
const urlStr = String(url);
const method = init?.method ?? "GET";
// POST /servers — first attempt returns 403 resource_limit_exceeded, retry succeeds
if (method === "POST" && urlStr.includes("/servers")) {
serverPostCount++;
if (serverPostCount <= 1) {
return Promise.resolve(
new Response(
JSON.stringify({
error: {
code: "resource_limit_exceeded",
message: "primary_ip_limit",
},
}),
{
status: 403,
},
),
);
}
return Promise.resolve(new Response(JSON.stringify(serverResp)));
}
if (callCount <= 3) {
// First create attempt — resource_limit_exceeded (HTTP 403)
// DELETE primary IP
if (method === "DELETE" && urlStr.includes("/primary_ips/")) {
return Promise.resolve(
new Response(
JSON.stringify({
error: {
code: "resource_limit_exceeded",
message: "primary_ip_limit",
},
}),
{
status: 403,
},
),
new Response("", {
status: 204,
}),
);
}
if (callCount <= 4) {
// List primary IPs for cleanup
// GET /primary_ips — list for cleanup
if (urlStr.includes("/primary_ips")) {
return Promise.resolve(
new Response(
JSON.stringify({
Expand All @@ -645,23 +638,34 @@ describe("hetzner/createServer", () => {
),
);
}
if (callCount <= 5) {
// Delete orphaned IP 100
// GET /ssh_keys
if (urlStr.includes("/ssh_keys")) {
return Promise.resolve(
new Response("", {
status: 204,
}),
new Response(
JSON.stringify({
ssh_keys: [],
}),
),
);
}
// Retry create — success
return Promise.resolve(new Response(JSON.stringify(serverResp)));
// Token validation — GET /servers
if (urlStr.includes("/servers")) {
return Promise.resolve(
new Response(
JSON.stringify({
servers: [],
}),
),
);
}
return Promise.resolve(new Response("{}"));
});
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);
// Verify the retry happened: first POST failed, second succeeded
expect(serverPostCount).toBe(2);
});

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