@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 112 lines 4.74 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AccessPolicy } from "@alchemy.run/aws/OpenSearchServerless";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as aoss from "@distilled.cloud/aws/opensearchserverless";
6 import * as Output from "alchemy/Output";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import AossIndexFunctionLive, {
12 AossIndexFunction,
13 COLLECTION_NAME,
14 } from "./index-bindings-handler";
15
16 const { test } = Test.make({ providers: AWS.providers() });
17
18 const ACC_POLICY = "alchemy-aossb-acc";
19
20 // A route answering 5xx is retried: the fixture's data access policy and IAM
21 // grants propagate asynchronously after deploy (typically well under a
22 // minute), and the handler's Effect.orDie surfaces AccessDenied as a 500.
23 const drive = (request: HttpClientRequest.HttpClientRequest) =>
24 HttpClient.execute(request).pipe(
25 Effect.flatMap((response) =>
26 response.status === 200
27 ? Effect.succeed(response)
28 : response.text.pipe(
29 Effect.flatMap((body) =>
30 Effect.fail(new Error(`route not ready: ${response.status} ${body}`)),
31 ),
32 ),
33 ),
34 Effect.retry({
35 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]),
36 }),
37 );
38
39 // Collections have a real-money OCU floor and take ~1-5 min to provision, so
40 // the index bindings (aoss:{Create,Get,Update,Delete}Index + APIAccessAll) are skipped under
41 // `--fast`, destroy IMMEDIATELY, and verify the collection is gone.
42 test.provider.skipIf(!!process.env.FAST)(
43 "index bindings roundtrip against a live collection",
44 (stack) =>
45 Effect.gen(function* () {
46 // Clean slate in case a previous run died mid-flight. Set
47 // AOSS_KEEP_COLLECTION=1 to skip it and converge onto the surviving
48 // resources instead (collections take minutes to provision, so reusing
49 // one keeps the re-run inside the runner's wall clock).
50 if (!process.env.AOSS_KEEP_COLLECTION) {
51 yield* stack.destroy();
52 }
53
54 const { fn } = yield* stack.deploy(
55 Effect.gen(function* () {
56 const fn = yield* AossIndexFunction;
57 // The data access policy granting the function's role index
58 // permissions — the control-plane index APIs enforce it in
59 // addition to the IAM aoss:{Create,Get,Update,Delete}Index grants.
60 yield* AccessPolicy("Acc", {
61 policyName: ACC_POLICY,
62 policy: Output.interpolate`[{"Rules":[{"ResourceType":"index","Resource":["index/${COLLECTION_NAME}/*"],"Permission":["aoss:*"]},{"ResourceType":"collection","Resource":["collection/${COLLECTION_NAME}"],"Permission":["aoss:*"]}],"Principal":["${fn.roleArn}"]}]`,
63 });
64 return { fn };
65 }).pipe(Effect.provide(AossIndexFunctionLive)),
66 );
67
68 expect(fn.functionUrl).toBeTruthy();
69 const baseUrl = fn.functionUrl!.replace(/\/+$/, "");
70
71 // GetCollection — the bound collection is ACTIVE with an endpoint.
72 const collection = (yield* drive(HttpClientRequest.get(`${baseUrl}/collection`)).pipe(
73 Effect.flatMap((r) => r.json),
74 )) as any;
75 expect(collection.status).toBe("ACTIVE");
76 expect(collection.endpoint).toContain("aoss.amazonaws.com");
77
78 // Create → read → update → delete an index at runtime.
79 const roundtrip = (yield* drive(HttpClientRequest.post(`${baseUrl}/index/roundtrip`)).pipe(
80 Effect.flatMap((r) => r.json),
81 )) as any;
82 expect(roundtrip.created).toBe(true);
83 expect(roundtrip.hadSchema).toBe(true);
84 expect(roundtrip.deleted).toBe(true);
85
86 // Destroy immediately — collections meter OCUs while they exist — and
87 // verify deletion out-of-band.
88 const collectionId = collection.id as string;
89 yield* stack.destroy();
90 yield* assertCollectionGone(collectionId);
91 }),
92 {
93 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:opensearchserverless", "live"],
94 timeout: 900_000,
95 },
96 );
97
98 // Deletion is verified as INITIATED (status `DELETING`) or fully gone — full
99 // disappearance takes another ~1-2 min server-side.
100 const assertCollectionGone = (id: string) =>
101 Effect.gen(function* () {
102 const response = yield* aoss.batchGetCollection({ ids: [id] });
103 const detail = response.collectionDetails?.[0];
104 const status = detail?.status ?? "gone";
105 if (status !== "gone" && status !== "DELETING") {
106 return yield* Effect.fail(new Error(`Collection '${id}' still exists (status: ${status})`));
107 }
108 }).pipe(
109 Effect.retry({
110 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]),
111 }),
112 );