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

main
ts 322 lines 11.8 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 ivschat from "@distilled.cloud/aws/ivschat";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import WebSocket from "ws";
12 import IVSChatTestFunctionLive, { IVSChatTestFunction } 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, "IVSChatBindings");
17
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 class WsFailure extends Data.TaggedError("WsFailure")<{ readonly reason: string }> {}
28
29 const post = (path: string) =>
30 HttpClient.execute(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(
31 Effect.flatMap((response) =>
32 response.status >= 500
33 ? response.text.pipe(
34 Effect.flatMap((body) =>
35 Effect.fail(new TransientUpstream({ status: response.status, body })),
36 ),
37 )
38 : Effect.succeed(response),
39 ),
40 Effect.retry({
41 while: (e) => e._tag === "TransientUpstream",
42 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]),
43 }),
44 );
45
46 describe(
47 "IVSChat Bindings",
48 { tags: ["provider:aws", "provider:aws:ivschat", "provider:aws:lambda", "live"] },
49 () => {
50 beforeAll(
51 Effect.gen(function* () {
52 yield* Effect.logInfo("IVSChat test setup: destroying previous resources");
53 yield* sharedStack.destroy();
54
55 yield* Effect.logInfo("IVSChat test setup: deploying fixture");
56 const { functionUrl } = yield* sharedStack.deploy(
57 Effect.gen(function* () {
58 return yield* IVSChatTestFunction;
59 }).pipe(Effect.provide(IVSChatTestFunctionLive)),
60 );
61
62 expect(functionUrl).toBeTruthy();
63 baseUrl = functionUrl!.replace(/\/+$/, "");
64 const readinessUrl = `${baseUrl}/ping`;
65
66 yield* Effect.logInfo(`IVSChat test setup: probing readiness at ${readinessUrl}`);
67 yield* HttpClient.get(readinessUrl).pipe(
68 Effect.flatMap((response) =>
69 response.status === 200
70 ? Effect.succeed(response)
71 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
72 ),
73 Effect.tapError((error) =>
74 Effect.logWarning(`IVSChat test setup: fixture not ready yet (${String(error)})`),
75 ),
76 Effect.retry({ schedule: readinessPolicy }),
77 );
78 }),
79 { timeout: 240_000 },
80 );
81
82 afterAll(sharedStack.destroy(), { timeout: 120_000 });
83
84 describe("IVSChat.CreateChatToken", () => {
85 test.provider(
86 "mints a redacted chat token honoring sessionDuration",
87 (_stack) =>
88 Effect.gen(function* () {
89 const response = (yield* post("/token").pipe(Effect.flatMap((r) => r.json))) as {
90 tokenLength: number;
91 tokenIsRedacted: boolean;
92 tokenExpirationTime?: string;
93 sessionExpirationTime?: string;
94 };
95
96 expect(response.tokenLength).toBeGreaterThan(0);
97 // SensitiveString in distilled decodes to a Redacted value.
98 expect(response.tokenIsRedacted).toBe(true);
99 expect(response.tokenExpirationTime).toBeTruthy();
100 expect(response.sessionExpirationTime).toBeTruthy();
101
102 // sessionDuration: "30 minutes" must reach the wire as
103 // sessionDurationInMinutes: 30 (the API default is 60).
104 const sessionMinutes =
105 (new Date(response.sessionExpirationTime!).getTime() - Date.now()) / 60_000;
106 expect(sessionMinutes).toBeGreaterThan(20);
107 expect(sessionMinutes).toBeLessThan(40);
108 }),
109 { timeout: 120_000 },
110 );
111 });
112
113 describe("IVSChat.SendEvent", () => {
114 test.provider(
115 "broadcasts an application event to the room",
116 (_stack) =>
117 Effect.gen(function* () {
118 const response = (yield* post("/send-event").pipe(Effect.flatMap((r) => r.json))) as {
119 id?: string;
120 };
121
122 expect(typeof response.id).toBe("string");
123 expect(response.id!.length).toBeGreaterThan(0);
124 }),
125 { timeout: 120_000 },
126 );
127 });
128
129 describe("IVSChat.DeleteMessage", () => {
130 test.provider(
131 "broadcasts a DELETEMESSAGE moderation event",
132 (_stack) =>
133 Effect.gen(function* () {
134 const response = (yield* post("/delete-message").pipe(
135 Effect.flatMap((r) => r.json),
136 )) as { deleted?: string };
137
138 expect(typeof response.deleted).toBe("string");
139 expect(response.deleted!.length).toBeGreaterThan(0);
140 }),
141 { timeout: 120_000 },
142 );
143 });
144
145 describe("IVSChat.RoomMessageReviewEventSource", () => {
146 /**
147 * The IVS Chat messaging wire format is PascalCase — send
148 * `{Action, Content, RequestId}`, receive `{Type, Content, ErrorCode,
149 * ErrorMessage}` frames.
150 */
151 interface ChatFrame {
152 Type: string;
153 Content?: string;
154 ErrorCode?: number;
155 ErrorMessage?: string;
156 }
157
158 /**
159 * Connect to the room's WebSocket messaging endpoint with a chat token,
160 * send one message, and resolve with the first frame the room sends
161 * back for it (`MESSAGE` when the review handler allows, `ERROR` when
162 * it denies). Encapsulated in `Effect.callback` so fiber interruption
163 * closes the socket.
164 */
165 const wsSendMessage = (endpoint: string, token: string, content: string) =>
166 Effect.callback<ChatFrame, WsFailure>((resume, signal) => {
167 const socket = new WebSocket(endpoint, token);
168 let settled = false;
169 const settle = (effect: Effect.Effect<ChatFrame, WsFailure>) => {
170 if (settled) return;
171 settled = true;
172 try {
173 socket.close();
174 } catch {
175 // already closed
176 }
177 resume(effect);
178 };
179 signal.addEventListener("abort", () =>
180 settle(Effect.fail(new WsFailure({ reason: "interrupted" }))),
181 );
182 socket.on("open", () =>
183 socket.send(
184 JSON.stringify({
185 Action: "SEND_MESSAGE",
186 RequestId: "alchemy-review-test",
187 Content: content,
188 }),
189 ),
190 );
191 socket.on("message", (data) => {
192 let frame: ChatFrame;
193 try {
194 frame = JSON.parse(String(data));
195 } catch {
196 return; // ignore unparsable frames
197 }
198 // MESSAGE (delivered) and ERROR (denied) both settle the probe;
199 // ignore unrelated frames (e.g. other EVENTs).
200 if (frame.Type === "MESSAGE" || frame.Type === "ERROR") {
201 settle(Effect.succeed(frame));
202 }
203 });
204 socket.on("error", (error) =>
205 settle(Effect.fail(new WsFailure({ reason: String(error) }))),
206 );
207 socket.on("close", (code) =>
208 settle(Effect.fail(new WsFailure({ reason: `closed early (${code})` }))),
209 );
210 }).pipe(
211 Effect.timeout("15 seconds"),
212 Effect.catchTag("TimeoutError", () =>
213 Effect.fail(new WsFailure({ reason: "no frame within 15s" })),
214 ),
215 );
216
217 const wsInfo = Effect.gen(function* () {
218 const info = (yield* post("/ws-info").pipe(Effect.flatMap((r) => r.json))) as {
219 token?: string;
220 endpoint?: string;
221 roomArn?: string;
222 };
223 expect(info.token).toBeTruthy();
224 expect(info.endpoint).toContain("wss://edge.ivschat.");
225 return info as { token: string; endpoint: string; roomArn: string };
226 });
227
228 test.provider(
229 "associates the handler + invoke permission on deploy",
230 (_stack) =>
231 Effect.gen(function* () {
232 const { roomArn } = yield* wsInfo;
233 // Out-of-band: the deployed room must carry the fixture Lambda as
234 // its messageReviewHandler (set through the binding contract).
235 const room = yield* ivschat.getRoom({ identifier: roomArn });
236 expect(room.messageReviewHandler?.uri).toContain(":function:");
237 expect(room.messageReviewHandler?.fallbackResult ?? "ALLOW").toBe("ALLOW");
238 }),
239 { timeout: 120_000 },
240 );
241
242 test.provider(
243 "review handler modifies allowed messages",
244 (_stack) =>
245 Effect.gen(function* () {
246 // The handler association + invoke Permission propagate shortly
247 // after deploy; until then IVS Chat's fallbackResult (ALLOW)
248 // delivers the ORIGINAL content — treat that as retryable.
249 const frame = yield* Effect.gen(function* () {
250 const { token, endpoint } = yield* wsInfo;
251 const received = yield* wsSendMessage(endpoint, token, "hello moderators");
252 if (received.Type !== "MESSAGE" || !received.Content?.includes("[reviewed]")) {
253 return yield* Effect.fail(
254 new WsFailure({ reason: `not yet reviewed: ${JSON.stringify(received)}` }),
255 );
256 }
257 return received;
258 }).pipe(
259 Effect.tapError((e) =>
260 Effect.logInfo(
261 `review-allow attempt failed: ${e._tag === "WsFailure" ? e.reason : String(e)}`,
262 ),
263 ),
264 Effect.retry({
265 schedule: Schedule.max([Schedule.exponential("2 seconds"), Schedule.recurs(5)]),
266 }),
267 );
268
269 expect(frame.Type).toBe("MESSAGE");
270 expect(frame.Content).toBe("hello moderators [reviewed]");
271 }),
272 { timeout: 120_000 },
273 );
274
275 test.provider(
276 "review handler denies flagged messages with a 406",
277 (_stack) =>
278 Effect.gen(function* () {
279 const frame = yield* Effect.gen(function* () {
280 const { token, endpoint } = yield* wsInfo;
281 const received = yield* wsSendMessage(endpoint, token, "please deny-me now");
282 if (received.Type !== "ERROR") {
283 return yield* Effect.fail(
284 new WsFailure({ reason: `not yet denied: ${JSON.stringify(received)}` }),
285 );
286 }
287 return received;
288 }).pipe(
289 Effect.tapError((e) =>
290 Effect.logInfo(
291 `review-deny attempt failed: ${e._tag === "WsFailure" ? e.reason : String(e)}`,
292 ),
293 ),
294 Effect.retry({
295 schedule: Schedule.max([Schedule.exponential("2 seconds"), Schedule.recurs(5)]),
296 }),
297 );
298
299 expect(frame.Type).toBe("ERROR");
300 expect(frame.ErrorCode).toBe(406);
301 expect(frame.ErrorMessage).toContain("alchemy-moderated");
302 }),
303 { timeout: 120_000 },
304 );
305 });
306
307 describe("IVSChat.DisconnectUser", () => {
308 test.provider(
309 "disconnects a user's room connections",
310 (_stack) =>
311 Effect.gen(function* () {
312 const response = (yield* post("/disconnect-user").pipe(
313 Effect.flatMap((r) => r.json),
314 )) as { ok?: boolean };
315
316 expect(response.ok).toBe(true);
317 }),
318 { timeout: 120_000 },
319 );
320 });
321 },
322 );