The best way to control Claude Code, Codex and more from any computer or phone
| 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 zones from "@distilled.cloud/hetzner/zones"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: Hetzner.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const hasHetznerCreds = !!process.env.HCLOUD_TOKEN; |
| 15 | |
| 16 | const waitUntilGone = (zoneId: number) => |
| 17 | zones.getZone({ id_or_name: String(zoneId) }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("1 second"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 10, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | test.provider.skipIf(!hasHetznerCreds)( |
| 28 | "create, update, and delete a zone", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const created = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | return yield* Hetzner.Zone("BasicZone", { |
| 36 | ttl: 3600, |
| 37 | labels: { env: "test" }, |
| 38 | }); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | expect(created.zoneId).toEqual(expect.any(Number)); |
| 43 | expect(created.name).toMatch(/\.com$/); |
| 44 | expect(created.mode).toEqual("primary"); |
| 45 | expect(created.ttl).toEqual(3600); |
| 46 | expect(created.deleteProtection).toEqual(false); |
| 47 | expect(created.labels).toMatchObject({ env: "test" }); |
| 48 | expect(created.assignedNameservers.length).toBeGreaterThan(0); |
| 49 | expect(created.created).toEqual(expect.any(String)); |
| 50 | |
| 51 | const fetched = yield* zones.getZone({ |
| 52 | id_or_name: String(created.zoneId), |
| 53 | }); |
| 54 | expect(fetched.zone.id).toEqual(created.zoneId); |
| 55 | expect(fetched.zone.name).toEqual(created.name); |
| 56 | expect(fetched.zone.ttl).toEqual(3600); |
| 57 | expect(fetched.zone.protection.delete).toEqual(false); |
| 58 | expect(fetched.zone.labels.env).toEqual("test"); |
| 59 | |
| 60 | const updated = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | return yield* Hetzner.Zone("BasicZone", { |
| 63 | name: created.name, |
| 64 | ttl: 7200, |
| 65 | labels: { env: "prod", role: "dns" }, |
| 66 | deleteProtection: true, |
| 67 | }); |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | expect(updated.zoneId).toEqual(created.zoneId); |
| 72 | expect(updated.name).toEqual(created.name); |
| 73 | expect(updated.ttl).toEqual(7200); |
| 74 | expect(updated.deleteProtection).toEqual(true); |
| 75 | expect(updated.labels).toMatchObject({ env: "prod", role: "dns" }); |
| 76 | |
| 77 | const refetched = yield* zones.getZone({ |
| 78 | id_or_name: String(updated.zoneId), |
| 79 | }); |
| 80 | expect(refetched.zone.ttl).toEqual(7200); |
| 81 | expect(refetched.zone.protection.delete).toEqual(true); |
| 82 | expect(refetched.zone.labels.env).toEqual("prod"); |
| 83 | expect(refetched.zone.labels.role).toEqual("dns"); |
| 84 | |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const gone = yield* waitUntilGone(created.zoneId); |
| 88 | expect(gone).toEqual("gone"); |
| 89 | }).pipe(logLevel), |
| 90 | { |
| 91 | tags: ["provider:hetzner", "provider:hetzner:service", "provider:hetzner:zone", "live"], |
| 92 | timeout: 120_000, |
| 93 | }, |
| 94 | ); |
| 95 | |
| 96 | test.provider.skipIf(!hasHetznerCreds)( |
| 97 | "replace when name changes", |
| 98 | (stack) => |
| 99 | Effect.gen(function* () { |
| 100 | yield* stack.destroy(); |
| 101 | |
| 102 | const created = yield* stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | return yield* Hetzner.Zone("ReplaceZone", { |
| 105 | ttl: 3600, |
| 106 | }); |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | // Keep the first label ≤ 63 chars (DNS). The generated apex is already |
| 111 | // at the cap, so we rewrite the tail instead of appending. |
| 112 | const apex = created.name.replace(/\.com$/, ""); |
| 113 | const replacementName = `${apex.slice(0, 61)}r.com`; |
| 114 | |
| 115 | const replaced = yield* stack.deploy( |
| 116 | Effect.gen(function* () { |
| 117 | return yield* Hetzner.Zone("ReplaceZone", { |
| 118 | name: replacementName, |
| 119 | ttl: 3600, |
| 120 | }); |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | expect(replaced.zoneId).not.toEqual(created.zoneId); |
| 125 | expect(replaced.name).toEqual(replacementName); |
| 126 | |
| 127 | const fetched = yield* zones.getZone({ |
| 128 | id_or_name: String(replaced.zoneId), |
| 129 | }); |
| 130 | expect(fetched.zone.id).toEqual(replaced.zoneId); |
| 131 | expect(fetched.zone.name).toEqual(replacementName); |
| 132 | |
| 133 | const oldGone = yield* waitUntilGone(created.zoneId); |
| 134 | expect(oldGone).toEqual("gone"); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | |
| 138 | const gone = yield* waitUntilGone(replaced.zoneId); |
| 139 | expect(gone).toEqual("gone"); |
| 140 | }).pipe(logLevel), |
| 141 | { |
| 142 | tags: ["provider:hetzner", "provider:hetzner:service", "provider:hetzner:zone", "live"], |
| 143 | timeout: 120_000, |
| 144 | }, |
| 145 | ); |
| 146 | |
| 147 | test.provider.skipIf(!hasHetznerCreds)( |
| 148 | "list enumerates the deployed zone", |
| 149 | (stack) => |
| 150 | Effect.gen(function* () { |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | const deployed = yield* stack.deploy( |
| 154 | Effect.gen(function* () { |
| 155 | return yield* Hetzner.Zone("ListZone", { |
| 156 | ttl: 3600, |
| 157 | }); |
| 158 | }), |
| 159 | ); |
| 160 | |
| 161 | const provider = yield* Provider.findProvider(Hetzner.Zone); |
| 162 | const all = yield* provider.list(); |
| 163 | const found = all.find((zone) => zone.zoneId === deployed.zoneId); |
| 164 | expect(found).toBeDefined(); |
| 165 | expect(found?.name).toEqual(deployed.name); |
| 166 | expect(found?.ttl).toEqual(3600); |
| 167 | expect(found?.mode).toEqual("primary"); |
| 168 | |
| 169 | yield* stack.destroy(); |
| 170 | |
| 171 | const gone = yield* waitUntilGone(deployed.zoneId); |
| 172 | expect(gone).toEqual("gone"); |
| 173 | }).pipe(logLevel), |
| 174 | { |
| 175 | tags: ["provider:hetzner", "provider:hetzner:service", "provider:hetzner:zone", "live"], |
| 176 | timeout: 120_000, |
| 177 | }, |
| 178 | ); |