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 Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import type * as HttpClientError from "effect/http/HttpClientError"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { DEFAULT_NETWORK } from "../networkQuota.ts"; |
| 12 | import { DATA_DEVICE, MARKER, REGION, webVm, ZONE } from "./fixtures/web-vm.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: GCP.providers() }); |
| 15 | |
| 16 | class PageNotServed extends Data.TaggedError("PageNotServed")<{ |
| 17 | readonly reason: string; |
| 18 | }> {} |
| 19 | |
| 20 | class StillExists extends Data.TaggedError("StillExists")<{ |
| 21 | readonly what: string; |
| 22 | }> {} |
| 23 | |
| 24 | // Poll the static IP until the startup script's HTTP server answers with the |
| 25 | // marker page. Transport errors (VM still booting) count as "not ready". |
| 26 | const fetchPage = (ip: string) => |
| 27 | HttpClient.get(`http://${ip}/index.html`).pipe( |
| 28 | Effect.timeout("5 seconds"), |
| 29 | Effect.flatMap((res): Effect.Effect<string, HttpClientError.HttpClientError | PageNotServed> => |
| 30 | res.status === 200 |
| 31 | ? res.text |
| 32 | : Effect.fail(new PageNotServed({ reason: `status ${res.status}` })), |
| 33 | ), |
| 34 | Effect.flatMap((body) => |
| 35 | body.includes(MARKER) |
| 36 | ? Effect.succeed(body) |
| 37 | : Effect.fail(new PageNotServed({ reason: `body ${body}` })), |
| 38 | ), |
| 39 | Effect.mapError((error) => |
| 40 | error instanceof PageNotServed ? error : new PageNotServed({ reason: String(error) }), |
| 41 | ), |
| 42 | Effect.retry({ |
| 43 | while: (error) => error._tag === "PageNotServed", |
| 44 | schedule: Schedule.spaced("5 seconds"), |
| 45 | times: 60, |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | // Bounded wait until an out-of-band probe reports the resource gone. |
| 50 | const waitUntilGone = <E, R>(what: string, probe: Effect.Effect<boolean, E, R>) => |
| 51 | probe.pipe( |
| 52 | Effect.flatMap((gone) => (gone ? Effect.void : Effect.fail(new StillExists({ what })))), |
| 53 | Effect.retry({ |
| 54 | while: (error) => error instanceof StillExists, |
| 55 | schedule: Schedule.spaced("3 seconds"), |
| 56 | times: 20, |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | // GCP counterpart of the AWS EC2 Instance + Storage smokes: a Debian VM in its |
| 61 | // project's `default` network behind a tcp:80 firewall, reachable on a reserved static IP, with an |
| 62 | // extra persistent disk attached. Heavy (VM boot), so skipped under `FAST=1`. |
| 63 | test.provider.skipIf(!!process.env.FAST)( |
| 64 | "deploys a VM with a static IP and data disk that serves HTTP, updates in place, and tears down", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | const { project } = yield* GcpEnvironment.current; |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | const created = yield* stack.deploy(webVm("initial")); |
| 71 | |
| 72 | expect(created.status).toEqual("RUNNING"); |
| 73 | expect(created.address).toEqual(expect.any(String)); |
| 74 | expect(created.natIP).toEqual(created.address); |
| 75 | expect(created.attachedDisks).toEqual([ |
| 76 | expect.objectContaining({ |
| 77 | source: expect.stringContaining(`/disks/${created.diskName}`), |
| 78 | deviceName: DATA_DEVICE, |
| 79 | mode: "READ_WRITE", |
| 80 | }), |
| 81 | ]); |
| 82 | |
| 83 | // Out-of-band: the VM runs with the static IP and the data disk attached. |
| 84 | const vm = yield* compute.getInstances({ |
| 85 | project, |
| 86 | zone: ZONE, |
| 87 | instance: created.instanceName, |
| 88 | }); |
| 89 | expect(vm.status).toEqual("RUNNING"); |
| 90 | expect(vm.networkInterfaces?.[0]?.accessConfigs?.[0]?.natIP).toEqual(created.address); |
| 91 | expect(vm.networkInterfaces?.[0]?.subnetwork).toContain(`/subnetworks/${DEFAULT_NETWORK}`); |
| 92 | expect( |
| 93 | vm.disks?.some( |
| 94 | (d) => |
| 95 | d.boot !== true && |
| 96 | d.deviceName === DATA_DEVICE && |
| 97 | d.source?.endsWith(`/disks/${created.diskName}`), |
| 98 | ), |
| 99 | ).toBe(true); |
| 100 | const disk = yield* compute.getDisks({ |
| 101 | project, |
| 102 | zone: ZONE, |
| 103 | disk: created.diskName, |
| 104 | }); |
| 105 | expect(disk.users ?? []).toEqual([ |
| 106 | expect.stringContaining(`/instances/${created.instanceName}`), |
| 107 | ]); |
| 108 | const address = yield* compute.getAddresses({ |
| 109 | project, |
| 110 | region: REGION, |
| 111 | address: created.addressName, |
| 112 | }); |
| 113 | expect(address.status).toEqual("IN_USE"); |
| 114 | |
| 115 | // In-band: the startup script serves the marker page on the static IP |
| 116 | // and sees the data disk as a guest block device. |
| 117 | const page = yield* fetchPage(created.address!); |
| 118 | expect(page).toContain(`host=${created.instanceName}`); |
| 119 | expect(page).toContain("disk=present"); |
| 120 | |
| 121 | // Label + metadata change reconciles in place (same instance id). |
| 122 | const updated = yield* stack.deploy(webVm("updated")); |
| 123 | expect(updated.instanceId).toEqual(created.instanceId); |
| 124 | expect(updated.instanceName).toEqual(created.instanceName); |
| 125 | expect(updated.labels).toMatchObject({ |
| 126 | smoke: "instance", |
| 127 | phase: "updated", |
| 128 | }); |
| 129 | expect(updated.metadata).toMatchObject({ "smoke-phase": "updated" }); |
| 130 | expect(updated.natIP).toEqual(created.address); |
| 131 | |
| 132 | const vm2 = yield* compute.getInstances({ |
| 133 | project, |
| 134 | zone: ZONE, |
| 135 | instance: created.instanceName, |
| 136 | }); |
| 137 | expect(vm2.id).toEqual(created.instanceId); |
| 138 | expect(vm2.labels?.phase).toEqual("updated"); |
| 139 | expect(vm2.metadata?.items?.find((item) => item.key === "smoke-phase")?.value).toEqual( |
| 140 | "updated", |
| 141 | ); |
| 142 | expect(vm2.disks?.filter((d) => d.boot !== true)).toHaveLength(1); |
| 143 | const updatedPage = yield* fetchPage(created.address!); |
| 144 | expect(updatedPage).toContain(MARKER); |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | |
| 148 | // Zero-orphan proof for every billed / named resource. |
| 149 | yield* waitUntilGone( |
| 150 | "instance", |
| 151 | compute |
| 152 | .getInstances({ |
| 153 | project, |
| 154 | zone: ZONE, |
| 155 | instance: created.instanceName, |
| 156 | }) |
| 157 | .pipe( |
| 158 | Effect.as(false), |
| 159 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 160 | ), |
| 161 | ); |
| 162 | yield* waitUntilGone( |
| 163 | "disk", |
| 164 | compute.getDisks({ project, zone: ZONE, disk: created.diskName }).pipe( |
| 165 | Effect.as(false), |
| 166 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 167 | ), |
| 168 | ); |
| 169 | yield* waitUntilGone( |
| 170 | "address", |
| 171 | compute |
| 172 | .getAddresses({ |
| 173 | project, |
| 174 | region: REGION, |
| 175 | address: created.addressName, |
| 176 | }) |
| 177 | .pipe( |
| 178 | Effect.as(false), |
| 179 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 180 | ), |
| 181 | ); |
| 182 | yield* waitUntilGone( |
| 183 | "firewall", |
| 184 | compute.getFirewalls({ project, firewall: created.firewallName }).pipe( |
| 185 | Effect.as(false), |
| 186 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 187 | ), |
| 188 | ); |
| 189 | }), |
| 190 | { |
| 191 | tags: ["provider:gcp", "provider:gcp:compute", "smoke", "live"], |
| 192 | timeout: 900_000, |
| 193 | }, |
| 194 | ); |