@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 376 lines 14.5 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as personalize from "@distilled.cloud/aws/personalize";
7 import * as Output from "alchemy/Output";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as Schedule from "effect/Schedule";
11 import PersonalizeTestFunctionLive, { PersonalizeTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "PersonalizeBindings");
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 let resourceArns:
23 | { group: string; tracker: string; datasets: string[]; schemas: string[] }
24 | undefined;
25
26 // Well-formed-but-nonexistent ARNs the probe routes are driven against —
27 // training a solution/campaign takes ~an hour of paid compute, so the
28 // campaign/solution routes only prove the IAM bind + typed error decode.
29 const probeArns = Effect.gen(function* () {
30 const { accountId, region } = yield* AWSEnvironment.current;
31 return {
32 dataset: `arn:aws:personalize:${region}:${accountId}:dataset/alchemy-probe/INTERACTIONS`,
33 importJob: `arn:aws:personalize:${region}:${accountId}:dataset-import-job/alchemy-probe`,
34 datasetGroup: `arn:aws:personalize:${region}:${accountId}:dataset-group/alchemy-probe`,
35 solution: `arn:aws:personalize:${region}:${accountId}:solution/alchemy-probe`,
36 solutionVersion: `arn:aws:personalize:${region}:${accountId}:solution/alchemy-probe/0123456789abcdef`,
37 campaign: `arn:aws:personalize:${region}:${accountId}:campaign/alchemy-probe`,
38 batchJob: `arn:aws:personalize:${region}:${accountId}:batch-inference-job/alchemy-probe`,
39 };
40 });
41
42 // A nonexistent target answers ResourceNotFoundException; malformed-but-
43 // plausible inputs can surface InvalidInputException. Both are typed tags
44 // decoded inside the Lambda — an IAM gap or schema break would surface a
45 // different tag (AccessDeniedException / UnknownAwsError) and fail.
46 const EXPECTED_PROBE_TAGS = ["ResourceNotFoundException", "InvalidInputException"];
47
48 const getJson = (path: string) =>
49 HttpClient.get(`${baseUrl}${path}`).pipe(
50 Effect.flatMap((response) =>
51 response.status >= 500
52 ? Effect.fail(new Error(`transient upstream ${response.status}`))
53 : Effect.succeed(response),
54 ),
55 Effect.retry({
56 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
57 }),
58 Effect.flatMap((r) => r.json),
59 );
60
61 // IAM policy statements on a freshly-created role can take up to ~a minute to
62 // propagate to the Personalize data plane, surfacing as a transient
63 // AccessDeniedException tag. Retry (bounded) until a different tag appears —
64 // a genuine IAM gap still fails the assertion with the AccessDeniedException
65 // tag after the retries are exhausted.
66 const routeTag = (path: string) =>
67 getJson(path).pipe(
68 Effect.map((response) => (response as { tag: string }).tag),
69 Effect.repeat({
70 schedule: Schedule.spaced("3 seconds"),
71 until: (tag: string): boolean => tag !== "AccessDeniedException",
72 times: 20,
73 }),
74 );
75
76 const probeTag = (path: string, arn: string) => routeTag(`${path}?arn=${encodeURIComponent(arn)}`);
77
78 // Create-probes that pass a data-access role: use a nonexistent role in OUR
79 // account so the service validates (and 404s) the primary resource rather
80 // than tripping cross-account PassRole checks.
81 const probeTagWithRole = (path: string, arn: string) =>
82 Effect.gen(function* () {
83 const { accountId } = yield* AWSEnvironment.current;
84 const role = `arn:aws:iam::${accountId}:role/alchemy-nonexistent-probe-role`;
85 return yield* routeTag(
86 `${path}?arn=${encodeURIComponent(arn)}&role=${encodeURIComponent(role)}`,
87 );
88 });
89
90 describe.sequential(
91 "Personalize Bindings",
92 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:personalize", "live"] },
93 () => {
94 beforeAll(
95 Effect.gen(function* () {
96 yield* Effect.logInfo("Personalize test setup: destroying previous resources");
97 yield* sharedStack.destroy();
98
99 yield* Effect.logInfo("Personalize test setup: deploying fixture");
100 const { functionUrl } = yield* sharedStack.deploy(
101 Effect.gen(function* () {
102 return yield* PersonalizeTestFunction;
103 }).pipe(Effect.provide(PersonalizeTestFunctionLive)),
104 );
105
106 // Resolve this generation's references before destroy removes its state.
107 const refOptions = { stack: sharedStack.name, stage: sharedStack.stage };
108 const group = yield* AWS.Personalize.DatasetGroup.ref("BindingsGroup", refOptions);
109 const tracker = yield* AWS.Personalize.EventTracker.ref("Tracker", refOptions);
110 const datasets = yield* Effect.forEach(["Interactions", "Items", "Users"], (id) =>
111 AWS.Personalize.Dataset.ref(id, refOptions),
112 );
113 const schemas = yield* Effect.forEach(
114 ["InteractionsSchema", "ItemsSchema", "UsersSchema"],
115 (id) => AWS.Personalize.Schema.ref(id, refOptions),
116 );
117 resourceArns = yield* Effect.all({
118 group: Output.evaluate(group.datasetGroupArn, {}),
119 tracker: Output.evaluate(tracker.eventTrackerArn, {}),
120 datasets: Effect.forEach(datasets, (dataset) => Output.evaluate(dataset.datasetArn, {})),
121 schemas: Effect.forEach(schemas, (schema) => Output.evaluate(schema.schemaArn, {})),
122 }).pipe(Effect.provide(sharedStack.state));
123
124 expect(functionUrl).toBeTruthy();
125 baseUrl = functionUrl!.replace(/\/+$/, "");
126
127 const readinessUrl = `${baseUrl}/bindings`;
128 yield* HttpClient.get(readinessUrl).pipe(
129 Effect.flatMap((response) =>
130 response.status === 200
131 ? Effect.succeed(response)
132 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
133 ),
134 Effect.retry({ schedule: readinessPolicy }),
135 );
136 }),
137 { timeout: 480_000 },
138 );
139
140 // Verify only resources owned by this fixture generation. Old runs with the
141 // same stack-name prefix must not hold this run's cleanup open.
142 const assertPersonalizeResourcesGone = Core.withProviders(
143 Effect.gen(function* () {
144 if (!resourceArns) return;
145 yield* Effect.forEach(
146 [
147 personalize
148 .describeDatasetGroup({ datasetGroupArn: resourceArns.group })
149 .pipe(Effect.asVoid),
150 personalize
151 .describeEventTracker({ eventTrackerArn: resourceArns.tracker })
152 .pipe(Effect.asVoid),
153 ...resourceArns.datasets.map((datasetArn) =>
154 personalize.describeDataset({ datasetArn }).pipe(Effect.asVoid),
155 ),
156 ...resourceArns.schemas.map((schemaArn) =>
157 personalize.describeSchema({ schemaArn }).pipe(Effect.asVoid),
158 ),
159 ],
160 (describe) =>
161 describe.pipe(
162 Effect.as(false),
163 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
164 Effect.repeat({
165 schedule: Schedule.spaced("2 seconds"),
166 until: (gone) => gone,
167 times: 10,
168 }),
169 Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))),
170 ),
171 { concurrency: 4 },
172 );
173 }),
174 testOptions,
175 sharedStack.name,
176 );
177
178 afterAll(sharedStack.destroy().pipe(Effect.andThen(assertPersonalizeResourcesGone)), {
179 timeout: 420_000,
180 });
181
182 describe("binding registration", () => {
183 test.provider("all nineteen capabilities initialize in the runtime", () =>
184 Effect.gen(function* () {
185 const response = (yield* getJson("/bindings")) as { bound: string[] };
186 expect(response.bound).toHaveLength(19);
187 }),
188 );
189 });
190
191 describe("PutEvents", () => {
192 test.provider("records a real event through the event tracker", () =>
193 Effect.gen(function* () {
194 const tag = yield* routeTag("/put-events");
195 expect(tag).toBe("Recorded");
196 }),
197 );
198 });
199
200 describe("PutItems", () => {
201 test.provider("upserts a real item into the Items dataset", () =>
202 Effect.gen(function* () {
203 const tag = yield* routeTag("/put-items");
204 expect(tag).toBe("Recorded");
205 }),
206 );
207 });
208
209 describe("PutUsers", () => {
210 test.provider("upserts a real user into the Users dataset", () =>
211 Effect.gen(function* () {
212 const tag = yield* routeTag("/put-users");
213 expect(tag).toBe("Recorded");
214 }),
215 );
216 });
217
218 describe("PutActions", () => {
219 test.provider("surfaces a typed tag when the bound dataset is not an Actions dataset", () =>
220 Effect.gen(function* () {
221 const tag = yield* routeTag("/put-actions-probe");
222 expect(EXPECTED_PROBE_TAGS).toContain(tag);
223 }),
224 );
225 });
226
227 describe("PutActionInteractions", () => {
228 test.provider("surfaces a typed tag without an Action interactions dataset", () =>
229 Effect.gen(function* () {
230 const tag = yield* routeTag("/put-action-interactions-probe");
231 expect(EXPECTED_PROBE_TAGS).toContain(tag);
232 }),
233 );
234 });
235
236 describe("GetRecommendations", () => {
237 test.provider("surfaces a typed tag for a nonexistent campaign", () =>
238 Effect.gen(function* () {
239 const arns = yield* probeArns;
240 const tag = yield* probeTag("/recommendations-probe", arns.campaign);
241 expect(EXPECTED_PROBE_TAGS).toContain(tag);
242 }),
243 );
244 });
245
246 describe("GetPersonalizedRanking", () => {
247 test.provider("surfaces a typed tag for a nonexistent campaign", () =>
248 Effect.gen(function* () {
249 const arns = yield* probeArns;
250 const tag = yield* probeTag("/ranking-probe", arns.campaign);
251 expect(EXPECTED_PROBE_TAGS).toContain(tag);
252 }),
253 );
254 });
255
256 describe("GetActionRecommendations", () => {
257 test.provider("surfaces a typed tag for a nonexistent campaign", () =>
258 Effect.gen(function* () {
259 const arns = yield* probeArns;
260 const tag = yield* probeTag("/action-recommendations-probe", arns.campaign);
261 expect(EXPECTED_PROBE_TAGS).toContain(tag);
262 }),
263 );
264 });
265
266 describe("CreateDatasetImportJob", () => {
267 test.provider("rejects a nonexistent dataset with a typed tag", () =>
268 Effect.gen(function* () {
269 const arns = yield* probeArns;
270 const tag = yield* probeTagWithRole("/import-create-probe", arns.dataset);
271 expect(EXPECTED_PROBE_TAGS).toContain(tag);
272 }),
273 );
274 });
275
276 describe("DescribeDatasetImportJob", () => {
277 test.provider("surfaces a typed tag for a nonexistent import job", () =>
278 Effect.gen(function* () {
279 const arns = yield* probeArns;
280 const tag = yield* probeTag("/import-probe", arns.importJob);
281 expect(EXPECTED_PROBE_TAGS).toContain(tag);
282 }),
283 );
284 });
285
286 describe("CreateSolution", () => {
287 test.provider("rejects a nonexistent dataset group with a typed tag", () =>
288 Effect.gen(function* () {
289 const arns = yield* probeArns;
290 const tag = yield* probeTag("/solution-create-probe", arns.datasetGroup);
291 expect(EXPECTED_PROBE_TAGS).toContain(tag);
292 }),
293 );
294 });
295
296 describe("CreateSolutionVersion", () => {
297 test.provider("rejects a nonexistent solution with a typed tag", () =>
298 Effect.gen(function* () {
299 const arns = yield* probeArns;
300 const tag = yield* probeTag("/solution-version-create-probe", arns.solution);
301 expect(EXPECTED_PROBE_TAGS).toContain(tag);
302 }),
303 );
304 });
305
306 describe("DescribeSolutionVersion", () => {
307 test.provider("surfaces a typed tag for a nonexistent version", () =>
308 Effect.gen(function* () {
309 const arns = yield* probeArns;
310 const tag = yield* probeTag("/solution-version-probe", arns.solutionVersion);
311 expect(EXPECTED_PROBE_TAGS).toContain(tag);
312 }),
313 );
314 });
315
316 describe("GetSolutionMetrics", () => {
317 test.provider("surfaces a typed tag for a nonexistent version", () =>
318 Effect.gen(function* () {
319 const arns = yield* probeArns;
320 const tag = yield* probeTag("/solution-metrics-probe", arns.solutionVersion);
321 expect(EXPECTED_PROBE_TAGS).toContain(tag);
322 }),
323 );
324 });
325
326 describe("CreateCampaign", () => {
327 test.provider("rejects a nonexistent solution version with a typed tag", () =>
328 Effect.gen(function* () {
329 const arns = yield* probeArns;
330 const tag = yield* probeTag("/campaign-create-probe", arns.solutionVersion);
331 expect(EXPECTED_PROBE_TAGS).toContain(tag);
332 }),
333 );
334 });
335
336 describe("UpdateCampaign", () => {
337 test.provider("surfaces a typed tag for a nonexistent campaign", () =>
338 Effect.gen(function* () {
339 const arns = yield* probeArns;
340 const tag = yield* probeTag("/campaign-update-probe", arns.campaign);
341 expect(EXPECTED_PROBE_TAGS).toContain(tag);
342 }),
343 );
344 });
345
346 describe("DescribeCampaign", () => {
347 test.provider("surfaces a typed tag for a nonexistent campaign", () =>
348 Effect.gen(function* () {
349 const arns = yield* probeArns;
350 const tag = yield* probeTag("/campaign-probe", arns.campaign);
351 expect(EXPECTED_PROBE_TAGS).toContain(tag);
352 }),
353 );
354 });
355
356 describe("CreateBatchInferenceJob", () => {
357 test.provider("rejects a nonexistent solution version with a typed tag", () =>
358 Effect.gen(function* () {
359 const arns = yield* probeArns;
360 const tag = yield* probeTagWithRole("/batch-create-probe", arns.solutionVersion);
361 expect(EXPECTED_PROBE_TAGS).toContain(tag);
362 }),
363 );
364 });
365
366 describe("DescribeBatchInferenceJob", () => {
367 test.provider("surfaces a typed tag for a nonexistent batch job", () =>
368 Effect.gen(function* () {
369 const arns = yield* probeArns;
370 const tag = yield* probeTag("/batch-probe", arns.batchJob);
371 expect(EXPECTED_PROBE_TAGS).toContain(tag);
372 }),
373 );
374 });
375 },
376 );