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

main
ts 160 lines 5.29 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as ConfigProvider from "effect/ConfigProvider";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8 import { expectUrlContains } from "../Utils/Http.ts";
9 import { startOtlpCollector } from "../Utils/OtlpCollector.ts";
10 import { makeTracedWorker } from "./fixtures/native-tracing/worker.ts";
11
12 /**
13 * `Cloudflare.Telemetry()` under `alchemy dev`: the Layer has no
14 * deploy-time effect on a local Worker and the compatibility date is not
15 * gated. Whether local workerd records the spans is the runtime's concern;
16 * these tests pin that the fixture (KV, a Durable Object, a queue consumer,
17 * forked fibers, failing and interrupted spans) runs unchanged under local
18 * emulation with the Layer provided, and that OTLP composition still works.
19 */
20 const { test } = Test.make({
21 providers: Cloudflare.providers(),
22 dev: true,
23 });
24
25 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
26
27 test.provider(
28 "local Cloudflare.Telemetry() does not break the Worker under emulation",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const deployed = yield* stack.deploy(
34 Effect.gen(function* () {
35 return {
36 producer: yield* makeTracedWorker("NativeTracingProducer"),
37 };
38 }),
39 );
40 expect(deployed.producer.url).toMatch(/^http:\/\/localhost:\d+$/);
41
42 // Every event path the fixture exercises works under local emulation
43 // with the Layer provided: fetch, forked fibers, failing and
44 // interrupted spans, a Durable Object RPC, and a queue round trip.
45 const url = deployed.producer.url!;
46 yield* expectUrlContains(`${url}/work?id=local-work`, "native-did-work");
47 yield* expectUrlContains(`${url}/fanout?id=local-fanout`, "local-fanout");
48 yield* expectUrlContains(`${url}/exits?id=local-exits`, "local-exits");
49 yield* expectUrlContains(`${url}/rpc?id=local-rpc`, "native-did-rpc:do-ok");
50 yield* expectUrlContains(`${url}/enqueue?id=local-queue`, "local-queue");
51 yield* expectUrlContains(`${url}/sampled`, "native-did-sample");
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 {
56 tags: [
57 "provider:cloudflare",
58 "provider:cloudflare:kv",
59 "provider:cloudflare:queue",
60 "provider:cloudflare:worker",
61 "local",
62 ],
63 timeout: 180_000,
64 },
65 );
66
67 test.provider(
68 "local Cloudflare.Telemetry() does not gate the compatibility date",
69 (stack) =>
70 Effect.gen(function* () {
71 yield* stack.destroy();
72
73 // Live deploys reject this date; dev just runs.
74 const worker = yield* stack.deploy(
75 Effect.gen(function* () {
76 return yield* makeTracedWorker("NativeTracingLocalOldDate", {
77 compatibility: { date: "2026-03-17" },
78 });
79 }),
80 );
81 expect(worker.url).toMatch(/^http:\/\/localhost:\d+$/);
82 yield* expectUrlContains(`${worker.url}/work`, "native-did-work");
83
84 yield* stack.destroy();
85 }).pipe(logLevel),
86 {
87 tags: [
88 "provider:cloudflare",
89 "provider:cloudflare:kv",
90 "provider:cloudflare:queue",
91 "provider:cloudflare:worker",
92 "local",
93 ],
94 timeout: 120_000,
95 },
96 );
97
98 const compositionTest = (order: "cf-last" | "otlp-last") =>
99 test.provider(
100 `local Cloudflare.Telemetry() + logs-only OTLP compose (${order})`,
101 (stack) =>
102 Effect.gen(function* () {
103 const collector = yield* startOtlpCollector();
104 const currentConfig = yield* ConfigProvider.ConfigProvider;
105 yield* stack.destroy();
106
107 const deployed = yield* stack
108 .deploy(
109 Effect.gen(function* () {
110 return {
111 producer: yield* makeTracedWorker(
112 order === "cf-last"
113 ? "NativeTracingComposeCfLast"
114 : "NativeTracingComposeOtlpLast",
115 { compatibility: { date: "2026-08-25" } },
116 ),
117 };
118 }),
119 )
120 .pipe(
121 Effect.provideService(
122 ConfigProvider.ConfigProvider,
123 ConfigProvider.orElse(
124 ConfigProvider.fromUnknown({
125 COLLECTOR_URL: collector.url,
126 COMPOSE_ORDER: order,
127 }),
128 currentConfig,
129 ),
130 ),
131 );
132
133 yield* expectUrlContains(deployed.producer.url!, "native-tracing-ok");
134 yield* expectUrlContains(`${deployed.producer.url}/work`, "native-did-work");
135
136 yield* Effect.sync(() => collector.completedRequests.value).pipe(
137 Effect.repeat({
138 schedule: Schedule.spaced("200 millis"),
139 until: (posts) => posts >= 1,
140 times: 40,
141 }),
142 );
143 expect(collector.completedRequests.value).toBeGreaterThanOrEqual(1);
144
145 yield* stack.destroy();
146 }).pipe(logLevel),
147 {
148 tags: [
149 "provider:cloudflare",
150 "provider:cloudflare:kv",
151 "provider:cloudflare:queue",
152 "provider:cloudflare:worker",
153 "local",
154 ],
155 timeout: 180_000,
156 },
157 );
158
159 compositionTest("cf-last");
160 compositionTest("otlp-last");