@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 245 lines 8.29 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import RAMTestFunctionLive, { RAMTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "RAMBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The shared Lambda fixture occasionally answers a transient 5xx under load
28 // (cold re-init, IAM propagation on the freshly attached policy that the
29 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
30 // 4xx/assertion failure surfaces immediately.
31 const send = (request: HttpClientRequest.HttpClientRequest) =>
32 HttpClient.execute(request).pipe(
33 Effect.flatMap((response) =>
34 response.status >= 500
35 ? response.text.pipe(
36 Effect.flatMap((body) =>
37 Effect.fail(new TransientUpstream({ status: response.status, body })),
38 ),
39 )
40 : Effect.succeed(response),
41 ),
42 Effect.retry({
43 while: (e) => e._tag === "TransientUpstream",
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
45 }),
46 );
47
48 const getJson = (path: string) =>
49 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
50
51 describe.sequential(
52 "RAM Bindings",
53 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:ram", "live"] },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* Effect.logInfo("RAM test setup: destroying previous resources");
58 yield* sharedStack.destroy();
59
60 yield* Effect.logInfo("RAM test setup: deploying fixture");
61 const { functionUrl } = yield* sharedStack.deploy(
62 Effect.gen(function* () {
63 return yield* RAMTestFunction;
64 }).pipe(Effect.provide(RAMTestFunctionLive)),
65 );
66
67 expect(functionUrl).toBeTruthy();
68 baseUrl = functionUrl!.replace(/\/+$/, "");
69
70 const readinessUrl = `${baseUrl}/bindings`;
71 yield* Effect.logInfo(`RAM test setup: probing readiness at ${readinessUrl}`);
72 yield* HttpClient.get(readinessUrl).pipe(
73 Effect.flatMap((response) =>
74 response.status === 200
75 ? Effect.succeed(response)
76 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
77 ),
78 Effect.tapError((error) =>
79 Effect.logWarning(`RAM test setup: fixture not ready yet (${String(error)})`),
80 ),
81 Effect.retry({ schedule: readinessPolicy }),
82 );
83 }),
84 { timeout: 240_000 },
85 );
86
87 afterAll(sharedStack.destroy(), { timeout: 120_000 });
88
89 describe("binding registration", () => {
90 test.provider("all 11 capabilities initialize in the runtime", (_stack) =>
91 Effect.gen(function* () {
92 const response = yield* getJson("/bindings");
93 expect((response as any).bound).toHaveLength(11);
94 }),
95 );
96 });
97
98 describe("GetResourceShares", () => {
99 test.provider(
100 "lists the account's shares including the fixture share",
101 (_stack) =>
102 Effect.gen(function* () {
103 const response = (yield* getJson("/shares")) as any;
104 expect(response.count).toBeGreaterThan(0);
105 }),
106 { timeout: 60_000 },
107 );
108 });
109
110 describe("GetResourceShareAssociations", () => {
111 test.provider(
112 "lists the fixture share's principal associations",
113 (_stack) =>
114 Effect.gen(function* () {
115 const response = (yield* getJson("/associations")) as any;
116 expect(response.tag).toBe("Ok");
117 expect(response.count).toBeGreaterThanOrEqual(0);
118 }),
119 { timeout: 60_000 },
120 );
121 });
122
123 describe("GetResourceShareInvitations", () => {
124 test.provider(
125 "lists the invitations sent to this account",
126 (_stack) =>
127 Effect.gen(function* () {
128 const response = (yield* getJson("/invitations")) as any;
129 expect(response.count).toBeGreaterThanOrEqual(0);
130 }),
131 { timeout: 60_000 },
132 );
133 });
134
135 describe("AcceptResourceShareInvitation", () => {
136 test.provider(
137 "surfaces the typed not-found for a nonexistent invitation (proving the grant)",
138 (_stack) =>
139 Effect.gen(function* () {
140 const response = (yield* getJson("/accept-nonexistent")) as any;
141 expect([
142 "ResourceShareInvitationArnNotFoundException",
143 "MalformedArnException",
144 ]).toContain(response.tag);
145 }),
146 { timeout: 60_000 },
147 );
148 });
149
150 describe("RejectResourceShareInvitation", () => {
151 test.provider(
152 "surfaces the typed not-found for a nonexistent invitation (proving the grant)",
153 (_stack) =>
154 Effect.gen(function* () {
155 const response = (yield* getJson("/reject-nonexistent")) as any;
156 expect([
157 "ResourceShareInvitationArnNotFoundException",
158 "MalformedArnException",
159 ]).toContain(response.tag);
160 }),
161 { timeout: 60_000 },
162 );
163 });
164
165 describe("ListPendingInvitationResources", () => {
166 test.provider(
167 "surfaces the typed not-found for a nonexistent invitation (proving the grant)",
168 (_stack) =>
169 Effect.gen(function* () {
170 const response = (yield* getJson("/pending-resources-nonexistent")) as any;
171 expect([
172 "ResourceShareInvitationArnNotFoundException",
173 "MalformedArnException",
174 "InvalidParameterException",
175 ]).toContain(response.tag);
176 }),
177 { timeout: 60_000 },
178 );
179 });
180
181 describe("ListResources", () => {
182 test.provider(
183 "lists the resources this account shares",
184 (_stack) =>
185 Effect.gen(function* () {
186 const response = (yield* getJson("/resources")) as any;
187 expect(response.count).toBeGreaterThanOrEqual(0);
188 }),
189 { timeout: 60_000 },
190 );
191 });
192
193 describe("ListPrincipals", () => {
194 test.provider(
195 "lists the principals this account shares with",
196 (_stack) =>
197 Effect.gen(function* () {
198 const response = (yield* getJson("/principals")) as any;
199 expect(response.count).toBeGreaterThanOrEqual(0);
200 }),
201 { timeout: 60_000 },
202 );
203 });
204
205 describe("GetResourcePolicies", () => {
206 test.provider(
207 "answers for a nonexistent resource (typed not-found or empty, proving the grant)",
208 (_stack) =>
209 Effect.gen(function* () {
210 const response = (yield* getJson("/resource-policies-nonexistent")) as any;
211 expect(["Ok", "ResourceArnNotFoundException", "MalformedArnException"]).toContain(
212 response.tag,
213 );
214 }),
215 { timeout: 60_000 },
216 );
217 });
218
219 describe("ListPermissions", () => {
220 test.provider(
221 "lists the AWS managed permissions",
222 (_stack) =>
223 Effect.gen(function* () {
224 const response = (yield* getJson("/permissions")) as any;
225 // Every account sees the AWS managed permissions.
226 expect(response.count).toBeGreaterThan(0);
227 }),
228 { timeout: 60_000 },
229 );
230 });
231
232 describe("GetPermission", () => {
233 test.provider(
234 "fetches the first listed permission",
235 (_stack) =>
236 Effect.gen(function* () {
237 const response = (yield* getJson("/permission")) as any;
238 expect(response.tag).toBe("Ok");
239 expect(typeof response.name).toBe("string");
240 }),
241 { timeout: 60_000 },
242 );
243 });
244 },
245 );