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.getLocationsBucketsLinks({ 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 "getLocationsBucketsLinks 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.getLocationsBucketsLinks({
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 locations 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("LocationsLinkBucket", {
61 analyticsEnabled: true,
62 description: "analytics parent",
63 });
64 const link = yield* GCP.Logging.LocationsBucketsLink(
65 "LocationsLink",
66 {
67 bucketName: bucket.name,
68 description: "bigquery analytics",
69 },
70 );
71 return { bucket, link };
72 }),
73 );
74
75 expect(created.link.linkId).toEqual(expect.any(String));
76 expect(created.link.linkId).toMatch(/^[a-z][a-z0-9_]*$/);
77 expect(created.link.bucketName).toEqual(created.bucket.name);
78 expect(created.link.name).toEqual(
79 `${created.bucket.name}/links/${created.link.linkId}`,
80 );
81 expect(created.link.description).toEqual("bigquery analytics");
82 expect(["ACTIVE", "CREATING"]).toContain(created.link.lifecycleState);
83
84 const fetched = yield* logging.getLocationsBucketsLinks({
85 name: created.link.name,
86 });
87 expect(fetched.name).toEqual(created.link.name);
88 expect(fetched.description).toContain("alchemy-id=");
89 expect(fetched.description).toContain("bigquery analytics");
90
91 yield* stack.destroy();
92
93 const gone = yield* waitUntilGone(created.link.name);
94 expect(gone).toEqual("gone");
95 }).pipe(logLevel),
96 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 180_000 },
97);