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

main
ts 213 lines 8.13 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 eventbridge from "@distilled.cloud/aws/eventbridge";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import ShieldTestFunctionLive, { ShieldTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "ShieldBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22 let functionArn: string;
23
24 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
25 readonly status: number;
26 readonly body: string;
27 }> {}
28
29 // The shared Lambda fixture occasionally answers a transient 5xx under load
30 // (cold re-init, IAM propagation on the freshly attached policy that the
31 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
32 // 4xx/assertion failure surfaces immediately.
33 const send = (request: HttpClientRequest.HttpClientRequest) =>
34 HttpClient.execute(request).pipe(
35 Effect.flatMap((response) =>
36 response.status >= 500
37 ? response.text.pipe(
38 Effect.flatMap((body) =>
39 Effect.fail(new TransientUpstream({ status: response.status, body })),
40 ),
41 )
42 : Effect.succeed(response),
43 ),
44 Effect.retry({
45 while: (e) => e._tag === "TransientUpstream",
46 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
47 }),
48 );
49
50 const getJson = (path: string) =>
51 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
52
53 // The testing account is NOT (and must never be) subscribed to Shield
54 // Advanced. GetSubscriptionState and DescribeAttackStatistics succeed on any
55 // account; the subscription-gated operations answer with their typed
56 // entitlement/not-found tags — asserting the tag proves the binding + IAM
57 // grant end-to-end at near-zero cost.
58 describe.sequential(
59 "Shield Bindings",
60 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:shield", "live"] },
61 () => {
62 beforeAll(
63 Effect.gen(function* () {
64 yield* Effect.logInfo("Shield test setup: destroying previous resources");
65 yield* sharedStack.destroy();
66
67 yield* Effect.logInfo("Shield test setup: deploying fixture");
68 const attrs = yield* sharedStack.deploy(
69 Effect.gen(function* () {
70 return yield* ShieldTestFunction;
71 }).pipe(Effect.provide(ShieldTestFunctionLive)),
72 );
73
74 expect(attrs.functionUrl).toBeTruthy();
75 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
76 functionArn = attrs.functionArn;
77
78 const readinessUrl = `${baseUrl}/bindings`;
79 yield* Effect.logInfo(`Shield test setup: probing readiness at ${readinessUrl}`);
80 yield* HttpClient.get(readinessUrl).pipe(
81 Effect.flatMap((response) =>
82 response.status === 200
83 ? Effect.succeed(response)
84 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
85 ),
86 Effect.tapError((error) =>
87 Effect.logWarning(`Shield test setup: fixture not ready yet (${String(error)})`),
88 ),
89 Effect.retry({ schedule: readinessPolicy }),
90 );
91 }),
92 { timeout: 240_000 },
93 );
94
95 afterAll(sharedStack.destroy(), { timeout: 120_000 });
96
97 describe("binding registration", () => {
98 test.provider("all 6 capabilities initialize in the runtime", (_stack) =>
99 Effect.gen(function* () {
100 const response = (yield* getJson("/bindings")) as { bound: string[] };
101 expect(response.bound).toHaveLength(6);
102 expect(response.bound).toContain("getSubscriptionState");
103 expect(response.bound).toContain("listAttacks");
104 }),
105 );
106 });
107
108 describe("GetSubscriptionState", () => {
109 test.provider("answers on any account, subscribed or not", (_stack) =>
110 Effect.gen(function* () {
111 const response = (yield* getJson("/subscription-state")) as { state: string };
112 expect(["ACTIVE", "INACTIVE"]).toContain(response.state);
113 }),
114 );
115 });
116
117 describe("DescribeAttackStatistics", () => {
118 test.provider(
119 "returns the yearly attack statistics (available to Standard customers)",
120 (_stack) =>
121 Effect.gen(function* () {
122 const response = (yield* getJson("/attack-stats")) as {
123 dataItems?: number;
124 errorTag?: string;
125 };
126 expect(response.errorTag).toBeUndefined();
127 expect(response.dataItems).toBeGreaterThanOrEqual(0);
128 }),
129 );
130 });
131
132 describe("ListAttacks", () => {
133 test.provider("answers with attacks or the typed subscription-gate tag", (_stack) =>
134 Effect.gen(function* () {
135 const response = (yield* getJson("/attacks")) as { count?: number; errorTag?: string };
136 if (response.errorTag) {
137 expect(["InvalidOperationException", "SubscriptionNotFound"]).toContain(
138 response.errorTag,
139 );
140 } else {
141 expect(response.count).toBeGreaterThanOrEqual(0);
142 }
143 }),
144 );
145 });
146
147 describe("DescribeAttack", () => {
148 test.provider(
149 "a nonexistent attack id answers with an empty document or the typed AccessDeniedException",
150 (_stack) =>
151 Effect.gen(function* () {
152 // Observed live: Shield answers a nonexistent attack id with an
153 // empty success (no Attack document); the documented alternative is
154 // the typed AccessDeniedException. Either outcome proves the
155 // binding + IAM grant end-to-end.
156 const response = (yield* getJson("/attack-detail")) as {
157 attackId?: string | null;
158 errorTag?: string;
159 };
160 if (response.errorTag) {
161 expect(response.errorTag).toBe("AccessDeniedException");
162 } else {
163 expect(response.attackId ?? null).toBeNull();
164 }
165 }),
166 );
167 });
168
169 describe("DescribeDRTAccess", () => {
170 test.provider(
171 "a non-subscribed account answers with the typed ResourceNotFoundException",
172 (_stack) =>
173 Effect.gen(function* () {
174 const response = (yield* getJson("/drt-access")) as {
175 roleArn?: string | null;
176 errorTag?: string;
177 };
178 if (response.errorTag) {
179 expect(response.errorTag).toBe("ResourceNotFoundException");
180 } else {
181 // A subscribed account answers with the (possibly empty) grant.
182 expect(response.roleArn !== undefined).toBe(true);
183 }
184 }),
185 );
186 });
187
188 describe("ListResourcesInProtectionGroup", () => {
189 test.provider(
190 "a nonexistent group answers with the typed ResourceNotFoundException",
191 (_stack) =>
192 Effect.gen(function* () {
193 const response = (yield* getJson("/group-members")) as { errorTag?: string };
194 expect(response.errorTag).toBe("ResourceNotFoundException");
195 }),
196 );
197 });
198
199 describe("consumeAttackEvents", { tags: ["provider:aws:eventbridge"] }, () => {
200 test.provider("the deploy created an EventBridge rule targeting the function", (_stack) =>
201 Effect.gen(function* () {
202 // Out-of-band via distilled: the fixture's consumeAttackEvents must
203 // have materialized as a rule on the default bus with the Lambda as
204 // target.
205 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
206 TargetArn: functionArn,
207 });
208 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
209 }),
210 );
211 });
212 },
213 );