@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 257 lines 9.29 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 listOutputs = (accountId: string, liveInputId: string) =>
20 stream.listLiveInputOutputs({ 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 findOutput = (accountId: string, liveInputId: string, outputId: string) =>
29 listOutputs(accountId, liveInputId).pipe(
30 Effect.map((page) => page.result.find((o) => o.uid === outputId)),
31 );
32
33 const expectGone = (accountId: string, liveInputId: string, outputId: string) =>
34 findOutput(accountId, liveInputId, outputId).pipe(
35 Effect.flatMap((found) =>
36 found === undefined ? Effect.void : Effect.fail({ _tag: "OutputNotDeleted" } as const),
37 ),
38 // The parent live input being gone counts as the output being gone.
39 Effect.catchTag("LiveInputNotFound", () => Effect.void),
40 Effect.retry({
41 while: (e) => e._tag === "OutputNotDeleted",
42 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
43 }),
44 );
45
46 test.provider(
47 "create, toggle enabled in place, replace destination, and delete",
48 (stack) =>
49 Effect.gen(function* () {
50 const { accountId } = yield* yield* CloudflareEnvironment;
51
52 yield* stack.destroy();
53
54 const deployOutput = (props: { url: string; streamKey: string; enabled?: boolean }) =>
55 Effect.gen(function* () {
56 const input = yield* Cloudflare.Stream.LiveInput("RestreamInput", {
57 meta: { name: "alchemy-stream-output-input" },
58 });
59 const output = yield* Cloudflare.Stream.LiveInputOutput("Restream", {
60 liveInputId: input.liveInputId,
61 ...props,
62 });
63 return { input, output };
64 });
65
66 const created = yield* stack.deploy(
67 deployOutput({
68 url: "rtmps://a.rtmps.youtube.com/live2",
69 streamKey: "alchemy-test-stream-key",
70 }),
71 );
72
73 expect(created.output.outputId).toBeTruthy();
74 expect(created.output.liveInputId).toEqual(created.input.liveInputId);
75 expect(created.output.accountId).toEqual(accountId);
76 expect(created.output.url).toEqual("rtmps://a.rtmps.youtube.com/live2");
77 expect(created.output.streamKey).toEqual("alchemy-test-stream-key");
78 expect(created.output.enabled).toBe(true);
79
80 // Out-of-band verify via distilled.
81 const observed = yield* findOutput(
82 accountId,
83 created.input.liveInputId,
84 created.output.outputId,
85 );
86 expect(observed?.uid).toEqual(created.output.outputId);
87 expect(observed?.enabled).toBe(true);
88
89 // Toggle `enabled` — updates in place, same uid.
90 const disabled = yield* stack.deploy(
91 deployOutput({
92 url: "rtmps://a.rtmps.youtube.com/live2",
93 streamKey: "alchemy-test-stream-key",
94 enabled: false,
95 }),
96 );
97 expect(disabled.output.outputId).toEqual(created.output.outputId);
98 expect(disabled.output.enabled).toBe(false);
99
100 const observedDisabled = yield* findOutput(
101 accountId,
102 created.input.liveInputId,
103 created.output.outputId,
104 );
105 expect(observedDisabled?.enabled).toBe(false);
106
107 // Redeploying identical props is a no-op (still the same output).
108 const noop = yield* stack.deploy(
109 deployOutput({
110 url: "rtmps://a.rtmps.youtube.com/live2",
111 streamKey: "alchemy-test-stream-key",
112 enabled: false,
113 }),
114 );
115 expect(noop.output.outputId).toEqual(created.output.outputId);
116
117 // Changing the destination (url/streamKey) replaces the output.
118 const replaced = yield* stack.deploy(
119 deployOutput({
120 url: "rtmps://b.rtmps.youtube.com/live2?backup=1",
121 streamKey: "alchemy-test-stream-key-v2",
122 }),
123 );
124 expect(replaced.output.outputId).not.toEqual(created.output.outputId);
125 expect(replaced.output.url).toEqual("rtmps://b.rtmps.youtube.com/live2?backup=1");
126 expect(replaced.output.enabled).toBe(true);
127
128 // The replaced (old) output is gone; the new one exists.
129 yield* expectGone(accountId, created.input.liveInputId, created.output.outputId);
130 const observedReplaced = yield* findOutput(
131 accountId,
132 replaced.input.liveInputId,
133 replaced.output.outputId,
134 );
135 expect(observedReplaced?.uid).toEqual(replaced.output.outputId);
136
137 yield* stack.destroy();
138
139 yield* expectGone(accountId, replaced.input.liveInputId, replaced.output.outputId);
140 }).pipe(logLevel),
141 {
142 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
143 timeout: 120_000,
144 },
145 );
146
147 test.provider(
148 "recreates after out-of-band delete",
149 (stack) =>
150 Effect.gen(function* () {
151 const { accountId } = yield* yield* CloudflareEnvironment;
152
153 yield* stack.destroy();
154
155 const deployOutput = (enabled?: boolean) =>
156 Effect.gen(function* () {
157 const input = yield* Cloudflare.Stream.LiveInput("HealOutputInput", {
158 meta: { name: "alchemy-stream-output-heal-input" },
159 });
160 const output = yield* Cloudflare.Stream.LiveInputOutput("HealOutput", {
161 liveInputId: input.liveInputId,
162 url: "rtmps://a.rtmps.youtube.com/live2",
163 streamKey: "alchemy-heal-stream-key",
164 enabled,
165 });
166 return { input, output };
167 });
168
169 const created = yield* stack.deploy(deployOutput());
170
171 // Delete the output out-of-band. A redeploy with identical props is
172 // a planner no-op, so toggle `enabled` to force reconcile — it must
173 // observe the output as missing and recreate it instead of failing.
174 yield* stream
175 .deleteLiveInputOutput({
176 accountId,
177 liveInputIdentifier: created.input.liveInputId,
178 outputIdentifier: created.output.outputId,
179 })
180 .pipe(
181 Effect.retry({
182 while: (e) => e._tag === "Forbidden",
183 schedule: Schedule.exponential("500 millis"),
184 times: 8,
185 }),
186 );
187
188 const healed = yield* stack.deploy(deployOutput(false));
189
190 expect(healed.output.outputId).not.toEqual(created.output.outputId);
191 expect(healed.output.enabled).toBe(false);
192
193 yield* stack.destroy();
194
195 yield* expectGone(accountId, healed.input.liveInputId, healed.output.outputId);
196 }).pipe(logLevel),
197 {
198 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
199 timeout: 120_000,
200 },
201 );
202
203 // `list()` enumerates every live input on the account (via
204 // `stream.listLiveInputs`) and then lists each input's outputs.
205 test.provider(
206 "list enumerates outputs across all live inputs",
207 (stack) =>
208 Effect.gen(function* () {
209 const { accountId } = yield* yield* CloudflareEnvironment;
210
211 yield* stack.destroy();
212
213 const deployed = yield* stack.deploy(
214 Effect.gen(function* () {
215 const input = yield* Cloudflare.Stream.LiveInput("ListOutputInput", {
216 meta: { name: "alchemy-stream-output-list-input" },
217 });
218 const output = yield* Cloudflare.Stream.LiveInputOutput("ListOutput", {
219 liveInputId: input.liveInputId,
220 url: "rtmps://a.rtmps.youtube.com/live2",
221 streamKey: "alchemy-list-stream-key",
222 });
223 return { input, output };
224 }),
225 );
226
227 const provider = yield* Provider.findProvider(Cloudflare.Stream.LiveInputOutput);
228
229 // Edge propagation: the freshly-created output (and its parent live
230 // input enumeration) is eventually consistent — retry until present.
231 const all = yield* provider.list().pipe(
232 Effect.flatMap((rows) =>
233 rows.some((o) => o.outputId === deployed.output.outputId)
234 ? Effect.succeed(rows)
235 : Effect.fail({ _tag: "OutputNotListed" } as const),
236 ),
237 Effect.retry({
238 while: (e) => e._tag === "OutputNotListed",
239 schedule: Schedule.spaced("2 seconds"),
240 times: 10,
241 }),
242 );
243
244 const found = all.find((o) => o.outputId === deployed.output.outputId);
245 expect(found).toBeDefined();
246 expect(found?.liveInputId).toEqual(deployed.input.liveInputId);
247 expect(found?.accountId).toEqual(accountId);
248 expect(found?.url).toEqual("rtmps://a.rtmps.youtube.com/live2");
249 expect(found?.enabled).toBe(true);
250
251 yield* stack.destroy();
252 }).pipe(logLevel),
253 {
254 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
255 timeout: 120_000,
256 },
257 );