| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as firebaseappcheck from "@distilled.cloud/gcp/firebaseappcheck_v1"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { MinimumLogLevel } from "effect/References"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | |
| 7 | export const logLevel = Effect.provideService( |
| 8 | MinimumLogLevel, |
| 9 | process.env.DEBUG ? "Debug" : "Info", |
| 10 | ); |
| 11 | |
| 12 | export const currentProject = GcpEnvironment.current.pipe( |
| 13 | Effect.map((env) => env.project), |
| 14 | ); |
| 15 | |
| 16 | // App Check needs a Firebase project with the App Check API enabled; the |
| 17 | // testing project has neither (App Check calls fail with ServiceDisabled). |
| 18 | // Set GCP_TEST_FIREBASE_APP_ID to a registered Firebase app id on such a |
| 19 | // project to run the lifecycles. |
| 20 | export const lifecycleAppId = process.env.GCP_TEST_FIREBASE_APP_ID; |
| 21 | export const runLifecycle = !!lifecycleAppId; |
| 22 | |
| 23 | export const missingDebugToken = () => |
| 24 | currentProject.pipe( |
| 25 | Effect.map( |
| 26 | (project) => |
| 27 | `projects/${project}/apps/1:0:web:deadbeef/debugTokens/missing`, |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | export const missingResourcePolicy = () => |
| 32 | currentProject.pipe( |
| 33 | Effect.map( |
| 34 | (project) => |
| 35 | `projects/${project}/services/oauth2.googleapis.com/resourcePolicies/missing`, |
| 36 | ), |
| 37 | ); |
| 38 | |
| 39 | export const waitUntilDebugTokenGone = (name: string) => |
| 40 | firebaseappcheck.getProjectsAppsDebugTokens({ name }).pipe( |
| 41 | Effect.as("found" as const), |
| 42 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 43 | Effect.repeat({ |
| 44 | schedule: Schedule.spaced("1 second"), |
| 45 | until: (status) => status === "gone", |
| 46 | times: 10, |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | export const waitUntilResourcePolicyGone = (name: string) => |
| 51 | firebaseappcheck.getProjectsServicesResourcePolicies({ name }).pipe( |
| 52 | Effect.as("found" as const), |
| 53 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 54 | Effect.repeat({ |
| 55 | schedule: Schedule.spaced("1 second"), |
| 56 | until: (status) => status === "gone", |
| 57 | times: 10, |
| 58 | }), |
| 59 | ); |