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

main
ts 214 lines 8.27 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 servicequotas from "@distilled.cloud/aws/service-quotas";
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 ServiceQuotasTestFunctionLive, { ServiceQuotasTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "ServiceQuotasBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy under parallel-suite load.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22
23 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
24 readonly status: number;
25 readonly body: string;
26 }> {}
27
28 // Retry 5xx only — cold re-inits under parallel load surface as transient
29 // 500s; 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("1 second"), Schedule.recurs(5)]),
44 }),
45 );
46
47 describe(
48 "ServiceQuotas Bindings",
49 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:servicequotas", "live"] },
50 () => {
51 beforeAll(
52 Effect.gen(function* () {
53 yield* Effect.logInfo("ServiceQuotas test setup: destroying previous resources");
54 yield* sharedStack.destroy();
55
56 yield* Effect.logInfo("ServiceQuotas test setup: deploying fixture");
57 const { functionUrl } = yield* sharedStack.deploy(
58 Effect.gen(function* () {
59 return yield* ServiceQuotasTestFunction;
60 }).pipe(Effect.provide(ServiceQuotasTestFunctionLive)),
61 );
62
63 expect(functionUrl).toBeTruthy();
64 baseUrl = functionUrl!.replace(/\/+$/, "");
65 const readinessUrl = `${baseUrl}/ping`;
66
67 yield* Effect.logInfo(`ServiceQuotas test setup: probing readiness at ${readinessUrl}`);
68 yield* HttpClient.get(readinessUrl).pipe(
69 Effect.flatMap((response) =>
70 response.status === 200
71 ? Effect.succeed(response)
72 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
73 ),
74 Effect.tapError((error) =>
75 Effect.logWarning(`ServiceQuotas test setup: fixture not ready yet (${String(error)})`),
76 ),
77 Effect.retry({ schedule: readinessPolicy }),
78 );
79 }),
80 { timeout: 240_000 },
81 );
82
83 afterAll(sharedStack.destroy(), { timeout: 120_000 });
84
85 describe("GetServiceQuota", () => {
86 test.provider(
87 "surfaces the typed NoSuchResourceException for a bogus quota",
88 (_stack) =>
89 Effect.gen(function* () {
90 const response = yield* send(
91 HttpClientRequest.get(`${baseUrl}/quota?service=vpc&quota=L-00000000`),
92 );
93 expect(response.status).toBe(404);
94 const body = (yield* response.json) as { tag: string };
95 expect(body.tag).toBe("NoSuchResourceException");
96 }),
97 { timeout: 120_000 },
98 );
99
100 test.provider(
101 "reads the applied quota value through the deployed Lambda",
102 (_stack) =>
103 Effect.gen(function* () {
104 // Same account/region as the Lambda — establish the expected
105 // behavior out-of-band (some quotas have no applied value and
106 // return the typed not-found).
107 const expected = yield* servicequotas
108 .getServiceQuota({ ServiceCode: "lambda", QuotaCode: "L-B99A9384" })
109 .pipe(
110 Effect.map((r) => r.Quota?.Value),
111 Effect.catchTag("NoSuchResourceException", () => Effect.succeed(undefined)),
112 );
113
114 const response = yield* send(
115 HttpClientRequest.get(`${baseUrl}/quota?service=lambda&quota=L-B99A9384`),
116 );
117 if (expected === undefined) {
118 expect(response.status).toBe(404);
119 const body = (yield* response.json) as { tag: string };
120 expect(body.tag).toBe("NoSuchResourceException");
121 } else {
122 expect(response.status).toBe(200);
123 const body = (yield* response.json) as { quotaCode: string; value: number };
124 expect(body.quotaCode).toBe("L-B99A9384");
125 expect(body.value).toBe(expected);
126 }
127 }),
128 { timeout: 120_000 },
129 );
130 });
131
132 describe("GetAWSDefaultServiceQuota", () => {
133 test.provider(
134 "reads the AWS default quota value through the deployed Lambda",
135 (_stack) =>
136 Effect.gen(function* () {
137 // Out-of-band expectation from the same account/region.
138 const expected = yield* servicequotas
139 .getAWSDefaultServiceQuota({ ServiceCode: "vpc", QuotaCode: "L-F678F1CE" })
140 .pipe(Effect.map((r) => r.Quota?.Value));
141
142 const response = yield* send(
143 HttpClientRequest.get(`${baseUrl}/default-quota?service=vpc&quota=L-F678F1CE`),
144 );
145 expect(response.status).toBe(200);
146 const body = (yield* response.json) as { quotaCode: string; value: number };
147 expect(body.quotaCode).toBe("L-F678F1CE");
148 expect(body.value).toBe(expected);
149 }),
150 { timeout: 120_000 },
151 );
152 });
153
154 describe("ListServices", () => {
155 test.provider(
156 "lists Service Quotas service codes through the deployed Lambda",
157 (_stack) =>
158 Effect.gen(function* () {
159 const response = yield* send(HttpClientRequest.get(`${baseUrl}/services`));
160 expect(response.status).toBe(200);
161 const body = (yield* response.json) as { serviceCodes: string[] };
162 expect(body.serviceCodes.length).toBeGreaterThan(0);
163 }),
164 { timeout: 120_000 },
165 );
166 });
167
168 describe("ListServiceQuotas", () => {
169 test.provider(
170 "lists a service's applied quotas through the deployed Lambda",
171 (_stack) =>
172 Effect.gen(function* () {
173 const response = yield* send(HttpClientRequest.get(`${baseUrl}/quotas?service=vpc`));
174 expect(response.status).toBe(200);
175 const body = (yield* response.json) as { quotaCodes: string[] };
176 expect(body.quotaCodes.length).toBeGreaterThan(0);
177 }),
178 { timeout: 120_000 },
179 );
180 });
181
182 describe("ListRequestedServiceQuotaChangeHistoryByQuota", () => {
183 test.provider(
184 "lists a quota's request history through the deployed Lambda",
185 (_stack) =>
186 Effect.gen(function* () {
187 const response = yield* send(
188 HttpClientRequest.get(`${baseUrl}/history?service=vpc&quota=L-F678F1CE`),
189 );
190 expect(response.status).toBe(200);
191 const body = (yield* response.json) as { count: number };
192 expect(body.count).toBeGreaterThanOrEqual(0);
193 }),
194 { timeout: 120_000 },
195 );
196 });
197
198 describe("RequestServiceQuotaIncrease", () => {
199 test.provider(
200 "surfaces the typed NoSuchResourceException for a bogus quota (write-path IAM verified, no request submitted)",
201 (_stack) =>
202 Effect.gen(function* () {
203 const response = yield* send(
204 HttpClientRequest.post(`${baseUrl}/request-increase?service=vpc&quota=L-00000000`),
205 );
206 expect(response.status).toBe(404);
207 const body = (yield* response.json) as { tag: string };
208 expect(body.tag).toBe("NoSuchResourceException");
209 }),
210 { timeout: 120_000 },
211 );
212 });
213 },
214 );