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";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11// Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials
12// administer the organization (the testing service account does not).
13const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(
14 /^organizations\//,
15 "",
16);
17const organization = `organizations/${organizationId}`;
18
19const logLevel = Effect.provideService(
20 MinimumLogLevel,
21 process.env.DEBUG ? "Debug" : "Info",
22);
23
24const waitUntilGone = (name: string) =>
25 logging.getOrganizationsLocationsBucketsViews({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider.skipIf(!organizationId)(
36 "getOrganizationsLocationsBucketsViews on a missing view fails with NotFound",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 logging.getOrganizationsLocationsBucketsViews({
43 name: `${organization}/locations/global/buckets/_Default/views/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("NotFound");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
51);
52
53test.provider.skipIf(!organizationId)(
54 "create, update, replace, and delete an organization logging bucket view",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* GCP.Logging.OrganizationBucketsView("Stdout", {
62 organization,
63 filter: 'LOG_ID("stdout")',
64 description: "stdout only",
65 });
66 }),
67 );
68
69 expect(created.viewId).toEqual(expect.any(String));
70 expect(created.organization).toEqual(organization);
71 expect(created.bucketId).toEqual("_Default");
72 expect(created.filter).toEqual('LOG_ID("stdout")');
73 expect(created.description).toEqual("stdout only");
74
75 const fetched = yield* logging.getOrganizationsLocationsBucketsViews({
76 name: created.name,
77 });
78 expect(fetched.filter).toEqual('LOG_ID("stdout")');
79 expect(fetched.description).toContain("alchemy-id=");
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* GCP.Logging.OrganizationBucketsView("Stdout", {
84 organization,
85 viewId: created.viewId,
86 bucket: created.bucket,
87 filter: 'LOG_ID("stderr")',
88 description: "stderr only",
89 });
90 }),
91 );
92
93 expect(updated.name).toEqual(created.name);
94 expect(updated.filter).toEqual('LOG_ID("stderr")');
95
96 const last = created.viewId.at(-1) ?? "a";
97 const nextViewId = `${created.viewId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
98
99 const replaced = yield* stack.deploy(
100 Effect.gen(function* () {
101 return yield* GCP.Logging.OrganizationBucketsView("Stdout", {
102 organization,
103 viewId: nextViewId,
104 bucket: created.bucket,
105 filter: 'LOG_ID("stdout")',
106 description: "replaced view",
107 });
108 }),
109 );
110
111 expect(replaced.viewId).not.toEqual(created.viewId);
112
113 const previousGone = yield* waitUntilGone(created.name);
114 expect(previousGone).toEqual("gone");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(replaced.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
122);