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

main
ts 214 lines 8.03 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 Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import PollyTestFunctionLive, { BUCKET, LEXICON_NAME, PollyTestFunction } from "./handler.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "PollyBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The shared Lambda fixture occasionally answers a transient 5xx under load
28 // (cold re-init, IAM propagation). Retry only 5xx; a genuine 4xx/assertion
29 // failure surfaces immediately.
30 const send = (request: HttpClientRequest.HttpClientRequest) =>
31 HttpClient.execute(request).pipe(
32 Effect.flatMap((response) =>
33 response.status >= 500
34 ? response.text.pipe(
35 Effect.flatMap((body) =>
36 Effect.fail(new TransientUpstream({ status: response.status, body })),
37 ),
38 )
39 : Effect.succeed(response),
40 ),
41 Effect.retry({
42 while: (e) => e._tag === "TransientUpstream",
43 schedule: Schedule.max([Schedule.exponential("2 seconds"), Schedule.recurs(5)]),
44 }),
45 );
46
47 describe.sequential(
48 "Polly Bindings",
49 {
50 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:polly", "provider:aws:s3", "live"],
51 },
52 () => {
53 beforeAll(
54 Effect.gen(function* () {
55 yield* Effect.logInfo("Polly test setup: destroying previous resources");
56 yield* sharedStack.destroy();
57
58 yield* Effect.logInfo("Polly test setup: deploying fixture");
59 const { functionUrl } = yield* sharedStack.deploy(
60 Effect.gen(function* () {
61 return yield* PollyTestFunction;
62 }).pipe(Effect.provide(PollyTestFunctionLive)),
63 );
64
65 expect(functionUrl).toBeTruthy();
66 baseUrl = functionUrl!.replace(/\/+$/, "");
67
68 const readinessUrl = `${baseUrl}/ping`;
69 yield* Effect.logInfo(`Polly test setup: probing readiness at ${readinessUrl}`);
70 yield* HttpClient.get(readinessUrl).pipe(
71 Effect.flatMap((response) =>
72 response.status === 200
73 ? Effect.succeed(response)
74 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
75 ),
76 Effect.tapError((error) =>
77 Effect.logWarning(`Polly test setup: fixture not ready yet (${String(error)})`),
78 ),
79 Effect.retry({ schedule: readinessPolicy }),
80 );
81 }),
82 { timeout: 300_000 },
83 );
84
85 afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 180_000 });
86
87 describe("DescribeVoices", () => {
88 test.provider(
89 "lists en-US voices including Joanna",
90 (_stack) =>
91 Effect.gen(function* () {
92 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/voices`)).pipe(
93 Effect.flatMap((r) => r.json),
94 )) as { error?: string; count: number; voiceIds: string[] };
95
96 expect(response.error).toBeUndefined();
97 expect(response.count).toBeGreaterThan(0);
98 expect(response.voiceIds).toContain("Joanna");
99 }),
100 { timeout: 120_000 },
101 );
102 });
103
104 describe("ListLexicons", () => {
105 test.provider(
106 "lists the fixture's deployed lexicon",
107 (_stack) =>
108 Effect.gen(function* () {
109 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/lexicons`)).pipe(
110 Effect.flatMap((r) => r.json),
111 )) as { error?: string; names: string[] };
112
113 expect(response.error).toBeUndefined();
114 expect(response.names).toContain(LEXICON_NAME);
115 }),
116 { timeout: 120_000 },
117 );
118 });
119
120 describe("GetLexicon", () => {
121 test.provider(
122 "reads the lexicon's PLS content scoped to its ARN",
123 (_stack) =>
124 Effect.gen(function* () {
125 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/lexicon-content`)).pipe(
126 Effect.flatMap((r) => r.json),
127 )) as {
128 error?: string;
129 name: string | null;
130 containsAlias: boolean;
131 lexemesCount: number | null;
132 };
133
134 expect(response.error).toBeUndefined();
135 expect(response.name).toBe(LEXICON_NAME);
136 expect(response.containsAlias).toBe(true);
137 expect(response.lexemesCount).toBe(1);
138 }),
139 { timeout: 120_000 },
140 );
141 });
142
143 describe("StartSpeechSynthesisStream", () => {
144 // skipIf-gated: the distilled HTTP transport cannot send an input event
145 // stream (bidirectional streaming). A direct probe of
146 // `polly.startSpeechSynthesisStream` with a finite
147 // `Stream.make({ TextEvent }, { CloseStreamEvent })` ActionStream never
148 // receives response headers — it hangs until `Effect.timeout` fires
149 // (`TimeoutError` after 20s locally; the Lambda route hangs to the
150 // test timeout). Same transport gap leaves transcribe-streaming and
151 // lex StartConversation unbound fleet-wide. Re-enable with
152 // AWS_TEST_POLLY_STREAM=1 once distilled core supports bidirectional
153 // event-stream request bodies.
154 test.provider.skipIf(!process.env.AWS_TEST_POLLY_STREAM)(
155 "streams text events in and collects audio events out",
156 (_stack) =>
157 Effect.gen(function* () {
158 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/stream`)).pipe(
159 Effect.flatMap((r) => r.json),
160 )) as { error?: string; events: number; audioBytes: number; closed: boolean };
161
162 expect(response.error).toBeUndefined();
163 expect(response.events).toBeGreaterThan(0);
164 expect(response.audioBytes).toBeGreaterThan(1000);
165 expect(response.closed).toBe(true);
166 }),
167 { timeout: 120_000 },
168 );
169 });
170
171 describe("SynthesizeSpeech", () => {
172 test.provider(
173 "synthesizes text with the lexicon applied to non-empty mp3 bytes",
174 (_stack) =>
175 Effect.gen(function* () {
176 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/synthesize`)).pipe(
177 Effect.flatMap((r) => r.json),
178 )) as { error?: string; contentType?: string; byteLength: number };
179
180 expect(response.error).toBeUndefined();
181 expect(response.contentType).toContain("audio/mpeg");
182 expect(response.byteLength).toBeGreaterThan(1000);
183 }),
184 { timeout: 120_000 },
185 );
186 });
187
188 describe("StartSpeechSynthesisTask + GetSpeechSynthesisTask + ListSpeechSynthesisTasks", () => {
189 test.provider(
190 "runs an async synthesis task to completion in S3",
191 (_stack) =>
192 Effect.gen(function* () {
193 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/task`)).pipe(
194 Effect.flatMap((r) => r.json),
195 )) as {
196 error?: string;
197 taskId: string;
198 status: string | null;
199 statusReason: string | null;
200 outputUri: string | null;
201 listedCount: number;
202 };
203
204 expect(response.error).toBeUndefined();
205 expect(response.taskId).toBeTruthy();
206 expect(response.status).toBe("completed");
207 expect(response.outputUri).toContain(BUCKET);
208 expect(response.listedCount).toBeGreaterThan(0);
209 }),
210 { timeout: 120_000 },
211 );
212 });
213 },
214 );