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

main
ts 310 lines 12 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 ComprehendTestFunctionLive, { ComprehendTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "ComprehendBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy under parallel-suite load.
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 // Freshly attached IAM policies propagate eventually; an early Comprehend
28 // call can surface AccessDenied as a 500 through the handler's orDie. Retry
29 // 5xx only; a genuine 4xx/assertion 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): boolean => e._tag === "TransientUpstream",
43 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]),
44 }),
45 );
46
47 const getJson = (path: string) =>
48 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
49
50 const postJson = (path: string) =>
51 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
52
53 describe(
54 "Comprehend Bindings",
55 {
56 tags: [
57 "provider:aws",
58 "provider:aws:batch",
59 "provider:aws:comprehend",
60 "provider:aws:iam",
61 "provider:aws:lambda",
62 "provider:aws:s3",
63 "live",
64 ],
65 },
66 () => {
67 beforeAll(
68 Effect.gen(function* () {
69 yield* Effect.logInfo("Comprehend test setup: destroying previous resources");
70 yield* sharedStack.destroy();
71
72 yield* Effect.logInfo("Comprehend test setup: deploying fixture");
73 const { functionUrl } = yield* sharedStack.deploy(
74 Effect.gen(function* () {
75 return yield* ComprehendTestFunction;
76 }).pipe(Effect.provide(ComprehendTestFunctionLive)),
77 );
78
79 expect(functionUrl).toBeTruthy();
80 baseUrl = functionUrl!.replace(/\/+$/, "");
81 const readinessUrl = `${baseUrl}/ping`;
82
83 yield* Effect.logInfo(`Comprehend test setup: probing readiness at ${readinessUrl}`);
84 yield* HttpClient.get(readinessUrl).pipe(
85 Effect.flatMap((response) =>
86 response.status === 200
87 ? Effect.succeed(response)
88 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
89 ),
90 Effect.tapError((error) =>
91 Effect.logWarning(`Comprehend test setup: fixture not ready yet (${String(error)})`),
92 ),
93 Effect.retry({ schedule: readinessPolicy }),
94 );
95 }),
96 { timeout: 240_000 },
97 );
98
99 afterAll(sharedStack.destroy(), { timeout: 180_000 });
100
101 describe("real-time analysis (DetectDominantLanguage, DetectEntities, DetectKeyPhrases, DetectPiiEntities, DetectSentiment, DetectSyntax, DetectTargetedSentiment, DetectToxicContent, ContainsPiiEntities)", () => {
102 test.provider(
103 "every single-document detect binding returns a real inference",
104 (_stack) =>
105 Effect.gen(function* () {
106 const result = (yield* getJson("/detect-all")) as {
107 languageCode: string;
108 entityTypes: string[];
109 keyPhraseCount: number;
110 piiTypes: string[];
111 sentiment: string;
112 syntaxTags: string[];
113 targetedEntityCount: number;
114 toxicity: number;
115 piiLabels: string[];
116 };
117
118 // DetectDominantLanguage
119 expect(result.languageCode).toBe("en");
120 // DetectEntities — "Bob" is a PERSON, "Seattle" a LOCATION.
121 expect(result.entityTypes).toContain("PERSON");
122 expect(result.entityTypes).toContain("LOCATION");
123 // DetectKeyPhrases
124 expect(result.keyPhraseCount).toBeGreaterThan(0);
125 // DetectPiiEntities — the text contains a name and an email.
126 expect(result.piiTypes).toContain("NAME");
127 expect(result.piiTypes).toContain("EMAIL");
128 // DetectSentiment
129 expect(result.sentiment).toBe("POSITIVE");
130 // DetectSyntax — "cat" and "mat" are nouns.
131 expect(result.syntaxTags).toContain("NOUN");
132 // DetectTargetedSentiment — screen + battery entities.
133 expect(result.targetedEntityCount).toBeGreaterThan(0);
134 // DetectToxicContent — a compliment scores near zero.
135 expect(result.toxicity).toBeLessThan(0.5);
136 // ContainsPiiEntities
137 expect(result.piiLabels).toContain("NAME");
138 }),
139 { timeout: 120_000 },
140 );
141 });
142
143 describe("batch real-time analysis (BatchDetectDominantLanguage, BatchDetectEntities, BatchDetectKeyPhrases, BatchDetectSentiment, BatchDetectSyntax, BatchDetectTargetedSentiment)", () => {
144 test.provider(
145 "every batch detect binding returns index-aligned results",
146 (_stack) =>
147 Effect.gen(function* () {
148 const result = (yield* getJson("/batch-all")) as {
149 languageResults: number;
150 firstLanguage: string;
151 entityResults: number;
152 keyPhraseResults: number;
153 sentimentResults: number;
154 firstSentiment: string;
155 syntaxResults: number;
156 targetedResults: number;
157 };
158
159 expect(result.languageResults).toBe(2);
160 expect(result.firstLanguage).toBe("en");
161 expect(result.entityResults).toBe(1);
162 expect(result.keyPhraseResults).toBe(2);
163 expect(result.sentimentResults).toBe(2);
164 expect(result.firstSentiment).toBe("POSITIVE");
165 expect(result.syntaxResults).toBe(1);
166 expect(result.targetedResults).toBe(1);
167 }),
168 { timeout: 120_000 },
169 );
170 });
171
172 describe("ClassifyDocument", () => {
173 test.provider(
174 "drives the typed ResourceUnavailableException path for a missing endpoint",
175 (_stack) =>
176 Effect.gen(function* () {
177 const result = (yield* getJson("/classify-document")) as { tag: string };
178 expect(result.tag).toBe("ResourceUnavailableException");
179 }),
180 { timeout: 120_000 },
181 );
182 });
183
184 describe("List*Jobs (all nine families)", () => {
185 test.provider(
186 "every list binding returns a page",
187 (_stack) =>
188 Effect.gen(function* () {
189 const counts = (yield* getJson("/jobs/list-all")) as Record<string, number>;
190 for (const family of [
191 "documentClassification",
192 "dominantLanguage",
193 "entities",
194 "events",
195 "keyPhrases",
196 "piiEntities",
197 "sentiment",
198 "targetedSentiment",
199 "topics",
200 ]) {
201 expect(counts[family]).toBeGreaterThanOrEqual(0);
202 }
203 }),
204 { timeout: 120_000 },
205 );
206 });
207
208 describe("Describe*Job (all nine families)", () => {
209 test.provider(
210 "every describe binding surfaces the typed JobNotFoundException",
211 (_stack) =>
212 Effect.gen(function* () {
213 const tags = (yield* getJson("/jobs/describe-not-found-all")) as Record<string, string>;
214 expect(tags).toEqual({
215 documentClassification: "JobNotFoundException",
216 dominantLanguage: "JobNotFoundException",
217 entities: "JobNotFoundException",
218 // Events detection is closed to new customers; entitled accounts
219 // return JobNotFoundException instead.
220 events: expect.stringMatching(/^(JobNotFoundException|NotAuthorizedException)$/),
221 keyPhrases: "JobNotFoundException",
222 piiEntities: "JobNotFoundException",
223 sentiment: "JobNotFoundException",
224 targetedSentiment: "JobNotFoundException",
225 topics: "JobNotFoundException",
226 });
227 }),
228 { timeout: 120_000 },
229 );
230 });
231
232 describe("Stop*Job (all seven stoppable families)", () => {
233 test.provider(
234 "every stop binding surfaces the typed JobNotFoundException",
235 (_stack) =>
236 Effect.gen(function* () {
237 const tags = (yield* postJson("/jobs/stop-not-found-all")) as Record<string, string>;
238 expect(tags).toEqual({
239 dominantLanguage: "JobNotFoundException",
240 entities: "JobNotFoundException",
241 // Events detection is closed to new customers — the stop path
242 // hits the entitlement gate before the job lookup, surfacing the
243 // (patched) typed NotAuthorizedException.
244 events: expect.stringMatching(/^(JobNotFoundException|NotAuthorizedException)$/),
245 keyPhrases: "JobNotFoundException",
246 piiEntities: "JobNotFoundException",
247 sentiment: "JobNotFoundException",
248 targetedSentiment: "JobNotFoundException",
249 });
250 }),
251 { timeout: 120_000 },
252 );
253 });
254
255 describe("Start*Job (eight non-sentiment families)", () => {
256 test.provider(
257 "every start binding reaches Comprehend and surfaces the typed validation error",
258 (_stack) =>
259 Effect.gen(function* () {
260 const tags = (yield* postJson("/jobs/start-invalid-all")) as Record<string, string>;
261 // A missing IAM/PassRole grant would surface AccessDenied as a 500
262 // (untyped, through orDie) — a typed server-side validation tag
263 // proves role injection + the grant end-to-end.
264 for (const [family, tag] of Object.entries(tags)) {
265 expect(
266 [
267 "ValidationException",
268 "InvalidRequestException",
269 "ResourceNotFoundException",
270 "NotAuthorizedException",
271 ],
272 `family ${family} returned ${tag}`,
273 ).toContain(tag);
274 }
275 expect(Object.keys(tags).sort()).toEqual([
276 "documentClassification",
277 "dominantLanguage",
278 "entities",
279 "events",
280 "keyPhrases",
281 "piiEntities",
282 "targetedSentiment",
283 "topics",
284 ]);
285 }),
286 { timeout: 120_000 },
287 );
288 });
289
290 describe("StartSentimentDetectionJob + DescribeSentimentDetectionJob + StopSentimentDetectionJob", () => {
291 test.provider(
292 "runs a real async job lifecycle: start, describe, stop",
293 (_stack) =>
294 Effect.gen(function* () {
295 const result = (yield* postJson("/jobs/sentiment/lifecycle")) as {
296 jobId: string;
297 startStatus: string;
298 describedStatus: string;
299 stopStatus: string;
300 };
301 expect(result.jobId.length).toBeGreaterThan(0);
302 expect(["SUBMITTED", "IN_PROGRESS"]).toContain(result.startStatus);
303 expect(["SUBMITTED", "IN_PROGRESS"]).toContain(result.describedStatus);
304 expect(["STOP_REQUESTED", "STOPPED"]).toContain(result.stopStatus);
305 }),
306 { timeout: 120_000 },
307 );
308 });
309 },
310 );