@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 190 lines 6.77 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 LexTestFunctionLive, { LexTestFunction } from "./handler.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "LexV2Bindings");
15
16 let baseUrl: string;
17
18 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
19 readonly status: number;
20 readonly body: string;
21 }> {}
22
23 /**
24 * POST /recognize with a bounded 5xx retry — the Lambda role's fresh
25 * `lex:RecognizeText` policy propagates eventually, surfacing as 502
26 * AccessDeniedException for the first seconds.
27 */
28 const call = (request: HttpClientRequest.HttpClientRequest) =>
29 Effect.gen(function* () {
30 const response = yield* HttpClient.execute(request);
31 const body = yield* response.text;
32 if (response.status >= 500) {
33 return yield* Effect.fail(new TransientUpstream({ status: response.status, body }));
34 }
35 if (response.status !== 200) {
36 // Terminal — fail fast with the typed tag in the message.
37 return yield* Effect.die(new Error(`status ${response.status}: ${body}`));
38 }
39 return JSON.parse(body) as Record<string, any>;
40 }).pipe(
41 Effect.retry({
42 while: (e) => e._tag === "TransientUpstream",
43 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
44 }),
45 );
46
47 const recognize = (text: string, sessionId: string) =>
48 call(
49 HttpClientRequest.post(`${baseUrl}/recognize`).pipe(
50 HttpClientRequest.bodyJsonUnsafe({ text, sessionId }),
51 ),
52 );
53
54 describe(
55 "LexV2 Bindings",
56 {
57 tags: ["provider:aws", "provider:aws:iam", "provider:aws:lambda", "provider:aws:lexv2", "live"],
58 },
59 () => {
60 beforeAll(
61 Effect.gen(function* () {
62 yield* Effect.logInfo("LexV2 e2e setup: destroying previous resources");
63 yield* sharedStack.destroy();
64
65 yield* Effect.logInfo(
66 "LexV2 e2e setup: deploying role -> bot -> locale -> intent -> version (build) -> alias -> Lambda",
67 );
68 const { functionUrl } = yield* sharedStack.deploy(
69 Effect.gen(function* () {
70 return yield* LexTestFunction;
71 }).pipe(Effect.provide(LexTestFunctionLive)),
72 );
73 expect(functionUrl).toBeTruthy();
74 baseUrl = functionUrl!.replace(/\/+$/, "");
75
76 // Readiness probe — fresh function URLs take seconds to serve 200s.
77 yield* HttpClient.get(`${baseUrl}/health`).pipe(
78 Effect.flatMap((response) =>
79 response.status === 200
80 ? Effect.succeed(response)
81 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
82 ),
83 Effect.retry({
84 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]),
85 }),
86 );
87 }),
88 // The bounded locale build can consume ~90s by itself. Lambda creation
89 // and IAM/function-URL propagation follow that build, so leave headroom
90 // for those bounded phases when the full AWS sweep saturates the account.
91 { timeout: 210_000 },
92 );
93 afterAll(sharedStack.destroy(), { timeout: 180_000 });
94
95 describe("RecognizeText", () => {
96 test.provider(
97 "recognizes a greeting utterance as the Greet intent",
98 () =>
99 Effect.gen(function* () {
100 const body = yield* recognize("hello", "alchemy-e2e-greet");
101 expect(body.intent).toBe("Greet");
102 }),
103 { timeout: 120_000 },
104 );
105
106 test.provider(
107 "routes an unmatched utterance to the fallback intent",
108 () =>
109 Effect.gen(function* () {
110 const body = yield* recognize("purple monkey dishwasher", "alchemy-e2e-fallback");
111 expect(body.intent).toBe("FallbackIntent");
112 }),
113 { timeout: 120_000 },
114 );
115 });
116
117 describe("CodeHookEventSource", () => {
118 test.provider(
119 "fulfillment code hook closes the intent with the handler's message",
120 () =>
121 Effect.gen(function* () {
122 const body = yield* recognize("order a pizza", "alchemy-e2e-order");
123 expect(body.intent).toBe("OrderPizza");
124 expect(body.state).toBe("Fulfilled");
125 expect(body.messages).toContain("Order placed for alchemy-e2e-order!");
126 }),
127 { timeout: 120_000 },
128 );
129 });
130
131 describe("Sessions", () => {
132 test.provider(
133 "put, get, and delete a session",
134 () =>
135 Effect.gen(function* () {
136 const sessionId = "alchemy-e2e-session";
137
138 // PutSession seeds attributes.
139 const put = yield* call(
140 HttpClientRequest.post(`${baseUrl}/session`).pipe(
141 HttpClientRequest.bodyJsonUnsafe({
142 sessionId,
143 attributes: { favorite: "pepperoni" },
144 }),
145 ),
146 );
147 expect(put.sessionId).toBe(sessionId);
148
149 // GetSession reads them back.
150 const got = yield* call(
151 HttpClientRequest.get(`${baseUrl}/session?sessionId=${sessionId}`),
152 );
153 expect(got.attributes).toMatchObject({ favorite: "pepperoni" });
154
155 // DeleteSession ends the conversation ...
156 const deleted = yield* call(
157 HttpClientRequest.delete(`${baseUrl}/session?sessionId=${sessionId}`),
158 );
159 expect(deleted.sessionId).toBe(sessionId);
160
161 // ... so a fresh GetSession reports the typed not-found tag.
162 const response = yield* HttpClient.get(`${baseUrl}/session?sessionId=${sessionId}`);
163 const body = (yield* response.json) as { error?: string };
164 expect(body.error).toBe("ResourceNotFoundException");
165 }),
166 { timeout: 120_000 },
167 );
168 });
169
170 describe("RecognizeUtterance", () => {
171 test.provider(
172 "recognizes a text utterance and returns response metadata",
173 () =>
174 Effect.gen(function* () {
175 const body = yield* call(
176 HttpClientRequest.post(`${baseUrl}/utterance`).pipe(
177 HttpClientRequest.bodyJsonUnsafe({
178 text: "hello",
179 sessionId: "alchemy-e2e-utterance",
180 }),
181 ),
182 );
183 expect(body.sessionId).toBe("alchemy-e2e-utterance");
184 expect(body.contentType).toContain("text/plain");
185 }),
186 { timeout: 120_000 },
187 );
188 });
189 },
190 );