| 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.getProjectsLocationsBackendsDomains({ 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 | "getProjectsLocationsBackendsDomains on a missing domain 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.getProjectsLocationsBackendsDomains({ |
| 39 | name: `${missingBackend()}/domains/alchemy-missing.example.com`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | |
| 44 | const page = |
| 45 | yield* firebaseapphosting.listProjectsLocationsBackendsDomains({ |
| 46 | parent: `projects/${project}/locations/-/backends/-`, |
| 47 | pageSize: 10, |
| 48 | }); |
| 49 | expect((page.domains ?? []).map((item) => item.name)).not.toContain( |
| 50 | `${missingBackend()}/domains/alchemy-missing.example.com`, |
| 51 | ); |
| 52 | |
| 53 | yield* stack.destroy(); |
| 54 | }).pipe(logLevel), |
| 55 | { |
| 56 | tags: ["provider:gcp", "provider:gcp:firebaseapphosting", "live"], |
| 57 | timeout: 90_000, |
| 58 | }, |
| 59 | ); |
| 60 | |
| 61 | test.provider( |
| 62 | "create against a missing backend is rejected with a typed tag", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | const project = yield* currentProject; |
| 66 | const missingBackend = missingBackendOf(project); |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | const error = yield* Effect.flip( |
| 71 | stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | return yield* GCP.FirebaseAppHosting.BackendsDomain("Www", { |
| 74 | backend: missingBackend(), |
| 75 | domainId: "alchemy-missing.example.com", |
| 76 | labels: { env: "test" }, |
| 77 | }); |
| 78 | }), |
| 79 | ), |
| 80 | ); |
| 81 | expect(error._tag).toEqual("NotFound"); |
| 82 | |
| 83 | yield* stack.destroy(); |
| 84 | }).pipe(logLevel), |
| 85 | { |
| 86 | tags: ["provider:gcp", "provider:gcp:firebaseapphosting", "live"], |
| 87 | timeout: 90_000, |
| 88 | }, |
| 89 | ); |
| 90 | |
| 91 | // Custom-domain create stays pending (the LRO did not finish within 5 minutes) |
| 92 | // until the domain's DNS points at App Hosting. Set |
| 93 | // GCP_TEST_FIREBASE_APP_HOSTING_DOMAIN to a domain you control to run this. |
| 94 | const customDomain = process.env.GCP_TEST_FIREBASE_APP_HOSTING_DOMAIN; |
| 95 | |
| 96 | test.provider.skipIf(!customDomain)( |
| 97 | "create, update, and delete a backend domain", |
| 98 | (stack) => |
| 99 | Effect.gen(function* () { |
| 100 | const project = yield* currentProject; |
| 101 | const serviceAccount = serviceAccountOf(project); |
| 102 | |
| 103 | yield* stack.destroy(); |
| 104 | |
| 105 | const created = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | const backend = yield* GCP.FirebaseAppHosting.Backend("Web", { |
| 108 | serviceAccount, |
| 109 | servingLocality: "GLOBAL_ACCESS", |
| 110 | displayName: "alchemy-test-domain-backend", |
| 111 | labels: { env: "test" }, |
| 112 | }); |
| 113 | const domain = yield* GCP.FirebaseAppHosting.BackendsDomain("Www", { |
| 114 | domainId: customDomain, |
| 115 | backend: backend.name, |
| 116 | displayName: "alchemy-test-domain", |
| 117 | disabled: true, |
| 118 | labels: { env: "test" }, |
| 119 | }); |
| 120 | return { backend, domain }; |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | expect(created.domain.name).toContain("/domains/"); |
| 125 | expect(created.domain.backend).toEqual(created.backend.name); |
| 126 | expect(created.domain.location).toEqual(location); |
| 127 | expect(created.domain.disabled).toEqual(true); |
| 128 | expect(created.domain.displayName).toEqual("alchemy-test-domain"); |
| 129 | expect(created.domain.labels).toMatchObject({ env: "test" }); |
| 130 | |
| 131 | const fetched = |
| 132 | yield* firebaseapphosting.getProjectsLocationsBackendsDomains({ |
| 133 | name: created.domain.name, |
| 134 | }); |
| 135 | expect(fetched.name).toEqual(created.domain.name); |
| 136 | expect(fetched.displayName).toEqual("alchemy-test-domain"); |
| 137 | expect(fetched.labels?.env).toEqual("test"); |
| 138 | |
| 139 | const updated = yield* stack.deploy( |
| 140 | Effect.gen(function* () { |
| 141 | const backend = yield* GCP.FirebaseAppHosting.Backend("Web", { |
| 142 | backendId: created.backend.backendId, |
| 143 | serviceAccount, |
| 144 | servingLocality: "GLOBAL_ACCESS", |
| 145 | displayName: "alchemy-test-domain-backend", |
| 146 | labels: { env: "test" }, |
| 147 | }); |
| 148 | const domain = yield* GCP.FirebaseAppHosting.BackendsDomain("Www", { |
| 149 | domainId: created.domain.domainId, |
| 150 | backend: backend.name, |
| 151 | displayName: "alchemy-prod-domain", |
| 152 | disabled: true, |
| 153 | labels: { env: "prod", role: "web" }, |
| 154 | }); |
| 155 | return domain; |
| 156 | }), |
| 157 | ); |
| 158 | |
| 159 | expect(updated.name).toEqual(created.domain.name); |
| 160 | expect(updated.displayName).toEqual("alchemy-prod-domain"); |
| 161 | expect(updated.labels).toMatchObject({ env: "prod", role: "web" }); |
| 162 | |
| 163 | yield* stack.destroy(); |
| 164 | |
| 165 | const gone = yield* waitUntilGone(created.domain.name); |
| 166 | expect(gone).toEqual("gone"); |
| 167 | }).pipe(logLevel), |
| 168 | { |
| 169 | tags: ["provider:gcp", "provider:gcp:firebaseapphosting", "live"], |
| 170 | timeout: 420_000, |
| 171 | }, |
| 172 | ); |