@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 297 lines 10.8 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 ControlTowerTestFunctionLive, { ControlTowerTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "ControlTowerBindings");
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 // The freshly attached IAM policy can take ~10-30s to propagate to the
49 // Lambda's role; until it does, granted calls surface a transient
50 // AccessDeniedException. Poll (bounded) until the response is no longer
51 // access-denied so grant-proof assertions see steady-state behavior.
52 const getJson = (path: string) =>
53 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
54 Effect.flatMap((r) => r.json),
55 Effect.repeat({
56 schedule: Schedule.spaced("3 seconds"),
57 until: (response): boolean => (response as { tag?: string }).tag !== "AccessDeniedException",
58 times: 10,
59 }),
60 );
61
62 // The testing account has no Control Tower landing zone — the account-level
63 // bindings still prove the IAM grant + typed error unions end-to-end: an IAM
64 // gap would surface AccessDeniedException, while a granted call returns
65 // either real data (the baseline catalog is served without a landing zone)
66 // or the landing-zone-gated typed tag.
67 describe.sequential(
68 "ControlTower Bindings",
69 { tags: ["provider:aws", "provider:aws:controltower", "provider:aws:lambda", "live"] },
70 () => {
71 beforeAll(
72 Effect.gen(function* () {
73 yield* Effect.logInfo("ControlTower test setup: destroying previous resources");
74 yield* sharedStack.destroy();
75
76 yield* Effect.logInfo("ControlTower test setup: deploying fixture");
77 const { functionUrl } = yield* sharedStack.deploy(
78 Effect.gen(function* () {
79 return yield* ControlTowerTestFunction;
80 }).pipe(Effect.provide(ControlTowerTestFunctionLive)),
81 );
82
83 expect(functionUrl).toBeTruthy();
84 baseUrl = functionUrl!.replace(/\/+$/, "");
85
86 const readinessUrl = `${baseUrl}/bindings`;
87 yield* Effect.logInfo(`ControlTower test setup: probing readiness at ${readinessUrl}`);
88 yield* HttpClient.get(readinessUrl).pipe(
89 Effect.flatMap((response) =>
90 response.status === 200
91 ? Effect.succeed(response)
92 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
93 ),
94 Effect.tapError((error) =>
95 Effect.logWarning(`ControlTower test setup: fixture not ready yet (${String(error)})`),
96 ),
97 Effect.retry({ schedule: readinessPolicy }),
98 );
99 }),
100 { timeout: 240_000 },
101 );
102
103 afterAll(sharedStack.destroy(), { timeout: 120_000 });
104
105 describe("binding registration", () => {
106 test.provider("all 11 capabilities initialize in the runtime", (_stack) =>
107 Effect.gen(function* () {
108 const response = (yield* getJson("/bindings")) as { bound: string[] };
109 expect(response.bound).toHaveLength(11);
110 }),
111 );
112 });
113
114 describe("ListBaselines", () => {
115 test.provider("lists the baseline catalog", (_stack) =>
116 Effect.gen(function* () {
117 const response = (yield* getJson("/baselines")) as
118 | { ok: true; names: (string | null)[] }
119 | { ok: false; tag: string };
120 if (response.ok) {
121 expect(response.names).toContain("AWSControlTowerBaseline");
122 } else {
123 expect(["AccessDeniedException", "UnauthorizedException"]).toContain(response.tag);
124 }
125 }),
126 );
127 });
128
129 describe("GetBaseline", () => {
130 test.provider("reads a catalog baseline discovered via list", (_stack) =>
131 Effect.gen(function* () {
132 const response = (yield* getJson("/baseline")) as
133 | { ok: true; name: string }
134 | { ok: false; tag: string };
135 if (response.ok) {
136 expect(response.name).toBe("AWSControlTowerBaseline");
137 } else {
138 expect(["AccessDeniedException", "UnauthorizedException", "NoCatalog"]).toContain(
139 response.tag,
140 );
141 }
142 }),
143 );
144 });
145
146 describe("ListEnabledBaselines", () => {
147 test.provider("yields a count or the landing-zone-gated typed tag", (_stack) =>
148 Effect.gen(function* () {
149 const response = (yield* getJson("/enabled-baselines")) as
150 | { ok: true; count: number }
151 | { ok: false; tag: string };
152 if (response.ok) {
153 expect(response.count).toBeGreaterThanOrEqual(0);
154 } else {
155 expect([
156 "AccessDeniedException",
157 "UnauthorizedException",
158 "ValidationException",
159 ]).toContain(response.tag);
160 }
161 }),
162 );
163 });
164
165 describe("ListEnabledControls", () => {
166 test.provider("yields a count or the landing-zone-gated typed tag", (_stack) =>
167 Effect.gen(function* () {
168 const response = (yield* getJson("/enabled-controls")) as
169 | { ok: true; count: number }
170 | { ok: false; tag: string };
171 if (response.ok) {
172 expect(response.count).toBeGreaterThanOrEqual(0);
173 } else {
174 expect([
175 "AccessDeniedException",
176 "ResourceNotFoundException",
177 "ValidationException",
178 ]).toContain(response.tag);
179 }
180 }),
181 );
182 });
183
184 describe("ListControlOperations", () => {
185 test.provider("yields a count or the landing-zone-gated typed tag", (_stack) =>
186 Effect.gen(function* () {
187 const response = (yield* getJson("/control-operations")) as
188 | { ok: true; count: number }
189 | { ok: false; tag: string };
190 if (response.ok) {
191 expect(response.count).toBeGreaterThanOrEqual(0);
192 } else {
193 expect(["AccessDeniedException", "ValidationException"]).toContain(response.tag);
194 }
195 }),
196 );
197 });
198
199 describe("ListLandingZones", () => {
200 test.provider("lists the account's landing zones (none)", (_stack) =>
201 Effect.gen(function* () {
202 const response = (yield* getJson("/landing-zones")) as
203 | { ok: true; count: number }
204 | { ok: false; tag: string };
205 if (response.ok) {
206 // No landing zone on the testing account — an empty (or
207 // singleton) list, never a crash.
208 expect(response.count).toBeLessThanOrEqual(1);
209 } else {
210 expect(["AccessDeniedException", "UnauthorizedException"]).toContain(response.tag);
211 }
212 }),
213 );
214 });
215
216 describe("ListLandingZoneOperations", () => {
217 test.provider("yields a count or the landing-zone-gated typed tag", (_stack) =>
218 Effect.gen(function* () {
219 const response = (yield* getJson("/landing-zone-operations")) as
220 | { ok: true; count: number }
221 | { ok: false; tag: string };
222 if (response.ok) {
223 expect(response.count).toBeGreaterThanOrEqual(0);
224 } else {
225 expect([
226 "AccessDeniedException",
227 "UnauthorizedException",
228 "ValidationException",
229 ]).toContain(response.tag);
230 }
231 }),
232 );
233 });
234
235 describe("GetLandingZone", () => {
236 test.provider(
237 "surfaces a typed error for a nonexistent landing zone (proving the grant)",
238 (_stack) =>
239 Effect.gen(function* () {
240 const response = (yield* getJson("/landing-zone-not-found")) as { tag: string };
241 expect([
242 "ResourceNotFoundException",
243 "ValidationException",
244 "UnauthorizedException",
245 ]).toContain(response.tag);
246 }),
247 );
248 });
249
250 describe("GetControlOperation", () => {
251 test.provider(
252 "surfaces a typed error for a nonexistent operation (proving the grant)",
253 (_stack) =>
254 Effect.gen(function* () {
255 const response = (yield* getJson("/control-operation-not-found")) as { tag: string };
256 expect(["ResourceNotFoundException", "ValidationException"]).toContain(response.tag);
257 }),
258 );
259 });
260
261 describe("GetBaselineOperation", () => {
262 test.provider(
263 "surfaces a typed error for a nonexistent operation (proving the grant)",
264 (_stack) =>
265 Effect.gen(function* () {
266 const response = (yield* getJson("/baseline-operation-not-found")) as {
267 tag: string;
268 message?: string;
269 };
270 expect([
271 "ResourceNotFoundException",
272 "ValidationException",
273 "UnauthorizedException",
274 ]).toContain(response.tag);
275 }),
276 );
277 });
278
279 describe("GetLandingZoneOperation", () => {
280 test.provider(
281 "surfaces a typed error for a nonexistent operation (proving the grant)",
282 (_stack) =>
283 Effect.gen(function* () {
284 const response = (yield* getJson("/landing-zone-operation-not-found")) as {
285 tag: string;
286 message?: string;
287 };
288 expect([
289 "ResourceNotFoundException",
290 "ValidationException",
291 "UnauthorizedException",
292 ]).toContain(response.tag);
293 }),
294 );
295 });
296 },
297 );