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

main
ts 144 lines 4.9 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 securitylake from "@distilled.cloud/aws/securitylake";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Result from "effect/Result";
9 import * as Schedule from "effect/Schedule";
10 import SecurityLakeBindingsFunctionLive, { SecurityLakeBindingsFunction } from "./bindings-handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14
15 // Enabling Security Lake onboards the whole account in the Region (S3
16 // buckets, Lake Formation, Glue metastore), so the live Lambda E2E is gated
17 // behind AWS_TEST_SECURITYLAKE=1 like the lifecycle test.
18 const RUN_LIVE = !!process.env.AWS_TEST_SECURITYLAKE;
19
20 // Ungated typed-error probes: prove the distilled error unions the bindings
21 // depend on are typed on every account, onboarded or not, at near-zero cost.
22 test.provider(
23 "listDataLakeExceptions returns exceptions or a typed not-onboarded rejection",
24 () =>
25 Effect.gen(function* () {
26 const result = yield* Effect.result(securitylake.listDataLakeExceptions({}));
27 if (Result.isSuccess(result)) {
28 expect(Array.isArray(result.success.exceptions ?? [])).toBe(true);
29 } else {
30 expect([
31 "AccessDeniedException",
32 "ResourceNotFoundException",
33 "UnauthorizedException",
34 ]).toContain(result.failure._tag);
35 }
36 }),
37 { tags: ["provider:aws", "provider:aws:securitylake", "live"] },
38 );
39
40 test.provider(
41 "getDataLakeSources returns sources or a typed not-onboarded rejection",
42 () =>
43 Effect.gen(function* () {
44 const result = yield* Effect.result(securitylake.getDataLakeSources({}));
45 if (Result.isSuccess(result)) {
46 expect(Array.isArray(result.success.dataLakeSources ?? [])).toBe(true);
47 } else {
48 expect([
49 "AccessDeniedException",
50 "ResourceNotFoundException",
51 "UnauthorizedException",
52 ]).toContain(result.failure._tag);
53 }
54 }),
55 { tags: ["provider:aws", "provider:aws:securitylake", "live"] },
56 );
57
58 const sharedStack = Core.scratchStack(testOptions, "SecurityLakeBindings");
59
60 let baseUrl: string;
61
62 const get = (path: string) =>
63 HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
64
65 describe(
66 "SecurityLake Bindings (E2E)",
67 {
68 tags: [
69 "provider:aws",
70 "provider:aws:iam",
71 "provider:aws:lambda",
72 "provider:aws:securitylake",
73 "live",
74 ],
75 },
76 () => {
77 beforeAll(
78 Effect.gen(function* () {
79 if (!RUN_LIVE) return;
80 yield* Effect.logInfo("SecurityLake E2E setup: destroying previous run");
81 yield* sharedStack.destroy();
82
83 yield* Effect.logInfo("SecurityLake E2E setup: deploying data lake + Lambda");
84 const { functionUrl } = yield* sharedStack.deploy(
85 Effect.gen(function* () {
86 return yield* SecurityLakeBindingsFunction;
87 }).pipe(Effect.provide(SecurityLakeBindingsFunctionLive)),
88 );
89
90 expect(functionUrl).toBeTruthy();
91 baseUrl = functionUrl!.replace(/\/+$/, "");
92
93 // Readiness probe — fresh function URLs take seconds to serve 200s.
94 yield* HttpClient.get(`${baseUrl}/bindings`).pipe(
95 Effect.flatMap((response) =>
96 response.status === 200
97 ? Effect.succeed(response)
98 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
99 ),
100 Effect.retry({
101 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]),
102 }),
103 );
104 }),
105 { timeout: 600_000 },
106 );
107 afterAll(
108 Effect.gen(function* () {
109 if (!RUN_LIVE) return;
110 yield* sharedStack.destroy();
111 }),
112 { timeout: 600_000 },
113 );
114
115 test.provider.skipIf(!RUN_LIVE)("both capabilities initialize in the runtime", () =>
116 Effect.gen(function* () {
117 const response = (yield* get("/bindings")) as { bound: string[] };
118 expect(response.bound).toHaveLength(2);
119 }),
120 );
121
122 test.provider.skipIf(!RUN_LIVE)(
123 "ListDataLakeExceptions grant + call round-trips from the Lambda",
124 () =>
125 Effect.gen(function* () {
126 const response = (yield* get("/exceptions")) as { count: number };
127 expect(response.count).toBeGreaterThanOrEqual(0);
128 }),
129 );
130
131 test.provider.skipIf(!RUN_LIVE)(
132 "GetDataLakeSources grant + call round-trips from the Lambda",
133 () =>
134 Effect.gen(function* () {
135 const response = (yield* get("/sources")) as {
136 dataLakeArn: string | undefined;
137 count: number;
138 };
139 expect(response.dataLakeArn).toContain(":securitylake:");
140 expect(response.count).toBeGreaterThanOrEqual(0);
141 }),
142 );
143 },
144 );