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

main
ts 173 lines 5.93 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 stream from "@distilled.cloud/cloudflare/stream";
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 // The scoped API token the test harness mints propagates eventually-
16 // consistently across Cloudflare's edge — ride out 403 blips
17 // (`Forbidden`, declared in the distilled error union) on the test's
18 // own out-of-band verification calls.
19 const getLiveInput = (accountId: string, liveInputId: string) =>
20 stream.getLiveInput({ accountId, liveInputIdentifier: liveInputId }).pipe(
21 Effect.retry({
22 while: (e) => e._tag === "Forbidden",
23 schedule: Schedule.exponential("500 millis"),
24 times: 8,
25 }),
26 );
27
28 const expectGone = (accountId: string, liveInputId: string) =>
29 getLiveInput(accountId, liveInputId).pipe(
30 Effect.flatMap(() => Effect.fail({ _tag: "LiveInputNotDeleted" } as const)),
31 // A missing live input surfaces as `LiveInputNotFound` (Cloudflare
32 // error code 10003) — that's the success condition here.
33 Effect.catchTag("LiveInputNotFound", () => Effect.void),
34 Effect.retry({
35 while: (e) => e._tag === "LiveInputNotDeleted",
36 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
37 }),
38 );
39
40 test.provider(
41 "create, update in place, and delete a live input",
42 (stack) =>
43 Effect.gen(function* () {
44 const { accountId } = yield* yield* CloudflareEnvironment;
45
46 yield* stack.destroy();
47
48 const input = yield* stack.deploy(
49 Cloudflare.Stream.LiveInput("BroadcastInput", {
50 meta: { name: "alchemy-stream-live-input" },
51 recording: { mode: "automatic", timeoutSeconds: 10 },
52 }),
53 );
54
55 expect(input.liveInputId).toBeTruthy();
56 expect(input.accountId).toEqual(accountId);
57 expect(input.enabled).toBe(true);
58 expect(input.meta).toMatchObject({ name: "alchemy-stream-live-input" });
59
60 const live = yield* getLiveInput(accountId, input.liveInputId);
61 expect(live.uid).toEqual(input.liveInputId);
62 expect(live.enabled).toBe(true);
63
64 // Update mutable props in place — same uid, no replacement.
65 const updated = yield* stack.deploy(
66 Cloudflare.Stream.LiveInput("BroadcastInput", {
67 enabled: false,
68 meta: { name: "alchemy-stream-live-input-v2" },
69 recording: { mode: "automatic", timeoutSeconds: 10 },
70 }),
71 );
72
73 expect(updated.liveInputId).toEqual(input.liveInputId);
74 expect(updated.enabled).toBe(false);
75 expect(updated.meta).toMatchObject({
76 name: "alchemy-stream-live-input-v2",
77 });
78
79 const observed = yield* getLiveInput(accountId, updated.liveInputId);
80 expect(observed.enabled).toBe(false);
81
82 // Redeploying identical props is a no-op (still the same input).
83 const noop = yield* stack.deploy(
84 Cloudflare.Stream.LiveInput("BroadcastInput", {
85 enabled: false,
86 meta: { name: "alchemy-stream-live-input-v2" },
87 recording: { mode: "automatic", timeoutSeconds: 10 },
88 }),
89 );
90 expect(noop.liveInputId).toEqual(input.liveInputId);
91
92 yield* stack.destroy();
93
94 yield* expectGone(accountId, input.liveInputId);
95 }).pipe(logLevel),
96 {
97 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
98 timeout: 120_000,
99 },
100 );
101
102 test.provider(
103 "recreates after out-of-band delete",
104 (stack) =>
105 Effect.gen(function* () {
106 const { accountId } = yield* yield* CloudflareEnvironment;
107
108 yield* stack.destroy();
109
110 const input = yield* stack.deploy(
111 Cloudflare.Stream.LiveInput("HealInput", {
112 meta: { name: "alchemy-stream-heal-input" },
113 }),
114 );
115
116 // Delete the live input out-of-band. A redeploy with identical props
117 // is a planner no-op, so change a prop to force reconcile — it must
118 // observe the input as missing and recreate it instead of failing.
119 yield* stream.deleteLiveInput({ accountId, liveInputIdentifier: input.liveInputId }).pipe(
120 Effect.retry({
121 while: (e) => e._tag === "Forbidden",
122 schedule: Schedule.exponential("500 millis"),
123 times: 8,
124 }),
125 );
126
127 const healed = yield* stack.deploy(
128 Cloudflare.Stream.LiveInput("HealInput", {
129 enabled: false,
130 meta: { name: "alchemy-stream-heal-input" },
131 }),
132 );
133
134 expect(healed.liveInputId).not.toEqual(input.liveInputId);
135 expect(healed.enabled).toBe(false);
136
137 yield* stack.destroy();
138
139 yield* expectGone(accountId, healed.liveInputId);
140 }).pipe(logLevel),
141 {
142 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
143 timeout: 120_000,
144 },
145 );
146
147 // Canonical `list()` test (account collection): deploy a live input, then
148 // resolve the typed provider and assert the deployed uid appears in the
149 // exhaustively-enumerated result.
150 test.provider(
151 "list enumerates the deployed live input",
152 (stack) =>
153 Effect.gen(function* () {
154 yield* stack.destroy();
155
156 const input = yield* stack.deploy(
157 Cloudflare.Stream.LiveInput("ListInput", {
158 meta: { name: "alchemy-stream-list-input" },
159 }),
160 );
161
162 const provider = yield* Provider.findProvider(Cloudflare.Stream.LiveInput);
163 const all = yield* provider.list();
164
165 expect(all.some((x) => x.liveInputId === input.liveInputId)).toBe(true);
166
167 yield* stack.destroy();
168 }).pipe(logLevel),
169 {
170 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
171 timeout: 120_000,
172 },
173 );