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 | // Resize requests only accept accelerator (GPU) templates — a CPU template |
| 15 | // fails with `Resize requests without accelerators are not supported.` GPUs |
| 16 | // need quota and cost real money, so set GCP_TEST_MIG_RESIZE_REQUEST=1 (with |
| 17 | // GPU quota) to opt in. |
| 18 | const runLifecycle = !!process.env.GCP_TEST_MIG_RESIZE_REQUEST && !process.env.FAST; |
| 19 | |
| 20 | const zone = "us-central1-a"; |
| 21 | |
| 22 | const templateProps: GCP.Compute.InstanceTemplateProps = { |
| 23 | machineType: "e2-micro", |
| 24 | disks: [ |
| 25 | { |
| 26 | boot: true, |
| 27 | autoDelete: true, |
| 28 | sourceImage: "projects/debian-cloud/global/images/family/debian-12", |
| 29 | diskSizeGb: 10, |
| 30 | }, |
| 31 | ], |
| 32 | networkInterfaces: [{ network: "global/networks/default" }], |
| 33 | }; |
| 34 | |
| 35 | const waitUntilGone = ( |
| 36 | project: string, |
| 37 | zoneName: string, |
| 38 | instanceGroupManager: string, |
| 39 | resizeRequest: string, |
| 40 | ) => |
| 41 | compute |
| 42 | .getInstanceGroupManagerResizeRequests({ |
| 43 | project, |
| 44 | zone: zoneName, |
| 45 | instanceGroupManager, |
| 46 | resizeRequest, |
| 47 | }) |
| 48 | .pipe( |
| 49 | Effect.as("found" as const), |
| 50 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 51 | Effect.repeat({ |
| 52 | schedule: Schedule.spaced("1 second"), |
| 53 | until: (status) => status === "gone", |
| 54 | times: 10, |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | test.provider( |
| 59 | "insertInstanceGroupManagerResizeRequests on a missing manager fails with NotFound", |
| 60 | () => |
| 61 | Effect.gen(function* () { |
| 62 | const { project } = yield* GcpEnvironment.current; |
| 63 | const error = yield* Effect.flip( |
| 64 | compute.insertInstanceGroupManagerResizeRequests({ |
| 65 | project, |
| 66 | zone, |
| 67 | instanceGroupManager: "does-not-exist", |
| 68 | body: { |
| 69 | name: "alchemy-rr-probe", |
| 70 | description: "alchemy entitlement probe", |
| 71 | resizeBy: 1, |
| 72 | }, |
| 73 | }), |
| 74 | ); |
| 75 | expect(error._tag).toEqual("NotFound"); |
| 76 | }).pipe(logLevel), |
| 77 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 60_000 }, |
| 78 | ); |
| 79 | |
| 80 | test.provider.skipIf(!runLifecycle)( |
| 81 | "create and delete an instance group manager resize request", |
| 82 | (stack) => |
| 83 | Effect.gen(function* () { |
| 84 | yield* stack.destroy(); |
| 85 | |
| 86 | const created = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | const template = yield* GCP.Compute.InstanceTemplate("Template", { |
| 89 | ...templateProps, |
| 90 | }); |
| 91 | const manager = yield* GCP.Compute.InstanceGroupManager("Manager", { |
| 92 | zone, |
| 93 | instanceTemplate: template.templateName, |
| 94 | targetSize: 0, |
| 95 | // Resize requests are rejected while automatic repair is on. |
| 96 | instanceLifecyclePolicy: { defaultActionOnFailure: "DO_NOTHING" }, |
| 97 | }); |
| 98 | const request = yield* GCP.Compute.InstanceGroupManagerResizeRequest("Burst", { |
| 99 | zone, |
| 100 | instanceGroupManager: manager.managerName, |
| 101 | resizeBy: 1, |
| 102 | requestedRunDuration: { seconds: "600" }, |
| 103 | description: "queued burst", |
| 104 | }); |
| 105 | return { template, manager, request }; |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(created.request.resizeRequestName).toEqual(expect.any(String)); |
| 110 | expect(created.request.resizeBy).toEqual(1); |
| 111 | expect(created.request.description).toEqual("queued burst"); |
| 112 | expect(created.request.instanceGroupManager).toEqual(created.manager.managerName); |
| 113 | |
| 114 | const fetched = yield* compute.getInstanceGroupManagerResizeRequests({ |
| 115 | project: created.request.project, |
| 116 | zone, |
| 117 | instanceGroupManager: created.manager.managerName, |
| 118 | resizeRequest: created.request.resizeRequestName, |
| 119 | }); |
| 120 | expect(fetched.name).toEqual(created.request.resizeRequestName); |
| 121 | expect(fetched.description).toContain("[alchemy "); |
| 122 | |
| 123 | yield* stack.destroy(); |
| 124 | const gone = yield* waitUntilGone( |
| 125 | created.request.project, |
| 126 | zone, |
| 127 | created.manager.managerName, |
| 128 | created.request.resizeRequestName, |
| 129 | ); |
| 130 | expect(gone).toEqual("gone"); |
| 131 | }).pipe(logLevel), |
| 132 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 }, |
| 133 | ); |