| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as ces from "@distilled.cloud/gcp/ces_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 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( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | // CES only serves the `us` and `eu` multi-regions; `us-central1` returns |
| 18 | // Forbidden ("Location us-central1 is not found or access is unauthorized"). |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | ces.getProjectsLocationsApps({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsLocationsApps on a missing app fails with a typed tag", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | ces.getProjectsLocationsApps({ |
| 41 | name: `projects/${project}/locations/us/apps/alchemy-missing-app`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toEqual("NotFound"); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 }, |
| 49 | ); |
| 50 | |
| 51 | test.provider( |
| 52 | "create, update, and delete an app", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const created = yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | return yield* GCP.CES.App("Support", { |
| 60 | location: "us", |
| 61 | displayName: "support", |
| 62 | description: "help desk", |
| 63 | timeZone: "America/Los_Angeles", |
| 64 | }); |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | expect(created.name).toContain("/apps/"); |
| 69 | expect(created.location).toEqual("us"); |
| 70 | expect(created.displayName).toEqual("support"); |
| 71 | expect(created.description).toEqual("help desk"); |
| 72 | expect(created.timeZone).toEqual("America/Los_Angeles"); |
| 73 | |
| 74 | const fetched = yield* ces.getProjectsLocationsApps({ |
| 75 | name: created.name, |
| 76 | }); |
| 77 | expect(fetched.name).toEqual(created.name); |
| 78 | expect(fetched.metadata?.["alchemy-id"]).toEqual(expect.any(String)); |
| 79 | |
| 80 | const updated = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | return yield* GCP.CES.App("Support", { |
| 83 | appId: created.appId, |
| 84 | location: created.location, |
| 85 | displayName: "support-desk", |
| 86 | description: "updated help desk", |
| 87 | timeZone: "America/New_York", |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(updated.name).toEqual(created.name); |
| 93 | expect(updated.displayName).toEqual("support-desk"); |
| 94 | expect(updated.description).toEqual("updated help desk"); |
| 95 | expect(updated.timeZone).toEqual("America/New_York"); |
| 96 | |
| 97 | yield* stack.destroy(); |
| 98 | const gone = yield* waitUntilGone(created.name); |
| 99 | expect(gone).toEqual("gone"); |
| 100 | }).pipe(logLevel), |
| 101 | { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 }, |
| 102 | ); |