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

main
ts 114 lines 4.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 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const waitUntilGone = (metricName: string) =>
14 logging.getProjectsMetrics({ metricName }).pipe(
15 Effect.as("found" as const),
16 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
17 Effect.repeat({
18 schedule: Schedule.spaced("1 second"),
19 until: (status) => status === "gone",
20 times: 10,
21 }),
22 );
23
24 test.provider(
25 "create, update, replace, and delete a logs-based metric",
26 (stack) =>
27 Effect.gen(function* () {
28 yield* stack.destroy();
29
30 const created = yield* stack.deploy(
31 Effect.gen(function* () {
32 return yield* GCP.Logging.Metric("Errors", {
33 filter: "severity>=ERROR",
34 description: "count errors",
35 });
36 }),
37 );
38
39 expect(created.metricId).toEqual(expect.any(String));
40 expect(created.name).toContain("/metrics/");
41 expect(created.filter).toEqual("severity>=ERROR");
42 expect(created.description).toEqual("count errors");
43 expect(created.disabled).toEqual(false);
44 expect(created.metricDescriptor?.metricKind).toEqual("DELTA");
45 expect(created.metricDescriptor?.valueType).toEqual("INT64");
46
47 const fetched = yield* logging.getProjectsMetrics({
48 metricName: created.name,
49 });
50 expect(fetched.name).toEqual(created.metricId);
51 expect(fetched.filter).toEqual("severity>=ERROR");
52 expect(fetched.description).toContain("alchemy-id=");
53 expect(fetched.description).toContain("count errors");
54 expect(fetched.disabled ?? false).toEqual(false);
55
56 const updated = yield* stack.deploy(
57 Effect.gen(function* () {
58 return yield* GCP.Logging.Metric("Errors", {
59 metricId: created.metricId,
60 filter: "severity>=WARNING",
61 description: "count warnings",
62 disabled: true,
63 });
64 }),
65 );
66
67 expect(updated.name).toEqual(created.name);
68 expect(updated.metricId).toEqual(created.metricId);
69 expect(updated.filter).toEqual("severity>=WARNING");
70 expect(updated.description).toEqual("count warnings");
71 expect(updated.disabled).toEqual(true);
72 expect(updated.createTime).toEqual(created.createTime);
73
74 const fetchedUpdate = yield* logging.getProjectsMetrics({
75 metricName: updated.name,
76 });
77 expect(fetchedUpdate.filter).toEqual("severity>=WARNING");
78 expect(fetchedUpdate.disabled).toEqual(true);
79 expect(fetchedUpdate.description).toContain("count warnings");
80
81 const last = created.metricId.at(-1) ?? "a";
82 const nextMetricId = `${created.metricId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
83
84 const replaced = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* GCP.Logging.Metric("Errors", {
87 metricId: nextMetricId,
88 filter: "severity>=ERROR",
89 description: "replaced counter",
90 });
91 }),
92 );
93
94 expect(replaced.metricId).not.toEqual(created.metricId);
95 expect(replaced.filter).toEqual("severity>=ERROR");
96 expect(replaced.disabled).toEqual(false);
97 expect(replaced.description).toEqual("replaced counter");
98
99 const fetchedReplace = yield* logging.getProjectsMetrics({
100 metricName: replaced.name,
101 });
102 expect(fetchedReplace.name).toEqual(replaced.metricId);
103 expect(fetchedReplace.filter).toEqual("severity>=ERROR");
104
105 const oldGone = yield* waitUntilGone(created.name);
106 expect(oldGone).toEqual("gone");
107
108 yield* stack.destroy();
109
110 const gone = yield* waitUntilGone(replaced.name);
111 expect(gone).toEqual("gone");
112 }).pipe(logLevel),
113 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
114 );