@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 153 lines 5.75 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 NetworkFirewallBindingsFunctionLive, { NetworkFirewallBindingsFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "NetworkFirewallBindings");
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). Retry only
29 // 5xx; a genuine 4xx/assertion failure surfaces immediately.
30 const send = (request: HttpClientRequest.HttpClientRequest) =>
31 HttpClient.execute(request).pipe(
32 Effect.flatMap((response) =>
33 response.status >= 500
34 ? response.text.pipe(
35 Effect.flatMap((body) =>
36 Effect.fail(new TransientUpstream({ status: response.status, body })),
37 ),
38 )
39 : Effect.succeed(response),
40 ),
41 Effect.retry({
42 while: (e) => e._tag === "TransientUpstream",
43 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
44 }),
45 );
46
47 const getJson = (path: string) =>
48 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
49
50 describe.sequential(
51 "NetworkFirewall Bindings",
52 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:networkfirewall", "live"] },
53 () => {
54 beforeAll(
55 Effect.gen(function* () {
56 yield* Effect.logInfo("NetworkFirewall test setup: destroying previous resources");
57 yield* sharedStack.destroy();
58
59 yield* Effect.logInfo("NetworkFirewall test setup: deploying fixture");
60 const attrs = yield* sharedStack.deploy(
61 Effect.gen(function* () {
62 return yield* NetworkFirewallBindingsFunction;
63 }).pipe(Effect.provide(NetworkFirewallBindingsFunctionLive)),
64 );
65
66 expect(attrs.functionUrl).toBeTruthy();
67 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
68
69 const readinessUrl = `${baseUrl}/bindings`;
70 yield* Effect.logInfo(`NetworkFirewall test setup: probing readiness at ${readinessUrl}`);
71 yield* HttpClient.get(readinessUrl).pipe(
72 Effect.flatMap((response) =>
73 response.status === 200
74 ? Effect.succeed(response)
75 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
76 ),
77 Effect.tapError((error) =>
78 Effect.logWarning(
79 `NetworkFirewall test setup: fixture not ready yet (${String(error)})`,
80 ),
81 ),
82 Effect.retry({ schedule: readinessPolicy }),
83 );
84 }),
85 { timeout: 300_000 },
86 );
87
88 afterAll(process.env.NO_DESTROY ? Effect.void : sharedStack.destroy(), { timeout: 300_000 });
89
90 describe("binding registration", () => {
91 test.provider("all 4 capabilities initialize in the runtime", (_stack) =>
92 Effect.gen(function* () {
93 const response = (yield* getJson("/bindings")) as { bound: string[] };
94 expect(response.bound).toHaveLength(4);
95 expect(response.bound).toContain("describeFirewallPolicy");
96 expect(response.bound).toContain("describeRuleGroup");
97 expect(response.bound).toContain("describeRuleGroupSummary");
98 expect(response.bound).toContain("describeRuleGroupMetadata");
99 }),
100 );
101 });
102
103 describe("DescribeFirewallPolicy", () => {
104 test.provider("reads the bound policy's definition (ARN injected)", (_stack) =>
105 Effect.gen(function* () {
106 const response = (yield* getJson("/policy")) as {
107 status: string;
108 statelessDefaultActions: string[];
109 };
110 expect(response.status).toBe("ACTIVE");
111 expect(response.statelessDefaultActions).toEqual(["aws:forward_to_sfe"]);
112 }),
113 );
114 });
115
116 describe("DescribeRuleGroup", () => {
117 test.provider("reads the bound rule group's rules (ARN injected)", (_stack) =>
118 Effect.gen(function* () {
119 const response = (yield* getJson("/rule-group")) as {
120 status: string;
121 type: string;
122 rulesString: string;
123 };
124 expect(response.status).toBe("ACTIVE");
125 expect(response.type).toBe("STATEFUL");
126 expect(response.rulesString).toContain("allow https");
127 }),
128 );
129 });
130
131 describe("DescribeRuleGroupSummary", () => {
132 test.provider("summarizes the stateful rules", (_stack) =>
133 Effect.gen(function* () {
134 const response = (yield* getJson("/rule-group/summary")) as { ruleCount: number };
135 expect(response.ruleCount).toBe(1);
136 }),
137 );
138 });
139
140 describe("DescribeRuleGroupMetadata", () => {
141 test.provider("reads capacity and type without the definition", (_stack) =>
142 Effect.gen(function* () {
143 const response = (yield* getJson("/rule-group/metadata")) as {
144 capacity: number;
145 type: string;
146 };
147 expect(response.capacity).toBe(10);
148 expect(response.type).toBe("STATEFUL");
149 }),
150 );
151 });
152 },
153 );