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

main
ts 129 lines 4.83 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as logging from "@distilled.cloud/gcp/logging_v2";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const waitUntilGone = (name: string) =>
15 logging.getProjectsLocationsBucketsViews({ name }).pipe(
16 Effect.as("found" as const),
17 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
18 Effect.repeat({
19 schedule: Schedule.spaced("1 second"),
20 until: (status) => status === "gone",
21 times: 10,
22 }),
23 );
24
25 test.provider(
26 "create, update, replace, and delete a logging bucket view",
27 (stack) =>
28 Effect.gen(function* () {
29 const { project } = yield* GcpEnvironment.current;
30
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
36 description: "view parent",
37 });
38 const view = yield* GCP.Logging.BucketsView("Stdout", {
39 bucket: bucket.name,
40 filter: 'LOG_ID("stdout")',
41 description: "stdout only",
42 });
43 return { bucket, view };
44 }),
45 );
46
47 expect(created.view.viewId).toEqual(expect.any(String));
48 expect(created.view.bucket).toEqual(created.bucket.name);
49 expect(created.view.project).toEqual(project);
50 expect(created.view.location).toEqual("global");
51 expect(created.view.name).toEqual(`${created.bucket.name}/views/${created.view.viewId}`);
52 expect(created.view.filter).toEqual('LOG_ID("stdout")');
53 expect(created.view.description).toEqual("stdout only");
54
55 const fetched = yield* logging.getProjectsLocationsBucketsViews({
56 name: created.view.name,
57 });
58 expect(fetched.name).toEqual(created.view.name);
59 expect(fetched.filter).toEqual('LOG_ID("stdout")');
60 expect(fetched.description).toContain("alchemy-id=");
61 expect(fetched.description).toContain("stdout only");
62
63 const updated = yield* stack.deploy(
64 Effect.gen(function* () {
65 const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
66 bucketId: created.bucket.bucketId,
67 location: created.bucket.location,
68 description: "view parent",
69 });
70 const view = yield* GCP.Logging.BucketsView("Stdout", {
71 bucket: bucket.name,
72 viewId: created.view.viewId,
73 filter: 'LOG_ID("stderr")',
74 description: "stderr only",
75 });
76 return { bucket, view };
77 }),
78 );
79
80 expect(updated.view.name).toEqual(created.view.name);
81 expect(updated.view.viewId).toEqual(created.view.viewId);
82 expect(updated.view.filter).toEqual('LOG_ID("stderr")');
83 expect(updated.view.description).toEqual("stderr only");
84
85 const fetchedUpdate = yield* logging.getProjectsLocationsBucketsViews({
86 name: created.view.name,
87 });
88 expect(fetchedUpdate.filter).toEqual('LOG_ID("stderr")');
89 expect(fetchedUpdate.description).toContain("stderr only");
90
91 const last = created.view.viewId.at(-1) ?? "a";
92 const nextViewId = `${created.view.viewId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
93
94 const replaced = yield* stack.deploy(
95 Effect.gen(function* () {
96 const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
97 bucketId: created.bucket.bucketId,
98 location: created.bucket.location,
99 description: "view parent",
100 });
101 const view = yield* GCP.Logging.BucketsView("Stdout", {
102 bucket: bucket.name,
103 viewId: nextViewId,
104 filter: 'LOG_ID("stderr")',
105 description: "replaced view",
106 });
107 return { bucket, view };
108 }),
109 );
110
111 expect(replaced.view.viewId).not.toEqual(created.view.viewId);
112 expect(replaced.view.name).toEqual(`${created.bucket.name}/views/${replaced.view.viewId}`);
113 expect(replaced.view.description).toEqual("replaced view");
114
115 const fetchedReplacement = yield* logging.getProjectsLocationsBucketsViews({
116 name: replaced.view.name,
117 });
118 expect(fetchedReplacement.name).toEqual(replaced.view.name);
119
120 const previousGone = yield* waitUntilGone(created.view.name);
121 expect(previousGone).toEqual("gone");
122
123 yield* stack.destroy();
124
125 const gone = yield* waitUntilGone(replaced.view.name);
126 expect(gone).toEqual("gone");
127 }).pipe(logLevel),
128 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
129 );