@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 196 lines 7.04 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 { HELLO_PNG_TEXT, TRANSLATE_TEXT } from "./constants.ts";
11 import AICapabilitiesTestFunctionLive, { AICapabilitiesTestFunction } from "./handler.ts";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "AICapabilitiesBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy under parallel-suite load. Budget ~150s of
19 // readiness polling.
20 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
21
22 let baseUrl: string;
23
24 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
25 readonly status: number;
26 readonly body: string;
27 }> {}
28
29 // The shared Lambda fixture occasionally answers a transient 5xx under
30 // parallel load (cold re-init, IAM propagation, upstream throttling surfaced
31 // as a defect). Retry 5xx only; a genuine 4xx/assertion failure surfaces
32 // immediately.
33 const send = (request: HttpClientRequest.HttpClientRequest) =>
34 HttpClient.execute(request).pipe(
35 Effect.flatMap((response) =>
36 response.status >= 500
37 ? response.text.pipe(
38 Effect.flatMap((body) =>
39 Effect.fail(new TransientUpstream({ status: response.status, body })),
40 ),
41 )
42 : Effect.succeed(response),
43 ),
44 Effect.retry({
45 while: (e) => e._tag === "TransientUpstream",
46 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]),
47 }),
48 );
49
50 describe(
51 "AI Capabilities Bindings",
52 {
53 tags: [
54 "provider:aws",
55 "provider:aws:comprehend",
56 "provider:aws:lambda",
57 "provider:aws:polly",
58 "provider:aws:rekognition",
59 "provider:aws:textract",
60 "provider:aws:translate",
61 "live",
62 ],
63 },
64 () => {
65 beforeAll(
66 Effect.gen(function* () {
67 yield* Effect.logInfo("AICapabilities test setup: destroying previous resources");
68 yield* sharedStack.destroy();
69
70 yield* Effect.logInfo("AICapabilities test setup: deploying fixture");
71 const { functionUrl } = yield* sharedStack.deploy(
72 Effect.gen(function* () {
73 return yield* AICapabilitiesTestFunction;
74 }).pipe(Effect.provide(AICapabilitiesTestFunctionLive)),
75 );
76
77 expect(functionUrl).toBeTruthy();
78 baseUrl = functionUrl!.replace(/\/+$/, "");
79 const readinessUrl = `${baseUrl}/ping`;
80
81 yield* Effect.logInfo(`AICapabilities test setup: probing readiness at ${readinessUrl}`);
82 yield* HttpClient.get(readinessUrl).pipe(
83 Effect.flatMap((response) =>
84 response.status === 200
85 ? Effect.succeed(response)
86 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
87 ),
88 Effect.tapError((error) =>
89 Effect.logWarning(
90 `AICapabilities test setup: fixture not ready yet (${String(error)})`,
91 ),
92 ),
93 Effect.retry({ schedule: readinessPolicy }),
94 );
95 }),
96 { timeout: 240_000 },
97 );
98
99 afterAll(sharedStack.destroy(), { timeout: 120_000 });
100
101 describe("Rekognition.DetectLabels", () => {
102 test.provider(
103 "detects labels in checked-in image bytes",
104 (_stack) =>
105 Effect.gen(function* () {
106 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/detect-labels`)).pipe(
107 Effect.flatMap((r) => r.json),
108 )) as { labels: string[]; labelModelVersion?: string };
109
110 expect(Array.isArray(response.labels)).toBe(true);
111 expect(response.labels.length).toBeGreaterThan(0);
112 // The fixture image is rendered text — Rekognition consistently
113 // labels it as textual content.
114 expect(response.labels).toContain("Text");
115 }),
116 { timeout: 120_000 },
117 );
118 });
119
120 describe("Textract.DetectDocumentText", () => {
121 test.provider(
122 "detects the rendered text in checked-in document bytes",
123 (_stack) =>
124 Effect.gen(function* () {
125 const response = (yield* send(
126 HttpClientRequest.get(`${baseUrl}/detect-document-text`),
127 ).pipe(Effect.flatMap((r) => r.json))) as { lines: string[]; pages?: number };
128
129 expect(response.pages).toBe(1);
130 expect(Array.isArray(response.lines)).toBe(true);
131 expect(response.lines).toContain(HELLO_PNG_TEXT);
132 }),
133 { timeout: 120_000 },
134 );
135 });
136
137 describe("Polly.SynthesizeSpeech", () => {
138 test.provider(
139 "synthesizes text to non-empty mp3 audio bytes",
140 (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* send(
143 HttpClientRequest.get(`${baseUrl}/synthesize-speech`),
144 ).pipe(Effect.flatMap((r) => r.json))) as { contentType?: string; byteLength: number };
145
146 expect(response.contentType).toContain("audio/mpeg");
147 expect(response.byteLength).toBeGreaterThan(1000);
148 }),
149 { timeout: 120_000 },
150 );
151 });
152
153 describe("Translate.TranslateText", () => {
154 test.provider(
155 "translates English to Spanish",
156 (_stack) =>
157 Effect.gen(function* () {
158 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/translate-text`)).pipe(
159 Effect.flatMap((r) => r.json),
160 )) as {
161 translatedText: string;
162 sourceLanguageCode: string;
163 targetLanguageCode: string;
164 };
165
166 expect(response.sourceLanguageCode).toBe("en");
167 expect(response.targetLanguageCode).toBe("es");
168 expect(response.translatedText.trim().length).toBeGreaterThan(0);
169 expect(response.translatedText).not.toBe(TRANSLATE_TEXT);
170 }),
171 { timeout: 120_000 },
172 );
173 });
174
175 describe("Comprehend.DetectSentiment", () => {
176 test.provider(
177 "detects positive sentiment",
178 (_stack) =>
179 Effect.gen(function* () {
180 const response = (yield* send(
181 HttpClientRequest.get(`${baseUrl}/detect-sentiment`),
182 ).pipe(Effect.flatMap((r) => r.json))) as {
183 sentiment?: string;
184 sentimentScore?: { Positive?: number };
185 };
186
187 expect(["POSITIVE", "NEGATIVE", "NEUTRAL", "MIXED"]).toContain(response.sentiment);
188 // SENTIMENT_TEXT is unambiguously positive.
189 expect(response.sentiment).toBe("POSITIVE");
190 expect(response.sentimentScore?.Positive ?? 0).toBeGreaterThan(0.5);
191 }),
192 { timeout: 120_000 },
193 );
194 });
195 },
196 );