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.getBillingAccountsExclusions({ 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 "getBillingAccountsExclusions on a missing exclusion fails with NotFound",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 logging.getBillingAccountsExclusions({
43 name: `billingAccounts/${account}/exclusions/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 exclusion",
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.BillingExclusion("DropDebug", {
62 billingAccountId: account,
63 filter: "severity=DEBUG",
64 description: "drop debug entries",
65 });
66 }),
67 );
68
69 expect(created.exclusionId).toEqual(expect.any(String));
70 expect(created.billingAccountId).toEqual(account);
71 expect(created.name).toEqual(
72 `billingAccounts/${account}/exclusions/${created.exclusionId}`,
73 );
74 expect(created.filter).toEqual("severity=DEBUG");
75 expect(created.description).toEqual("drop debug entries");
76 expect(created.disabled).toEqual(false);
77
78 const fetched = yield* logging.getBillingAccountsExclusions({
79 name: created.name,
80 });
81 expect(fetched.filter).toEqual("severity=DEBUG");
82 expect(fetched.description).toContain("alchemy-id=");
83 expect(fetched.description).toContain("drop debug entries");
84
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.Logging.BillingExclusion("DropDebug", {
88 billingAccountId: account,
89 exclusionId: created.exclusionId,
90 filter: "severity<ERROR",
91 description: "drop non-errors",
92 disabled: true,
93 });
94 }),
95 );
96
97 expect(updated.name).toEqual(created.name);
98 expect(updated.filter).toEqual("severity<ERROR");
99 expect(updated.description).toEqual("drop non-errors");
100 expect(updated.disabled).toEqual(true);
101
102 const last = created.exclusionId.at(-1) ?? "a";
103 const nextExclusionId = `${created.exclusionId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
104
105 const replaced = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* GCP.Logging.BillingExclusion("DropDebug", {
108 billingAccountId: account,
109 exclusionId: nextExclusionId,
110 filter: "severity=DEBUG",
111 description: "replaced exclusion",
112 });
113 }),
114 );
115
116 expect(replaced.exclusionId).not.toEqual(created.exclusionId);
117 expect(replaced.description).toEqual("replaced exclusion");
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);