|
| 1 | +import { createServer, type Server } from "node:http"; |
| 2 | +import { once } from "node:events"; |
| 3 | +import { afterEach, describe, expect, it } from "vitest"; |
| 4 | +import { createFlyMachinesClient, FlyClientError, type FlyMachine } from "../src/fly-machines-client.js"; |
| 5 | + |
| 6 | +const token = "test-fly-secret-do-not-leak"; |
| 7 | +const servers: Server[] = []; |
| 8 | + |
| 9 | +async function fakeFly(handler: (request: any, response: any) => void | Promise<void>) { |
| 10 | + const server = createServer((request, response) => { void handler(request, response); }); |
| 11 | + servers.push(server); |
| 12 | + server.listen(0, "127.0.0.1"); |
| 13 | + await once(server, "listening"); |
| 14 | + const address = server.address(); |
| 15 | + if (!address || typeof address === "string") throw new Error("Fake Fly server did not bind a TCP port"); |
| 16 | + return `http://127.0.0.1:${address.port}`; |
| 17 | +} |
| 18 | + |
| 19 | +function json(response: any, status: number, body: unknown) { |
| 20 | + response.writeHead(status, { "content-type": "application/json" }); |
| 21 | + response.end(JSON.stringify(body)); |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(async () => { |
| 25 | + await Promise.all(servers.splice(0).map(async (server) => { |
| 26 | + server.closeAllConnections(); |
| 27 | + if (server.listening) { |
| 28 | + server.close(); |
| 29 | + await once(server, "close"); |
| 30 | + } |
| 31 | + })); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("bounded Fly Machines client", () => { |
| 35 | + it("creates, inspects, starts, stops, updates config, and destroys with documented paths", async () => { |
| 36 | + const calls: Array<{ method: string; path: string; auth: string | undefined }> = []; |
| 37 | + const machine: FlyMachine = { id: "m-1", state: "created" }; |
| 38 | + const origin = await fakeFly(async (req, res) => { |
| 39 | + calls.push({ method: req.method, path: req.url, auth: req.headers.authorization }); |
| 40 | + if (req.method === "GET" && req.url === "/v1/apps/app-a/machines") return json(res, 200, []); |
| 41 | + if (req.method === "POST" && req.url === "/v1/apps/app-a/machines") { |
| 42 | + let raw = ""; for await (const chunk of req) raw += chunk; |
| 43 | + const body = JSON.parse(raw); |
| 44 | + expect(body.config.metadata.factorize_operation_id).toBe("op-1"); |
| 45 | + return json(res, 200, machine); |
| 46 | + } |
| 47 | + if (req.method === "GET" && req.url === "/v1/apps/app-a/machines/m-1") return json(res, 200, machine); |
| 48 | + if (req.method === "POST" && ["/v1/apps/app-a/machines/m-1/start", "/v1/apps/app-a/machines/m-1/stop"].includes(req.url)) { |
| 49 | + res.writeHead(204); return res.end(); |
| 50 | + } |
| 51 | + if (req.method === "POST" && req.url === "/v1/apps/app-a/machines/m-1") return json(res, 200, machine); |
| 52 | + if (req.method === "DELETE" && req.url === "/v1/apps/app-a/machines/m-1") { res.writeHead(204); return res.end(); } |
| 53 | + json(res, 404, { error: "not found" }); |
| 54 | + }); |
| 55 | + const client = createFlyMachinesClient({ token, apiOrigin: origin, wait: async () => undefined }); |
| 56 | + const created = await client.createMachine("app-a", { image: "example/image", metadata: { keep: "yes" } }, "op-1"); |
| 57 | + expect(created.id).toBe("m-1"); |
| 58 | + expect((await client.inspectMachine("app-a", "m-1")).id).toBe("m-1"); |
| 59 | + await client.startMachine("app-a", "m-1"); |
| 60 | + await client.stopMachine("app-a", "m-1"); |
| 61 | + expect((await client.updateMachineConfig("app-a", "m-1", { image: "example/new" })).id).toBe("m-1"); |
| 62 | + await client.destroyMachine("app-a", "m-1"); |
| 63 | + expect(calls.every((call) => call.auth === `Bearer ${token}`)).toBe(true); |
| 64 | + expect(calls.map(({ method, path }) => `${method} ${path}`)).toEqual([ |
| 65 | + "GET /v1/apps/app-a/machines", "POST /v1/apps/app-a/machines", "GET /v1/apps/app-a/machines/m-1", |
| 66 | + "POST /v1/apps/app-a/machines/m-1/start", "POST /v1/apps/app-a/machines/m-1/stop", |
| 67 | + "POST /v1/apps/app-a/machines/m-1", "DELETE /v1/apps/app-a/machines/m-1", |
| 68 | + ]); |
| 69 | + }); |
| 70 | + |
| 71 | + it("returns typed auth errors without exposing credentials or provider bodies", async () => { |
| 72 | + const origin = await fakeFly((_req, res) => json(res, 401, { detail: token })); |
| 73 | + const client = createFlyMachinesClient({ token, apiOrigin: origin }); |
| 74 | + await expect(client.inspectMachine("app", "machine")).rejects.toMatchObject({ kind: "auth", status: 401 }); |
| 75 | + try { await client.inspectMachine("app", "machine"); } catch (error) { |
| 76 | + expect(String(error)).not.toContain(token); |
| 77 | + expect(String(error)).not.toContain("detail"); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it("bounds a stalled response body and reports a secret-safe timeout", async () => { |
| 82 | + let count = 0; |
| 83 | + const origin = await fakeFly((_req, res) => { |
| 84 | + count++; |
| 85 | + res.writeHead(200, { "content-type": "application/json" }); |
| 86 | + res.flushHeaders(); |
| 87 | + // Deliberately leave the body open until the client deadline aborts it. |
| 88 | + }); |
| 89 | + const client = createFlyMachinesClient({ token, apiOrigin: origin, timeoutMs: 30, maxRetries: 0 }); |
| 90 | + await expect(client.inspectMachine("app", "machine")).rejects.toMatchObject({ kind: "timeout" }); |
| 91 | + expect(count).toBe(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it("retries a safe read only within the configured request bound for 429 and 5xx", async () => { |
| 95 | + for (const status of [429, 503]) { |
| 96 | + let count = 0; |
| 97 | + const origin = await fakeFly((_req, res) => { |
| 98 | + count++; |
| 99 | + if (count < 3) return json(res, status, { secret: token }); |
| 100 | + json(res, 200, { id: "m-1" }); |
| 101 | + }); |
| 102 | + const client = createFlyMachinesClient({ token, apiOrigin: origin, maxRetries: 2, wait: async () => undefined }); |
| 103 | + expect((await client.inspectMachine("app", "m-1")).id).toBe("m-1"); |
| 104 | + expect(count).toBe(3); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it("times out a stalled response and reconciles an ambiguous create by operation id", async () => { |
| 109 | + const machines: FlyMachine[] = []; |
| 110 | + let createCount = 0; |
| 111 | + const origin = await fakeFly(async (req, res) => { |
| 112 | + if (req.method === "GET") return json(res, 200, machines); |
| 113 | + if (req.method === "POST") { |
| 114 | + createCount++; |
| 115 | + let raw = ""; for await (const chunk of req) raw += chunk; |
| 116 | + const body = JSON.parse(raw); |
| 117 | + const machine = { id: "m-created", config: body.config }; |
| 118 | + machines.push(machine); |
| 119 | + // Commit the resource but leave the response stalled past the client's deadline. |
| 120 | + res.writeHead(200, { "content-type": "application/json" }); |
| 121 | + } |
| 122 | + }); |
| 123 | + const client = createFlyMachinesClient({ token, apiOrigin: origin, timeoutMs: 30, maxRetries: 0 }); |
| 124 | + const created = await client.createMachine("app", { image: "image" }, "op-ambiguous"); |
| 125 | + expect(created.id).toBe("m-created"); |
| 126 | + expect(createCount).toBe(1); |
| 127 | + }); |
| 128 | + |
| 129 | + it("reconciles a duplicate invocation before POST and never creates a second Machine", async () => { |
| 130 | + const machines: FlyMachine[] = []; |
| 131 | + let createCount = 0; |
| 132 | + const origin = await fakeFly(async (req, res) => { |
| 133 | + if (req.method === "GET") return json(res, 200, machines); |
| 134 | + createCount++; |
| 135 | + let raw = ""; for await (const chunk of req) raw += chunk; |
| 136 | + const body = JSON.parse(raw); |
| 137 | + const machine = { id: `m-${createCount}`, config: body.config }; |
| 138 | + machines.push(machine); |
| 139 | + json(res, 200, machine); |
| 140 | + }); |
| 141 | + const client = createFlyMachinesClient({ token, apiOrigin: origin }); |
| 142 | + expect((await client.createMachine("app", { image: "image" }, "stable-op")).id).toBe("m-1"); |
| 143 | + expect((await client.createMachine("app", { image: "image" }, "stable-op")).id).toBe("m-1"); |
| 144 | + expect(createCount).toBe(1); |
| 145 | + }); |
| 146 | + |
| 147 | + it("reconciles a provider duplicate response using the stable operation id", async () => { |
| 148 | + let machine: FlyMachine | undefined; |
| 149 | + let gets = 0; |
| 150 | + let posts = 0; |
| 151 | + const origin = await fakeFly(async (req, res) => { |
| 152 | + if (req.method === "GET") { |
| 153 | + gets++; |
| 154 | + return json(res, 200, machine ? [machine] : []); |
| 155 | + } |
| 156 | + posts++; |
| 157 | + let raw = ""; for await (const chunk of req) raw += chunk; |
| 158 | + machine = { id: "created-by-concurrent-request", config: JSON.parse(raw).config }; |
| 159 | + return json(res, 409, { detail: "duplicate create" }); |
| 160 | + }); |
| 161 | + const client = createFlyMachinesClient({ token, apiOrigin: origin }); |
| 162 | + expect((await client.createMachine("app", { image: "image" }, "concurrent-op")).id).toBe("created-by-concurrent-request"); |
| 163 | + expect(gets).toBe(2); |
| 164 | + expect(posts).toBe(1); |
| 165 | + }); |
| 166 | + |
| 167 | + it("does not retry create after an ambiguous outcome that cannot be reconciled", async () => { |
| 168 | + let count = 0; |
| 169 | + const origin = await fakeFly((req, res) => { |
| 170 | + if (req.method === "GET") return json(res, 200, []); |
| 171 | + count++; |
| 172 | + res.writeHead(503, { "content-type": "application/json" }); res.end(JSON.stringify({ detail: token })); |
| 173 | + }); |
| 174 | + const client = createFlyMachinesClient({ token, apiOrigin: origin, wait: async () => undefined }); |
| 175 | + await expect(client.createMachine("app", { image: "image" }, "op-fail")).rejects.toMatchObject({ kind: "ambiguous_create" }); |
| 176 | + expect(count).toBe(1); |
| 177 | + }); |
| 178 | +}); |
0 commit comments