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

main
ts 181 lines 6.89 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 Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as Schedule from "effect/Schedule";
8 import NetworkFirewallFirewallBindingsFunctionLive, {
9 NetworkFirewallFirewallBindingsFunction,
10 } from "./firewall-handler";
11
12 // A firewall takes ~5-10 minutes to provision its endpoints (and a similar
13 // window to deprovision), so the firewall-scoped bindings fixture is gated
14 // behind AWS_TEST_NETWORKFIREWALL=1 — same gate as the Firewall lifecycle
15 // test.
16 const gated = !process.env.AWS_TEST_NETWORKFIREWALL;
17
18 const testOptions = { providers: AWS.providers() };
19 const { test, beforeAll, afterAll } = Test.make(testOptions);
20 const sharedStack = Core.scratchStack(testOptions, "NetworkFirewallFirewallBindings");
21
22 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
23
24 let baseUrl: string;
25
26 const getJson = (path: string) =>
27 HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
28
29 describe.sequential(
30 "NetworkFirewall Firewall Bindings",
31 {
32 tags: [
33 "provider:aws",
34 "provider:aws:ec2",
35 "provider:aws:lambda",
36 "provider:aws:networkfirewall",
37 "live",
38 ],
39 },
40 () => {
41 beforeAll(
42 gated
43 ? Effect.void
44 : Effect.gen(function* () {
45 yield* Effect.logInfo(
46 "NetworkFirewall firewall-bindings setup: destroying previous resources",
47 );
48 yield* sharedStack.destroy();
49
50 yield* Effect.logInfo(
51 "NetworkFirewall firewall-bindings setup: deploying fixture (firewall provisioning takes 5-10 minutes)",
52 );
53 const attrs = yield* sharedStack.deploy(
54 Effect.gen(function* () {
55 return yield* NetworkFirewallFirewallBindingsFunction;
56 }).pipe(Effect.provide(NetworkFirewallFirewallBindingsFunctionLive)),
57 );
58
59 expect(attrs.functionUrl).toBeTruthy();
60 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
61
62 yield* HttpClient.get(`${baseUrl}/bindings`).pipe(
63 Effect.flatMap((response) =>
64 response.status === 200
65 ? Effect.succeed(response)
66 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
67 ),
68 Effect.retry({ schedule: readinessPolicy }),
69 );
70 }),
71 // firewall create (~10 min) + Lambda deploy.
72 { timeout: 1_500_000 },
73 );
74
75 afterAll(
76 gated || process.env.NO_DESTROY ? Effect.void : sharedStack.destroy(),
77 // firewall delete waits for endpoint deprovisioning (~10 min).
78 { timeout: 1_500_000 },
79 );
80
81 describe("binding registration", () => {
82 test.provider.skipIf(gated)(
83 "all 9 firewall-scoped capabilities initialize in the runtime",
84 (_stack) =>
85 Effect.gen(function* () {
86 const response = (yield* getJson("/bindings")) as { bound: string[] };
87 expect(response.bound).toHaveLength(9);
88 expect(response.bound).toContain("describeFirewall");
89 expect(response.bound).toContain("startFlowCapture");
90 expect(response.bound).toContain("startAnalysisReport");
91 }),
92 );
93 });
94
95 describe("DescribeFirewall", () => {
96 test.provider.skipIf(gated)("reads the bound firewall's status (ARN injected)", (_stack) =>
97 Effect.gen(function* () {
98 const response = (yield* getJson("/firewall")) as {
99 status: string;
100 endpointCount: number;
101 };
102 expect(response.status).toBe("READY");
103 expect(response.endpointCount).toBeGreaterThanOrEqual(1);
104 }),
105 );
106 });
107
108 describe("StartFlowCapture / DescribeFlowOperation / ListFlowOperations / ListFlowOperationResults", () => {
109 test.provider.skipIf(gated)(
110 "runs a flow capture end-to-end on the bound firewall",
111 (_stack) =>
112 Effect.gen(function* () {
113 type FlowResponse = {
114 step: string;
115 tag?: string;
116 error?: string;
117 flowOperationId?: string;
118 status?: string;
119 operations?: number;
120 flows?: number;
121 };
122 // The role policy is attached moments before this test — IAM
123 // propagation can lag, surfacing as AccessDeniedException from a
124 // step. Retry (bounded) until the grant lands.
125 const response = yield* getJson("/flow").pipe(
126 Effect.map((r) => r as FlowResponse),
127 Effect.repeat({
128 schedule: Schedule.spaced("5 seconds"),
129 until: (r): boolean => r.tag !== "AccessDeniedException",
130 times: 10,
131 }),
132 );
133 yield* Effect.logInfo(`/flow response: ${JSON.stringify(response)}`);
134 expect(response).toMatchObject({ step: "ok" });
135 expect(response.flowOperationId).toBeTruthy();
136 // A capture can outlive the handler's bounded (~40s) poll; the
137 // describe/list/results calls all succeeding is what proves the
138 // grants, so a still-running capture is accepted too.
139 expect(["IN_PROGRESS", "COMPLETED", "COMPLETED_WITH_ERRORS", "FAILED"]).toContain(
140 response.status,
141 );
142 expect(response.operations).toBeGreaterThanOrEqual(1);
143 expect(response.flows).toBeGreaterThanOrEqual(0);
144 }),
145 { timeout: 180_000 },
146 );
147 });
148
149 describe("StartFlowFlush", () => {
150 test.provider.skipIf(gated)(
151 "starts a flow flush (grant proven by success or typed rejection)",
152 (_stack) =>
153 Effect.gen(function* () {
154 const response = (yield* getJson("/flush")) as { tag: string };
155 expect(response.tag).not.toBe("AccessDeniedException");
156 }),
157 );
158 });
159
160 describe("StartAnalysisReport / ListAnalysisReports / GetAnalysisReportResults", () => {
161 test.provider.skipIf(gated)(
162 "exercises the analysis-report interface (typed rejections prove grants)",
163 (_stack) =>
164 Effect.gen(function* () {
165 const response = (yield* getJson("/analysis")) as {
166 step: string;
167 startTag: string;
168 reports: number;
169 getTag: string;
170 };
171 expect(response.step).toBe("ok");
172 // The fixture firewall has no analysis types enabled — any typed
173 // tag except AccessDenied proves the grant round-tripped.
174 expect(response.startTag).not.toBe("AccessDeniedException");
175 expect(response.reports).toBeGreaterThanOrEqual(0);
176 expect(response.getTag).not.toBe("AccessDeniedException");
177 }),
178 );
179 });
180 },
181 );