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

main
ts 490 lines 18.3 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 workers from "@distilled.cloud/cloudflare/workers";
6 import { adopt } from "alchemy/AdoptPolicy";
7 import type * as Plan from "alchemy/Plan";
8 import * as Provider from "alchemy/Provider";
9 import { isActionState, State } from "alchemy/State/State";
10 import * as Effect from "effect/Effect";
11 import * as HttpClient from "effect/http/HttpClient";
12 import * as Redacted from "effect/Redacted";
13 import { MinimumLogLevel } from "effect/References";
14 import * as Schedule from "effect/Schedule";
15 import * as Stream from "effect/Stream";
16 import * as pathe from "pathe";
17 import { testName } from "../../names.ts";
18
19 const { test } = Test.make({ providers: Cloudflare.providers() });
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 const main = pathe.resolve(import.meta.dirname, "fixtures", "observability-sink-worker.ts");
24
25 // Deterministic per-test destination names. Cloudflare enforces one
26 // destination per name account-wide, so each test owns disjoint names and
27 // reuses them on every run (never derive physical names from
28 // Date.now()/random).
29 const NAME_DEFAULT_URL = "https://example.com";
30 const NAME_UPDATE = testName("alchemy-obsdest-update");
31 const NAME_REPLACE_V1 = testName("alchemy-obsdest-replace-v1");
32 const NAME_REPLACE_V2 = testName("alchemy-obsdest-replace-v2");
33 const NAME_LIST = testName("alchemy-obsdest-list");
34
35 // The scoped API token the test harness mints propagates eventually-
36 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
37 // declared in the distilled error union) on the test's own out-of-band
38 // verification calls.
39 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
40
41 const listDestinations = (accountId: string) =>
42 workers.listObservabilityDestinations.items({ accountId }).pipe(
43 Stream.runCollect,
44 Effect.map((chunk) => Array.from(chunk)),
45 Effect.retry({
46 while: (e) => e._tag === "Forbidden",
47 schedule: forbiddenRetrySchedule,
48 times: 8,
49 }),
50 );
51
52 const findByName = (accountId: string, name: string) =>
53 listDestinations(accountId).pipe(Effect.map((ds) => ds.find((d) => d.name === name)));
54
55 // Delete every destination with the given name — used to purge leftovers
56 // from interrupted runs so tests start from a clean slate (Cloudflare
57 // enforces name uniqueness, so a leaked destination would conflict).
58 const purgeByName = (accountId: string, name: string) =>
59 listDestinations(accountId).pipe(
60 Effect.flatMap(
61 Effect.forEach((d) =>
62 d.name === name
63 ? workers
64 .deleteObservabilityDestination({ accountId, slug: d.slug })
65 .pipe(Effect.catch(() => Effect.void))
66 : Effect.void,
67 ),
68 ),
69 );
70
71 const actionOf = (plan: Plan.Plan, fqn: string) => plan.resources[fqn]?.action;
72
73 // Poll a freshly deployed workers.dev sink until it answers 200, so an
74 // update's preflight POST lands on a serving endpoint.
75 const warmSink = (url: string) =>
76 Effect.gen(function* () {
77 const client = yield* HttpClient.HttpClient;
78 return yield* client.get(url).pipe(
79 Effect.retry({
80 schedule: Schedule.exponential("500 millis"),
81 times: 5,
82 }),
83 Effect.repeat({
84 schedule: Schedule.spaced("2 seconds"),
85 until: (res) => res.status === 200,
86 times: 60,
87 }),
88 );
89 });
90
91 const expectGone = (accountId: string, name: string) =>
92 findByName(accountId, name).pipe(
93 Effect.flatMap((found) =>
94 found ? Effect.fail({ _tag: "DestinationNotDeleted" } as const) : Effect.void,
95 ),
96 Effect.retry({
97 while: (e) => e._tag === "DestinationNotDeleted",
98 schedule: Schedule.exponential("500 millis"),
99 times: 10,
100 }),
101 );
102
103 test.provider(
104 "create and delete a destination with default name",
105 (stack) =>
106 Effect.gen(function* () {
107 const { accountId } = yield* yield* CloudflareEnvironment;
108
109 yield* stack.destroy();
110
111 const dest = yield* stack.deploy(
112 Effect.gen(function* () {
113 return yield* Cloudflare.Workers.ObservabilityDestination("DefaultDest", {
114 url: NAME_DEFAULT_URL,
115 logpushDataset: "opentelemetry-logs",
116 // example.com rejects POSTs, so skip the create-time probe —
117 // this test never updates, and updates are what re-preflight.
118 skipPreflightCheck: true,
119 }).pipe(adopt(true));
120 }),
121 );
122
123 expect(dest.slug).toBeDefined();
124 expect(dest.accountId).toEqual(accountId);
125 expect(dest.url).toEqual(NAME_DEFAULT_URL);
126 expect(dest.logpushDataset).toEqual("opentelemetry-logs");
127 expect(dest.enabled).toBe(true);
128 expect(dest.scripts).toEqual([]);
129
130 const live = yield* findByName(accountId, dest.name);
131 expect(live?.slug).toEqual(dest.slug);
132 expect(live?.configuration.url).toEqual(NAME_DEFAULT_URL);
133 expect(live?.configuration.logpushDataset).toEqual("opentelemetry-logs");
134
135 yield* stack.destroy();
136
137 yield* expectGone(accountId, dest.name);
138 }).pipe(logLevel),
139 {
140 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
141 timeout: 300_000,
142 },
143 );
144
145 test.provider(
146 "update url, headers, and enabled in place (same slug)",
147 (stack) =>
148 Effect.gen(function* () {
149 const { accountId } = yield* yield* CloudflareEnvironment;
150
151 yield* stack.destroy();
152 yield* purgeByName(accountId, NAME_UPDATE);
153
154 // The destination's endpoint is a deployed Worker that answers 200
155 // to everything — Cloudflare re-runs the endpoint preflight (a POST)
156 // on every in-place update, so the sink must be live.
157 const initial = yield* stack.deploy(
158 Effect.gen(function* () {
159 const worker = yield* Cloudflare.Worker("ObsSinkWorker", {
160 main,
161 compatibility: { date: "2024-01-01" },
162 });
163 const dest = yield* Cloudflare.Workers.ObservabilityDestination("Dest", {
164 name: NAME_UPDATE,
165 url: worker.url.as<string>(),
166 logpushDataset: "opentelemetry-traces",
167 // Fresh workers.dev URLs take a few seconds to start serving —
168 // skip the create-time probe; the update below exercises the
169 // real preflight.
170 skipPreflightCheck: true,
171 }).pipe(adopt(true));
172 return { worker, dest };
173 }),
174 );
175
176 expect(initial.dest.name).toEqual(NAME_UPDATE);
177 expect(initial.dest.url).toEqual(initial.worker.url);
178 expect(initial.dest.enabled).toBe(true);
179
180 // Warm the sink through edge propagation so the update's preflight
181 // POST lands on a serving workers.dev URL. Fresh workers.dev hosts
182 // answer 404 until the subdomain propagates, so poll until 200.
183 const warm = yield* warmSink(initial.worker.url!);
184 expect(warm.status).toBe(200);
185
186 const updated = yield* stack.deploy(
187 Effect.gen(function* () {
188 const worker = yield* Cloudflare.Worker("ObsSinkWorker", {
189 main,
190 compatibility: { date: "2024-01-01" },
191 });
192 const dest = yield* Cloudflare.Workers.ObservabilityDestination("Dest", {
193 name: NAME_UPDATE,
194 url: worker.url.as<string>(),
195 headers: { "x-alchemy-test": "1" },
196 logpushDataset: "opentelemetry-traces",
197 enabled: false,
198 }).pipe(adopt(true));
199 return { worker, dest };
200 }),
201 );
202
203 // Same destination mutated in place — not a replacement.
204 expect(updated.dest.slug).toEqual(initial.dest.slug);
205 expect(updated.dest.url).toEqual(updated.worker.url);
206 expect(updated.dest.enabled).toBe(false);
207
208 const live = yield* findByName(accountId, NAME_UPDATE);
209 expect(live?.slug).toEqual(initial.dest.slug);
210 expect(live?.enabled).toBe(false);
211 expect(live?.configuration.url).toEqual(updated.worker.url);
212 // Cloudflare masks header values (`null`); only the names are observable.
213 expect(Object.keys(live?.configuration.headers ?? {})).toContain("x-alchemy-test");
214
215 // Redeploying identical props is a no-op (and must not re-preflight).
216 const noop = yield* stack.deploy(
217 Effect.gen(function* () {
218 const worker = yield* Cloudflare.Worker("ObsSinkWorker", {
219 main,
220 compatibility: { date: "2024-01-01" },
221 });
222 const dest = yield* Cloudflare.Workers.ObservabilityDestination("Dest", {
223 name: NAME_UPDATE,
224 url: worker.url.as<string>(),
225 headers: { "x-alchemy-test": "1" },
226 logpushDataset: "opentelemetry-traces",
227 enabled: false,
228 }).pipe(adopt(true));
229 return { worker, dest };
230 }),
231 );
232 expect(noop.dest.slug).toEqual(initial.dest.slug);
233
234 yield* stack.destroy();
235
236 yield* expectGone(accountId, NAME_UPDATE);
237 }).pipe(logLevel),
238 {
239 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
240 timeout: 300_000,
241 },
242 );
243
244 test.provider(
245 "create and update with unchanged headers do not PATCH despite masked values",
246 (stack) =>
247 Effect.gen(function* () {
248 const { accountId } = yield* yield* CloudflareEnvironment;
249
250 yield* stack.destroy();
251
252 // Cloudflare masks header values (`null`) on read. The endpoint
253 // rejects POSTs, so a PATCH — which always re-runs the preflight —
254 // fails the deploy: create and the update below only succeed if
255 // reconcile sees the unchanged headers as converged and skips it.
256 const program = (skipPreflightCheck: boolean) =>
257 Cloudflare.Workers.ObservabilityDestination("MaskedDest", {
258 url: NAME_DEFAULT_URL,
259 headers: { "x-alchemy-test": "1" },
260 logpushDataset: "opentelemetry-logs",
261 skipPreflightCheck,
262 });
263
264 const initial = yield* stack.deploy(program(true));
265 const live = yield* findByName(accountId, initial.name);
266 expect(live?.configuration.headers["x-alchemy-test"]).toBeNull();
267
268 // `skipPreflightCheck` only applies to create, so flipping it runs
269 // reconcile with nothing to sync.
270 expect(actionOf(yield* stack.plan(program(false)), "MaskedDest")).toBe("update");
271 const updated = yield* stack.deploy(program(false));
272 expect(updated.slug).toEqual(initial.slug);
273
274 yield* stack.destroy();
275
276 yield* expectGone(accountId, initial.name);
277 }).pipe(logLevel),
278 {
279 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
280 timeout: 300_000,
281 },
282 );
283
284 test.provider(
285 "replacement on name change (new slug, old destination removed)",
286 (stack) =>
287 Effect.gen(function* () {
288 const { accountId } = yield* yield* CloudflareEnvironment;
289
290 yield* stack.destroy();
291 yield* purgeByName(accountId, NAME_REPLACE_V1);
292 yield* purgeByName(accountId, NAME_REPLACE_V2);
293
294 const v1 = yield* stack.deploy(
295 Effect.gen(function* () {
296 return yield* Cloudflare.Workers.ObservabilityDestination("ReplaceDest", {
297 name: NAME_REPLACE_V1,
298 url: NAME_DEFAULT_URL,
299 logpushDataset: "opentelemetry-logs",
300 skipPreflightCheck: true,
301 }).pipe(adopt(true));
302 }),
303 );
304 expect(v1.name).toEqual(NAME_REPLACE_V1);
305
306 // The slug is derived from the name and the update API cannot
307 // rename — a name change replaces the physical destination.
308 const v2 = yield* stack.deploy(
309 Effect.gen(function* () {
310 return yield* Cloudflare.Workers.ObservabilityDestination("ReplaceDest", {
311 name: NAME_REPLACE_V2,
312 url: NAME_DEFAULT_URL,
313 logpushDataset: "opentelemetry-logs",
314 skipPreflightCheck: true,
315 }).pipe(adopt(true));
316 }),
317 );
318
319 expect(v2.name).toEqual(NAME_REPLACE_V2);
320 expect(v2.slug).not.toEqual(v1.slug);
321
322 const liveV2 = yield* findByName(accountId, NAME_REPLACE_V2);
323 expect(liveV2?.slug).toEqual(v2.slug);
324 // The replaced destination is cleaned up.
325 yield* expectGone(accountId, NAME_REPLACE_V1);
326
327 yield* stack.destroy();
328
329 yield* expectGone(accountId, NAME_REPLACE_V2);
330 }).pipe(logLevel),
331 {
332 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
333 timeout: 300_000,
334 },
335 );
336
337 test.provider(
338 "list enumerates the deployed destination",
339 (stack) =>
340 Effect.gen(function* () {
341 const { accountId } = yield* yield* CloudflareEnvironment;
342
343 yield* stack.destroy();
344 yield* purgeByName(accountId, NAME_LIST);
345
346 const dest = yield* stack.deploy(
347 Effect.gen(function* () {
348 return yield* Cloudflare.Workers.ObservabilityDestination("ListDest", {
349 name: NAME_LIST,
350 url: NAME_DEFAULT_URL,
351 logpushDataset: "opentelemetry-logs",
352 // example.com rejects POSTs, so skip the create-time probe.
353 skipPreflightCheck: true,
354 }).pipe(adopt(true));
355 }),
356 );
357
358 // Typed provider lookup — `findProvider` infers the element type as the
359 // resource's Attributes, so `list()` is fully typed (no `any`).
360 const provider = yield* Provider.findProvider(Cloudflare.Workers.ObservabilityDestination);
361 const all = yield* provider.list();
362
363 // The exhaustively-paginated result contains our deployed destination,
364 // hydrated into the exact `read` Attributes shape.
365 const found = all.find((d) => d.slug === dest.slug);
366 expect(found).toBeDefined();
367 expect(found?.accountId).toEqual(accountId);
368 expect(found?.name).toEqual(NAME_LIST);
369 expect(found?.url).toEqual(NAME_DEFAULT_URL);
370 expect(found?.logpushDataset).toEqual("opentelemetry-logs");
371 // Cloudflare may omit `destinationConf`; when present it is redacted.
372 expect(typeof found?.destinationConf).not.toBe("string");
373
374 yield* stack.destroy();
375
376 yield* expectGone(accountId, NAME_LIST);
377 }).pipe(logLevel),
378 {
379 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
380 timeout: 300_000,
381 },
382 );
383
384 test.provider(
385 "redacted headers are sent unwrapped and destinationConf stays redacted",
386 (stack) =>
387 Effect.gen(function* () {
388 const { accountId } = yield* yield* CloudflareEnvironment;
389
390 yield* stack.destroy();
391
392 // Engine-generated name (derived from the stage), so concurrent runs by
393 // other users or worktrees never collide. Synthetic credential — the
394 // sink Worker accepts any request.
395 const program = (token: string) =>
396 Effect.gen(function* () {
397 const worker = yield* Cloudflare.Worker("ObsSinkWorker", {
398 main,
399 compatibility: { date: "2024-01-01" },
400 });
401 const dest = yield* Cloudflare.Workers.ObservabilityDestination("RedactedDest", {
402 url: worker.url.as<string>(),
403 headers: {
404 authorization: Redacted.make(`Bearer ${token}`),
405 "x-alchemy-test": "1",
406 },
407 logpushDataset: "opentelemetry-traces",
408 skipPreflightCheck: true,
409 });
410 return { worker, dest };
411 });
412
413 const initial = yield* stack.deploy(program("alchemy-test-token-v1"));
414
415 expect(typeof initial.dest.destinationConf).not.toBe("string");
416 expect(JSON.stringify(initial.dest)).not.toContain("alchemy-test-token-v1");
417
418 // Cloudflare masks header values (`null`); only the names are observable.
419 const live = yield* findByName(accountId, initial.dest.name);
420 expect(Object.keys(live?.configuration.headers ?? {})).toEqual(
421 expect.arrayContaining(["authorization", "x-alchemy-test"]),
422 );
423 expect(JSON.stringify(live)).not.toContain("alchemy-test-token-v1");
424
425 // A fresh `Redacted` wrapper around the same secret is not a change.
426 expect(actionOf(yield* stack.plan(program("alchemy-test-token-v1")), "RedactedDest")).toBe(
427 "noop",
428 );
429
430 // Rotating the secret updates the destination in place. The PATCH
431 // re-runs the endpoint preflight, so the sink must be serving.
432 const warm = yield* warmSink(initial.worker.url!);
433 expect(warm.status).toBe(200);
434
435 expect(actionOf(yield* stack.plan(program("alchemy-test-token-v2")), "RedactedDest")).toBe(
436 "update",
437 );
438 const rotated = yield* stack.deploy(program("alchemy-test-token-v2"));
439 expect(rotated.dest.slug).toEqual(initial.dest.slug);
440 expect(typeof rotated.dest.destinationConf).not.toBe("string");
441 expect(JSON.stringify(rotated.dest)).not.toContain("alchemy-test-token-v2");
442 // The masked header value must not make the next deploy perpetually dirty.
443 expect(actionOf(yield* stack.plan(program("alchemy-test-token-v2")), "RedactedDest")).toBe(
444 "noop",
445 );
446
447 // State written by earlier versions holds `destinationConf` as a
448 // plain string. The next deploy reconciles it back to `Redacted`.
449 yield* Effect.gen(function* () {
450 const state = yield* yield* State;
451 const address = { stack: stack.name, stage: stack.stage, fqn: "RedactedDest" };
452 const stored = yield* state.get(address);
453 if (
454 !stored ||
455 isActionState(stored) ||
456 (stored.status !== "created" && stored.status !== "updated")
457 ) {
458 return yield* Effect.die(new Error("Expected a settled destination row"));
459 }
460 yield* state.set({
461 ...address,
462 value: {
463 ...stored,
464 attr: {
465 ...stored.attr,
466 destinationConf: "legacy-plain-destination-conf",
467 },
468 },
469 });
470 }).pipe(Effect.provide(stack.state));
471
472 expect(actionOf(yield* stack.plan(program("alchemy-test-token-v2")), "RedactedDest")).toBe(
473 "update",
474 );
475 const migrated = yield* stack.deploy(program("alchemy-test-token-v2"));
476 expect(migrated.dest.slug).toEqual(initial.dest.slug);
477 expect(typeof migrated.dest.destinationConf).not.toBe("string");
478 expect(actionOf(yield* stack.plan(program("alchemy-test-token-v2")), "RedactedDest")).toBe(
479 "noop",
480 );
481
482 yield* stack.destroy();
483
484 yield* expectGone(accountId, initial.dest.name);
485 }).pipe(logLevel),
486 {
487 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
488 timeout: 300_000,
489 },
490 );