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

main
ts 247 lines 8.85 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 FMSTestFunctionLive, { FMSTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "FMSBindings");
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 // The testing organization is deliberately NOT onboarded to Firewall Manager
52 // (see AdminAccount.test.ts — offboarding is blocked for >10 minutes after
53 // onboarding, so the AdminAccount lifecycle is gated behind AWS_TEST_FMS).
54 // Each route therefore proves its binding + IAM grant by observing either
55 // real data (an FMS-onboarded account) or the exact typed
56 // not-an-FMS-admin rejection captured by live probe. A genuine IAM gap
57 // surfaces as an uncaught "not authorized to perform" AccessDeniedException
58 // that 500s the route and fails the test.
59 describe.sequential(
60 "FMS Bindings",
61 {
62 tags: ["provider:aws", "provider:aws:batch", "provider:aws:fms", "provider:aws:lambda", "live"],
63 },
64 () => {
65 beforeAll(
66 Effect.gen(function* () {
67 yield* Effect.logInfo("FMS test setup: destroying previous resources");
68 yield* sharedStack.destroy();
69
70 yield* Effect.logInfo("FMS test setup: deploying fixture");
71 const { functionUrl } = yield* sharedStack.deploy(
72 Effect.gen(function* () {
73 return yield* FMSTestFunction;
74 }).pipe(Effect.provide(FMSTestFunctionLive)),
75 );
76
77 expect(functionUrl).toBeTruthy();
78 baseUrl = functionUrl!.replace(/\/+$/, "");
79
80 const readinessUrl = `${baseUrl}/bindings`;
81 yield* Effect.logInfo(`FMS test setup: probing readiness at ${readinessUrl}`);
82 yield* HttpClient.get(readinessUrl).pipe(
83 Effect.flatMap((response) =>
84 response.status === 200
85 ? Effect.succeed(response)
86 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
87 ),
88 Effect.tapError((error) =>
89 Effect.logWarning(`FMS test setup: fixture not ready yet (${String(error)})`),
90 ),
91 Effect.retry({ schedule: readinessPolicy }),
92 );
93 }),
94 { timeout: 240_000 },
95 );
96
97 afterAll(sharedStack.destroy(), { timeout: 120_000 });
98
99 describe("binding registration", () => {
100 test.provider("all 35 capabilities initialize in the runtime", (_stack) =>
101 Effect.gen(function* () {
102 const response = yield* getJson("/bindings");
103 expect((response as any).bound).toHaveLength(35);
104 }),
105 );
106 });
107
108 describe("ListAdminsManagingAccount", () => {
109 test.provider(
110 "lists the admins managing this account (typed ResourceNotFoundException when none exist)",
111 (_stack) =>
112 Effect.gen(function* () {
113 const response = (yield* getJson("/admins-managing-account")) as any;
114 expect(["Ok", "ResourceNotFoundException"]).toContain(response.tag);
115 expect(response.count).toBeGreaterThanOrEqual(0);
116 }),
117 { timeout: 60_000 },
118 );
119 });
120
121 describe("ListAdminAccountsForOrganization", () => {
122 test.provider(
123 "lists the organization's FMS admins (typed InvalidOperationException when none designated)",
124 (_stack) =>
125 Effect.gen(function* () {
126 const response = (yield* getJson("/admin-accounts-for-organization")) as any;
127 expect(["Ok", "InvalidOperationException", "ResourceNotFoundException"]).toContain(
128 response.tag,
129 );
130 }),
131 { timeout: 60_000 },
132 );
133 });
134
135 describe("ListPolicies", () => {
136 test.provider(
137 "lists FMS policies (typed AccessDeniedException on a non-admin account)",
138 (_stack) =>
139 Effect.gen(function* () {
140 const response = (yield* getJson("/policies")) as any;
141 expect(["Ok", "AccessDeniedException", "InvalidOperationException"]).toContain(
142 response.tag,
143 );
144 expect(response.count).toBeGreaterThanOrEqual(0);
145 }),
146 { timeout: 60_000 },
147 );
148 });
149
150 describe("ListResourceSets", () => {
151 test.provider(
152 "lists resource sets (typed AccessDeniedException on a non-admin account)",
153 (_stack) =>
154 Effect.gen(function* () {
155 const response = (yield* getJson("/resource-sets")) as any;
156 expect(["Ok", "AccessDeniedException", "InvalidOperationException"]).toContain(
157 response.tag,
158 );
159 }),
160 { timeout: 60_000 },
161 );
162 });
163
164 describe("ListMemberAccounts", () => {
165 test.provider(
166 "lists member accounts (typed AccessDeniedException on a non-admin account)",
167 (_stack) =>
168 Effect.gen(function* () {
169 const response = (yield* getJson("/member-accounts")) as any;
170 expect(["Ok", "AccessDeniedException", "ResourceNotFoundException"]).toContain(
171 response.tag,
172 );
173 }),
174 { timeout: 60_000 },
175 );
176 });
177
178 describe("GetNotificationChannel", () => {
179 test.provider(
180 "reads the notification channel (typed AccessDeniedException on a non-admin account)",
181 (_stack) =>
182 Effect.gen(function* () {
183 const response = (yield* getJson("/notification-channel")) as any;
184 expect([
185 "Ok",
186 "AccessDeniedException",
187 "InvalidOperationException",
188 "ResourceNotFoundException",
189 ]).toContain(response.tag);
190 }),
191 { timeout: 60_000 },
192 );
193 });
194
195 describe("ListAppsLists", () => {
196 test.provider(
197 "lists applications lists (typed AccessDeniedException on a non-admin account)",
198 (_stack) =>
199 Effect.gen(function* () {
200 const response = (yield* getJson("/apps-lists")) as any;
201 expect([
202 "Ok",
203 "AccessDeniedException",
204 "InvalidOperationException",
205 "ResourceNotFoundException",
206 ]).toContain(response.tag);
207 }),
208 { timeout: 60_000 },
209 );
210 });
211
212 describe("ListProtocolsLists", () => {
213 test.provider(
214 "lists protocols lists (typed AccessDeniedException on a non-admin account)",
215 (_stack) =>
216 Effect.gen(function* () {
217 const response = (yield* getJson("/protocols-lists")) as any;
218 expect([
219 "Ok",
220 "AccessDeniedException",
221 "InvalidOperationException",
222 "ResourceNotFoundException",
223 ]).toContain(response.tag);
224 }),
225 { timeout: 60_000 },
226 );
227 });
228
229 describe("GetThirdPartyFirewallAssociationStatus", () => {
230 test.provider(
231 "reads third-party firewall onboarding status (typed rejection on a non-onboarded account)",
232 (_stack) =>
233 Effect.gen(function* () {
234 const response = (yield* getJson("/third-party-firewall-status")) as any;
235 expect([
236 "Ok",
237 "AccessDeniedException",
238 "InvalidOperationException",
239 "InvalidInputException",
240 "ResourceNotFoundException",
241 ]).toContain(response.tag);
242 }),
243 { timeout: 60_000 },
244 );
245 });
246 },
247 );