@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 262 lines 9.45 KB
Raw
1 import { fileURLToPath } from "node:url";
2 import * as AWS from "@alchemy.run/aws";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Lambda from "@distilled.cloud/aws/lambda";
6 import * as S3 from "@distilled.cloud/aws/s3";
7 import * as Provider from "alchemy/Provider";
8 import { zipFiles } from "alchemy/Util/zip";
9 import * as Effect from "effect/Effect";
10
11 const layerV1Path = fileURLToPath(new URL("./fixtures/layer-v1", import.meta.url));
12 const layerV2Path = fileURLToPath(new URL("./fixtures/layer-v2", import.meta.url));
13 const timeoutHandlerPath = fileURLToPath(new URL("./timeout-handler.ts", import.meta.url));
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 test.provider(
18 "publish, republish, attach to a function, list, delete layer version",
19 (stack) =>
20 Effect.gen(function* () {
21 yield* stack.destroy();
22
23 const program = ({
24 path,
25 description,
26 withFunction = false,
27 layered = true,
28 }: {
29 path: string;
30 description?: string;
31 withFunction?: boolean;
32 layered?: boolean;
33 }) =>
34 Effect.gen(function* () {
35 const layer = yield* AWS.Lambda.LayerVersion("Deps", {
36 path,
37 description,
38 compatibleRuntimes: ["nodejs22.x"],
39 });
40
41 const fn = withFunction
42 ? yield* AWS.Lambda.Function("LayeredFn", {
43 main: timeoutHandlerPath,
44 handler: "handler",
45 isExternal: true,
46 functionUrl: false,
47 // Pass the resource itself — `layers` also accepts a raw ARN.
48 layers: layered ? [layer] : [],
49 })
50 : undefined;
51
52 return { layer, fn };
53 });
54
55 // --- create ---
56 const created = yield* stack.deploy(program({ path: layerV1Path }));
57 const v1 = created.layer;
58
59 expect(v1.version).toBeGreaterThan(0);
60 expect(v1.layerVersionArn).toBe(`${v1.layerArn}:${v1.version}`);
61 expect(v1.compatibleRuntimes).toEqual(["nodejs22.x"]);
62
63 const cloudV1 = yield* getLayerVersionOrUndefined(v1.layerName, v1.version);
64 expect(cloudV1).toBeDefined();
65 expect(cloudV1!.LayerVersionArn).toBe(v1.layerVersionArn);
66 expect(cloudV1!.Content?.CodeSha256).toBe(v1.codeSha256);
67
68 // --- noop (identical content must not publish a new version) ---
69 const unchanged = yield* stack.deploy(program({ path: layerV1Path }));
70 expect(unchanged.layer.version).toBe(v1.version);
71 expect(unchanged.layer.layerVersionArn).toBe(v1.layerVersionArn);
72
73 // --- update (changed content publishes a new version of the SAME
74 // layer, rather than stranding a fresh layer at version 1) ---
75 const republished = yield* stack.deploy(program({ path: layerV2Path }));
76 const v2 = republished.layer;
77
78 expect(v2.layerName).toBe(v1.layerName);
79 expect(v2.layerArn).toBe(v1.layerArn);
80 expect(v2.version).toBe(v1.version + 1);
81 expect(v2.layerVersionArn).not.toBe(v1.layerVersionArn);
82 expect(v2.codeSha256).not.toBe(v1.codeSha256);
83
84 // The superseded version is retired, so updates don't leak one
85 // version per deploy.
86 expect(yield* getLayerVersionOrUndefined(v1.layerName, v1.version)).toBeUndefined();
87
88 // --- update (changed description alone also republishes) ---
89 const described = yield* stack.deploy(
90 program({ path: layerV2Path, description: "with description" }),
91 );
92 expect(described.layer.layerName).toBe(v1.layerName);
93 expect(described.layer.version).toBe(v2.version + 1);
94 expect(described.layer.description).toBe("with description");
95
96 const currentLayer = described.layer;
97
98 // --- attach to a function ---
99 const attached = yield* stack.deploy(
100 program({
101 path: layerV2Path,
102 description: "with description",
103 withFunction: true,
104 }),
105 );
106 const functionName = attached.fn!.functionName;
107
108 expect((yield* getFunctionLayers(functionName)).map((layer) => layer.Arn)).toEqual([
109 currentLayer.layerVersionArn,
110 ]);
111
112 // --- detach ---
113 yield* stack.deploy(
114 program({
115 path: layerV2Path,
116 description: "with description",
117 withFunction: true,
118 layered: false,
119 }),
120 );
121 expect(yield* getFunctionLayers(functionName)).toEqual([]);
122
123 // --- list ---
124 const provider = yield* Provider.findProvider(AWS.Lambda.LayerVersion);
125 const versions = yield* provider.list();
126 expect(
127 versions.some((version) => version.layerVersionArn === currentLayer.layerVersionArn),
128 ).toBe(true);
129
130 // --- delete ---
131 yield* stack.destroy();
132
133 expect(
134 yield* getLayerVersionOrUndefined(currentLayer.layerName, currentLayer.version),
135 ).toBeUndefined();
136 }).pipe(
137 Effect.tap(() => stack.destroy()),
138 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
139 ),
140 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 360_000 },
141 );
142
143 // Both `layers` and `s3.bucket` accept either the resource itself or the raw
144 // identifier. Switching a deployed stack between the two forms must converge
145 // on the same cloud state rather than churning the layer version.
146 test.provider(
147 "layers and s3.bucket accept a resource or a raw identifier, and cycle between them",
148 (stack) =>
149 Effect.gen(function* () {
150 yield* stack.destroy();
151
152 const archive = yield* zipFiles([
153 { path: "nodejs/from-s3.mjs", content: "export const via = 's3';\n" },
154 ]);
155
156 // --- s3.bucket: resource form ---
157 const s3Program = (byName: boolean) =>
158 Effect.gen(function* () {
159 const bucket = yield* AWS.S3.Bucket("LayerSource", {
160 forceDestroy: true,
161 });
162 const layer = yield* AWS.Lambda.LayerVersion("FromS3", {
163 s3: {
164 bucket: byName ? bucket.bucketName : bucket,
165 key: LAYER_KEY,
166 },
167 });
168 return { bucket, layer };
169 });
170
171 // The object has to exist before the layer publishes, so create the
172 // bucket on its own first.
173 const bucketOnly = yield* stack.deploy(
174 Effect.gen(function* () {
175 return yield* AWS.S3.Bucket("LayerSource", { forceDestroy: true });
176 }),
177 );
178 yield* S3.putObject({
179 Bucket: bucketOnly.bucketName,
180 Key: LAYER_KEY,
181 Body: new Uint8Array(archive),
182 });
183
184 const byResource = yield* stack.deploy(s3Program(false));
185 expect(byResource.layer.version).toBe(1);
186 expect(byResource.layer.layerVersionArn).toContain(":1");
187
188 // --- s3.bucket: cycle to the raw-name form ---
189 const byName = yield* stack.deploy(s3Program(true));
190 // Same bucket, expressed differently — must NOT republish.
191 expect(byName.layer.layerVersionArn).toBe(byResource.layer.layerVersionArn);
192 expect(byName.layer.version).toBe(byResource.layer.version);
193
194 // --- and back again ---
195 const backToResource = yield* stack.deploy(s3Program(false));
196 expect(backToResource.layer.layerVersionArn).toBe(byResource.layer.layerVersionArn);
197
198 // --- layers: both forms, cycled ---
199 const fnProgram = (byArn: boolean) =>
200 Effect.gen(function* () {
201 const bucket = yield* AWS.S3.Bucket("LayerSource", {
202 forceDestroy: true,
203 });
204 const layer = yield* AWS.Lambda.LayerVersion("FromS3", {
205 s3: { bucket, key: LAYER_KEY },
206 });
207 const fn = yield* AWS.Lambda.Function("RefFormFn", {
208 main: timeoutHandlerPath,
209 handler: "handler",
210 isExternal: true,
211 functionUrl: false,
212 layers: [byArn ? layer.layerVersionArn : layer],
213 });
214 return { bucket, layer, fn };
215 });
216
217 const withResource = yield* stack.deploy(fnProgram(false));
218 const layerArn = backToResource.layer.layerVersionArn;
219 expect((yield* getFunctionLayers(withResource.fn.functionName)).map((l) => l.Arn)).toEqual([
220 layerArn,
221 ]);
222
223 const withArn = yield* stack.deploy(fnProgram(true));
224 // Cycling the reference form is a no-op, not a phantom update: same
225 // function, same attached layer, no re-issued configuration call.
226 expect(withArn.fn.functionName).toBe(withResource.fn.functionName);
227 expect((yield* getFunctionLayers(withArn.fn.functionName)).map((l) => l.Arn)).toEqual([
228 layerArn,
229 ]);
230
231 // ...and back to the resource form.
232 const backToRef = yield* stack.deploy(fnProgram(false));
233 expect((yield* getFunctionLayers(backToRef.fn.functionName)).map((l) => l.Arn)).toEqual([
234 layerArn,
235 ]);
236
237 yield* stack.destroy();
238 }).pipe(
239 Effect.tap(() => stack.destroy()),
240 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
241 ),
242 {
243 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3", "live"],
244 timeout: 360_000,
245 },
246 );
247
248 const LAYER_KEY = "layers/from-s3.zip";
249
250 const getLayerVersionOrUndefined = Effect.fn(function* (layerName: string, version: number) {
251 return yield* Lambda.getLayerVersion({
252 LayerName: layerName,
253 VersionNumber: version,
254 }).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)));
255 });
256
257 const getFunctionLayers = Effect.fn(function* (functionName: string) {
258 const configuration = yield* Lambda.getFunctionConfiguration({
259 FunctionName: functionName,
260 });
261 return configuration.Layers ?? [];
262 });