| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as firebaseapphosting from "@distilled.cloud/gcp/firebaseapphosting_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import { |
| 8 | location, |
| 9 | logLevel, |
| 10 | missingBackendOf, |
| 11 | currentProject, |
| 12 | serviceAccountOf, |
| 13 | } from "./common.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: GCP.providers() }); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | firebaseapphosting.getProjectsLocationsBackends({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("3 seconds"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsBackends on a missing backend fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const project = yield* currentProject; |
| 33 | const missingBackend = missingBackendOf(project); |
| 34 | |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | firebaseapphosting.getProjectsLocationsBackends({ |
| 39 | name: missingBackend(), |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | |
| 44 | const page = yield* firebaseapphosting.listProjectsLocationsBackends({ |
| 45 | parent: `projects/${project}/locations/-`, |
| 46 | pageSize: 10, |
| 47 | }); |
| 48 | expect((page.backends ?? []).map((item) => item.name)).not.toContain( |
| 49 | missingBackend(), |
| 50 | ); |
| 51 | |
| 52 | yield* stack.destroy(); |
| 53 | }).pipe(logLevel), |
| 54 | { |
| 55 | tags: ["provider:gcp", "provider:gcp:firebaseapphosting", "live"], |
| 56 | timeout: 90_000, |
| 57 | }, |
| 58 | ); |
| 59 | |
| 60 | test.provider( |
| 61 | "create, update, and delete a backend", |
| 62 | (stack) => |
| 63 | Effect.gen(function* () { |
| 64 | const project = yield* currentProject; |
| 65 | const serviceAccount = serviceAccountOf(project); |
| 66 | |
| 67 | yield* stack.destroy(); |
| 68 | |
| 69 | const created = yield* stack.deploy( |
| 70 | Effect.gen(function* () { |
| 71 | return yield* GCP.FirebaseAppHosting.Backend("Web", { |
| 72 | serviceAccount, |
| 73 | servingLocality: "GLOBAL_ACCESS", |
| 74 | displayName: "alchemy-test-backend", |
| 75 | labels: { env: "test" }, |
| 76 | }); |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | expect(created.name).toContain("/backends/"); |
| 81 | expect(created.backendId).toEqual(expect.any(String)); |
| 82 | expect(created.location).toEqual(location); |
| 83 | expect(created.displayName).toEqual("alchemy-test-backend"); |
| 84 | expect(created.labels).toMatchObject({ env: "test" }); |
| 85 | expect(created.servingLocality).toEqual("GLOBAL_ACCESS"); |
| 86 | |
| 87 | const fetched = yield* firebaseapphosting.getProjectsLocationsBackends({ |
| 88 | name: created.name, |
| 89 | }); |
| 90 | expect(fetched.name).toEqual(created.name); |
| 91 | expect(fetched.displayName).toEqual("alchemy-test-backend"); |
| 92 | expect(fetched.labels?.env).toEqual("test"); |
| 93 | |
| 94 | const updated = yield* stack.deploy( |
| 95 | Effect.gen(function* () { |
| 96 | return yield* GCP.FirebaseAppHosting.Backend("Web", { |
| 97 | backendId: created.backendId, |
| 98 | serviceAccount, |
| 99 | servingLocality: "GLOBAL_ACCESS", |
| 100 | displayName: "alchemy-prod-backend", |
| 101 | labels: { env: "prod", role: "web" }, |
| 102 | }); |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | expect(updated.name).toEqual(created.name); |
| 107 | expect(updated.displayName).toEqual("alchemy-prod-backend"); |
| 108 | expect(updated.labels).toMatchObject({ env: "prod", role: "web" }); |
| 109 | |
| 110 | yield* stack.destroy(); |
| 111 | |
| 112 | const gone = yield* waitUntilGone(created.name); |
| 113 | expect(gone).toEqual("gone"); |
| 114 | }).pipe(logLevel), |
| 115 | { |
| 116 | tags: ["provider:gcp", "provider:gcp:firebaseapphosting", "live"], |
| 117 | timeout: 300_000, |
| 118 | }, |
| 119 | ); |