@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 130 lines 4.53 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 monitoring from "@distilled.cloud/gcp/monitoring_v3";
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 cpuFilter =
14 'resource.type = "gce_instance" AND metric.type = "compute.googleapis.com/instance/cpu/utilization"';
15
16 const waitUntilGone = (name: string) =>
17 monitoring.getProjectsAlertPolicies({ 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, and delete an alert policy",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 return yield* GCP.Monitoring.AlertPolicy("CpuHigh", {
36 combiner: "OR",
37 conditions: [
38 {
39 displayName: "CPU > 90%",
40 conditionThreshold: {
41 filter: cpuFilter,
42 comparison: "COMPARISON_GT",
43 thresholdValue: 0.9,
44 duration: "60s",
45 aggregations: [
46 {
47 alignmentPeriod: "60s",
48 perSeriesAligner: "ALIGN_MEAN",
49 },
50 ],
51 },
52 },
53 ],
54 labels: { env: "test" },
55 });
56 }),
57 );
58
59 expect(created.name).toContain("/alertPolicies/");
60 expect(created.alertPolicyId).toEqual(expect.any(String));
61 expect(created.combiner).toEqual("OR");
62 expect(created.enabled).toEqual(true);
63 expect(created.conditions.length).toEqual(1);
64 expect(created.conditions[0]?.displayName).toEqual("CPU > 90%");
65 expect(created.conditions[0]?.conditionThreshold?.thresholdValue).toEqual(0.9);
66 expect(created.labels).toMatchObject({ env: "test" });
67 expect(created.notificationChannels).toEqual([]);
68
69 const fetched = yield* monitoring.getProjectsAlertPolicies({
70 name: created.name,
71 });
72 expect(fetched.name).toEqual(created.name);
73 expect(fetched.combiner).toEqual("OR");
74 expect(fetched.conditions?.[0]?.conditionThreshold?.thresholdValue).toEqual(0.9);
75 expect(fetched.userLabels?.env).toEqual("test");
76 expect(
77 Object.keys(fetched.userLabels ?? {}).some((key) => key.startsWith("alchemy-")),
78 ).toEqual(true);
79
80 const updated = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* GCP.Monitoring.AlertPolicy("CpuHigh", {
83 displayName: created.displayName,
84 combiner: "OR",
85 enabled: false,
86 conditions: [
87 {
88 displayName: "CPU > 50%",
89 conditionThreshold: {
90 filter: cpuFilter,
91 comparison: "COMPARISON_GT",
92 thresholdValue: 0.5,
93 duration: "60s",
94 aggregations: [
95 {
96 alignmentPeriod: "60s",
97 perSeriesAligner: "ALIGN_MEAN",
98 },
99 ],
100 },
101 },
102 ],
103 labels: { env: "prod", role: "alerts" },
104 });
105 }),
106 );
107
108 expect(updated.name).toEqual(created.name);
109 expect(updated.enabled).toEqual(false);
110 expect(updated.conditions[0]?.displayName).toEqual("CPU > 50%");
111 expect(updated.conditions[0]?.conditionThreshold?.thresholdValue).toEqual(0.5);
112 expect(updated.labels).toMatchObject({ env: "prod", role: "alerts" });
113
114 const fetchedUpdate = yield* monitoring.getProjectsAlertPolicies({
115 name: updated.name,
116 });
117 expect(fetchedUpdate.enabled).toEqual(false);
118 expect(fetchedUpdate.conditions?.[0]?.conditionThreshold?.thresholdValue).toEqual(0.5);
119 expect(fetchedUpdate.userLabels?.env).toEqual("prod");
120
121 yield* stack.destroy();
122
123 const gone = yield* waitUntilGone(created.name);
124 expect(gone).toEqual("gone");
125 }).pipe(logLevel),
126 {
127 tags: ["provider:gcp", "provider:gcp:monitoring", "live"],
128 timeout: 90_000,
129 },
130 );