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, 06:00 UTC
| 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 | const vmName = "alchemy-ti-backend"; |
| 16 | |
| 17 | const lastSegment = (value: string | undefined): string => { |
| 18 | if (value === undefined || value.length === 0) return ""; |
| 19 | const parts = value.split("/").filter((part) => part.length > 0); |
| 20 | return parts[parts.length - 1] ?? ""; |
| 21 | }; |
| 22 | |
| 23 | const waitZoneOp = (operation: compute.Operation) => { |
| 24 | if (operation.status === "DONE") return Effect.succeed(operation); |
| 25 | const name = lastSegment(operation.name); |
| 26 | return GcpEnvironment.current.pipe( |
| 27 | Effect.flatMap(({ project }) => |
| 28 | compute.getZoneOperations({ project, zone, operation: name }).pipe( |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("2 seconds"), |
| 31 | until: (op) => op.status === "DONE", |
| 32 | times: 20, |
| 33 | }), |
| 34 | ), |
| 35 | ), |
| 36 | ); |
| 37 | }; |
| 38 | |
| 39 | const waitUntilGone = (targetInstance: string) => |
| 40 | GcpEnvironment.current.pipe( |
| 41 | Effect.flatMap(({ project }) => |
| 42 | compute.getTargetInstances({ project, zone, targetInstance }).pipe( |
| 43 | Effect.as("found" as const), |
| 44 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 45 | Effect.repeat({ |
| 46 | schedule: Schedule.spaced("2 seconds"), |
| 47 | until: (status) => status === "gone", |
| 48 | times: 10, |
| 49 | }), |
| 50 | ), |
| 51 | ), |
| 52 | ); |
| 53 | |
| 54 | const waitVmGone = () => |
| 55 | GcpEnvironment.current.pipe( |
| 56 | Effect.flatMap(({ project }) => |
| 57 | compute.getInstances({ project, zone, instance: vmName }).pipe( |
| 58 | Effect.as("found" as const), |
| 59 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 60 | Effect.repeat({ |
| 61 | schedule: Schedule.spaced("3 seconds"), |
| 62 | until: (status) => status === "gone", |
| 63 | times: 16, |
| 64 | }), |
| 65 | ), |
| 66 | ), |
| 67 | ); |
| 68 | |
| 69 | const getVm = () => |
| 70 | GcpEnvironment.current.pipe( |
| 71 | Effect.flatMap(({ project }) => |
| 72 | compute |
| 73 | .getInstances({ project, zone, instance: vmName }) |
| 74 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))), |
| 75 | ), |
| 76 | ); |
| 77 | |
| 78 | const ensureVm = () => |
| 79 | Effect.gen(function* () { |
| 80 | const { project } = yield* GcpEnvironment.current; |
| 81 | const existing = yield* getVm(); |
| 82 | if (existing !== undefined) return existing; |
| 83 | const operation = yield* compute |
| 84 | .insertInstances({ |
| 85 | project, |
| 86 | zone, |
| 87 | body: { |
| 88 | name: vmName, |
| 89 | machineType: `zones/${zone}/machineTypes/e2-micro`, |
| 90 | canIpForward: true, |
| 91 | disks: [ |
| 92 | { |
| 93 | boot: true, |
| 94 | autoDelete: true, |
| 95 | type: "PERSISTENT", |
| 96 | initializeParams: { |
| 97 | sourceImage: "projects/debian-cloud/global/images/family/debian-12", |
| 98 | diskSizeGb: "10", |
| 99 | }, |
| 100 | }, |
| 101 | ], |
| 102 | networkInterfaces: [{ network: "global/networks/default" }], |
| 103 | }, |
| 104 | }) |
| 105 | .pipe(Effect.catchTag("Conflict", () => Effect.succeed(undefined))); |
| 106 | if (operation !== undefined) { |
| 107 | yield* waitZoneOp(operation); |
| 108 | } |
| 109 | return yield* compute.getInstances({ project, zone, instance: vmName }).pipe( |
| 110 | Effect.retry({ |
| 111 | while: (error) => error._tag === "NotFound", |
| 112 | schedule: Schedule.spaced("2 seconds"), |
| 113 | times: 10, |
| 114 | }), |
| 115 | ); |
| 116 | }); |
| 117 | |
| 118 | const deleteVm = () => |
| 119 | Effect.gen(function* () { |
| 120 | const { project } = yield* GcpEnvironment.current; |
| 121 | const operation = yield* compute |
| 122 | .deleteInstances({ project, zone, instance: vmName }) |
| 123 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 124 | if (operation !== undefined) { |
| 125 | yield* waitZoneOp(operation).pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 126 | } |
| 127 | yield* waitVmGone(); |
| 128 | }); |
| 129 | |
| 130 | test.provider.skipIf(!!process.env.FAST)( |
| 131 | "create, update, and delete a target instance", |
| 132 | (stack) => |
| 133 | Effect.gen(function* () { |
| 134 | const { project } = yield* GcpEnvironment.current; |
| 135 | yield* stack.destroy(); |
| 136 | yield* deleteVm().pipe(Effect.ignore); |
| 137 | |
| 138 | const vm = yield* ensureVm(); |
| 139 | |
| 140 | const created = yield* stack.deploy( |
| 141 | Effect.gen(function* () { |
| 142 | return yield* GCP.Compute.TargetInstance("Target", { |
| 143 | description: "protocol frontend", |
| 144 | instance: vmName, |
| 145 | zone, |
| 146 | }); |
| 147 | }), |
| 148 | ); |
| 149 | |
| 150 | expect(created.targetInstanceName).toEqual(expect.any(String)); |
| 151 | expect(created.zone).toEqual(zone); |
| 152 | expect(created.description).toEqual("protocol frontend"); |
| 153 | expect(lastSegment(created.instance)).toEqual(vmName); |
| 154 | expect(created.natPolicy).toEqual("NO_NAT"); |
| 155 | |
| 156 | const fetched = yield* compute.getTargetInstances({ |
| 157 | project, |
| 158 | zone, |
| 159 | targetInstance: created.targetInstanceName, |
| 160 | }); |
| 161 | expect(fetched.name).toEqual(created.targetInstanceName); |
| 162 | expect(lastSegment(fetched.instance)).toEqual(vmName); |
| 163 | expect(fetched.description).toContain("[alchemy "); |
| 164 | expect(fetched.description).toContain("protocol frontend"); |
| 165 | expect(fetched.natPolicy).toEqual("NO_NAT"); |
| 166 | expect(lastSegment(vm.name)).toEqual(vmName); |
| 167 | |
| 168 | const updated = yield* stack.deploy( |
| 169 | Effect.gen(function* () { |
| 170 | return yield* GCP.Compute.TargetInstance("Target", { |
| 171 | targetInstanceName: created.targetInstanceName, |
| 172 | description: "updated protocol frontend", |
| 173 | instance: vmName, |
| 174 | zone, |
| 175 | }); |
| 176 | }), |
| 177 | ); |
| 178 | |
| 179 | expect(updated.targetInstanceName).toEqual(created.targetInstanceName); |
| 180 | expect(updated.description).toEqual("updated protocol frontend"); |
| 181 | expect(lastSegment(updated.instance)).toEqual(vmName); |
| 182 | |
| 183 | const refetched = yield* compute.getTargetInstances({ |
| 184 | project, |
| 185 | zone, |
| 186 | targetInstance: updated.targetInstanceName, |
| 187 | }); |
| 188 | expect(refetched.description).toContain("updated protocol frontend"); |
| 189 | expect(lastSegment(refetched.instance)).toEqual(vmName); |
| 190 | |
| 191 | yield* stack.destroy(); |
| 192 | |
| 193 | const gone = yield* waitUntilGone(created.targetInstanceName); |
| 194 | expect(gone).toEqual("gone"); |
| 195 | |
| 196 | yield* deleteVm(); |
| 197 | }).pipe(logLevel), |
| 198 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 }, |
| 199 | ); |