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 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 = (targetSslProxyName: string) => |
| 16 | GcpEnvironment.current.pipe( |
| 17 | Effect.flatMap(({ project }) => |
| 18 | compute.getTargetSslProxies({ project, targetSslProxy: targetSslProxyName }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ), |
| 27 | ), |
| 28 | ); |
| 29 | |
| 30 | const resourceTail = (value: string | undefined): string => { |
| 31 | if (value === undefined || value.length === 0) return ""; |
| 32 | const parts = value.split("/").filter((part) => part.length > 0); |
| 33 | return parts[parts.length - 1] ?? ""; |
| 34 | }; |
| 35 | |
| 36 | test.provider( |
| 37 | "create, update, and delete a target ssl proxy", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | const { project } = yield* GcpEnvironment.current; |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const created = yield* stack.deploy( |
| 44 | Effect.gen(function* () { |
| 45 | const check = yield* GCP.Compute.HealthCheck("Probe", { |
| 46 | description: "ssl probe", |
| 47 | type: "TCP", |
| 48 | tcpHealthCheck: { port: 443 }, |
| 49 | }); |
| 50 | const backend = yield* GCP.Compute.BackendService("SslA", { |
| 51 | protocol: "SSL", |
| 52 | loadBalancingScheme: "EXTERNAL", |
| 53 | description: "ssl backend a", |
| 54 | healthChecks: [check.selfLink.as<string>()], |
| 55 | }); |
| 56 | const cert = yield* GCP.Compute.SslCertificate("TlsA", { |
| 57 | description: "frontend tls a", |
| 58 | certificate: CERT_A_PEM, |
| 59 | privateKey: KEY_A_PEM, |
| 60 | }); |
| 61 | const proxy = yield* GCP.Compute.TargetSslProxy("Proxy", { |
| 62 | description: "ssl frontend", |
| 63 | service: backend.name, |
| 64 | sslCertificates: [cert.sslCertificateName], |
| 65 | }); |
| 66 | return { check, backend, cert, proxy }; |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | expect(created.proxy.targetSslProxyName).toEqual(expect.any(String)); |
| 71 | expect(created.proxy.description).toEqual("ssl frontend"); |
| 72 | expect( |
| 73 | created.proxy.proxyHeader === "NONE" || created.proxy.proxyHeader === undefined, |
| 74 | ).toEqual(true); |
| 75 | expect(resourceTail(created.proxy.service)).toEqual(created.backend.name); |
| 76 | expect(created.proxy.sslCertificates.map(resourceTail)).toContain( |
| 77 | created.cert.sslCertificateName, |
| 78 | ); |
| 79 | |
| 80 | const fetched = yield* compute.getTargetSslProxies({ |
| 81 | project, |
| 82 | targetSslProxy: created.proxy.targetSslProxyName, |
| 83 | }); |
| 84 | expect(fetched.name).toEqual(created.proxy.targetSslProxyName); |
| 85 | expect(resourceTail(fetched.service)).toEqual(resourceTail(created.proxy.service)); |
| 86 | expect(fetched.description).toContain("[alchemy "); |
| 87 | expect(fetched.description).toContain("ssl frontend"); |
| 88 | expect((fetched.sslCertificates ?? []).map(resourceTail)).toContain( |
| 89 | created.cert.sslCertificateName, |
| 90 | ); |
| 91 | |
| 92 | const updated = yield* stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | const check = yield* GCP.Compute.HealthCheck("Probe", { |
| 95 | healthCheckName: created.check.healthCheckName, |
| 96 | description: "ssl probe", |
| 97 | type: "TCP", |
| 98 | tcpHealthCheck: { port: 443 }, |
| 99 | }); |
| 100 | yield* GCP.Compute.BackendService("SslA", { |
| 101 | name: created.backend.name, |
| 102 | protocol: "SSL", |
| 103 | loadBalancingScheme: "EXTERNAL", |
| 104 | description: "ssl backend a", |
| 105 | healthChecks: [check.selfLink.as<string>()], |
| 106 | }); |
| 107 | const other = yield* GCP.Compute.BackendService("SslB", { |
| 108 | protocol: "SSL", |
| 109 | loadBalancingScheme: "EXTERNAL", |
| 110 | description: "ssl backend b", |
| 111 | healthChecks: [check.selfLink.as<string>()], |
| 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.TargetSslProxy("Proxy", { |
| 125 | targetSslProxyName: created.proxy.targetSslProxyName, |
| 126 | description: "ssl frontend", |
| 127 | service: other.name, |
| 128 | sslCertificates: [certB.sslCertificateName], |
| 129 | proxyHeader: "PROXY_V1", |
| 130 | }); |
| 131 | return { other, certB, proxy }; |
| 132 | }), |
| 133 | ); |
| 134 | |
| 135 | expect(updated.proxy.targetSslProxyName).toEqual(created.proxy.targetSslProxyName); |
| 136 | expect(updated.proxy.description).toEqual("ssl frontend"); |
| 137 | expect(updated.proxy.proxyHeader).toEqual("PROXY_V1"); |
| 138 | expect(resourceTail(updated.proxy.service)).toEqual(updated.other.name); |
| 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.getTargetSslProxies({ |
| 147 | project, |
| 148 | targetSslProxy: updated.proxy.targetSslProxyName, |
| 149 | }); |
| 150 | expect(refetched.description).toContain("ssl frontend"); |
| 151 | expect(refetched.proxyHeader).toEqual("PROXY_V1"); |
| 152 | expect(resourceTail(refetched.service)).toEqual(resourceTail(updated.proxy.service)); |
| 153 | expect(resourceTail(refetched.service)).not.toEqual(resourceTail(created.proxy.service)); |
| 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.targetSslProxyName); |
| 161 | expect(gone).toEqual("gone"); |
| 162 | }).pipe(logLevel), |
| 163 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 }, |
| 164 | ); |