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

main
ts 133 lines 4.84 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 const destinationOf = (project: string) =>
14 `logging.googleapis.com/projects/${project}/locations/global/buckets/_Default`;
15
16 const waitUntilGone = (name: string) =>
17 logging.getSinks({ sinkName: name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27 test.provider(
28 "create, update, replace, and delete a logging sink",
29 (stack) =>
30 Effect.gen(function* () {
31 const { project } = yield* GcpEnvironment.current;
32
33 yield* stack.destroy();
34
35 const created = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* GCP.Logging.Sink("Errors", {
38 destination: destinationOf(project),
39 filter: "severity>=ERROR",
40 description: "application errors",
41 });
42 }),
43 );
44
45 expect(created.sinkId).toEqual(expect.any(String));
46 expect(created.name).toEqual(`projects/${project}/sinks/${created.sinkId}`);
47 expect(created.project).toEqual(project);
48 expect(created.destination).toEqual(destinationOf(project));
49 expect(created.filter).toEqual("severity>=ERROR");
50 expect(created.description).toEqual("application errors");
51 expect(created.disabled).toEqual(false);
52 expect(created.exclusions).toEqual([]);
53
54 const fetched = yield* logging.getSinks({ sinkName: created.name });
55 expect(fetched.name).toEqual(created.sinkId);
56 expect(fetched.destination).toEqual(destinationOf(project));
57 expect(fetched.filter).toEqual("severity>=ERROR");
58 expect(fetched.description).toContain("alchemy-id=");
59 expect(fetched.description).toContain("application errors");
60
61 const updated = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.Logging.Sink("Errors", {
64 sinkId: created.sinkId,
65 destination: destinationOf(project),
66 filter: "severity>=WARNING",
67 description: "warnings and errors",
68 disabled: true,
69 exclusions: [
70 {
71 name: "drop-healthchecks",
72 filter: 'httpRequest.requestUrl="/healthz"',
73 description: "skip probes",
74 },
75 ],
76 });
77 }),
78 );
79
80 expect(updated.name).toEqual(created.name);
81 expect(updated.sinkId).toEqual(created.sinkId);
82 expect(updated.filter).toEqual("severity>=WARNING");
83 expect(updated.description).toEqual("warnings and errors");
84 expect(updated.disabled).toEqual(true);
85 expect(updated.exclusions).toEqual([
86 expect.objectContaining({
87 name: "drop-healthchecks",
88 filter: 'httpRequest.requestUrl="/healthz"',
89 description: "skip probes",
90 disabled: false,
91 }),
92 ]);
93
94 const fetchedUpdate = yield* logging.getSinks({
95 sinkName: created.name,
96 });
97 expect(fetchedUpdate.filter).toEqual("severity>=WARNING");
98 expect(fetchedUpdate.disabled).toEqual(true);
99 expect(fetchedUpdate.exclusions?.[0]?.name).toEqual("drop-healthchecks");
100
101 const last = created.sinkId.at(-1) ?? "a";
102 const nextSinkId = `${created.sinkId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
103
104 const replaced = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* GCP.Logging.Sink("Errors", {
107 sinkId: nextSinkId,
108 destination: destinationOf(project),
109 filter: "severity>=WARNING",
110 description: "replaced sink",
111 });
112 }),
113 );
114
115 expect(replaced.sinkId).not.toEqual(created.sinkId);
116 expect(replaced.name).toEqual(`projects/${project}/sinks/${replaced.sinkId}`);
117 expect(replaced.description).toEqual("replaced sink");
118
119 const fetchedReplacement = yield* logging.getSinks({
120 sinkName: replaced.name,
121 });
122 expect(fetchedReplacement.name).toEqual(replaced.sinkId);
123
124 const previousGone = yield* waitUntilGone(created.name);
125 expect(previousGone).toEqual("gone");
126
127 yield* stack.destroy();
128
129 const gone = yield* waitUntilGone(replaced.name);
130 expect(gone).toEqual("gone");
131 }).pipe(logLevel),
132 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
133 );