1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as logging from "@distilled.cloud/gcp/logging_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 logging.getProjectsLocationsBucketsLinks({ name }).pipe(
19 Effect.map((link) =>
20 link.lifecycleState === "DELETE_REQUESTED"
21 ? ("gone" as const)
22 : ("found" as const),
23 ),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32test.provider.skipIf(!!process.env.FAST)(
33 "create and delete a logging bucket link",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37
38 yield* stack.destroy();
39
40 const created = yield* stack.deploy(
41 Effect.gen(function* () {
42 const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
43 analyticsEnabled: true,
44 description: "analytics parent",
45 });
46 const link = yield* GCP.Logging.BucketsLink("Analytics", {
47 bucket: bucket.name,
48 description: "bigquery analytics",
49 });
50 return { bucket, link };
51 }),
52 );
53
54 expect(created.link.linkId).toEqual(expect.any(String));
55 expect(created.link.linkId).toMatch(/^[a-z][a-z0-9_]*$/);
56 expect(created.link.bucket).toEqual(created.bucket.name);
57 expect(created.link.project).toEqual(project);
58 expect(created.link.location).toEqual("global");
59 expect(created.link.name).toEqual(
60 `${created.bucket.name}/links/${created.link.linkId}`,
61 );
62 expect(created.link.description).toEqual("bigquery analytics");
63 expect(created.link.lifecycleState).toEqual("ACTIVE");
64
65 const fetched = yield* logging.getProjectsLocationsBucketsLinks({
66 name: created.link.name,
67 });
68 expect(fetched.name).toEqual(created.link.name);
69 expect(fetched.lifecycleState).toEqual("ACTIVE");
70 expect(fetched.description).toContain("alchemy-id=");
71 expect(fetched.description).toContain("bigquery analytics");
72
73 yield* stack.destroy();
74
75 const gone = yield* waitUntilGone(created.link.name);
76 expect(gone).toEqual("gone");
77 }).pipe(logLevel),
78 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 180_000 },
79);