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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 12 | |
| 13 | const waitUntilGone = (project: string, image: string) => |
| 14 | compute.getImages({ project, image }).pipe( |
| 15 | Effect.as("found" as const), |
| 16 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 17 | Effect.repeat({ |
| 18 | schedule: Schedule.spaced("2 seconds"), |
| 19 | until: (status) => status === "gone", |
| 20 | times: 18, |
| 21 | }), |
| 22 | ); |
| 23 | |
| 24 | test.provider.skipIf(!!process.env.FAST)( |
| 25 | "create, update, and delete an image", |
| 26 | (stack) => |
| 27 | Effect.gen(function* () { |
| 28 | yield* stack.destroy(); |
| 29 | |
| 30 | const created = yield* stack.deploy( |
| 31 | Effect.gen(function* () { |
| 32 | const disk = yield* GCP.Compute.Disk("Data", { |
| 33 | zone: "us-central1-a", |
| 34 | type: "pd-standard", |
| 35 | sizeGb: 10, |
| 36 | }); |
| 37 | const image = yield* GCP.Compute.Image("Boot", { |
| 38 | sourceDisk: disk.selfLink.as<string>(), |
| 39 | family: "alchemy-test", |
| 40 | description: "alchemy test image", |
| 41 | labels: { env: "test" }, |
| 42 | storageLocations: ["us-central1"], |
| 43 | }); |
| 44 | return { disk, image }; |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | expect(created.image.imageName).toEqual(expect.any(String)); |
| 49 | expect(created.image.status).toEqual("READY"); |
| 50 | expect(created.image.family).toEqual("alchemy-test"); |
| 51 | expect(created.image.description).toEqual("alchemy test image"); |
| 52 | expect(created.image.labels).toMatchObject({ env: "test" }); |
| 53 | expect(created.image.project).toEqual(expect.any(String)); |
| 54 | expect(created.image.selfLink).toEqual(expect.any(String)); |
| 55 | |
| 56 | const fetched = yield* compute.getImages({ |
| 57 | project: created.image.project, |
| 58 | image: created.image.imageName, |
| 59 | }); |
| 60 | expect(fetched.name).toEqual(created.image.imageName); |
| 61 | expect(fetched.status).toEqual("READY"); |
| 62 | expect(fetched.family).toEqual("alchemy-test"); |
| 63 | expect(fetched.labels?.env).toEqual("test"); |
| 64 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 65 | |
| 66 | const updated = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | const disk = yield* GCP.Compute.Disk("Data", { |
| 69 | diskName: created.disk.diskName, |
| 70 | zone: "us-central1-a", |
| 71 | type: "pd-standard", |
| 72 | sizeGb: 10, |
| 73 | }); |
| 74 | const image = yield* GCP.Compute.Image("Boot", { |
| 75 | imageName: created.image.imageName, |
| 76 | sourceDisk: disk.selfLink.as<string>(), |
| 77 | family: "alchemy-prod", |
| 78 | description: "updated alchemy test image", |
| 79 | labels: { env: "prod", role: "boot" }, |
| 80 | storageLocations: ["us-central1"], |
| 81 | }); |
| 82 | return { disk, image }; |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | expect(updated.image.imageName).toEqual(created.image.imageName); |
| 87 | expect(updated.image.family).toEqual("alchemy-prod"); |
| 88 | expect(updated.image.description).toEqual("updated alchemy test image"); |
| 89 | expect(updated.image.labels).toMatchObject({ env: "prod", role: "boot" }); |
| 90 | |
| 91 | const refetched = yield* compute.getImages({ |
| 92 | project: updated.image.project, |
| 93 | image: updated.image.imageName, |
| 94 | }); |
| 95 | expect(refetched.family).toEqual("alchemy-prod"); |
| 96 | expect(refetched.description).toEqual("updated alchemy test image"); |
| 97 | expect(refetched.labels?.env).toEqual("prod"); |
| 98 | expect(refetched.labels?.role).toEqual("boot"); |
| 99 | |
| 100 | yield* stack.destroy(); |
| 101 | |
| 102 | const gone = yield* waitUntilGone(created.image.project, created.image.imageName); |
| 103 | expect(gone).toEqual("gone"); |
| 104 | }).pipe(logLevel), |
| 105 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 240_000 }, |
| 106 | ); |