@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 415 lines 15.6 KB
Raw
1 import { fileURLToPath } from "node:url";
2 /**
3 * `AWS.Lambda.Function` under `alchemy dev`: the dualized provider deploys
4 * the function INTO the floci emulator (RPC-sidecar-hosted
5 * `FlociFunctionProvider`) and hot-swaps its code on file change.
6 *
7 * Proof structure:
8 * - attrs carry the emulator shape — dummy-account ARN
9 * (`:000000000000:`) and a `*.lambda-url.us-east-1.localhost:4566`
10 * function URL — which the live cloud can never produce;
11 * - the function URL is host-reachable and serves the fixture's marker;
12 * - rewriting the fixture source (in a CLONE, the repo tree stays clean)
13 * is observed at the URL without another deploy — the sidecar watch
14 * loop rebuilds, uploads to the emulator's assets bucket, and floci's
15 * reactive S3 sync re-extracts (measured latency reported);
16 * - an engine-driven update (env change) between two rewrites does not
17 * detach the function from the watch loop's dev key — the next rewrite
18 * still serves (regression: the watcher memoized its one-time
19 * enrollment and never re-pointed the function after the live
20 * reconcile moved it to a content-addressed key);
21 * - a dualized Queue + a `bundle: false` Function + a distilled
22 * event-source mapping pump SQS messages through the containerized
23 * function into a dualized Bucket;
24 * - after destroy the function and its `/aws/lambda/<name>` log group
25 * are gone from the emulator.
26 *
27 * Requires Docker (floci + the Lambda runtime container); skipped when the
28 * daemon is unavailable.
29 */
30 import * as AWS from "@alchemy.run/aws";
31 import * as Endpoint from "@alchemy.run/aws/Endpoint";
32 import * as Region from "@alchemy.run/aws/Region";
33 import { expect } from "@alchemy.run/test";
34 import * as Test from "@alchemy.run/test/Alchemy";
35 import * as Logs from "@distilled.cloud/aws/cloudwatch-logs";
36 import { Credentials } from "@distilled.cloud/aws/Credentials";
37 import * as Lambda from "@distilled.cloud/aws/lambda";
38 import type { RegionName } from "@distilled.cloud/aws/Region";
39 import * as SQS from "@distilled.cloud/aws/sqs";
40 import * as Effect from "effect/Effect";
41 import * as FileSystem from "effect/FileSystem";
42 import * as HttpClient from "effect/http/HttpClient";
43 import * as Layer from "effect/Layer";
44 import * as Path from "effect/Path";
45 import * as Redacted from "effect/Redacted";
46 import * as Schedule from "effect/Schedule";
47 import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts";
48 import { FLOCI_ENDPOINT, rawS3GetObject } from "../Local/fixtures/raw.ts";
49
50 const { test } = Test.make({ providers: AWS.providers(), dev: true });
51
52 const devFixtureDir = fileURLToPath(new URL("../Lambda/fixtures/floci-dev", import.meta.url));
53 const esmHandlerPath = fileURLToPath(
54 new URL("../Lambda/fixtures/floci-esm/handler.mjs", import.meta.url),
55 );
56
57 /** Floci-scoped context for the raw distilled calls the test makes itself. */
58 const flociContext = Layer.mergeAll(
59 Endpoint.of(FLOCI_ENDPOINT),
60 Region.of("us-east-1"),
61 Layer.succeed(
62 Credentials,
63 Effect.succeed({
64 accessKeyId: Redacted.make("test"),
65 secretAccessKey: Redacted.make("test"),
66 sessionToken: undefined,
67 region: "us-east-1" as RegionName,
68 }),
69 ),
70 );
71
72 test.provider(
73 "dev deploys a Function into floci with a reachable URL and hot reload",
74 (stack) =>
75 Effect.gen(function* () {
76 const fs = yield* FileSystem.FileSystem;
77 const path = yield* Path.Path;
78 const client = yield* HttpClient.HttpClient;
79
80 yield* stack.destroy();
81
82 // Clone the fixture so the hot-reload rewrite never touches the repo
83 // tree.
84 const clone = yield* cloneFixture(devFixtureDir, {
85 prefix: "floci-dev-fn-",
86 });
87 const mainPath = path.join(clone, "handler.ts");
88
89 const fn = yield* stack.deploy(
90 AWS.Lambda.Function("DevFn", {
91 main: mainPath,
92 handler: "handler",
93 isExternal: true,
94 functionUrl: true,
95 env: { DEV_MARKER: "env-carried" },
96 }),
97 );
98
99 // Emulator-shaped identity: the live cloud can never produce these.
100 expect(fn.functionArn).toContain(":000000000000:");
101 expect(fn.functionArn).toContain(":us-east-1:");
102 expect(fn.functionUrl).toBeDefined();
103 expect(fn.functionUrl).toContain("localhost:4566");
104
105 // First invoke pulls the Lambda runtime image (~4.4s cold), then warm.
106 const first = yield* client.get(fn.functionUrl!).pipe(
107 Effect.flatMap((response) =>
108 Effect.map(response.text, (body) => ({
109 status: response.status,
110 body,
111 })),
112 ),
113 Effect.retry({
114 while: (): boolean => true,
115 schedule: Schedule.spaced("2 seconds"),
116 times: 60,
117 }),
118 Effect.repeat({
119 schedule: Schedule.spaced("2 seconds"),
120 until: (res): boolean => res.status === 200 && res.body.startsWith("marker-v1"),
121 times: 60,
122 }),
123 );
124 expect(first.status).toBe(200);
125 expect(first.body).toBe("marker-v1:env-carried");
126
127 // Hot reload: rewrite the CLONED source and wait (bounded) for the
128 // new marker to serve — no deploy in between. The sidecar watch loop
129 // rebuilds + uploads; floci's reactive S3 sync re-extracts.
130 const source = yield* fs.readFileString(mainPath);
131 const swapStartedAt = Date.now();
132 yield* fs.writeFileString(mainPath, source.replace(`"marker-v1"`, `"marker-v2"`));
133
134 const swapped = yield* client.get(fn.functionUrl!).pipe(
135 Effect.flatMap((response) =>
136 Effect.map(response.text, (body) => ({
137 status: response.status,
138 body,
139 })),
140 ),
141 Effect.retry({
142 while: (): boolean => true,
143 schedule: Schedule.spaced("250 millis"),
144 times: 20,
145 }),
146 Effect.repeat({
147 schedule: Schedule.spaced("250 millis"),
148 until: (res): boolean => res.status === 200 && res.body.startsWith("marker-v2"),
149 times: 240,
150 }),
151 );
152 const swapLatencyMs = Date.now() - swapStartedAt;
153 expect(swapped.body).toBe("marker-v2:env-carried");
154 yield* Effect.log(`hot reload observed at the function URL in ${swapLatencyMs}ms`);
155
156 // Second swap: the function's code is now enrolled on the stable dev
157 // S3 key, so this one is a bare PutObject + floci reactive re-extract
158 // (no Lambda API call).
159 const secondStartedAt = Date.now();
160 yield* fs.writeFileString(mainPath, source.replace(`"marker-v1"`, `"marker-v3"`));
161 const reswapped = yield* client.get(fn.functionUrl!).pipe(
162 Effect.flatMap((response) =>
163 Effect.map(response.text, (body) => ({
164 status: response.status,
165 body,
166 })),
167 ),
168 Effect.retry({
169 while: (): boolean => true,
170 schedule: Schedule.spaced("250 millis"),
171 times: 20,
172 }),
173 Effect.repeat({
174 schedule: Schedule.spaced("250 millis"),
175 until: (res): boolean => res.status === 200 && res.body.startsWith("marker-v3"),
176 times: 240,
177 }),
178 );
179 const secondSwapLatencyMs = Date.now() - secondStartedAt;
180 expect(reswapped.body).toBe("marker-v3:env-carried");
181 yield* Effect.log(
182 `second hot reload (reactive S3 sync) observed in ${secondSwapLatencyMs}ms`,
183 );
184
185 // Engine-driven UPDATE between swaps (a prop change — here `env`):
186 // the live reconcile re-uploads the bundle to a content-addressed key
187 // and re-points the function THERE, detaching it from the watch
188 // loop's stable dev key. The watcher must notice and re-enroll on its
189 // next swap; before the fix the marker below never served (the
190 // PutObject landed on a key the function no longer read) until the
191 // next engine update happened to re-bundle it.
192 const updated = yield* stack.deploy(
193 AWS.Lambda.Function("DevFn", {
194 main: mainPath,
195 handler: "handler",
196 isExternal: true,
197 functionUrl: true,
198 env: { DEV_MARKER: "env-updated" },
199 }),
200 );
201 expect(updated.functionUrl).toBe(fn.functionUrl);
202 const afterUpdate = yield* client.get(fn.functionUrl!).pipe(
203 Effect.flatMap((response) =>
204 Effect.map(response.text, (body) => ({
205 status: response.status,
206 body,
207 })),
208 ),
209 Effect.retry({
210 while: (): boolean => true,
211 schedule: Schedule.spaced("250 millis"),
212 times: 20,
213 }),
214 Effect.repeat({
215 schedule: Schedule.spaced("250 millis"),
216 until: (res): boolean => res.status === 200 && res.body === "marker-v3:env-updated",
217 times: 240,
218 }),
219 );
220 expect(afterUpdate.body).toBe("marker-v3:env-updated");
221
222 yield* fs.writeFileString(mainPath, source.replace(`"marker-v1"`, `"marker-v4"`));
223 const afterUpdateSwap = yield* client.get(fn.functionUrl!).pipe(
224 Effect.flatMap((response) =>
225 Effect.map(response.text, (body) => ({
226 status: response.status,
227 body,
228 })),
229 ),
230 Effect.retry({
231 while: (): boolean => true,
232 schedule: Schedule.spaced("250 millis"),
233 times: 20,
234 }),
235 Effect.repeat({
236 schedule: Schedule.spaced("250 millis"),
237 until: (res): boolean => res.status === 200 && res.body.startsWith("marker-v4"),
238 times: 240,
239 }),
240 );
241 expect(afterUpdateSwap.body).toBe("marker-v4:env-updated");
242
243 // The invocations above made the emulator create the function's log
244 // group (control: the destroy assertion below is not vacuous).
245 const logGroupName = `/aws/lambda/${fn.functionName}`;
246 const logGroupExists = Logs.describeLogGroups({
247 logGroupNamePrefix: logGroupName,
248 }).pipe(
249 Effect.map((r) => (r.logGroups ?? []).some((g) => g.logGroupName === logGroupName)),
250 Effect.provide(flociContext),
251 );
252 const existedBeforeDestroy = yield* logGroupExists.pipe(
253 Effect.repeat({
254 schedule: Schedule.spaced("500 millis"),
255 until: (exists): boolean => exists,
256 times: 20,
257 }),
258 );
259 expect(existedBeforeDestroy).toBe(true);
260
261 // Destroy: the function (and its role) must be gone from the
262 // emulator.
263 yield* stack.destroy();
264 expect(yield* logGroupExists).toBe(false);
265 const gone = yield* Lambda.getFunction({
266 FunctionName: fn.functionName,
267 }).pipe(
268 Effect.map(() => false),
269 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
270 Effect.provide(flociContext),
271 Effect.repeat({
272 schedule: Schedule.spaced("1 second"),
273 until: (isGone): boolean => isGone,
274 times: 20,
275 }),
276 );
277 expect(gone).toBe(true);
278 }),
279 { tags: ["provider:aws", "provider:aws:lambda", "local"], timeout: 540_000 },
280 );
281
282 test.provider(
283 "SQS -> dev Function (bundle: false) -> S3 event pumping in the emulator",
284 (stack) =>
285 Effect.gen(function* () {
286 yield* stack.destroy();
287
288 const markerKey = "floci-dev-esm-consumed.txt";
289
290 const outputs = yield* stack.deploy(
291 Effect.gen(function* () {
292 const queue = yield* AWS.SQS.Queue("DevEsmQueue");
293 const bucket = yield* AWS.S3.Bucket("DevEsmTarget", {
294 forceDestroy: true,
295 });
296 const fn = yield* AWS.Lambda.Function("DevEsmFn", {
297 main: esmHandlerPath,
298 handler: "handler",
299 bundle: false,
300 functionUrl: false,
301 env: { TARGET_BUCKET: bucket.bucketName },
302 });
303 // The dualized RESOURCE (requires the alchemy floci fork ≥
304 // 1.6.0-alchemy.2: its reconciler's ownership scan calls lambda
305 // ListTags on the `event-source-mapping:` ARN, and creation
306 // brands it with Tags).
307 const esm = yield* AWS.Lambda.EventSourceMapping("DevEsm", {
308 functionName: fn.functionName,
309 eventSourceArn: queue.queueArn,
310 batchSize: 1,
311 enabled: true,
312 });
313 return { queue, bucket, fn, esm };
314 }),
315 );
316 expect(outputs.queue.queueArn).toContain(":000000000000:");
317 expect(outputs.fn.functionArn).toContain(":000000000000:");
318 expect(outputs.esm.eventSourceMappingArn).toContain(":000000000000:");
319
320 yield* SQS.sendMessage({
321 QueueUrl: outputs.queue.queueUrl,
322 MessageBody: markerKey,
323 }).pipe(Effect.provide(flociContext));
324
325 // Bounded poll: floci's ESM poller delivers to the containerized
326 // function, whose S3 write-back proves consumption.
327 const consumed = yield* rawS3GetObject(outputs.bucket.bucketName, markerKey).pipe(
328 Effect.repeat({
329 schedule: Schedule.spaced("3 seconds"),
330 until: (res): boolean => res.status === 200,
331 times: 40,
332 }),
333 );
334 expect(consumed.status).toBe(200);
335
336 // Destroy tears down the mapping (stopping its poller), the
337 // function, the queue, and the bucket in dependency order.
338 yield* stack.destroy();
339
340 const esmGone = yield* Lambda.getEventSourceMapping({
341 UUID: outputs.esm.uuid,
342 }).pipe(
343 Effect.map(() => false),
344 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
345 Effect.provide(flociContext),
346 );
347 expect(esmGone).toBe(true);
348
349 const gone = yield* Lambda.getFunction({
350 FunctionName: outputs.fn.functionName,
351 }).pipe(
352 Effect.map(() => false),
353 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
354 Effect.provide(flociContext),
355 Effect.repeat({
356 schedule: Schedule.spaced("1 second"),
357 until: (isGone): boolean => isGone,
358 times: 20,
359 }),
360 );
361 expect(gone).toBe(true);
362 }),
363 {
364 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3", "provider:aws:sqs", "local"],
365 timeout: 540_000,
366 },
367 );
368
369 test.provider(
370 "logging.retention creates the log group with its policy before the first invoke",
371 (stack) =>
372 Effect.gen(function* () {
373 yield* stack.destroy();
374
375 const deploy = (logging?: AWS.Lambda.FunctionProps["logging"]) =>
376 stack.deploy(
377 AWS.Lambda.Function("DevRetentionFn", {
378 main: esmHandlerPath,
379 handler: "handler",
380 bundle: false,
381 functionUrl: false,
382 logging,
383 }),
384 );
385
386 // Alchemy always owns the function's log group (the execution role
387 // cannot create it), so it exists before any invoke — with no
388 // retention until `logging` sets one.
389 const fn = yield* deploy();
390 const logGroupName = `/aws/lambda/${fn.functionName}`;
391 const findGroup = Logs.describeLogGroups({ logGroupNamePrefix: logGroupName }).pipe(
392 Effect.map((page) => page.logGroups?.find((g) => g.logGroupName === logGroupName)),
393 Effect.provide(flociContext),
394 );
395 yield* Effect.addFinalizer(() =>
396 Logs.deleteLogGroup({ logGroupName }).pipe(Effect.provide(flociContext), Effect.ignore),
397 );
398 const initial = yield* findGroup;
399 expect(initial).toBeDefined();
400 expect(initial?.retentionInDays).toBeUndefined();
401
402 // 10 days rounds up to CloudWatch's 14.
403 yield* deploy({ retention: "10 days" });
404 expect((yield* findGroup)?.retentionInDays).toBe(14);
405
406 // "forever" clears the policy and keeps the group.
407 yield* deploy({ retention: "forever" });
408 const cleared = yield* findGroup;
409 expect(cleared).toBeDefined();
410 expect(cleared?.retentionInDays).toBeUndefined();
411
412 yield* stack.destroy();
413 }),
414 { tags: ["provider:aws", "provider:aws:lambda", "local"], timeout: 300_000 },
415 );