The best way to control Claude Code, Codex and more from any computer or phone

main
ts 123 lines 4.12 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as logging from "@distilled.cloud/gcp/logging_v2";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { 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).
13 const billingAccountId = process.env.GOOGLE_BILLING_ACCOUNT?.trim().replace(
14 /^billingAccounts\//,
15 "",
16 );
17 const account = billingAccountId ?? "";
18
19 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
20
21 const waitUntilGone = (name: string) =>
22 logging.getBillingAccountsSinks({ sinkName: name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("1 second"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider.skipIf(!billingAccountId)(
33 "getBillingAccountsSinks on a missing sink fails with NotFound",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 logging.getBillingAccountsSinks({
40 sinkName: `billingAccounts/${account}/sinks/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
48 );
49
50 test.provider.skipIf(!billingAccountId)(
51 "create, update, replace, and delete a billing sink",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const destination = `logging.googleapis.com/billingAccounts/${account}/locations/global/buckets/_Default`;
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 return yield* GCP.Logging.BillingSink("Errors", {
61 billingAccountId: account,
62 destination,
63 filter: "severity>=ERROR",
64 description: "application errors",
65 });
66 }),
67 );
68
69 expect(created.sinkId).toEqual(expect.any(String));
70 expect(created.name).toEqual(`billingAccounts/${account}/sinks/${created.sinkId}`);
71 expect(created.destination).toEqual(destination);
72 expect(created.filter).toEqual("severity>=ERROR");
73 expect(created.description).toEqual("application errors");
74
75 const fetched = yield* logging.getBillingAccountsSinks({
76 sinkName: created.name,
77 });
78 expect(fetched.destination).toEqual(destination);
79 expect(fetched.description).toContain("alchemy-id=");
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* GCP.Logging.BillingSink("Errors", {
84 billingAccountId: account,
85 sinkId: created.sinkId,
86 destination,
87 filter: "severity>=WARNING",
88 description: "warnings and errors",
89 disabled: true,
90 });
91 }),
92 );
93
94 expect(updated.filter).toEqual("severity>=WARNING");
95 expect(updated.disabled).toEqual(true);
96
97 const last = created.sinkId.at(-1) ?? "a";
98 const nextSinkId = `${created.sinkId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
99
100 const replaced = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* GCP.Logging.BillingSink("Errors", {
103 billingAccountId: account,
104 sinkId: nextSinkId,
105 destination,
106 filter: "severity>=WARNING",
107 description: "replaced sink",
108 });
109 }),
110 );
111
112 expect(replaced.sinkId).not.toEqual(created.sinkId);
113
114 const previousGone = yield* waitUntilGone(created.name);
115 expect(previousGone).toEqual("gone");
116
117 yield* stack.destroy();
118
119 const gone = yield* waitUntilGone(replaced.name);
120 expect(gone).toEqual("gone");
121 }).pipe(logLevel),
122 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
123 );