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 | import { CERT_A_PEM, CERT_B_PEM, KEY_A_PEM, KEY_B_PEM } from "./fixtures/https-proxy-cert.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const waitUntilGone = (targetHttpsProxyName: string) => |
| 16 | GcpEnvironment.current.pipe( |
| 17 | Effect.flatMap(({ project }) => |
| 18 | compute |
| 19 | .getTargetHttpsProxies({ |
| 20 | project, |
| 21 | targetHttpsProxy: targetHttpsProxyName, |
| 22 | }) |
| 23 | .pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("1 second"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ), |
| 32 | ), |
| 33 | ); |
| 34 | |
| 35 | const resourceTail = (value: string | undefined): string => { |
| 36 | if (value === undefined || value.length === 0) return ""; |
| 37 | const parts = value.split("/").filter((part) => part.length > 0); |
| 38 | return parts[parts.length - 1] ?? ""; |
| 39 | }; |
| 40 | |
| 41 | test.provider( |
| 42 | "create, update, and delete a target https proxy", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | const { project } = yield* GcpEnvironment.current; |
| 46 | yield* stack.destroy(); |
| 47 | |
| 48 | const created = yield* stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | const map = yield* GCP.Compute.UrlMap("Web", { |
| 51 | description: "https redirect", |
| 52 | defaultUrlRedirect: { |
| 53 | httpsRedirect: true, |
| 54 | hostRedirect: "example.com", |
| 55 | stripQuery: false, |
| 56 | }, |
| 57 | }); |
| 58 | const cert = yield* GCP.Compute.SslCertificate("TlsA", { |
| 59 | description: "frontend tls a", |
| 60 | certificate: CERT_A_PEM, |
| 61 | privateKey: KEY_A_PEM, |
| 62 | }); |
| 63 | const proxy = yield* GCP.Compute.TargetHttpsProxy("Proxy", { |
| 64 | description: "https frontend", |
| 65 | urlMap: map.urlMapName, |
| 66 | sslCertificates: [cert.sslCertificateName], |
| 67 | }); |
| 68 | return { map, cert, proxy }; |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(created.proxy.targetHttpsProxyName).toEqual(expect.any(String)); |
| 73 | expect(created.proxy.description).toEqual("https frontend"); |
| 74 | expect( |
| 75 | created.proxy.quicOverride === "NONE" || created.proxy.quicOverride === undefined, |
| 76 | ).toEqual(true); |
| 77 | expect(resourceTail(created.proxy.urlMap)).toEqual(created.map.urlMapName); |
| 78 | expect(created.proxy.sslCertificates.map(resourceTail)).toContain( |
| 79 | created.cert.sslCertificateName, |
| 80 | ); |
| 81 | |
| 82 | const fetched = yield* compute.getTargetHttpsProxies({ |
| 83 | project, |
| 84 | targetHttpsProxy: created.proxy.targetHttpsProxyName, |
| 85 | }); |
| 86 | expect(fetched.name).toEqual(created.proxy.targetHttpsProxyName); |
| 87 | expect(resourceTail(fetched.urlMap)).toEqual(resourceTail(created.proxy.urlMap)); |
| 88 | expect(fetched.description).toContain("[alchemy "); |
| 89 | expect(fetched.description).toContain("https frontend"); |
| 90 | expect((fetched.sslCertificates ?? []).map(resourceTail)).toContain( |
| 91 | created.cert.sslCertificateName, |
| 92 | ); |
| 93 | |
| 94 | const updated = yield* stack.deploy( |
| 95 | Effect.gen(function* () { |
| 96 | yield* GCP.Compute.UrlMap("Web", { |
| 97 | urlMapName: created.map.urlMapName, |
| 98 | description: "https redirect", |
| 99 | defaultUrlRedirect: { |
| 100 | httpsRedirect: true, |
| 101 | hostRedirect: "example.com", |
| 102 | stripQuery: false, |
| 103 | }, |
| 104 | }); |
| 105 | const other = yield* GCP.Compute.UrlMap("Other", { |
| 106 | description: "alt redirect", |
| 107 | defaultUrlRedirect: { |
| 108 | httpsRedirect: true, |
| 109 | hostRedirect: "example.org", |
| 110 | stripQuery: true, |
| 111 | }, |
| 112 | }); |
| 113 | yield* GCP.Compute.SslCertificate("TlsA", { |
| 114 | sslCertificateName: created.cert.sslCertificateName, |
| 115 | description: "frontend tls a", |
| 116 | certificate: CERT_A_PEM, |
| 117 | privateKey: KEY_A_PEM, |
| 118 | }); |
| 119 | const certB = yield* GCP.Compute.SslCertificate("TlsB", { |
| 120 | description: "frontend tls b", |
| 121 | certificate: CERT_B_PEM, |
| 122 | privateKey: KEY_B_PEM, |
| 123 | }); |
| 124 | const proxy = yield* GCP.Compute.TargetHttpsProxy("Proxy", { |
| 125 | targetHttpsProxyName: created.proxy.targetHttpsProxyName, |
| 126 | description: "updated https", |
| 127 | urlMap: other.urlMapName, |
| 128 | sslCertificates: [certB.sslCertificateName], |
| 129 | quicOverride: "ENABLE", |
| 130 | }); |
| 131 | return { other, certB, proxy }; |
| 132 | }), |
| 133 | ); |
| 134 | |
| 135 | expect(updated.proxy.targetHttpsProxyName).toEqual(created.proxy.targetHttpsProxyName); |
| 136 | expect(updated.proxy.description).toEqual("updated https"); |
| 137 | expect(updated.proxy.quicOverride).toEqual("ENABLE"); |
| 138 | expect(resourceTail(updated.proxy.urlMap)).toEqual(updated.other.urlMapName); |
| 139 | expect(updated.proxy.sslCertificates.map(resourceTail)).toContain( |
| 140 | updated.certB.sslCertificateName, |
| 141 | ); |
| 142 | expect(updated.proxy.sslCertificates.map(resourceTail)).not.toContain( |
| 143 | created.cert.sslCertificateName, |
| 144 | ); |
| 145 | |
| 146 | const refetched = yield* compute.getTargetHttpsProxies({ |
| 147 | project, |
| 148 | targetHttpsProxy: updated.proxy.targetHttpsProxyName, |
| 149 | }); |
| 150 | expect(refetched.description).toContain("updated https"); |
| 151 | expect(refetched.quicOverride).toEqual("ENABLE"); |
| 152 | expect(resourceTail(refetched.urlMap)).toEqual(resourceTail(updated.proxy.urlMap)); |
| 153 | expect(resourceTail(refetched.urlMap)).not.toEqual(resourceTail(created.proxy.urlMap)); |
| 154 | expect((refetched.sslCertificates ?? []).map(resourceTail)).toContain( |
| 155 | updated.certB.sslCertificateName, |
| 156 | ); |
| 157 | |
| 158 | yield* stack.destroy(); |
| 159 | |
| 160 | const gone = yield* waitUntilGone(created.proxy.targetHttpsProxyName); |
| 161 | expect(gone).toEqual("gone"); |
| 162 | }).pipe(logLevel), |
| 163 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 164 | ); |