@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 263 lines 9.62 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as appflow from "@distilled.cloud/aws/appflow";
5 import * as eventbridge from "@distilled.cloud/aws/eventbridge";
6 import * as s3 from "@distilled.cloud/aws/s3";
7 import * as sqs from "@distilled.cloud/aws/sqs";
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 {
14 AppFlowApiFunction,
15 AppFlowApiFunctionLive,
16 FLOW_BUCKET,
17 FLOW_NAME,
18 FlowBucket,
19 FlowFixture,
20 } from "./handler.ts";
21
22 const { test } = Test.make({ providers: AWS.providers() });
23
24 // The full fixture: bucket + flow + events queue + the API function with all
25 // four Flow bindings and the flowEvents consume loop.
26 const program = Effect.gen(function* () {
27 const { flow, eventsQueue } = yield* FlowFixture;
28 const fn = yield* AppFlowApiFunction;
29 return { fn, flow, eventsQueue };
30 }).pipe(Effect.provide(AppFlowApiFunctionLive));
31
32 // Lambda Function URLs cold-start and a fresh role's IAM grants are
33 // eventually consistent; retrying on any non-200 lets the FIRST request wait
34 // through that window.
35 const readinessSchedule = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]);
36
37 // Function URLs come back with a trailing slash; strip it before joining so
38 // pathnames match the fixture's routes.
39 const urlOf = (baseUrl: string, path: string) => `${baseUrl.replace(/\/+$/, "")}${path}`;
40
41 const getJson = (baseUrl: string, path: string) =>
42 HttpClient.get(urlOf(baseUrl, path)).pipe(
43 Effect.flatMap((response) =>
44 response.status === 200
45 ? response.json
46 : Effect.fail(new Error(`Request failed: ${response.status}`)),
47 ),
48 Effect.retry({ schedule: readinessSchedule }),
49 );
50
51 const postJson = (baseUrl: string, path: string, body: unknown) =>
52 HttpClient.execute(
53 HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(urlOf(baseUrl, path)), body),
54 ).pipe(
55 Effect.flatMap((response) =>
56 response.status === 200
57 ? response.json
58 : Effect.fail(new Error(`Request failed: ${response.status}`)),
59 ),
60 Effect.retry({ schedule: readinessSchedule }),
61 );
62
63 class ExecutionNotFinished extends Data.TaggedError("ExecutionNotFinished")<{
64 lastStatus: string | undefined;
65 }> {
66 override get message() {
67 return this.lastStatus === undefined
68 ? "execution record never appeared"
69 : `execution still '${this.lastStatus}' when the poll budget exhausted`;
70 }
71 }
72
73 /**
74 * Poll the fixture's /executions route until the given run finishes.
75 *
76 * Budget: every observed pass completes the execution well inside 50s of
77 * polling (whole test 104–117s); ~90s is the speed-doctrine ceiling for
78 * async platform waits. If this exhausts again, the error's `lastStatus`
79 * says whether AppFlow was still queueing (`InProgress`) — the signal to
80 * skipIf-gate the execution wait rather than widen the poll further.
81 */
82 const waitForExecution = (baseUrl: string, executionId: string) =>
83 getJson(baseUrl, "/executions").pipe(
84 Effect.flatMap((response) => {
85 const record = (
86 response as {
87 flowExecutions: Array<{
88 executionId?: string;
89 executionStatus?: string;
90 }>;
91 }
92 ).flowExecutions.find((r) => r.executionId === executionId);
93 return record?.executionStatus && record.executionStatus !== "InProgress"
94 ? Effect.succeed(record)
95 : Effect.fail(new ExecutionNotFinished({ lastStatus: record?.executionStatus }));
96 }),
97 Effect.retry({
98 while: (e): boolean => e instanceof ExecutionNotFinished,
99 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(18)]),
100 }),
101 );
102
103 class EventNotDelivered extends Data.TaggedError("EventNotDelivered") {}
104
105 interface DeliveredFlowEvent {
106 detailType: string;
107 detail: { "flow-name"?: string; status?: string; "execution-id"?: string };
108 }
109
110 /**
111 * Re-publish a run report until the fresh EventBridge rule routes it.
112 *
113 * AppFlow's native run-report delivery is explicitly best-effort, so a live
114 * flow can finish without emitting an event. Publishing the documented event
115 * envelope tests `flowEvents` deterministically while also riding out the
116 * new rule's propagation window (EventBridge never retro-delivers events).
117 */
118 const publishUntilFlowEvent = (queueUrl: string, executionId: string) =>
119 Effect.gen(function* () {
120 const published = yield* eventbridge.putEvents({
121 Entries: [
122 {
123 Source: "alchemy.test.appflow",
124 DetailType: "AppFlow End Flow Run Report",
125 Detail: JSON.stringify({
126 "flow-name": FLOW_NAME,
127 "execution-id": executionId,
128 status: "Execution Successful",
129 }),
130 },
131 ],
132 });
133 if ((published.FailedEntryCount ?? 0) !== 0) {
134 return yield* Effect.fail(new EventNotDelivered());
135 }
136
137 const result = yield* sqs.receiveMessage({
138 QueueUrl: queueUrl,
139 MaxNumberOfMessages: 10,
140 WaitTimeSeconds: 2,
141 });
142 const match = (result.Messages ?? [])
143 .flatMap((message) => (message.Body ? [JSON.parse(message.Body) as DeliveredFlowEvent] : []))
144 .find(
145 (body) =>
146 body.detail?.["flow-name"] === FLOW_NAME && body.detail?.["execution-id"] === executionId,
147 );
148 if (!match) {
149 return yield* Effect.fail(new EventNotDelivered());
150 }
151 return match;
152 }).pipe(
153 Effect.retry({
154 while: (e): boolean => e._tag === "EventNotDelivered",
155 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(8)]),
156 }),
157 );
158
159 const assertFlowGone = (flowName: string) =>
160 Effect.gen(function* () {
161 const result = yield* appflow.describeFlow({ flowName }).pipe(
162 Effect.map(() => "present" as const),
163 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
164 );
165 if (result === "present") {
166 return yield* Effect.fail(new Error(`Flow '${flowName}' still exists`));
167 }
168 }).pipe(
169 Effect.retry({
170 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]),
171 }),
172 );
173
174 // One sequential lifecycle: the S3-to-S3 path is credential-free, but the
175 // source prefix must be seeded BEFORE the flow exists, so the bucket deploys
176 // first, the object lands out-of-band, then the full fixture deploys.
177 test.provider(
178 "StartFlow / DescribeFlowExecutionRecords / StopFlow / CancelFlowExecutions / flowEvents",
179 (stack) =>
180 Effect.gen(function* () {
181 yield* stack.destroy();
182
183 // Phase 1: the bucket alone; seed the source prefix.
184 yield* stack.deploy(FlowBucket);
185 yield* s3.putObject({
186 Bucket: FLOW_BUCKET,
187 Key: "input/data.csv",
188 Body: new TextEncoder().encode("id,name\n1,alpha\n2,beta\n"),
189 ContentType: "text/csv",
190 });
191
192 // Phase 2: flow + queue + function with all bindings.
193 const outputs = yield* stack.deploy(program);
194 const url = outputs.fn.functionUrl!;
195 const queueUrl = outputs.eventsQueue.queueUrl;
196 expect(outputs.flow.flowName).toBe(FLOW_NAME);
197 expect(url).toBeDefined();
198
199 yield* getJson(url, "/ready");
200
201 // StartFlow — trigger an on-demand run.
202 const started = (yield* postJson(url, "/start", {})) as {
203 executionId?: string;
204 flowArn?: string;
205 };
206 expect(started.executionId).toBeTruthy();
207 expect(started.flowArn).toContain(":appflow:");
208
209 // StopFlow — on-demand flows cannot be stopped; the binding surfaces
210 // the typed tag.
211 const stopped = (yield* postJson(url, "/stop", {})) as {
212 ok: boolean;
213 error?: string;
214 };
215 expect(stopped.ok).toBe(false);
216 expect(stopped.error).toBe("UnsupportedOperationException");
217
218 // DescribeFlowExecutionRecords — poll until the run leaves InProgress.
219 const record = yield* waitForExecution(url, started.executionId!);
220 expect(record.executionId).toBe(started.executionId);
221 expect(record.executionStatus).toBeTruthy();
222
223 // flowEvents — AppFlow's native delivery is best-effort. Publish the
224 // documented run-report envelope until the fresh rule is active, then
225 // verify the consume loop forwards it into the SQS sink.
226 const event = yield* publishUntilFlowEvent(queueUrl, started.executionId!);
227 expect(event.detail["flow-name"]).toBe(FLOW_NAME);
228 expect(event.detailType).toContain("Flow Run Report");
229
230 // CancelFlowExecutions — start a second run and cancel it right away.
231 // The run may already be finishing, so only the response shape is
232 // asserted (finished runs land in invalidExecutions).
233 const second = (yield* postJson(url, "/start", {})) as {
234 executionId?: string;
235 };
236 const canceled = (yield* postJson(url, "/cancel", {
237 executionIds: [second.executionId],
238 })) as { ok: boolean; invalidExecutions?: string[] };
239 expect(canceled.ok).toBe(true);
240 expect(Array.isArray(canceled.invalidExecutions)).toBe(true);
241
242 // Destroy and verify the flow is gone.
243 yield* stack.destroy();
244 yield* assertFlowGone(FLOW_NAME);
245 }),
246 // Observed pass baseline is 104–117s under full-suite load with the old
247 // ~50s execution poll; the poll is now ~90s, so 210s + the 40s poll delta.
248 // Every constituent readiness poll remains independently bounded.
249 {
250 tags: [
251 "provider:aws",
252 "provider:aws:appflow",
253 "provider:aws:eventbridge",
254 "provider:aws:iam",
255 "provider:aws:lambda",
256 "provider:aws:s3",
257 "provider:aws:sqs",
258 "live",
259 ],
260 timeout: 250_000,
261 retry: 0,
262 },
263 );