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

main
ts 124 lines 4.19 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.getBillingAccountsLocationsBuckets({ name }).pipe(
23 Effect.map((bucket) =>
24 bucket.lifecycleState === "DELETE_REQUESTED" ? ("gone" as const) : ("found" as const),
25 ),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 test.provider.skipIf(!billingAccountId)(
35 "getBillingAccountsLocationsBuckets on a missing bucket fails with NotFound",
36 (stack) =>
37 Effect.gen(function* () {
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 logging.getBillingAccountsLocationsBuckets({
42 name: `billingAccounts/${account}/locations/global/buckets/alchemy-missing`,
43 }),
44 );
45 expect(error._tag).toEqual("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel),
49 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
50 );
51
52 test.provider.skipIf(!billingAccountId)(
53 "create, update, replace, and delete a billing log bucket",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 return yield* GCP.Logging.BillingBucket("AppLogs", {
61 billingAccountId: account,
62 description: "application logs",
63 retentionDays: 31,
64 });
65 }),
66 );
67
68 expect(created.bucketId).toEqual(expect.any(String));
69 expect(created.location).toEqual("global");
70 expect(created.billingAccountId).toEqual(account);
71 expect(created.name).toEqual(
72 `billingAccounts/${account}/locations/global/buckets/${created.bucketId}`,
73 );
74 expect(created.description).toEqual("application logs");
75 expect(created.retentionDays).toEqual(31);
76
77 const fetched = yield* logging.getBillingAccountsLocationsBuckets({
78 name: created.name,
79 });
80 expect(fetched.retentionDays).toEqual(31);
81 expect(fetched.description).toContain("alchemy-id=");
82
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 return yield* GCP.Logging.BillingBucket("AppLogs", {
86 billingAccountId: account,
87 bucketId: created.bucketId,
88 location: created.location,
89 description: "retained application logs",
90 retentionDays: 60,
91 });
92 }),
93 );
94
95 expect(updated.name).toEqual(created.name);
96 expect(updated.retentionDays).toEqual(60);
97
98 const last = created.bucketId.at(-1) ?? "a";
99 const nextBucketId = `${created.bucketId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
100
101 const replaced = yield* stack.deploy(
102 Effect.gen(function* () {
103 return yield* GCP.Logging.BillingBucket("AppLogs", {
104 billingAccountId: account,
105 bucketId: nextBucketId,
106 location: "global",
107 description: "replaced bucket",
108 retentionDays: 31,
109 });
110 }),
111 );
112
113 expect(replaced.bucketId).not.toEqual(created.bucketId);
114
115 const previousGone = yield* waitUntilGone(created.name);
116 expect(previousGone).toEqual("gone");
117
118 yield* stack.destroy();
119
120 const gone = yield* waitUntilGone(replaced.name);
121 expect(gone).toEqual("gone");
122 }).pipe(logLevel),
123 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
124 );