The best way to control Claude Code, Codex and more from any computer or phone

main
ts 269 lines 8.93 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 LakeFormationTestFunctionLive, { LakeFormationTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "LakeFormationBindings");
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 // Retry only 5xx (cold re-init, IAM propagation); a genuine 4xx/assertion
28 // failure surfaces immediately.
29 const send = (request: HttpClientRequest.HttpClientRequest) =>
30 HttpClient.execute(request).pipe(
31 Effect.flatMap((response) =>
32 response.status >= 500
33 ? response.text.pipe(
34 Effect.flatMap((body) =>
35 Effect.fail(new TransientUpstream({ status: response.status, body })),
36 ),
37 )
38 : Effect.succeed(response),
39 ),
40 Effect.retry({
41 while: (e) => e._tag === "TransientUpstream",
42 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
43 }),
44 );
45
46 const getJson = (path: string) =>
47 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
48
49 describe.sequential(
50 "LakeFormation Bindings",
51 {
52 tags: [
53 "provider:aws",
54 "provider:aws:glue",
55 "provider:aws:lakeformation",
56 "provider:aws:lambda",
57 "live",
58 ],
59 },
60 () => {
61 beforeAll(
62 Effect.gen(function* () {
63 yield* Effect.logInfo("LakeFormation test setup: destroying previous resources");
64 yield* sharedStack.destroy();
65
66 yield* Effect.logInfo("LakeFormation test setup: deploying fixture");
67 const { functionUrl } = yield* sharedStack.deploy(
68 Effect.gen(function* () {
69 return yield* LakeFormationTestFunction;
70 }).pipe(Effect.provide(LakeFormationTestFunctionLive)),
71 );
72
73 expect(functionUrl).toBeTruthy();
74 baseUrl = functionUrl!.replace(/\/+$/, "");
75
76 const readinessUrl = `${baseUrl}/bindings`;
77 yield* HttpClient.get(readinessUrl).pipe(
78 Effect.flatMap((response) =>
79 response.status === 200
80 ? Effect.succeed(response)
81 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
82 ),
83 Effect.tapError((error) =>
84 Effect.logWarning(`LakeFormation test setup: fixture not ready yet (${String(error)})`),
85 ),
86 Effect.retry({ schedule: readinessPolicy }),
87 );
88 }),
89 { timeout: 240_000 },
90 );
91
92 afterAll(sharedStack.destroy(), { timeout: 120_000 });
93
94 describe("binding registration", () => {
95 test.provider("all 11 capabilities initialize in the runtime", (_stack) =>
96 Effect.gen(function* () {
97 const response = yield* getJson("/bindings");
98 expect((response as any).bound).toHaveLength(11);
99 }),
100 );
101 });
102
103 describe("GetDataLakePrincipal", () => {
104 test.provider(
105 "returns the calling principal",
106 (_stack) =>
107 Effect.gen(function* () {
108 const response = (yield* getJson("/principal")) as any;
109 expect(response.tag).toBe("Ok");
110 // the identity is the Lambda's assumed-role session
111 expect(response.identity).toBeTruthy();
112 }),
113 { timeout: 60_000 },
114 );
115 });
116
117 describe("ListLFTags", () => {
118 test.provider(
119 "lists visible tag definitions",
120 (_stack) =>
121 Effect.gen(function* () {
122 const response = (yield* getJson("/tags")) as any;
123 expect(response.tag).toBe("Ok");
124 expect(response.count).toBeGreaterThanOrEqual(0);
125 }),
126 { timeout: 60_000 },
127 );
128 });
129
130 describe("GetLFTag", () => {
131 test.provider(
132 "typed error for a missing tag key",
133 (_stack) =>
134 Effect.gen(function* () {
135 const response = (yield* getJson("/tag-missing")) as any;
136 expect(["EntityNotFoundException", "AccessDeniedException"]).toContain(response.tag);
137 }),
138 { timeout: 60_000 },
139 );
140 });
141
142 describe("ListPermissions", () => {
143 test.provider(
144 "lists grants visible to the caller",
145 (_stack) =>
146 Effect.gen(function* () {
147 const response = (yield* getJson("/permissions")) as any;
148 expect(response.tag).toBe("Ok");
149 expect(response.count).toBeGreaterThanOrEqual(0);
150 }),
151 { timeout: 60_000 },
152 );
153 });
154
155 describe("SearchDatabasesByLFTags", () => {
156 test.provider(
157 "typed rejection for a nonexistent tag key",
158 (_stack) =>
159 Effect.gen(function* () {
160 const response = (yield* getJson("/search-databases")) as any;
161 expect(["Ok", "EntityNotFoundException", "AccessDeniedException"]).toContain(
162 response.tag,
163 );
164 expect(response.count).toBe(0);
165 }),
166 { timeout: 60_000 },
167 );
168 });
169
170 describe("SearchTablesByLFTags", () => {
171 test.provider(
172 "typed rejection for a nonexistent tag key",
173 (_stack) =>
174 Effect.gen(function* () {
175 const response = (yield* getJson("/search-tables")) as any;
176 expect(["Ok", "EntityNotFoundException", "AccessDeniedException"]).toContain(
177 response.tag,
178 );
179 expect(response.count).toBe(0);
180 }),
181 { timeout: 60_000 },
182 );
183 });
184
185 describe("GetResourceLFTags", () => {
186 test.provider(
187 "reads the fixture database's LF-tags",
188 (_stack) =>
189 Effect.gen(function* () {
190 const response = (yield* getJson("/resource-tags")) as any;
191 // Ok (no tags assigned) under the default catalog settings; a
192 // Lake-Formation-locked catalog surfaces the typed denial instead.
193 expect(["Ok", "AccessDeniedException", "EntityNotFoundException"]).toContain(
194 response.tag,
195 );
196 }),
197 { timeout: 60_000 },
198 );
199 });
200
201 describe("GetEffectivePermissionsForPath", () => {
202 test.provider(
203 "empty permissions (or typed rejection) for an unregistered path",
204 (_stack) =>
205 Effect.gen(function* () {
206 const response = (yield* getJson("/effective-permissions")) as any;
207 // verified live: an unregistered path returns an empty permission
208 // list rather than EntityNotFound
209 expect(["Ok", "EntityNotFoundException", "InvalidInputException"]).toContain(
210 response.tag,
211 );
212 expect(response.count).toBe(0);
213 }),
214 { timeout: 60_000 },
215 );
216 });
217
218 describe("GetTemporaryGlueTableCredentials", () => {
219 test.provider(
220 "typed rejection proves lakeformation:GetDataAccess reaches the API",
221 (_stack) =>
222 Effect.gen(function* () {
223 const response = (yield* getJson("/table-credentials")) as any;
224 expect([
225 "AccessDeniedException",
226 "EntityNotFoundException",
227 "InvalidInputException",
228 "PermissionTypeMismatchException",
229 ]).toContain(response.tag);
230 }),
231 { timeout: 60_000 },
232 );
233 });
234
235 describe("GetTemporaryGluePartitionCredentials", () => {
236 test.provider(
237 "typed rejection proves lakeformation:GetDataAccess reaches the API",
238 (_stack) =>
239 Effect.gen(function* () {
240 const response = (yield* getJson("/partition-credentials")) as any;
241 expect([
242 "AccessDeniedException",
243 "EntityNotFoundException",
244 "InvalidInputException",
245 "PermissionTypeMismatchException",
246 ]).toContain(response.tag);
247 }),
248 { timeout: 60_000 },
249 );
250 });
251
252 describe("GetTemporaryDataLocationCredentials", () => {
253 test.provider(
254 "typed rejection for an unregistered location",
255 (_stack) =>
256 Effect.gen(function* () {
257 const response = (yield* getJson("/location-credentials")) as any;
258 expect([
259 "AccessDeniedException",
260 "EntityNotFoundException",
261 "InvalidInputException",
262 "ConflictException",
263 ]).toContain(response.tag);
264 }),
265 { timeout: 60_000 },
266 );
267 });
268 },
269 );