The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Hetzner from "@alchemy.run/hetzner"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as floatingIps from "@distilled.cloud/hetzner/floating_ips"; |
| 5 | import * as servers from "@distilled.cloud/hetzner/servers"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Hetzner.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const hasHetznerCreds = !!process.env.HCLOUD_TOKEN; |
| 16 | |
| 17 | const waitUntilFloatingIpGone = (id: number) => |
| 18 | floatingIps.getFloatingIp({ id }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | const waitUntilServerGone = (id: number) => |
| 29 | servers.getServer({ id }).pipe( |
| 30 | Effect.as("found" as const), |
| 31 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 32 | Effect.repeat({ |
| 33 | schedule: Schedule.spaced("1 second"), |
| 34 | until: (status) => status === "gone", |
| 35 | times: 10, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | test.provider.skipIf(!hasHetznerCreds)( |
| 40 | "create update replace and delete assignment", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const created = yield* stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | const server = yield* Hetzner.Server("Web", { |
| 48 | serverType: "cx23", |
| 49 | image: "ubuntu-24.04", |
| 50 | location: "nbg1", |
| 51 | }); |
| 52 | const ip = yield* Hetzner.FloatingIp("PublicIp", { |
| 53 | type: "ipv4", |
| 54 | homeLocation: "nbg1", |
| 55 | }); |
| 56 | const assignment = yield* Hetzner.FloatingIpAssignment("Assign", { |
| 57 | floatingIp: ip, |
| 58 | server, |
| 59 | }); |
| 60 | return { server, ip, assignment }; |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | expect(created.assignment.floatingIpId).toEqual(created.ip.id); |
| 65 | expect(created.assignment.serverId).toEqual(created.server.serverId); |
| 66 | |
| 67 | const fetched = yield* floatingIps.getFloatingIp({ |
| 68 | id: created.ip.id, |
| 69 | }); |
| 70 | expect(fetched.floating_ip.server).toEqual(created.server.serverId); |
| 71 | |
| 72 | const provider = yield* Provider.findProvider(Hetzner.FloatingIpAssignment); |
| 73 | const listed = yield* provider.list(); |
| 74 | const found = listed.find((row) => row.floatingIpId === created.ip.id); |
| 75 | expect(found).toBeDefined(); |
| 76 | expect(found?.serverId).toEqual(created.server.serverId); |
| 77 | |
| 78 | const updated = yield* stack.deploy( |
| 79 | Effect.gen(function* () { |
| 80 | const server = yield* Hetzner.Server("Web", { |
| 81 | serverType: "cx23", |
| 82 | image: "ubuntu-24.04", |
| 83 | location: "nbg1", |
| 84 | }); |
| 85 | const ip = yield* Hetzner.FloatingIp("PublicIp", { |
| 86 | type: "ipv4", |
| 87 | homeLocation: "nbg1", |
| 88 | }); |
| 89 | const assignment = yield* Hetzner.FloatingIpAssignment("Assign", { |
| 90 | floatingIp: ip, |
| 91 | server, |
| 92 | }); |
| 93 | return { server, ip, assignment }; |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | expect(updated.assignment.floatingIpId).toEqual(created.ip.id); |
| 98 | expect(updated.assignment.serverId).toEqual(created.server.serverId); |
| 99 | expect(updated.server.id).toEqual(created.server.id); |
| 100 | expect(updated.ip.id).toEqual(created.ip.id); |
| 101 | |
| 102 | const stillAssigned = yield* floatingIps.getFloatingIp({ |
| 103 | id: created.ip.id, |
| 104 | }); |
| 105 | expect(stillAssigned.floating_ip.server).toEqual(created.server.serverId); |
| 106 | |
| 107 | const replaced = yield* stack.deploy( |
| 108 | Effect.gen(function* () { |
| 109 | const server = yield* Hetzner.Server("Web", { |
| 110 | serverType: "cx23", |
| 111 | image: "ubuntu-24.04", |
| 112 | location: "nbg1", |
| 113 | }); |
| 114 | const ip = yield* Hetzner.FloatingIp("PublicIp", { |
| 115 | type: "ipv4", |
| 116 | homeLocation: "nbg1", |
| 117 | }); |
| 118 | const nextIp = yield* Hetzner.FloatingIp("PublicIp2", { |
| 119 | type: "ipv4", |
| 120 | homeLocation: "nbg1", |
| 121 | }); |
| 122 | const assignment = yield* Hetzner.FloatingIpAssignment("Assign", { |
| 123 | floatingIp: nextIp, |
| 124 | server, |
| 125 | }); |
| 126 | return { server, ip, nextIp, assignment }; |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | expect(replaced.assignment.floatingIpId).toEqual(replaced.nextIp.id); |
| 131 | expect(replaced.assignment.serverId).toEqual(created.server.serverId); |
| 132 | expect(replaced.nextIp.id).not.toEqual(created.ip.id); |
| 133 | |
| 134 | const oldIp = yield* floatingIps.getFloatingIp({ |
| 135 | id: created.ip.id, |
| 136 | }); |
| 137 | expect(oldIp.floating_ip.server).toBeNull(); |
| 138 | |
| 139 | const newIp = yield* floatingIps.getFloatingIp({ |
| 140 | id: replaced.nextIp.id, |
| 141 | }); |
| 142 | expect(newIp.floating_ip.server).toEqual(created.server.serverId); |
| 143 | |
| 144 | yield* stack.destroy(); |
| 145 | |
| 146 | const ipGone = yield* waitUntilFloatingIpGone(created.ip.id); |
| 147 | expect(ipGone).toEqual("gone"); |
| 148 | const nextIpGone = yield* waitUntilFloatingIpGone(replaced.nextIp.id); |
| 149 | expect(nextIpGone).toEqual("gone"); |
| 150 | const serverGone = yield* waitUntilServerGone(created.server.id); |
| 151 | expect(serverGone).toEqual("gone"); |
| 152 | }).pipe(logLevel), |
| 153 | { |
| 154 | tags: [ |
| 155 | "provider:hetzner", |
| 156 | "provider:hetzner:floatingip", |
| 157 | "provider:hetzner:floatingipassignment", |
| 158 | "provider:hetzner:server", |
| 159 | "provider:hetzner:service", |
| 160 | "live", |
| 161 | ], |
| 162 | timeout: 180_000, |
| 163 | exclusive: "hetzner", |
| 164 | }, |
| 165 | ); |