@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 245 lines 9.5 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 AppSyncBindingsFunctionLive, { AppSyncBindingsFunction } from "./fixtures/bindings-handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "AppSyncBindings");
15
16 let baseUrl: string;
17
18 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
19 readonly status: number;
20 readonly body: string;
21 }> {}
22
23 // Fresh Lambda role + appsync:GraphQL permissions propagate eventually — the
24 // first calls can 500 with an auth error under the handler's `Effect.orDie`.
25 // Retry 5xx only; a genuine 4xx fails immediately.
26 const send = (request: HttpClientRequest.HttpClientRequest) =>
27 HttpClient.execute(request).pipe(
28 Effect.flatMap((response) =>
29 response.status >= 500
30 ? response.text.pipe(
31 Effect.flatMap((body) =>
32 Effect.fail(new TransientUpstream({ status: response.status, body })),
33 ),
34 )
35 : Effect.succeed(response),
36 ),
37 Effect.retry({
38 while: (e) => e._tag === "TransientUpstream",
39 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
40 }),
41 );
42
43 describe(
44 "AppSync Bindings",
45 { tags: ["provider:aws", "provider:aws:appsync", "provider:aws:lambda", "live"] },
46 () => {
47 beforeAll(
48 Effect.gen(function* () {
49 yield* Effect.logInfo("AppSync bindings setup: destroying previous resources");
50 yield* sharedStack.destroy();
51
52 yield* Effect.logInfo("AppSync bindings setup: deploying api -> resolvers -> Lambda");
53 const { functionUrl } = yield* sharedStack.deploy(
54 Effect.gen(function* () {
55 return yield* AppSyncBindingsFunction;
56 }).pipe(Effect.provide(AppSyncBindingsFunctionLive)),
57 );
58
59 expect(functionUrl).toBeTruthy();
60 baseUrl = functionUrl!.replace(/\/+$/, "");
61
62 // Readiness probe — fresh function URLs take seconds to serve 200s.
63 yield* HttpClient.get(`${baseUrl}/ping`).pipe(
64 Effect.flatMap((response) =>
65 response.status === 200
66 ? Effect.succeed(response)
67 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
68 ),
69 Effect.retry({
70 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]),
71 }),
72 );
73 }),
74 { timeout: 300_000 },
75 );
76 afterAll(sharedStack.destroy(), { timeout: 300_000 });
77
78 describe("GraphQL", () => {
79 test.provider(
80 "the Lambda executes a SigV4-signed query through the binding",
81 () =>
82 Effect.gen(function* () {
83 const result = yield* send(
84 HttpClientRequest.post(`${baseUrl}/graphql`).pipe(
85 HttpClientRequest.bodyJsonUnsafe({
86 query: "query($a: Int!, $b: Int!) { add(a: $a, b: $b) }",
87 variables: { a: 2, b: 3 },
88 }),
89 ),
90 ).pipe(
91 Effect.flatMap((response) => response.json),
92 Effect.map(
93 (json) => json as { data?: { add?: number }; errors?: Array<{ message: string }> },
94 ),
95 Effect.filterOrFail(
96 (json) => json.data?.add !== undefined,
97 (json) => new Error(`no data.add: ${JSON.stringify(json)}`),
98 ),
99 Effect.retry({
100 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]),
101 }),
102 );
103 expect(result.data?.add).toBe(5);
104 }),
105 { timeout: 180_000 },
106 );
107
108 test.provider(
109 "GraphQL field errors surface on result.errors (not as failures)",
110 () =>
111 Effect.gen(function* () {
112 const result = yield* send(
113 HttpClientRequest.post(`${baseUrl}/graphql`).pipe(
114 HttpClientRequest.bodyJsonUnsafe({ query: "query { nonexistentField }" }),
115 ),
116 ).pipe(
117 Effect.flatMap((response) => response.json),
118 Effect.map((json) => json as { errors?: Array<{ message: string }> }),
119 );
120 expect(result.errors?.length).toBeGreaterThan(0);
121 }),
122 { timeout: 120_000 },
123 );
124
125 test.provider(
126 "resolvers read environmentVariables via ctx.env",
127 () =>
128 Effect.gen(function* () {
129 const result = yield* send(
130 HttpClientRequest.post(`${baseUrl}/graphql`).pipe(
131 HttpClientRequest.bodyJsonUnsafe({ query: "query { greeting }" }),
132 ),
133 ).pipe(
134 Effect.flatMap((response) => response.json),
135 Effect.map((json) => json as { data?: { greeting?: string } }),
136 Effect.filterOrFail(
137 (json) => json.data?.greeting != null,
138 (json) => new Error(`no data.greeting: ${JSON.stringify(json)}`),
139 ),
140 Effect.retry({
141 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
142 }),
143 );
144 expect(result.data?.greeting).toBe("hello from ctx.env");
145 }),
146 { timeout: 120_000 },
147 );
148 });
149
150 describe("GetIntrospectionSchema", () => {
151 test.provider(
152 "the Lambda reads the API's live SDL through the binding",
153 () =>
154 Effect.gen(function* () {
155 const result = yield* send(HttpClientRequest.get(`${baseUrl}/schema`)).pipe(
156 Effect.flatMap((response) => response.json),
157 Effect.map((json) => json as { sdl?: string }),
158 Effect.filterOrFail(
159 (json) => json.sdl != null,
160 (json) => new Error(`no sdl: ${JSON.stringify(json)}`),
161 ),
162 Effect.retry({
163 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
164 }),
165 );
166 expect(result.sdl).toContain("add(a: Int!, b: Int!): Int!");
167 expect(result.sdl).toContain("type Query");
168 }),
169 { timeout: 120_000 },
170 );
171 });
172
173 describe("EvaluateCode", () => {
174 test.provider(
175 "the Lambda evaluates APPSYNC_JS resolver code through the binding",
176 () =>
177 Effect.gen(function* () {
178 const result = yield* send(HttpClientRequest.post(`${baseUrl}/evaluate`)).pipe(
179 Effect.flatMap((response) => response.json),
180 Effect.map(
181 (json) => json as { evaluationResult?: string; error?: { message?: string } },
182 ),
183 Effect.filterOrFail(
184 (json) => json.evaluationResult != null,
185 (json) => new Error(`no evaluationResult: ${JSON.stringify(json)}`),
186 ),
187 Effect.retry({
188 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
189 }),
190 );
191 expect(JSON.parse(result.evaluationResult!)).toMatchObject({ payload: 5 });
192 }),
193 { timeout: 120_000 },
194 );
195 });
196
197 describe("EvaluateMappingTemplate", () => {
198 test.provider(
199 "the Lambda renders a VTL mapping template through the binding",
200 () =>
201 Effect.gen(function* () {
202 const result = yield* send(HttpClientRequest.post(`${baseUrl}/evaluate-template`)).pipe(
203 Effect.flatMap((response) => response.json),
204 Effect.map(
205 (json) => json as { evaluationResult?: string; error?: { message?: string } },
206 ),
207 Effect.filterOrFail(
208 (json) => json.evaluationResult != null,
209 (json) => new Error(`no evaluationResult: ${JSON.stringify(json)}`),
210 ),
211 Effect.retry({
212 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
213 }),
214 );
215 expect(JSON.parse(result.evaluationResult!)).toMatchObject({ sum: 5 });
216 }),
217 { timeout: 120_000 },
218 );
219 });
220
221 describe("FlushApiCache", () => {
222 test.provider(
223 "the flush call is authorized and returns the typed NotFoundException without a cache",
224 () =>
225 Effect.gen(function* () {
226 // The fixture API deliberately has NO cache (cache instances bill
227 // hourly): a typed NotFoundException proves the binding executed
228 // with the granted appsync:FlushApiCache; an IAM denial surfaces
229 // a different tag. Repeat briefly through IAM propagation.
230 const outcome = yield* send(HttpClientRequest.post(`${baseUrl}/flush`)).pipe(
231 Effect.flatMap((response) => response.json),
232 Effect.map((json) => json as { flushed: boolean; reason?: string; message?: string }),
233 Effect.repeat({
234 schedule: Schedule.spaced("2 seconds"),
235 until: (json): boolean => json.reason === "NotFoundException",
236 times: 10,
237 }),
238 );
239 expect(outcome).toMatchObject({ flushed: false, reason: "NotFoundException" });
240 }),
241 { timeout: 90_000 },
242 );
243 });
244 },
245 );