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

main
ts 117 lines 4.09 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.getBillingAccountsLocationsBucketsLinks({ name }).pipe(
23 Effect.map((link) =>
24 link.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("2 seconds"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 test.provider.skipIf(!billingAccountId)(
35 "getBillingAccountsLocationsBucketsLinks on a missing link fails with NotFound",
36 (stack) =>
37 Effect.gen(function* () {
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 logging.getBillingAccountsLocationsBucketsLinks({
42 name: `billingAccounts/${account}/locations/global/buckets/_Default/links/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, replace, and delete a billing bucket link",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
61 billingAccountId: account,
62 analyticsEnabled: true,
63 description: "analytics parent",
64 });
65 const link = yield* GCP.Logging.BillingBucketsLink("Analytics", {
66 billingAccountId: account,
67 location: bucket.location,
68 bucketId: bucket.bucketId,
69 description: "log analytics",
70 });
71 return { bucket, link };
72 }),
73 );
74
75 expect(created.link.linkId).toEqual(expect.any(String));
76 expect(created.link.bucketId).toEqual(created.bucket.bucketId);
77 expect(created.link.description).toEqual("log analytics");
78
79 const fetched = yield* logging.getBillingAccountsLocationsBucketsLinks({
80 name: created.link.name,
81 });
82 expect(fetched.description).toContain("alchemy-id=");
83
84 const nextLinkId = `${created.link.linkId.replace(/[^a-z0-9_]/g, "_")}_z`;
85
86 const replaced = yield* stack.deploy(
87 Effect.gen(function* () {
88 const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
89 billingAccountId: account,
90 bucketId: created.bucket.bucketId,
91 location: created.bucket.location,
92 analyticsEnabled: true,
93 description: "analytics parent",
94 });
95 const link = yield* GCP.Logging.BillingBucketsLink("Analytics", {
96 billingAccountId: account,
97 location: bucket.location,
98 bucketId: bucket.bucketId,
99 linkId: nextLinkId,
100 description: "replaced link",
101 });
102 return { bucket, link };
103 }),
104 );
105
106 expect(replaced.link.linkId).not.toEqual(created.link.linkId);
107
108 const previousGone = yield* waitUntilGone(created.link.name);
109 expect(previousGone).toEqual("gone");
110
111 yield* stack.destroy();
112
113 const gone = yield* waitUntilGone(replaced.link.name);
114 expect(gone).toEqual("gone");
115 }).pipe(logLevel),
116 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 120_000 },
117 );