1import { GcpEnvironment } from "@/GCP/Environment";
2import * as firebaseappcheck from "@distilled.cloud/gcp/firebaseappcheck_v1";
3import * as Effect from "effect/Effect";
4import { MinimumLogLevel } from "effect/References";
5import * as Schedule from "effect/Schedule";
6
7export const logLevel = Effect.provideService(
8 MinimumLogLevel,
9 process.env.DEBUG ? "Debug" : "Info",
10);
11
12export 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.
20export const lifecycleAppId = process.env.GCP_TEST_FIREBASE_APP_ID;
21export const runLifecycle = !!lifecycleAppId;
22
23export const missingDebugToken = () =>
24 currentProject.pipe(
25 Effect.map(
26 (project) =>
27 `projects/${project}/apps/1:0:web:deadbeef/debugTokens/missing`,
28 ),
29 );
30
31export const missingResourcePolicy = () =>
32 currentProject.pipe(
33 Effect.map(
34 (project) =>
35 `projects/${project}/services/oauth2.googleapis.com/resourcePolicies/missing`,
36 ),
37 );
38
39export 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
50export 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 );