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// Billing-account-scoped: set GOOGLE_BILLING_ACCOUNT when the credentials
12// administer the billing account (the testing service account does not).
13const billingAccountId = process.env.GOOGLE_BILLING_ACCOUNT?.trim().replace(
14 /^billingAccounts\//,
15 "",
16);
17const account = billingAccountId ?? "";
18
19const logLevel = Effect.provideService(
20 MinimumLogLevel,
21 process.env.DEBUG ? "Debug" : "Info",
22);
23
24const waitUntilGone = (name: string) =>
25 logging.getBillingAccountsSinks({ sinkName: 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(!billingAccountId)(
36 "getBillingAccountsSinks on a missing sink fails with NotFound",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 logging.getBillingAccountsSinks({
43 sinkName: `billingAccounts/${account}/sinks/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(!billingAccountId)(
54 "create, update, replace, and delete a billing sink",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const destination = `logging.googleapis.com/billingAccounts/${account}/locations/global/buckets/_Default`;
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.Logging.BillingSink("Errors", {
64 billingAccountId: account,
65 destination,
66 filter: "severity>=ERROR",
67 description: "application errors",
68 });
69 }),
70 );
71
72 expect(created.sinkId).toEqual(expect.any(String));
73 expect(created.name).toEqual(
74 `billingAccounts/${account}/sinks/${created.sinkId}`,
75 );
76 expect(created.destination).toEqual(destination);
77 expect(created.filter).toEqual("severity>=ERROR");
78 expect(created.description).toEqual("application errors");
79
80 const fetched = yield* logging.getBillingAccountsSinks({
81 sinkName: created.name,
82 });
83 expect(fetched.destination).toEqual(destination);
84 expect(fetched.description).toContain("alchemy-id=");
85
86 const updated = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.Logging.BillingSink("Errors", {
89 billingAccountId: account,
90 sinkId: created.sinkId,
91 destination,
92 filter: "severity>=WARNING",
93 description: "warnings and errors",
94 disabled: true,
95 });
96 }),
97 );
98
99 expect(updated.filter).toEqual("severity>=WARNING");
100 expect(updated.disabled).toEqual(true);
101
102 const last = created.sinkId.at(-1) ?? "a";
103 const nextSinkId = `${created.sinkId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
104
105 const replaced = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* GCP.Logging.BillingSink("Errors", {
108 billingAccountId: account,
109 sinkId: nextSinkId,
110 destination,
111 filter: "severity>=WARNING",
112 description: "replaced sink",
113 });
114 }),
115 );
116
117 expect(replaced.sinkId).not.toEqual(created.sinkId);
118
119 const previousGone = yield* waitUntilGone(created.name);
120 expect(previousGone).toEqual("gone");
121
122 yield* stack.destroy();
123
124 const gone = yield* waitUntilGone(replaced.name);
125 expect(gone).toEqual("gone");
126 }).pipe(logLevel),
127 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
128);