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

main
ts 141 lines 5.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.getBillingAccountsLocationsBucketsViews({ 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 "getBillingAccountsLocationsBucketsViews on a missing view fails with NotFound",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 logging.getBillingAccountsLocationsBucketsViews({
40 name: `billingAccounts/${account}/locations/global/buckets/_Default/views/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 bucket view",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
59 billingAccountId: account,
60 description: "view parent",
61 });
62 const view = yield* GCP.Logging.BillingBucketsView("Gce", {
63 billingAccountId: account,
64 location: bucket.location,
65 bucketId: bucket.bucketId,
66 filter: 'resource.type = "gce_instance"',
67 description: "compute logs",
68 });
69 return { bucket, view };
70 }),
71 );
72
73 expect(created.view.viewId).toEqual(expect.any(String));
74 expect(created.view.bucketId).toEqual(created.bucket.bucketId);
75 expect(created.view.filter).toEqual('resource.type = "gce_instance"');
76 expect(created.view.description).toEqual("compute logs");
77
78 const fetched = yield* logging.getBillingAccountsLocationsBucketsViews({
79 name: created.view.name,
80 });
81 expect(fetched.filter).toEqual('resource.type = "gce_instance"');
82 expect(fetched.description).toContain("alchemy-id=");
83
84 const updated = yield* stack.deploy(
85 Effect.gen(function* () {
86 const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
87 billingAccountId: account,
88 bucketId: created.bucket.bucketId,
89 location: created.bucket.location,
90 description: "view parent",
91 });
92 const view = yield* GCP.Logging.BillingBucketsView("Gce", {
93 billingAccountId: account,
94 location: bucket.location,
95 bucketId: bucket.bucketId,
96 viewId: created.view.viewId,
97 filter: 'resource.type = "gce_instance" AND severity>=ERROR',
98 description: "compute errors",
99 });
100 return { bucket, view };
101 }),
102 );
103
104 expect(updated.view.name).toEqual(created.view.name);
105 expect(updated.view.filter).toEqual('resource.type = "gce_instance" AND severity>=ERROR');
106
107 const last = created.view.viewId.at(-1) ?? "a";
108 const nextViewId = `${created.view.viewId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
109
110 const replaced = yield* stack.deploy(
111 Effect.gen(function* () {
112 const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
113 billingAccountId: account,
114 bucketId: created.bucket.bucketId,
115 location: created.bucket.location,
116 description: "view parent",
117 });
118 const view = yield* GCP.Logging.BillingBucketsView("Gce", {
119 billingAccountId: account,
120 location: bucket.location,
121 bucketId: bucket.bucketId,
122 viewId: nextViewId,
123 filter: 'resource.type = "gce_instance"',
124 description: "replaced view",
125 });
126 return { bucket, view };
127 }),
128 );
129
130 expect(replaced.view.viewId).not.toEqual(created.view.viewId);
131
132 const previousGone = yield* waitUntilGone(created.view.name);
133 expect(previousGone).toEqual("gone");
134
135 yield* stack.destroy();
136
137 const gone = yield* waitUntilGone(replaced.view.name);
138 expect(gone).toEqual("gone");
139 }).pipe(logLevel),
140 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
141 );