@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 176 lines 6.69 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as alerting from "@distilled.cloud/cloudflare/alerting";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const EMAIL = "test@alchemy.run";
16
17 const errorCount = (accountId: string) =>
18 `SELECT count() AS value FROM logs.workersLogs WHERE accountTag = '${accountId}' AND level = 'error'`;
19
20 // Every alert is disabled so the tests never evaluate queries or send mail.
21 const alert = (accountId: string, trigger: Cloudflare.Alerting.CustomAlertTrigger) =>
22 Cloudflare.Alerting.CustomAlert("Alert", {
23 query: errorCount(accountId),
24 trigger,
25 every: "5m",
26 enabled: false,
27 mechanisms: { email: [{ id: EMAIL }] },
28 });
29
30 test.provider(
31 "create, update, delete a threshold alert",
32 (stack) =>
33 Effect.gen(function* () {
34 const { accountId } = yield* yield* CloudflareEnvironment;
35
36 yield* stack.destroy();
37
38 const threshold = (value: number) =>
39 alert(accountId, { type: "threshold", operator: ">", value, window: "5m" });
40
41 const created = yield* stack.deploy(threshold(100));
42 expect(created.accountId).toEqual(accountId);
43 expect(created.enabled).toBe(false);
44
45 // Verify out-of-band: the trigger object is stored as the
46 // custom_analytics filters.
47 const policy = yield* alerting.getPolicy({ accountId, policyId: created.policyId });
48 expect(policy.alertType).toEqual("custom_analytics");
49 expect(policy.filters).toEqual({
50 source: ["sql_api"],
51 selectors: [errorCount(accountId)],
52 executionInterval: ["5m"],
53 alertTriggerPreferences: ["threshold"],
54 alertTriggerPreferencesOperator: [">"],
55 alertTriggerPreferencesValue: ["100"],
56 alertTriggerPreferencesShortWindow: ["5m"],
57 });
58
59 // The stored filters match what we send, so a redeploy does not PUT.
60 yield* stack.deploy(threshold(100));
61 const unchanged = yield* alerting.getPolicy({ accountId, policyId: created.policyId });
62 expect(unchanged.modified).toEqual(policy.modified);
63
64 // Changing the value updates in place.
65 const updated = yield* stack.deploy(threshold(250));
66 expect(updated.policyId).toEqual(created.policyId);
67 const afterUpdate = yield* alerting.getPolicy({ accountId, policyId: created.policyId });
68 expect(afterUpdate.filters?.alertTriggerPreferencesValue).toEqual(["250"]);
69
70 yield* stack.destroy();
71
72 yield* waitForPolicyDeleted(accountId, created.policyId);
73 }).pipe(logLevel),
74 { tags: ["provider:cloudflare", "provider:cloudflare:alerting", "live"] },
75 );
76
77 test.provider(
78 "switches between threshold, anomaly and SLO triggers in place",
79 (stack) =>
80 Effect.gen(function* () {
81 const { accountId } = yield* yield* CloudflareEnvironment;
82
83 yield* stack.destroy();
84
85 const created = yield* stack.deploy(
86 alert(accountId, { type: "threshold", operator: "<=", value: 5, window: "10m" }),
87 );
88
89 const anomaly = yield* stack.deploy(
90 alert(accountId, {
91 type: "anomaly",
92 direction: "both",
93 zScore: 3,
94 window: "5m",
95 baselineWindow: "1h",
96 minimumThreshold: 10,
97 }),
98 );
99 expect(anomaly.policyId).toEqual(created.policyId);
100 const anomalyPolicy = yield* alerting.getPolicy({ accountId, policyId: created.policyId });
101 expect(anomalyPolicy.filters?.alertTriggerPreferences).toEqual(["zscore_spike_and_drop"]);
102 expect(anomalyPolicy.filters?.alertTriggerPreferencesValue).toEqual(["3"]);
103 expect(anomalyPolicy.filters?.alertTriggerPreferencesLongWindow).toEqual(["1h"]);
104 expect(anomalyPolicy.filters?.alertTriggerPreferencesMinimumThreshold).toEqual(["10"]);
105
106 const slo = yield* stack.deploy(
107 alert(accountId, { type: "slo", target: 99.9, shortWindow: "5m", longWindow: "1h" }),
108 );
109 expect(slo.policyId).toEqual(created.policyId);
110 const sloPolicy = yield* alerting.getPolicy({ accountId, policyId: created.policyId });
111 expect(sloPolicy.filters?.alertTriggerPreferences).toEqual(["slo"]);
112 expect(sloPolicy.filters?.alertTriggerPreferencesValue).toEqual(["99.9"]);
113 expect(sloPolicy.filters?.alertTriggerPreferencesMinimumThreshold).toBeUndefined();
114
115 yield* stack.destroy();
116
117 yield* waitForPolicyDeleted(accountId, created.policyId);
118 }).pipe(logLevel),
119 { tags: ["provider:cloudflare", "provider:cloudflare:alerting", "live"] },
120 );
121
122 test.provider(
123 "rejects a query the SQL API refuses",
124 (stack) =>
125 Effect.gen(function* () {
126 yield* stack.destroy();
127
128 const error = yield* stack
129 .deploy(
130 Cloudflare.Alerting.CustomAlert("BadQuery", {
131 // No account scope: the SQL API rejects the query.
132 query: "SELECT count() AS value FROM logs.workersLogs",
133 trigger: { type: "threshold", operator: ">", value: 100, window: "5m" },
134 enabled: false,
135 mechanisms: { email: [{ id: EMAIL }] },
136 }),
137 )
138 .pipe(Effect.flip);
139 expect(error._tag).toEqual("InvalidAlertQuery");
140
141 yield* stack.destroy();
142 }).pipe(logLevel),
143 { tags: ["provider:cloudflare", "provider:cloudflare:alerting", "live"] },
144 );
145
146 test.provider(
147 "list enumerates the deployed custom alert",
148 (stack) =>
149 Effect.gen(function* () {
150 const { accountId } = yield* yield* CloudflareEnvironment;
151
152 yield* stack.destroy();
153
154 const deployed = yield* stack.deploy(
155 alert(accountId, { type: "threshold", operator: ">", value: 1, window: "5m" }),
156 );
157
158 const provider = yield* Provider.findProvider(Cloudflare.Alerting.CustomAlert);
159 const all = yield* provider.list();
160
161 expect(all.some((a) => a.policyId === deployed.policyId)).toBe(true);
162
163 yield* stack.destroy();
164 }).pipe(logLevel),
165 { tags: ["provider:cloudflare", "provider:cloudflare:alerting", "live"] },
166 );
167
168 const waitForPolicyDeleted = (accountId: string, policyId: string) =>
169 alerting.getPolicy({ accountId, policyId }).pipe(
170 Effect.flatMap(() => Effect.fail({ _tag: "PolicyNotDeleted" } as const)),
171 Effect.catchTag("PolicyNotFound", () => Effect.void),
172 Effect.retry({
173 while: (e) => e._tag === "PolicyNotDeleted",
174 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
175 }),
176 );