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 waitUntilGone = (targetHttpProxyName: string) => |
| 15 | GcpEnvironment.current.pipe( |
| 16 | Effect.flatMap(({ project }) => |
| 17 | compute.getTargetHttpProxies({ project, targetHttpProxy: targetHttpProxyName }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("1 second"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 10, |
| 24 | }), |
| 25 | ), |
| 26 | ), |
| 27 | ); |
| 28 | |
| 29 | const resourceTail = (value: string | undefined): string => { |
| 30 | if (value === undefined || value.length === 0) return ""; |
| 31 | const parts = value.split("/").filter((part) => part.length > 0); |
| 32 | return parts[parts.length - 1] ?? ""; |
| 33 | }; |
| 34 | |
| 35 | test.provider( |
| 36 | "create, update, and delete a target http proxy", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const created = yield* stack.deploy( |
| 43 | Effect.gen(function* () { |
| 44 | const map = yield* GCP.Compute.UrlMap("Web", { |
| 45 | description: "https redirect", |
| 46 | defaultUrlRedirect: { |
| 47 | httpsRedirect: true, |
| 48 | hostRedirect: "example.com", |
| 49 | stripQuery: false, |
| 50 | }, |
| 51 | }); |
| 52 | return yield* GCP.Compute.TargetHttpProxy("Proxy", { |
| 53 | description: "http frontend", |
| 54 | urlMap: map.urlMapName, |
| 55 | }); |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | expect(created.targetHttpProxyName).toEqual(expect.any(String)); |
| 60 | expect(created.description).toEqual("http frontend"); |
| 61 | expect(resourceTail(created.urlMap).length).toBeGreaterThan(0); |
| 62 | |
| 63 | const fetched = yield* compute.getTargetHttpProxies({ |
| 64 | project, |
| 65 | targetHttpProxy: created.targetHttpProxyName, |
| 66 | }); |
| 67 | expect(fetched.name).toEqual(created.targetHttpProxyName); |
| 68 | expect(resourceTail(fetched.urlMap)).toEqual(resourceTail(created.urlMap)); |
| 69 | expect(fetched.description).toContain("[alchemy "); |
| 70 | expect(fetched.description).toContain("http frontend"); |
| 71 | |
| 72 | const updated = yield* stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | yield* GCP.Compute.UrlMap("Web", { |
| 75 | description: "https redirect", |
| 76 | defaultUrlRedirect: { |
| 77 | httpsRedirect: true, |
| 78 | hostRedirect: "example.com", |
| 79 | stripQuery: false, |
| 80 | }, |
| 81 | }); |
| 82 | const other = yield* GCP.Compute.UrlMap("Other", { |
| 83 | description: "alt redirect", |
| 84 | defaultUrlRedirect: { |
| 85 | httpsRedirect: true, |
| 86 | hostRedirect: "example.org", |
| 87 | stripQuery: true, |
| 88 | }, |
| 89 | }); |
| 90 | return yield* GCP.Compute.TargetHttpProxy("Proxy", { |
| 91 | targetHttpProxyName: created.targetHttpProxyName, |
| 92 | description: "updated frontend", |
| 93 | urlMap: other.urlMapName, |
| 94 | }); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | expect(updated.targetHttpProxyName).toEqual(created.targetHttpProxyName); |
| 99 | expect(updated.description).toEqual("updated frontend"); |
| 100 | expect(resourceTail(updated.urlMap)).toEqual(expect.any(String)); |
| 101 | |
| 102 | const refetched = yield* compute.getTargetHttpProxies({ |
| 103 | project, |
| 104 | targetHttpProxy: updated.targetHttpProxyName, |
| 105 | }); |
| 106 | expect(refetched.description).toContain("updated frontend"); |
| 107 | expect(resourceTail(refetched.urlMap)).toEqual(resourceTail(updated.urlMap)); |
| 108 | expect(resourceTail(refetched.urlMap)).not.toEqual(resourceTail(created.urlMap)); |
| 109 | |
| 110 | yield* stack.destroy(); |
| 111 | |
| 112 | const gone = yield* waitUntilGone(created.targetHttpProxyName); |
| 113 | expect(gone).toEqual("gone"); |
| 114 | }).pipe(logLevel), |
| 115 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 116 | ); |