@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 381 lines 14.4 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 { Region } from "@distilled.cloud/aws/Region";
7 import * as sts from "@distilled.cloud/aws/sts";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as HttpClient from "effect/http/HttpClient";
11 import * as HttpClientRequest from "effect/http/HttpClientRequest";
12 import * as Schedule from "effect/Schedule";
13 import TranscribeTestFunctionLive, { TranscribeTestFunction } from "./handler";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17 const sharedStack = Core.scratchStack(testOptions, "TranscribeBindings");
18
19 // Deterministic fixture names (account-scoped; this suite owns them in the
20 // testing account). Never derived from Date.now().
21 const FILTER_NAME = "alchemy-test-transcribe-filter";
22 const CATEGORY_NAME = "alchemy-test-transcribe-category";
23 const BOGUS = "alchemy-nonexistent-transcribe-probe";
24
25 // Transcribe reports missing jobs as BadRequestException ("The requested job
26 // couldn't be found.") rather than NotFoundException — a documented API
27 // quirk. Missing vocabularies/models surface NotFoundException. Both tags
28 // are typed; routes below assert the observed one (or either where AWS is
29 // inconsistent across families).
30 const NOT_FOUND_TAGS = ["NotFoundException", "BadRequestException"];
31
32 let baseUrl: string;
33
34 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
35 readonly status: number;
36 readonly body: string;
37 }> {}
38
39 // The shared Lambda fixture occasionally answers a transient 5xx (cold
40 // re-init that the handler's `Effect.orDie` surfaces as a 500). Retry only
41 // 5xx; a genuine 4xx surfaces immediately.
42 const send = (request: HttpClientRequest.HttpClientRequest) =>
43 HttpClient.execute(request).pipe(
44 Effect.flatMap((response) =>
45 response.status >= 500
46 ? response.text.pipe(
47 Effect.flatMap((body) =>
48 Effect.fail(new TransientUpstream({ status: response.status, body })),
49 ),
50 )
51 : Effect.succeed(response),
52 ),
53 Effect.retry({
54 while: (e) => e._tag === "TransientUpstream",
55 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
56 }),
57 );
58
59 // A `type` (not `interface`) so it carries an implicit index signature and is
60 // comparable to the `JsonObject` member of `response.json`'s union.
61 type RouteResult = {
62 ok: boolean;
63 error?: string;
64 count?: number;
65 status?: string;
66 state?: string;
67 name?: string;
68 rules?: number;
69 downloadUri?: string;
70 };
71
72 const getJson = (path: string) =>
73 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
74 Effect.flatMap((r) => r.json),
75 Effect.map((r) => r as RouteResult),
76 );
77
78 const postJson = (path: string, body: object) =>
79 send(
80 HttpClientRequest.post(`${baseUrl}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)),
81 ).pipe(
82 Effect.flatMap((r) => r.json),
83 Effect.map((r) => r as RouteResult),
84 );
85
86 // Freshly attached IAM policies (especially the iam:PassRole grants on the
87 // data-access role) can take a few seconds to propagate; retry any route
88 // that answers with the typed AccessDeniedException, bounded well under a
89 // minute.
90 const untilAuthorized = <E, R>(effect: Effect.Effect<RouteResult, E, R>) =>
91 effect.pipe(
92 Effect.repeat({
93 schedule: Schedule.spaced("3 seconds"),
94 until: (r): boolean => r.error !== "AccessDeniedException",
95 times: 10,
96 }),
97 );
98
99 describe.sequential(
100 "Transcribe Bindings",
101 {
102 tags: [
103 "provider:aws",
104 "provider:aws:iam",
105 "provider:aws:lambda",
106 "provider:aws:transcribe",
107 "live",
108 ],
109 },
110 () => {
111 beforeAll(
112 Effect.gen(function* () {
113 yield* Effect.logInfo("Transcribe test setup: destroying previous resources");
114 yield* sharedStack.destroy();
115
116 yield* Effect.logInfo("Transcribe test setup: deploying fixture");
117 const { functionUrl } = yield* sharedStack.deploy(
118 Effect.gen(function* () {
119 return yield* TranscribeTestFunction;
120 }).pipe(Effect.provide(TranscribeTestFunctionLive)),
121 );
122
123 expect(functionUrl).toBeTruthy();
124 baseUrl = functionUrl!.replace(/\/+$/, "");
125
126 // Readiness probe — fresh function URLs take seconds (sometimes over a
127 // minute) to serve 200s.
128 yield* HttpClient.get(`${baseUrl}/jobs`).pipe(
129 Effect.flatMap((response) =>
130 response.status === 200
131 ? Effect.succeed(response)
132 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
133 ),
134 Effect.retry({
135 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]),
136 }),
137 );
138 }),
139 { timeout: 300_000 },
140 );
141 afterAll(sharedStack.destroy(), { timeout: 300_000 });
142
143 describe("List bindings", () => {
144 test.provider(
145 "all nine list bindings answer from the runtime",
146 () =>
147 Effect.gen(function* () {
148 for (const path of [
149 "/jobs",
150 "/callJobs",
151 "/medicalJobs",
152 "/vocabularies",
153 "/medicalVocabularies",
154 "/filters",
155 "/categories",
156 "/languageModels",
157 ]) {
158 const body = yield* getJson(path);
159 expect(body.error, path).toBeUndefined();
160 expect(typeof body.count, path).toBe("number");
161 }
162
163 // AWS HealthScribe is not available in every region (e.g.
164 // us-west-2): unavailable regions answer with a typed
165 // BadRequestException ("Your account isn't authorized to call this
166 // operation"). Either a count or that typed tag proves the binding
167 // + grant end-to-end — an IAM gap would be AccessDeniedException.
168 const scribe = yield* getJson("/scribeJobs");
169 expect(
170 scribe.error === undefined || scribe.error === "BadRequestException",
171 `/scribeJobs: ${scribe.error}`,
172 ).toBe(true);
173 }),
174 { timeout: 120_000 },
175 );
176 });
177
178 describe("Get bindings (typed not-found tags)", () => {
179 test.provider(
180 "reading nonexistent jobs/vocabularies/models surfaces typed tags",
181 () =>
182 Effect.gen(function* () {
183 // A typed not-found tag (never AccessDenied, never a 500) proves
184 // the grant and the typed error mapping end-to-end per binding.
185 const job = yield* getJson(`/job?name=${BOGUS}`);
186 expect(job.error).toBe("BadRequestException");
187
188 for (const path of [
189 "/callJob",
190 "/medicalJob",
191 "/scribeJob",
192 "/vocabulary",
193 "/medicalVocabulary",
194 "/languageModel",
195 ]) {
196 const body = yield* getJson(`${path}?name=${BOGUS}`);
197 expect(NOT_FOUND_TAGS, path).toContain(body.error);
198 }
199 }),
200 { timeout: 120_000 },
201 );
202 });
203
204 describe("Delete bindings", () => {
205 test.provider(
206 "deleting nonexistent resources is idempotent or surfaces typed tags",
207 () =>
208 Effect.gen(function* () {
209 // Transcribe's delete APIs are inconsistent across families: some
210 // succeed idempotently for a missing resource, others return a
211 // typed not-found tag. Either proves the grant + wire-up; an IAM
212 // gap would surface AccessDeniedException instead.
213 for (const path of [
214 "/job/delete",
215 "/callJob/delete",
216 "/medicalJob/delete",
217 "/scribeJob/delete",
218 "/vocabulary/delete",
219 "/medicalVocabulary/delete",
220 "/languageModel/delete",
221 ]) {
222 const body = yield* postJson(path, { name: BOGUS });
223 expect(
224 body.error === undefined || NOT_FOUND_TAGS.includes(body.error),
225 `${path}: ${body.error}`,
226 ).toBe(true);
227 }
228 }),
229 { timeout: 120_000 },
230 );
231 });
232
233 describe("Start/Create bindings (typed validation, nothing created)", () => {
234 test.provider(
235 "invalid S3 URIs surface typed BadRequestException per family",
236 () =>
237 Effect.gen(function* () {
238 // The invalid `Media`/`VocabularyFileUri` fails Transcribe's
239 // server-side pattern validation: the request is authorized (so an
240 // IAM gap would surface AccessDenied instead) but nothing is
241 // created and nothing is billed. The role-bound bindings
242 // (startCall, startScribe, languageModel/create) additionally
243 // prove the iam:PassRole wiring.
244 for (const path of [
245 "/startJob",
246 "/startCall",
247 "/startMedical",
248 "/startScribe",
249 "/vocabulary/create",
250 "/medicalVocabulary/create",
251 "/languageModel/create",
252 ]) {
253 const body = yield* untilAuthorized(postJson(path, { name: BOGUS }));
254 expect(body.ok, path).toBe(false);
255 expect(body.error, path).toBe("BadRequestException");
256 }
257
258 // Updating a nonexistent vocabulary surfaces its typed tag.
259 for (const path of ["/vocabulary/update", "/medicalVocabulary/update"]) {
260 const body = yield* postJson(path, { name: BOGUS });
261 expect(NOT_FOUND_TAGS, path).toContain(body.error);
262 }
263 }),
264 { timeout: 180_000 },
265 );
266 });
267
268 describe("VocabularyFilter lifecycle + tagging", () => {
269 test.provider(
270 "create, get, update, tag, untag, and delete a vocabulary filter",
271 () =>
272 Effect.gen(function* () {
273 // Cleanup leftovers from a crashed prior run (typed tag ignored).
274 yield* postJson("/filter/delete", { name: FILTER_NAME });
275
276 const created = yield* postJson("/filter/create", { name: FILTER_NAME });
277 expect(created.error).toBeUndefined();
278 expect(created.name).toBe(FILTER_NAME);
279
280 const got = yield* getJson(`/filter?name=${FILTER_NAME}`);
281 expect(got.error).toBeUndefined();
282 expect(got.name).toBe(FILTER_NAME);
283 expect(got.downloadUri).toBeTruthy();
284
285 const updated = yield* postJson("/filter/update", { name: FILTER_NAME });
286 expect(updated.error).toBeUndefined();
287
288 // Tag ops address Transcribe resources by ARN.
289 const region = yield* yield* Region;
290 const identity = yield* sts.getCallerIdentity({});
291 const arn = `arn:aws:transcribe:${region}:${identity.Account}:vocabulary-filter/${FILTER_NAME}`;
292
293 const tagged = yield* postJson("/tag", { arn });
294 expect(tagged.error).toBeUndefined();
295
296 const tags = yield* getJson(`/tags?arn=${encodeURIComponent(arn)}`);
297 expect(tags.error).toBeUndefined();
298 expect(tags.count).toBeGreaterThanOrEqual(1);
299
300 const untagged = yield* postJson("/untag", { arn });
301 expect(untagged.error).toBeUndefined();
302
303 const deleted = yield* postJson("/filter/delete", { name: FILTER_NAME });
304 expect(deleted.error).toBeUndefined();
305
306 // Wait until it is really gone (typed tag on the get).
307 const gone = yield* getJson(`/filter?name=${FILTER_NAME}`).pipe(
308 Effect.repeat({
309 schedule: Schedule.spaced("2 seconds"),
310 until: (r): boolean => r.error !== undefined,
311 times: 10,
312 }),
313 );
314 expect(NOT_FOUND_TAGS).toContain(gone.error);
315 }),
316 { timeout: 120_000 },
317 );
318 });
319
320 describe("CallAnalyticsCategory lifecycle", () => {
321 test.provider(
322 "create, get, update, and delete a call analytics category",
323 () =>
324 Effect.gen(function* () {
325 // Cleanup leftovers from a crashed prior run (typed tag ignored).
326 yield* postJson("/category/delete", { name: CATEGORY_NAME });
327
328 const created = yield* postJson("/category/create", { name: CATEGORY_NAME });
329 expect(created.error).toBeUndefined();
330 expect(created.name).toBe(CATEGORY_NAME);
331
332 const got = yield* getJson(`/category?name=${CATEGORY_NAME}`);
333 expect(got.error).toBeUndefined();
334 expect(got.rules).toBe(1);
335
336 const updated = yield* postJson("/category/update", { name: CATEGORY_NAME });
337 expect(updated.error).toBeUndefined();
338
339 const deleted = yield* postJson("/category/delete", { name: CATEGORY_NAME });
340 expect(deleted.error).toBeUndefined();
341
342 const gone = yield* getJson(`/category?name=${CATEGORY_NAME}`).pipe(
343 Effect.repeat({
344 schedule: Schedule.spaced("2 seconds"),
345 until: (r): boolean => r.error !== undefined,
346 times: 10,
347 }),
348 );
349 expect(NOT_FOUND_TAGS).toContain(gone.error);
350 }),
351 { timeout: 120_000 },
352 );
353 });
354
355 describe("consumeTranscriptionJobEvents", { tags: ["provider:aws:eventbridge"] }, () => {
356 test.provider(
357 "created the EventBridge rule for Transcribe job state changes",
358 () =>
359 Effect.gen(function* () {
360 // The rule's physical name embeds the event-source id
361 // (`TranscribeJobEvents`); find it on the default bus with bounded
362 // manual pagination.
363 let rule: eventbridge.Rule | undefined;
364 let nextToken: string | undefined;
365 for (let page = 0; page < 10 && !rule; page++) {
366 const result = yield* eventbridge.listRules({ NextToken: nextToken });
367 rule = (result.Rules ?? []).find((candidate) =>
368 candidate.Name?.includes("TranscribeJobEvents"),
369 );
370 nextToken = result.NextToken;
371 if (!nextToken) break;
372 }
373 expect(rule).toBeDefined();
374 expect(rule?.EventPattern).toContain("aws.transcribe");
375 expect(rule?.EventPattern).toContain("Transcribe Job State Change");
376 }),
377 { timeout: 60_000 },
378 );
379 });
380 },
381 );