@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 361 lines 13 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 * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import DmsTestFunctionLive, { DmsTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "DmsBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22
23 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
24 readonly status: number;
25 readonly body: string;
26 }> {}
27
28 // The shared Lambda fixture occasionally answers a transient 5xx under load
29 // (cold re-init, IAM propagation on the freshly attached policy that the
30 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
31 // 4xx/assertion failure surfaces immediately.
32 const send = (request: HttpClientRequest.HttpClientRequest) =>
33 HttpClient.execute(request).pipe(
34 Effect.flatMap((response) =>
35 response.status >= 500
36 ? response.text.pipe(
37 Effect.flatMap((body) =>
38 Effect.fail(new TransientUpstream({ status: response.status, body })),
39 ),
40 )
41 : Effect.succeed(response),
42 ),
43 Effect.retry({
44 while: (e) => e._tag === "TransientUpstream",
45 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
46 }),
47 );
48
49 describe.sequential(
50 "DMS Bindings",
51 { tags: ["provider:aws", "provider:aws:dms", "provider:aws:lambda", "live"] },
52 () => {
53 beforeAll(
54 Effect.gen(function* () {
55 yield* Effect.logInfo("DMS test setup: destroying previous resources");
56 yield* sharedStack.destroy();
57
58 yield* Effect.logInfo("DMS test setup: deploying fixture");
59 const { functionUrl } = yield* sharedStack.deploy(
60 Effect.gen(function* () {
61 return yield* DmsTestFunction;
62 }).pipe(Effect.provide(DmsTestFunctionLive)),
63 );
64
65 expect(functionUrl).toBeTruthy();
66 baseUrl = functionUrl!.replace(/\/+$/, "");
67
68 const readinessUrl = `${baseUrl}/bindings`;
69 yield* Effect.logInfo(`DMS test setup: probing readiness at ${readinessUrl}`);
70 yield* HttpClient.get(readinessUrl).pipe(
71 Effect.flatMap((response) =>
72 response.status === 200
73 ? Effect.succeed(response)
74 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
75 ),
76 Effect.tapError((error) =>
77 Effect.logWarning(`DMS test setup: fixture not ready yet (${String(error)})`),
78 ),
79 Effect.retry({ schedule: readinessPolicy }),
80 );
81 }),
82 { timeout: 240_000 },
83 );
84
85 afterAll(sharedStack.destroy(), { timeout: 120_000 });
86
87 describe("binding registration", () => {
88 test.provider("all capabilities initialize in the runtime", (_stack) =>
89 Effect.gen(function* () {
90 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
91 Effect.flatMap((r) => r.json),
92 );
93 expect((response as any).bound).toHaveLength(14);
94 }),
95 );
96 });
97
98 describe("DescribeSchemas", () => {
99 test.provider(
100 "answers with a typed fault for a never-refreshed endpoint",
101 (_stack) =>
102 Effect.gen(function* () {
103 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/schemas`)).pipe(
104 Effect.flatMap((r) => r.json),
105 )) as { schemas: unknown[]; fault: string | null };
106 // The fixture endpoint has never had a schema refresh (that needs
107 // a replication instance): DMS answers with a typed fault, or an
108 // empty schema list. An IAM grant gap would 500 the route instead.
109 if (response.fault !== null) {
110 expect(["ResourceNotFoundFault", "InvalidResourceStateFault"]).toContain(
111 response.fault,
112 );
113 } else {
114 expect(response.schemas).toEqual([]);
115 }
116 }),
117 { timeout: 60_000 },
118 );
119 });
120
121 describe("DescribeRefreshSchemasStatus", () => {
122 test.provider(
123 "answers with a typed fault for a never-refreshed endpoint",
124 (_stack) =>
125 Effect.gen(function* () {
126 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/refresh-status`)).pipe(
127 Effect.flatMap((r) => r.json),
128 )) as { status: string | null; fault: string | null };
129 if (response.fault !== null) {
130 expect(["ResourceNotFoundFault", "InvalidResourceStateFault"]).toContain(
131 response.fault,
132 );
133 } else {
134 expect(response.status).toBeNull();
135 }
136 }),
137 { timeout: 60_000 },
138 );
139 });
140
141 describe("DescribeConnections", () => {
142 test.provider(
143 "reports zero connection tests for the fresh endpoint",
144 (_stack) =>
145 Effect.gen(function* () {
146 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/connections`)).pipe(
147 Effect.flatMap((r) => r.json),
148 )) as { count: number };
149 expect(response.count).toBe(0);
150 }),
151 { timeout: 60_000 },
152 );
153 });
154
155 describe("DescribeEvents", () => {
156 test.provider(
157 "reads recent replication-instance events (empty on a quiet account)",
158 (_stack) =>
159 Effect.gen(function* () {
160 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/events`)).pipe(
161 Effect.flatMap((r) => r.json),
162 )) as { count: number };
163 expect(response.count).toBeGreaterThanOrEqual(0);
164 }),
165 { timeout: 60_000 },
166 );
167 });
168
169 describe("DescribeEndpointSettings", () => {
170 test.provider(
171 "enumerates the settings the mysql engine accepts",
172 (_stack) =>
173 Effect.gen(function* () {
174 const response = (yield* send(
175 HttpClientRequest.get(`${baseUrl}/endpoint-settings`),
176 ).pipe(Effect.flatMap((r) => r.json))) as { count: number };
177 expect(response.count).toBeGreaterThan(0);
178 }),
179 { timeout: 60_000 },
180 );
181 });
182
183 describe("DescribeOrderableReplicationInstances", () => {
184 test.provider(
185 "enumerates orderable replication instance classes",
186 (_stack) =>
187 Effect.gen(function* () {
188 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/orderable`)).pipe(
189 Effect.flatMap((r) => r.json),
190 )) as { count: number };
191 expect(response.count).toBeGreaterThan(0);
192 }),
193 { timeout: 60_000 },
194 );
195 });
196
197 describe("DescribeReplicationTasks", () => {
198 test.provider(
199 "answers a no-match filter with zero tasks or the typed fault",
200 (_stack) =>
201 Effect.gen(function* () {
202 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/tasks`)).pipe(
203 Effect.flatMap((r) => r.json),
204 )) as { count: number; fault: string | null };
205 expect(response.count).toBe(0);
206 if (response.fault !== null) {
207 expect(response.fault).toBe("ResourceNotFoundFault");
208 }
209 }),
210 { timeout: 60_000 },
211 );
212 });
213
214 describe("StartReplicationTask", () => {
215 test.provider(
216 "surfaces the typed not-found fault for a nonexistent task",
217 (_stack) =>
218 Effect.gen(function* () {
219 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/task/start`)).pipe(
220 Effect.flatMap((r) => r.json),
221 )) as { fault: string };
222 expect([
223 "ResourceNotFoundFault",
224 "InvalidResourceStateFault",
225 "AccessDeniedFault",
226 ]).toContain(response.fault);
227 }),
228 { timeout: 60_000 },
229 );
230 });
231
232 describe("StopReplicationTask", () => {
233 test.provider(
234 "surfaces the typed not-found fault for a nonexistent task",
235 (_stack) =>
236 Effect.gen(function* () {
237 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/task/stop`)).pipe(
238 Effect.flatMap((r) => r.json),
239 )) as { fault: string };
240 expect(["ResourceNotFoundFault", "InvalidResourceStateFault"]).toContain(
241 response.fault,
242 );
243 }),
244 { timeout: 60_000 },
245 );
246 });
247
248 describe("DescribeTableStatistics", () => {
249 test.provider(
250 "surfaces the typed not-found fault for a nonexistent task",
251 (_stack) =>
252 Effect.gen(function* () {
253 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/task/tables`)).pipe(
254 Effect.flatMap((r) => r.json),
255 )) as { fault: string };
256 expect([
257 "ResourceNotFoundFault",
258 "InvalidResourceStateFault",
259 "AccessDeniedFault",
260 ]).toContain(response.fault);
261 }),
262 { timeout: 60_000 },
263 );
264 });
265
266 describe("ReloadTables", () => {
267 test.provider(
268 "surfaces the typed not-found fault for a nonexistent task",
269 (_stack) =>
270 Effect.gen(function* () {
271 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/task/reload`)).pipe(
272 Effect.flatMap((r) => r.json),
273 )) as { fault: string };
274 expect(["ResourceNotFoundFault", "InvalidResourceStateFault"]).toContain(
275 response.fault,
276 );
277 }),
278 { timeout: 60_000 },
279 );
280 });
281
282 describe("DescribeReplications", () => {
283 test.provider(
284 "answers a no-match filter with zero replications or the typed fault",
285 (_stack) =>
286 Effect.gen(function* () {
287 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/replications`)).pipe(
288 Effect.flatMap((r) => r.json),
289 )) as { count: number; fault: string | null };
290 expect(response.count).toBe(0);
291 if (response.fault !== null) {
292 expect(response.fault).toBe("ResourceNotFoundFault");
293 }
294 }),
295 { timeout: 60_000 },
296 );
297 });
298
299 describe("StartReplication", () => {
300 test.provider(
301 "surfaces the typed not-found fault for a nonexistent config",
302 (_stack) =>
303 Effect.gen(function* () {
304 const response = (yield* send(
305 HttpClientRequest.post(`${baseUrl}/replication/start`),
306 ).pipe(Effect.flatMap((r) => r.json))) as { fault: string };
307 expect([
308 "ResourceNotFoundFault",
309 "InvalidResourceStateFault",
310 "AccessDeniedFault",
311 ]).toContain(response.fault);
312 }),
313 { timeout: 60_000 },
314 );
315 });
316
317 describe("StopReplication", () => {
318 test.provider(
319 "surfaces the typed not-found fault for a nonexistent config",
320 (_stack) =>
321 Effect.gen(function* () {
322 const response = (yield* send(
323 HttpClientRequest.post(`${baseUrl}/replication/stop`),
324 ).pipe(Effect.flatMap((r) => r.json))) as { fault: string };
325 expect([
326 "ResourceNotFoundFault",
327 "InvalidResourceStateFault",
328 "AccessDeniedFault",
329 ]).toContain(response.fault);
330 }),
331 { timeout: 60_000 },
332 );
333 });
334
335 describe("consumeReplicationEvents", { tags: ["provider:aws:eventbridge"] }, () => {
336 test.provider(
337 "created the EventBridge rule for DMS replication task state changes",
338 () =>
339 Effect.gen(function* () {
340 // The rule's physical name embeds the fixture's logical id
341 // (`DmsTestFunction-DmsReplicationEvents`); find it on the default
342 // bus with bounded manual pagination.
343 let rule: eventbridge.Rule | undefined;
344 let nextToken: string | undefined;
345 for (let page = 0; page < 10 && !rule; page++) {
346 const result = yield* eventbridge.listRules({ NextToken: nextToken });
347 rule = (result.Rules ?? []).find((candidate) =>
348 candidate.Name?.includes("DmsReplicationEvents"),
349 );
350 nextToken = result.NextToken;
351 if (!nextToken) break;
352 }
353 expect(rule).toBeDefined();
354 expect(rule?.EventPattern).toContain("aws.dms");
355 expect(rule?.EventPattern).toContain("DMS Replication Task State Change");
356 }),
357 { timeout: 60_000 },
358 );
359 });
360 },
361 );