@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 244 lines 9.44 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 AccessAnalyzerTestFunctionLive, { AccessAnalyzerTestFunction } from "./handler";
11 import { makeAccessAnalyzerTestLease } from "./TestLease.ts";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "AccessAnalyzerBindings");
16 const testLease = makeAccessAnalyzerTestLease();
17
18 beforeAll(testLease.acquire, { timeout: 240_000 });
19 afterAll(testLease.release);
20
21 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
22 // over 60s on a fresh deploy under parallel-suite load.
23 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
24
25 let baseUrl: string;
26 let functionRoleArn: string;
27
28 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
29 readonly status: number;
30 readonly body: string;
31 }> {}
32
33 // The shared Lambda fixture occasionally answers a transient 5xx under
34 // full-suite parallel load (cold re-init, IAM propagation on the freshly
35 // attached access-analyzer policy that the handler's `Effect.orDie` surfaces
36 // as a 500). Retry only 5xx; a genuine 4xx/assertion failure surfaces
37 // immediately.
38 const send = (request: HttpClientRequest.HttpClientRequest) =>
39 HttpClient.execute(request).pipe(
40 Effect.flatMap((response) =>
41 response.status >= 500
42 ? response.text.pipe(
43 Effect.flatMap((body) =>
44 Effect.fail(new TransientUpstream({ status: response.status, body })),
45 ),
46 )
47 : Effect.succeed(response),
48 ),
49 Effect.retry({
50 while: (e) => e._tag === "TransientUpstream",
51 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
52 }),
53 );
54
55 describe.sequential(
56 "AccessAnalyzer Bindings",
57 { tags: ["provider:aws", "provider:aws:accessanalyzer", "provider:aws:lambda", "live"] },
58 () => {
59 beforeAll(
60 Effect.gen(function* () {
61 yield* Effect.logInfo("AccessAnalyzer test setup: destroying previous resources");
62 yield* sharedStack.destroy();
63
64 yield* Effect.logInfo("AccessAnalyzer test setup: deploying fixture");
65 const { functionUrl, roleArn } = yield* sharedStack.deploy(
66 Effect.gen(function* () {
67 return yield* AccessAnalyzerTestFunction;
68 }).pipe(Effect.provide(AccessAnalyzerTestFunctionLive)),
69 );
70
71 expect(functionUrl).toBeTruthy();
72 baseUrl = functionUrl!.replace(/\/+$/, "");
73 functionRoleArn = roleArn;
74
75 const readinessUrl = `${baseUrl}/bindings`;
76 yield* Effect.logInfo(`AccessAnalyzer test setup: probing readiness at ${readinessUrl}`);
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(
85 `AccessAnalyzer test setup: fixture not ready yet (${String(error)})`,
86 ),
87 ),
88 Effect.retry({ schedule: readinessPolicy }),
89 );
90 }),
91 { timeout: 240_000 },
92 );
93
94 afterAll(sharedStack.destroy(), { timeout: 120_000 });
95
96 describe("binding registration", () => {
97 test.provider("all 24 capabilities initialize in the runtime", (_stack) =>
98 Effect.gen(function* () {
99 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
100 Effect.flatMap((r) => r.json),
101 );
102 expect((response as any).bound).toHaveLength(24);
103 }),
104 );
105 });
106
107 describe("ValidatePolicy", () => {
108 test.provider("validates a well-formed identity policy", (_stack) =>
109 Effect.gen(function* () {
110 const response = yield* send(HttpClientRequest.post(`${baseUrl}/validate-policy`)).pipe(
111 Effect.flatMap((r) => r.json),
112 );
113 // The sample policy is syntactically valid — no ERROR findings.
114 expect((response as any).findingTypes).not.toContain("ERROR");
115 }),
116 );
117 });
118
119 describe("CheckNoNewAccess", () => {
120 test.provider("passes when the new policy is a subset of the existing one", (_stack) =>
121 Effect.gen(function* () {
122 const response = yield* send(
123 HttpClientRequest.post(`${baseUrl}/check-no-new-access`),
124 ).pipe(Effect.flatMap((r) => r.json));
125 expect((response as any).result).toBe("PASS");
126 }),
127 );
128 });
129
130 describe("CheckAccessNotGranted", () => {
131 test.provider("passes when the policy does not grant the checked action", (_stack) =>
132 Effect.gen(function* () {
133 const response = yield* send(
134 HttpClientRequest.post(`${baseUrl}/check-access-not-granted`),
135 ).pipe(Effect.flatMap((r) => r.json));
136 expect((response as any).result).toBe("PASS");
137 }),
138 );
139 });
140
141 describe("CheckNoPublicAccess", () => {
142 test.provider("passes for a bucket policy scoped to a single account", (_stack) =>
143 Effect.gen(function* () {
144 const response = yield* send(
145 HttpClientRequest.post(`${baseUrl}/check-no-public-access`),
146 ).pipe(Effect.flatMap((r) => r.json));
147 expect((response as any).result).toBe("PASS");
148 }),
149 );
150 });
151
152 describe("ListFindingsV2", () => {
153 test.provider("lists the fixture analyzer's findings", (_stack) =>
154 Effect.gen(function* () {
155 const response = yield* send(HttpClientRequest.get(`${baseUrl}/list-findings`)).pipe(
156 Effect.flatMap((r) => r.json),
157 );
158 // A fresh unused-access analyzer typically has zero findings —
159 // assert the call round-trips with a well-formed page.
160 expect(typeof (response as any).count).toBe("number");
161 }),
162 );
163 });
164
165 describe("GetFindingV2", () => {
166 test.provider("returns the typed not-found error for an unknown finding id", (_stack) =>
167 Effect.gen(function* () {
168 const response = yield* send(
169 HttpClientRequest.get(`${baseUrl}/get-finding-not-found`),
170 ).pipe(Effect.flatMap((r) => r.json));
171 expect((response as any).found).toBe(false);
172 }),
173 );
174 });
175
176 describe("ListAnalyzedResources", () => {
177 test.provider("lists the analyzer's analyzed resources", (_stack) =>
178 Effect.gen(function* () {
179 const response = yield* send(
180 HttpClientRequest.get(`${baseUrl}/list-analyzed-resources`),
181 ).pipe(Effect.flatMap((r) => r.json));
182 expect(typeof (response as any).count).toBe("number");
183 }),
184 );
185 });
186
187 describe("GetFindingsStatistics", () => {
188 test.provider("returns the analyzer's statistics", (_stack) =>
189 Effect.gen(function* () {
190 const response = yield* send(
191 HttpClientRequest.get(`${baseUrl}/findings-statistics`),
192 ).pipe(Effect.flatMap((r) => r.json));
193 expect(Array.isArray((response as any).statistics)).toBe(true);
194 }),
195 );
196 });
197
198 describe("ApplyArchiveRule", () => {
199 test.provider("applies the fixture archive rule", (_stack) =>
200 Effect.gen(function* () {
201 const response = yield* send(
202 HttpClientRequest.post(`${baseUrl}/apply-archive-rule`),
203 ).pipe(Effect.flatMap((r) => r.json));
204 expect((response as any).applied).toBe(true);
205 }),
206 );
207 });
208
209 describe("ListPolicyGenerations", () => {
210 test.provider("lists the account's policy generations", (_stack) =>
211 Effect.gen(function* () {
212 const response = yield* send(
213 HttpClientRequest.get(`${baseUrl}/list-policy-generations`),
214 ).pipe(Effect.flatMap((r) => r.json));
215 expect(typeof (response as any).count).toBe("number");
216 }),
217 );
218 });
219
220 describe("StartPolicyGeneration / GetGeneratedPolicy / CancelPolicyGeneration", () => {
221 // A real policy generation needs a CloudTrail trail + access role (the
222 // live API rejects trail-less starts with ValidationException "Missing
223 // cloudTrailDetails"). The fixture drives all three bindings through
224 // their typed ValidationException paths — an IAM gap would surface
225 // AccessDeniedException instead, so these tags prove the grants and the
226 // typed error unions end-to-end.
227 test.provider(
228 "surfaces the typed ValidationException path for all three bindings",
229 (_stack) =>
230 Effect.gen(function* () {
231 const response = yield* send(
232 HttpClientRequest.bodyJsonUnsafe(
233 HttpClientRequest.post(`${baseUrl}/policy-generation`),
234 { principalArn: functionRoleArn },
235 ),
236 ).pipe(Effect.flatMap((r) => r.json));
237 expect(["Started", "ValidationException"]).toContain((response as any).startTag);
238 expect((response as any).getTag).toBe("ValidationException");
239 expect((response as any).cancelTag).toBe("ValidationException");
240 }),
241 );
242 });
243 },
244 );