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.getFoldersLocationsBucketsLinks({ 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(
33 "getFoldersLocationsBucketsLinks on a missing link fails with NotFound",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 logging.getFoldersLocationsBucketsLinks({
42 name: `projects/${project}/locations/global/buckets/_Default/links/alchemy-does-not-exist`,
43 }),
44 );
45 expect(error._tag).toBe("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel),
49 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
50);
51
52test.provider.skipIf(!!process.env.FAST)(
53 "create and delete a folder logging bucket link",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 const bucket = yield* GCP.Logging.LogBucket("FolderLinkBucket", {
61 analyticsEnabled: true,
62 description: "analytics parent",
63 });
64 const link = yield* GCP.Logging.FolderBucketsLink("FolderLink", {
65 bucketName: bucket.name,
66 description: "bigquery analytics",
67 });
68 return { bucket, link };
69 }),
70 );
71
72 expect(created.link.linkId).toEqual(expect.any(String));
73 expect(created.link.linkId).toMatch(/^[a-z][a-z0-9_]*$/);
74 expect(created.link.bucketName).toEqual(created.bucket.name);
75 expect(created.link.name).toEqual(
76 `${created.bucket.name}/links/${created.link.linkId}`,
77 );
78 expect(created.link.description).toEqual("bigquery analytics");
79 expect(["ACTIVE", "CREATING"]).toContain(created.link.lifecycleState);
80
81 const fetched = yield* logging.getFoldersLocationsBucketsLinks({
82 name: created.link.name,
83 });
84 expect(fetched.name).toEqual(created.link.name);
85 expect(fetched.description).toContain("alchemy-id=");
86 expect(fetched.description).toContain("bigquery analytics");
87
88 yield* stack.destroy();
89
90 const gone = yield* waitUntilGone(created.link.name);
91 expect(gone).toEqual("gone");
92 }).pipe(logLevel),
93 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 180_000 },
94);