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 appengine from "@distilled.cloud/gcp/appengine_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { isServiceEnabled } from "../ServiceState.ts"; |
| 10 | import { CERT_A, CERT_B, KEY_A, KEY_B } from "./fixtures/cert.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: GCP.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | // Needs an App Engine application in the project, which is permanent once |
| 17 | // created (it can never be deleted). The testing project has none and the |
| 18 | // App Engine Admin API is off (ServiceDisabled: "App Engine Admin API has not been |
| 19 | // used in project ... or it is disabled."). Set GCP_TEST_APPENGINE_APP=1 on a |
| 20 | // project with an app to run the lifecycle. |
| 21 | const runLifecycle = !!process.env.GCP_TEST_APPENGINE_APP; |
| 22 | |
| 23 | const location = "us-central"; |
| 24 | |
| 25 | const waitUntilGone = ( |
| 26 | projectsId: string, |
| 27 | locationsId: string, |
| 28 | applicationsId: string, |
| 29 | certificateId: string, |
| 30 | ) => |
| 31 | appengine |
| 32 | .getProjectsLocationsApplicationsAuthorizedCertificates({ |
| 33 | projectsId, |
| 34 | locationsId, |
| 35 | applicationsId, |
| 36 | authorizedCertificatesId: certificateId, |
| 37 | }) |
| 38 | .pipe( |
| 39 | Effect.as("found" as const), |
| 40 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 41 | Effect.repeat({ |
| 42 | schedule: Schedule.spaced("1 second"), |
| 43 | until: (status) => status === "gone", |
| 44 | times: 10, |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | test.provider( |
| 49 | "getProjectsLocationsApplicationsAuthorizedCertificates on a missing certificate fails with a typed tag", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | // With the API enabled, this project (no App Engine app / no Firebase |
| 53 | // setup) answers an untyped INTERNAL / INVALID_ARGUMENT: the probe only |
| 54 | // holds while the API is disabled. |
| 55 | if (yield* isServiceEnabled("appengine.googleapis.com")) return; |
| 56 | const { project } = yield* GcpEnvironment.current; |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const error = yield* Effect.flip( |
| 60 | appengine.getProjectsLocationsApplicationsAuthorizedCertificates({ |
| 61 | projectsId: project, |
| 62 | locationsId: location, |
| 63 | applicationsId: project, |
| 64 | authorizedCertificatesId: "missing", |
| 65 | }), |
| 66 | ); |
| 67 | expect(error._tag).toEqual("ServiceDisabled"); |
| 68 | |
| 69 | yield* stack.destroy(); |
| 70 | }).pipe(logLevel), |
| 71 | { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 }, |
| 72 | ); |
| 73 | |
| 74 | test.provider.skipIf(runLifecycle)( |
| 75 | "createProjectsLocationsApplicationsAuthorizedCertificates without an App Engine app fails with a typed tag", |
| 76 | (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | // With the API enabled, this project (no App Engine app / no Firebase |
| 79 | // setup) answers an untyped INTERNAL / INVALID_ARGUMENT: the probe only |
| 80 | // holds while the API is disabled. |
| 81 | if (yield* isServiceEnabled("appengine.googleapis.com")) return; |
| 82 | const { project } = yield* GcpEnvironment.current; |
| 83 | yield* stack.destroy(); |
| 84 | |
| 85 | const error = yield* Effect.flip( |
| 86 | appengine.createProjectsLocationsApplicationsAuthorizedCertificates({ |
| 87 | projectsId: project, |
| 88 | locationsId: location, |
| 89 | applicationsId: project, |
| 90 | body: { |
| 91 | displayName: "Alchemy Appengine Probe", |
| 92 | certificateRawData: { |
| 93 | publicCertificate: CERT_A, |
| 94 | privateKey: KEY_A, |
| 95 | }, |
| 96 | }, |
| 97 | }), |
| 98 | ); |
| 99 | expect(error._tag).toEqual("ServiceDisabled"); |
| 100 | |
| 101 | yield* stack.destroy(); |
| 102 | }).pipe(logLevel), |
| 103 | { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 }, |
| 104 | ); |
| 105 | |
| 106 | test.provider.skipIf(!runLifecycle)( |
| 107 | "create, update, and delete an applications authorized certificate", |
| 108 | (stack) => |
| 109 | Effect.gen(function* () { |
| 110 | const { project } = yield* GcpEnvironment.current; |
| 111 | yield* stack.destroy(); |
| 112 | |
| 113 | const created = yield* stack.deploy( |
| 114 | Effect.gen(function* () { |
| 115 | return yield* GCP.AppEngine.ApplicationsAuthorizedCertificate("FrontendTls", { |
| 116 | displayName: "frontend", |
| 117 | publicCertificate: CERT_A, |
| 118 | privateKey: KEY_A, |
| 119 | }); |
| 120 | }), |
| 121 | ); |
| 122 | |
| 123 | expect(created.certificateId.length).toBeGreaterThan(0); |
| 124 | expect(created.displayName).toEqual("frontend"); |
| 125 | expect(created.applicationsId).toEqual(project); |
| 126 | |
| 127 | const fetched = yield* appengine.getProjectsLocationsApplicationsAuthorizedCertificates({ |
| 128 | projectsId: created.project, |
| 129 | locationsId: created.location, |
| 130 | applicationsId: created.applicationsId, |
| 131 | authorizedCertificatesId: created.certificateId, |
| 132 | }); |
| 133 | expect(fetched.id).toEqual(created.certificateId); |
| 134 | expect(fetched.displayName).toContain("[alchemy "); |
| 135 | |
| 136 | const updated = yield* stack.deploy( |
| 137 | Effect.gen(function* () { |
| 138 | return yield* GCP.AppEngine.ApplicationsAuthorizedCertificate("FrontendTls", { |
| 139 | certificateId: created.certificateId, |
| 140 | location: created.location, |
| 141 | displayName: "frontend-prod", |
| 142 | publicCertificate: CERT_B, |
| 143 | privateKey: KEY_B, |
| 144 | }); |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | expect(updated.certificateId).toEqual(created.certificateId); |
| 149 | expect(updated.displayName).toEqual("frontend-prod"); |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | const gone = yield* waitUntilGone( |
| 154 | created.project, |
| 155 | created.location, |
| 156 | created.applicationsId, |
| 157 | created.certificateId, |
| 158 | ); |
| 159 | expect(gone).toEqual("gone"); |
| 160 | }).pipe(logLevel), |
| 161 | { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 }, |
| 162 | ); |