The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 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: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const zone = "us-central1-a"; |
| 15 | |
| 16 | const waitUntilGone = (instanceGroup: string) => |
| 17 | GcpEnvironment.current.pipe( |
| 18 | Effect.flatMap(({ project }) => |
| 19 | compute.getInstanceGroups({ project, zone, instanceGroup }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("1 second"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ), |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "create, update, replace, and delete an instance group", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const created = yield* stack.deploy( |
| 39 | Effect.gen(function* () { |
| 40 | return yield* GCP.Compute.InstanceGroup("Web", { |
| 41 | zone, |
| 42 | description: "unmanaged backends", |
| 43 | namedPorts: [{ name: "http", port: 80 }], |
| 44 | }); |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | expect(created.instanceGroupName).toEqual(expect.any(String)); |
| 49 | expect(created.zone).toEqual(zone); |
| 50 | expect(created.description).toEqual("unmanaged backends"); |
| 51 | expect(created.namedPorts).toEqual([{ name: "http", port: 80 }]); |
| 52 | expect(created.size).toEqual(0); |
| 53 | |
| 54 | const fetched = yield* compute.getInstanceGroups({ |
| 55 | project, |
| 56 | zone, |
| 57 | instanceGroup: created.instanceGroupName, |
| 58 | }); |
| 59 | expect(fetched.name).toEqual(created.instanceGroupName); |
| 60 | expect(fetched.description).toContain("[alchemy "); |
| 61 | expect(fetched.description).toContain("unmanaged backends"); |
| 62 | expect(fetched.namedPorts?.some((port) => port.name === "http")).toEqual(true); |
| 63 | |
| 64 | const updated = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | return yield* GCP.Compute.InstanceGroup("Web", { |
| 67 | instanceGroupName: created.instanceGroupName, |
| 68 | zone, |
| 69 | description: "unmanaged backends", |
| 70 | namedPorts: [ |
| 71 | { name: "http", port: 80 }, |
| 72 | { name: "https", port: 443 }, |
| 73 | ], |
| 74 | }); |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | expect(updated.instanceGroupName).toEqual(created.instanceGroupName); |
| 79 | expect(updated.id).toEqual(created.id); |
| 80 | expect(updated.namedPorts).toEqual([ |
| 81 | { name: "http", port: 80 }, |
| 82 | { name: "https", port: 443 }, |
| 83 | ]); |
| 84 | |
| 85 | const refetched = yield* compute.getInstanceGroups({ |
| 86 | project, |
| 87 | zone, |
| 88 | instanceGroup: updated.instanceGroupName, |
| 89 | }); |
| 90 | expect((refetched.namedPorts ?? []).map((port) => port.name).sort()).toEqual([ |
| 91 | "http", |
| 92 | "https", |
| 93 | ]); |
| 94 | |
| 95 | const replaced = yield* stack.deploy( |
| 96 | Effect.gen(function* () { |
| 97 | return yield* GCP.Compute.InstanceGroup("Web", { |
| 98 | instanceGroupName: created.instanceGroupName, |
| 99 | zone, |
| 100 | description: "replaced backends", |
| 101 | namedPorts: [ |
| 102 | { name: "http", port: 80 }, |
| 103 | { name: "https", port: 443 }, |
| 104 | ], |
| 105 | }); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(replaced.instanceGroupName).toEqual(created.instanceGroupName); |
| 110 | expect(replaced.description).toEqual("replaced backends"); |
| 111 | expect(replaced.id).not.toEqual(created.id); |
| 112 | |
| 113 | const replacedFetched = yield* compute.getInstanceGroups({ |
| 114 | project, |
| 115 | zone, |
| 116 | instanceGroup: replaced.instanceGroupName, |
| 117 | }); |
| 118 | expect(replacedFetched.description).toContain("replaced backends"); |
| 119 | expect(replacedFetched.id).toEqual(replaced.id); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | |
| 123 | const gone = yield* waitUntilGone(created.instanceGroupName); |
| 124 | expect(gone).toEqual("gone"); |
| 125 | }).pipe(logLevel), |
| 126 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 127 | ); |