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

main
ts 347 lines 12.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as eventbridge from "@distilled.cloud/aws/eventbridge";
6 import * as sts from "@distilled.cloud/aws/sts";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as HttpClientRequest from "effect/http/HttpClientRequest";
11 import * as Schedule from "effect/Schedule";
12 import IVSRealtimeTestFunctionLive, { IVSRealtimeTestFunction } from "./fixtures/handler.ts";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "IVSRealtimeBindings");
17
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]);
19
20 let baseUrl: string;
21 let functionArn: string;
22
23 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
24 readonly status: number;
25 readonly body: string;
26 }> {}
27
28 const send = (request: HttpClientRequest.HttpClientRequest) =>
29 HttpClient.execute(request).pipe(
30 Effect.flatMap((response) =>
31 response.status >= 500
32 ? response.text.pipe(
33 Effect.flatMap((body) =>
34 Effect.fail(new TransientUpstream({ status: response.status, body })),
35 ),
36 )
37 : Effect.succeed(response),
38 ),
39 Effect.retry({
40 while: (e) => e._tag === "TransientUpstream",
41 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]),
42 }),
43 );
44
45 const getJson = (path: string) =>
46 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
47
48 const postJson = (path: string) =>
49 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
50
51 /** Result of a probe route: success marker or the typed error tag. */
52 interface Probe {
53 ok?: boolean;
54 found?: boolean;
55 count?: number;
56 tag?: string;
57 }
58
59 describe(
60 "IVSRealtime Bindings",
61 { tags: ["provider:aws", "provider:aws:ivsrealtime", "provider:aws:lambda", "live"] },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* Effect.logInfo("IVSRealtime test setup: destroying previous resources");
66 yield* sharedStack.destroy();
67
68 yield* Effect.logInfo("IVSRealtime test setup: deploying fixture");
69 const attrs = yield* sharedStack.deploy(
70 Effect.gen(function* () {
71 return yield* IVSRealtimeTestFunction;
72 }).pipe(Effect.provide(IVSRealtimeTestFunctionLive)),
73 );
74
75 expect(attrs.functionUrl).toBeTruthy();
76 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
77 functionArn = attrs.functionArn;
78 const readinessUrl = `${baseUrl}/ping`;
79
80 yield* Effect.logInfo(`IVSRealtime test setup: probing readiness at ${readinessUrl}`);
81 yield* HttpClient.get(readinessUrl).pipe(
82 Effect.flatMap((response) =>
83 response.status === 200
84 ? Effect.succeed(response)
85 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
86 ),
87 Effect.tapError((error) =>
88 Effect.logWarning(`IVSRealtime test setup: fixture not ready yet (${String(error)})`),
89 ),
90 Effect.retry({ schedule: readinessPolicy }),
91 );
92 }),
93 { timeout: 240_000 },
94 );
95
96 afterAll(sharedStack.destroy(), { timeout: 120_000 });
97
98 describe("binding registration", () => {
99 test.provider("all fourteen capabilities initialize in the runtime", () =>
100 Effect.gen(function* () {
101 const response = (yield* getJson("/bindings")) as { bound: string[] };
102 expect(response.bound).toHaveLength(14);
103 }),
104 );
105 });
106
107 describe("IVSRealtime.CreateParticipantToken", () => {
108 test.provider(
109 "mints a redacted participant token honoring duration",
110 () =>
111 Effect.gen(function* () {
112 const response = (yield* postJson("/token")) as {
113 tokenLength: number;
114 tokenIsRedacted: boolean;
115 participantId?: string;
116 duration?: number;
117 };
118
119 expect(response.tokenLength).toBeGreaterThan(0);
120 // SensitiveString in distilled decodes to a Redacted value.
121 expect(response.tokenIsRedacted).toBe(true);
122 expect(response.participantId).toBeTruthy();
123 // duration: "30 minutes" must reach the wire as duration: 30.
124 expect(response.duration).toBe(30);
125 }),
126 { timeout: 120_000 },
127 );
128 });
129
130 describe("IVSRealtime.ListStageSessions", () => {
131 test.provider(
132 "lists the fresh stage's sessions (none yet)",
133 () =>
134 Effect.gen(function* () {
135 const response = (yield* getJson("/sessions")) as { count: number };
136 expect(response.count).toBe(0);
137 }),
138 { timeout: 120_000 },
139 );
140 });
141
142 describe("IVSRealtime.GetStageSession", () => {
143 test.provider(
144 "returns the typed not-found tag for a missing session",
145 () =>
146 Effect.gen(function* () {
147 const response = (yield* getJson("/session-missing")) as Probe;
148 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
149 }),
150 { timeout: 120_000 },
151 );
152 });
153
154 describe("IVSRealtime.ListParticipants", () => {
155 test.provider(
156 "round-trips a session-scoped list",
157 () =>
158 Effect.gen(function* () {
159 const response = (yield* getJson("/participants")) as Probe;
160 if (response.tag !== undefined) {
161 expect(response.tag).toBe("ValidationException");
162 } else {
163 expect(response.count).toBe(0);
164 }
165 }),
166 { timeout: 120_000 },
167 );
168 });
169
170 describe("IVSRealtime.GetParticipant", () => {
171 test.provider(
172 "returns the typed not-found tag for a missing participant",
173 () =>
174 Effect.gen(function* () {
175 const response = (yield* getJson("/participant-missing")) as Probe;
176 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
177 }),
178 { timeout: 120_000 },
179 );
180 });
181
182 describe("IVSRealtime.ListParticipantEvents", () => {
183 test.provider(
184 "round-trips a participant-scoped event list",
185 () =>
186 Effect.gen(function* () {
187 const response = (yield* getJson("/participant-events")) as Probe;
188 if (response.tag !== undefined) {
189 expect(response.tag).toBe("ValidationException");
190 } else {
191 expect(response.count).toBe(0);
192 }
193 }),
194 { timeout: 120_000 },
195 );
196 });
197
198 describe("IVSRealtime.ListParticipantReplicas", () => {
199 test.provider(
200 "round-trips a replica list for a missing participant",
201 () =>
202 Effect.gen(function* () {
203 const response = (yield* getJson("/replicas")) as Probe;
204 if (response.tag !== undefined) {
205 expect(response.tag).toBe("ValidationException");
206 } else {
207 expect(response.count).toBe(0);
208 }
209 }),
210 { timeout: 120_000 },
211 );
212 });
213
214 describe("IVSRealtime.DisconnectParticipant", () => {
215 test.provider(
216 "round-trips a disconnect for a missing participant",
217 () =>
218 Effect.gen(function* () {
219 const response = (yield* postJson("/disconnect")) as Probe;
220 if (response.tag !== undefined) {
221 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
222 } else {
223 expect(response.ok).toBe(true);
224 }
225 }),
226 { timeout: 120_000 },
227 );
228 });
229
230 describe("IVSRealtime.StartParticipantReplication", () => {
231 test.provider(
232 "returns a typed rejection for a missing participant",
233 () =>
234 Effect.gen(function* () {
235 const response = (yield* postJson("/replication-start")) as Probe;
236 expect([
237 "ResourceNotFoundException",
238 "ValidationException",
239 "ConflictException",
240 "PendingVerification",
241 ]).toContain(response.tag);
242 }),
243 { timeout: 120_000 },
244 );
245 });
246
247 describe("IVSRealtime.StopParticipantReplication", () => {
248 test.provider(
249 "round-trips a stop for a missing participant",
250 () =>
251 Effect.gen(function* () {
252 const response = (yield* postJson("/replication-stop")) as Probe;
253 if (response.tag !== undefined) {
254 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
255 } else {
256 expect(response.ok).toBe(true);
257 }
258 }),
259 { timeout: 120_000 },
260 );
261 });
262
263 describe("IVSRealtime.ListCompositions", () => {
264 test.provider(
265 "lists the account's compositions",
266 () =>
267 Effect.gen(function* () {
268 const response = (yield* getJson("/compositions")) as { count: number };
269 expect(response.count).toBeGreaterThanOrEqual(0);
270 }),
271 { timeout: 120_000 },
272 );
273 });
274
275 describe("IVSRealtime.GetComposition", () => {
276 test.provider(
277 "returns the typed not-found tag for a missing composition",
278 () =>
279 Effect.gen(function* () {
280 const region = yield* yield* AWS.Region;
281 const { Account } = yield* sts.getCallerIdentity({});
282 const arn = `arn:aws:ivs:${region}:${Account}:composition/AbCdEfGh1234`;
283 const response = (yield* getJson(
284 `/composition-missing?arn=${encodeURIComponent(arn)}`,
285 )) as Probe;
286 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
287 }),
288 { timeout: 120_000 },
289 );
290 });
291
292 describe("IVSRealtime.StopComposition", () => {
293 test.provider(
294 "returns the typed not-found tag for a missing composition",
295 () =>
296 Effect.gen(function* () {
297 const region = yield* yield* AWS.Region;
298 const { Account } = yield* sts.getCallerIdentity({});
299 const arn = `arn:aws:ivs:${region}:${Account}:composition/AbCdEfGh1234`;
300 const response = (yield* postJson(
301 `/composition-stop?arn=${encodeURIComponent(arn)}`,
302 )) as Probe;
303 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
304 }),
305 { timeout: 120_000 },
306 );
307 });
308
309 describe("IVSRealtime.StartComposition", () => {
310 test.provider(
311 "returns a typed rejection for a missing storage configuration",
312 () =>
313 Effect.gen(function* () {
314 const region = yield* yield* AWS.Region;
315 const { Account } = yield* sts.getCallerIdentity({});
316 const arn = `arn:aws:ivs:${region}:${Account}:storage-configuration/AbCdEfGh1234`;
317 const response = (yield* postJson(
318 `/composition-start?arn=${encodeURIComponent(arn)}`,
319 )) as Probe;
320 expect([
321 "ResourceNotFoundException",
322 "ValidationException",
323 "PendingVerification",
324 ]).toContain(response.tag);
325 }),
326 { timeout: 120_000 },
327 );
328 });
329
330 describe("consumeStageEvents", { tags: ["provider:aws:eventbridge"] }, () => {
331 test.provider(
332 "the deploy created an EventBridge rule targeting the function",
333 () =>
334 Effect.gen(function* () {
335 // Out-of-band via distilled: the fixture's consumeStageEvents must
336 // have materialized as a rule on the default bus with the Lambda
337 // as target.
338 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
339 TargetArn: functionArn,
340 });
341 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
342 }),
343 { timeout: 120_000 },
344 );
345 });
346 },
347 );