@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.18 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 Provider from "alchemy/Provider";
7 import { isResourceState, State, type ResourceState } from "alchemy/State";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const timeoutHandlerPath = fileURLToPath(new URL("./timeout-handler.ts", import.meta.url));
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 test.provider(
16 "create, update, list, replace, delete alias",
17 (stack) =>
18 Effect.gen(function* () {
19 yield* stack.destroy();
20
21 const program = ({
22 envVersion,
23 alias,
24 }: {
25 envVersion: string;
26 alias?: {
27 aliasName: string;
28 description?: string;
29 routingConfig?: Lambda.AliasRoutingConfiguration;
30 };
31 }) =>
32 Effect.gen(function* () {
33 const fn = yield* AWS.Lambda.Function("AliasFn", {
34 main: timeoutHandlerPath,
35 handler: "handler",
36 isExternal: true,
37 functionUrl: false,
38 env: {
39 VERSION: envVersion,
40 },
41 });
42
43 const version = yield* AWS.Lambda.Version("AliasVersion", {
44 function: fn,
45 });
46
47 const live = alias
48 ? yield* AWS.Lambda.Alias("LiveAlias", {
49 version,
50 aliasName: alias.aliasName,
51 description: alias.description,
52 routingConfig: alias.routingConfig,
53 })
54 : undefined;
55
56 return { fn, version, live };
57 });
58
59 const initial = yield* stack.deploy(program({ envVersion: "1" }));
60 const version1 = initial.version.version;
61
62 // --- create ---
63 const created = yield* stack.deploy(
64 program({
65 envVersion: "1",
66 alias: {
67 aliasName: "live",
68 description: "live v1",
69 },
70 }),
71 );
72 const createdAlias = created.live!;
73
74 expect(createdAlias.aliasName).toBe("live");
75 expect(createdAlias.functionVersion).toBe(version1);
76 expect(createdAlias.invokeArn).toContain(createdAlias.aliasArn);
77
78 // Assert the alias actually exists in the cloud.
79 const liveV1 = yield* getAliasOrUndefined(created.fn.functionName, "live");
80 expect(liveV1).toBeDefined();
81 expect(liveV1!.AliasArn).toBe(createdAlias.aliasArn);
82 expect(liveV1!.FunctionVersion).toBe(version1);
83 expect(liveV1!.Description).toBe("live v1");
84 expect(liveV1!.RoutingConfig?.AdditionalVersionWeights ?? {}).toEqual({});
85
86 // --- crash before the Version reference resolved ---
87 // An interrupted first create persists the Version reference as missing
88 // (it was still an unresolved Output at checkpoint time). The recovery
89 // read finds nothing to recover instead of crashing (#2002), and the
90 // re-driven create converges on the same alias.
91 const state = yield* yield* State;
92 const fqns = yield* state.list({ stack: stack.name, stage: stack.stage });
93 const rows = yield* Effect.forEach(fqns, (fqn) =>
94 state
95 .get({ stack: stack.name, stage: stack.stage, fqn })
96 .pipe(Effect.map((row) => ({ fqn, row }))),
97 );
98 const aliasRow = rows.find(
99 (row): row is { fqn: string; row: ResourceState } =>
100 isResourceState(row.row) && row.row.resourceType === "AWS.Lambda.Alias",
101 );
102 if (!aliasRow?.row.props) {
103 return yield* Effect.die(
104 new Error("no persisted AWS.Lambda.Alias props found after deploy"),
105 );
106 }
107 yield* state.set({
108 stack: stack.name,
109 stage: stack.stage,
110 fqn: aliasRow.fqn,
111 value: {
112 ...aliasRow.row,
113 props: { ...aliasRow.row.props, version: undefined },
114 status: "creating",
115 attr: undefined,
116 },
117 });
118 const recovered = yield* stack.deploy(
119 program({
120 envVersion: "1",
121 alias: {
122 aliasName: "live",
123 description: "live v1",
124 },
125 }),
126 );
127 expect(recovered.live!.aliasArn).toBe(createdAlias.aliasArn);
128 expect(recovered.live!.functionVersion).toBe(version1);
129
130 // --- update (function version + weighted routing + description) ---
131 const updated = yield* stack.deploy(
132 program({
133 envVersion: "2",
134 alias: {
135 aliasName: "live",
136 description: "weighted live",
137 routingConfig: {
138 AdditionalVersionWeights: {
139 [version1]: 0.25,
140 },
141 },
142 },
143 }),
144 );
145 const version2 = updated.version.version;
146 const updatedAlias = updated.live!;
147
148 // Updating in place must keep the same ARN (not a replacement).
149 expect(updatedAlias.aliasArn).toBe(createdAlias.aliasArn);
150 expect(updatedAlias.functionVersion).toBe(version2);
151 expect(updatedAlias.description).toBe("weighted live");
152 expect(updatedAlias.routingConfig).toEqual({
153 AdditionalVersionWeights: {
154 [version1]: 0.25,
155 },
156 });
157
158 // Assert the cloud reflects the update.
159 const liveV2 = yield* getAliasOrUndefined(updated.fn.functionName, "live");
160 expect(liveV2).toBeDefined();
161 expect(liveV2!.AliasArn).toBe(createdAlias.aliasArn);
162 expect(liveV2!.FunctionVersion).toBe(version2);
163 expect(liveV2!.Description).toBe("weighted live");
164 expect(liveV2!.RoutingConfig?.AdditionalVersionWeights).toEqual({
165 [version1]: 0.25,
166 });
167
168 // --- update (clear description + routing) ---
169 const cleared = yield* stack.deploy(
170 program({
171 envVersion: "2",
172 alias: {
173 aliasName: "live",
174 },
175 }),
176 );
177 const clearedAlias = cleared.live!;
178
179 expect(clearedAlias.aliasArn).toBe(createdAlias.aliasArn);
180 expect(clearedAlias.functionVersion).toBe(version2);
181 expect(clearedAlias.description).toBeUndefined();
182 expect(clearedAlias.routingConfig).toBeUndefined();
183
184 // Assert the cloud cleared description and routing.
185 const liveCleared = yield* getAliasOrUndefined(cleared.fn.functionName, "live");
186 expect(liveCleared).toBeDefined();
187 expect(liveCleared!.Description ?? "").toBe("");
188 expect(liveCleared!.RoutingConfig?.AdditionalVersionWeights ?? {}).toEqual({});
189
190 // --- replace (rename the alias) ---
191 // The provider's `diff` flags an aliasName change as a replacement, so a
192 // brand new alias is created and the old one deleted.
193 const replaced = yield* stack.deploy(
194 program({
195 envVersion: "2",
196 alias: {
197 aliasName: "stable",
198 description: "renamed",
199 },
200 }),
201 );
202 const replacedAlias = replaced.live!;
203
204 expect(replacedAlias.aliasName).toBe("stable");
205 // A replacement must mint a new ARN.
206 expect(replacedAlias.aliasArn).not.toBe(createdAlias.aliasArn);
207 expect(replacedAlias.functionVersion).toBe(version2);
208 expect(replacedAlias.description).toBe("renamed");
209
210 // The new alias exists in the cloud...
211 const stableAlias = yield* getAliasOrUndefined(replaced.fn.functionName, "stable");
212 expect(stableAlias).toBeDefined();
213 expect(stableAlias!.AliasArn).toBe(replacedAlias.aliasArn);
214 expect(stableAlias!.FunctionVersion).toBe(version2);
215 expect(stableAlias!.Description).toBe("renamed");
216
217 // ...and the old one was deleted as part of the replacement.
218 const oldLive = yield* getAliasOrUndefined(replaced.fn.functionName, "live");
219 expect(oldLive).toBeUndefined();
220
221 // --- list ---
222 const provider = yield* Provider.findProvider(AWS.Lambda.Alias);
223 const aliases = yield* provider.list();
224 expect(
225 aliases.some(
226 (alias) =>
227 alias.functionName === replaced.fn.functionName && alias.aliasName === "stable",
228 ),
229 ).toBe(true);
230
231 // --- delete ---
232 yield* stack.destroy();
233
234 const afterDestroy = yield* getAliasOrUndefined(replaced.fn.functionName, "stable");
235 expect(afterDestroy).toBeUndefined();
236
237 // ...and the host function itself is gone (bounded retry to ride out
238 // delete propagation).
239 yield* Lambda.getFunction({
240 FunctionName: replaced.fn.functionName,
241 }).pipe(
242 Effect.flatMap(() =>
243 Effect.fail(new Error(`Function ${replaced.fn.functionName} still exists`)),
244 ),
245 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
246 Effect.retry({
247 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
248 }),
249 );
250 }).pipe(
251 Effect.tap(() => stack.destroy()),
252 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
253 ),
254 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 360_000 },
255 );
256
257 const getAliasOrUndefined = Effect.fn(function* (functionName: string, name: string) {
258 return yield* Lambda.getAlias({
259 FunctionName: functionName,
260 Name: name,
261 }).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)));
262 });