@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 404 lines 15.4 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 { Region as AwsRegion } from "@distilled.cloud/aws/Region";
6 import * as route53domains from "@distilled.cloud/aws/route-53-domains";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as HttpClientRequest from "effect/http/HttpClientRequest";
11 import * as Schedule from "effect/Schedule";
12 import Route53DomainsTestFunctionLive, { Route53DomainsTestFunction } from "./handler";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "Route53DomainsBindings");
17
18 // The Route 53 Domains API is global and only served from us-east-1 — pin
19 // the region for out-of-band probes made directly from the test process.
20 const withRoute53DomainsRegion = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
21 effect.pipe(Effect.provideService(AwsRegion, Effect.succeed("us-east-1")));
22
23 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
24 // over 60s on a fresh deploy under parallel-suite load.
25 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
26
27 let baseUrl: string;
28
29 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
30 readonly status: number;
31 readonly body: string;
32 }> {}
33
34 // Retry transient 5xx only; a genuine 4xx/assertion failure surfaces
35 // immediately.
36 const send = (request: HttpClientRequest.HttpClientRequest) =>
37 HttpClient.execute(request).pipe(
38 Effect.flatMap((response) =>
39 response.status >= 500
40 ? response.text.pipe(
41 Effect.flatMap((body) =>
42 Effect.fail(new TransientUpstream({ status: response.status, body })),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]),
50 }),
51 );
52
53 // Ungated probes against the real API (read-only, free). This suite NEVER
54 // calls registerDomain — registration costs money and is irreversible.
55 test.provider(
56 "checkDomainAvailability succeeds for a deterministic unregistered name",
57 (_stack) =>
58 Effect.gen(function* () {
59 const result = yield* withRoute53DomainsRegion(
60 route53domains.checkDomainAvailability({
61 // Deterministic probe name — read-only availability check, no
62 // registration ever happens in this suite.
63 DomainName: "alchemy-effect-r53d-probe-a32892.com",
64 }),
65 );
66 // The exact verdict (AVAILABLE vs PENDING) is registry-dependent; the
67 // probe proves auth + the us-east-1 pin + response decoding.
68 expect(result.Availability).toBeDefined();
69 }),
70 { tags: ["provider:aws", "provider:aws:route53domains", "live"], timeout: 60_000 },
71 );
72
73 test.provider(
74 "getDomainDetail on a domain not in the account fails with typed DomainNotFound",
75 (_stack) =>
76 Effect.gen(function* () {
77 // example.com is never registered in the test account. The overloaded
78 // InvalidInput ("Domain example.com not found in account.") is
79 // specialized to DomainNotFound by the distilled patch.
80 const detail = yield* withRoute53DomainsRegion(
81 route53domains.getDomainDetail({ DomainName: "example.com" }),
82 ).pipe(Effect.catchTag("DomainNotFound", (error) => Effect.succeed(error._tag)));
83 expect(detail).toBe("DomainNotFound");
84 }),
85 { tags: ["provider:aws", "provider:aws:route53domains", "live"], timeout: 60_000 },
86 );
87
88 test.provider(
89 "listDomains succeeds for the account",
90 (_stack) =>
91 Effect.gen(function* () {
92 const result = yield* withRoute53DomainsRegion(route53domains.listDomains({ MaxItems: 100 }));
93 // The testing account owns no domains — the call succeeding with a
94 // (possibly empty) list proves auth and response decoding.
95 expect(Array.isArray(result.Domains ?? [])).toBe(true);
96 }),
97 { tags: ["provider:aws", "provider:aws:route53domains", "live"], timeout: 60_000 },
98 );
99
100 test.provider(
101 "retrieveDomainAuthCode on a domain not in the account fails with typed DomainNotFound",
102 (_stack) =>
103 Effect.gen(function* () {
104 // example.com is never registered in the test account. The overloaded
105 // InvalidInput ("Domain example.com not found in account.") is
106 // specialized to DomainNotFound by the distilled patch.
107 const tag = yield* withRoute53DomainsRegion(
108 route53domains.retrieveDomainAuthCode({ DomainName: "example.com" }),
109 ).pipe(
110 Effect.map(() => "success" as const),
111 Effect.catchTag("DomainNotFound", (error) => Effect.succeed(error._tag)),
112 );
113 expect(tag).toBe("DomainNotFound");
114 }),
115 { tags: ["provider:aws", "provider:aws:route53domains", "live"], timeout: 60_000 },
116 );
117
118 test.provider(
119 "updateDomainNameservers on a domain not in the account fails with typed DomainNotFound",
120 (_stack) =>
121 Effect.gen(function* () {
122 const tag = yield* withRoute53DomainsRegion(
123 route53domains.updateDomainNameservers({
124 DomainName: "example.com",
125 Nameservers: [{ Name: "ns-1.awsdns-01.org" }],
126 }),
127 ).pipe(
128 Effect.map(() => "success" as const),
129 Effect.catchTag("DomainNotFound", (error) => Effect.succeed(error._tag)),
130 );
131 expect(tag).toBe("DomainNotFound");
132 }),
133 { tags: ["provider:aws", "provider:aws:route53domains", "live"], timeout: 60_000 },
134 );
135
136 test.provider(
137 "getDomainSuggestions returns suggestions for a seed name",
138 (_stack) =>
139 Effect.gen(function* () {
140 const result = yield* withRoute53DomainsRegion(
141 route53domains.getDomainSuggestions({
142 DomainName: "example.com",
143 SuggestionCount: 5,
144 OnlyAvailable: false,
145 }),
146 );
147 expect(Array.isArray(result.SuggestionsList ?? [])).toBe(true);
148 }),
149 { tags: ["provider:aws", "provider:aws:route53domains", "live"], timeout: 60_000 },
150 );
151
152 test.provider(
153 "listPrices returns registration pricing for the com TLD",
154 (_stack) =>
155 Effect.gen(function* () {
156 const result = yield* withRoute53DomainsRegion(route53domains.listPrices({ Tld: "com" }));
157 expect((result.Prices ?? []).length).toBeGreaterThan(0);
158 expect(result.Prices?.[0]?.RegistrationPrice?.Price).toBeGreaterThan(0);
159 }),
160 { tags: ["provider:aws", "provider:aws:route53domains", "live"], timeout: 60_000 },
161 );
162
163 describe(
164 "Route53Domains Bindings",
165 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:route53domains", "live"] },
166 () => {
167 beforeAll(
168 Effect.gen(function* () {
169 yield* Effect.logInfo("Route53Domains test setup: destroying previous resources");
170 yield* sharedStack.destroy();
171
172 yield* Effect.logInfo("Route53Domains test setup: deploying fixture");
173 const { functionUrl } = yield* sharedStack.deploy(
174 Effect.gen(function* () {
175 return yield* Route53DomainsTestFunction;
176 }).pipe(Effect.provide(Route53DomainsTestFunctionLive)),
177 );
178
179 expect(functionUrl).toBeTruthy();
180 baseUrl = functionUrl!.replace(/\/+$/, "");
181 const readinessUrl = `${baseUrl}/ping`;
182
183 yield* Effect.logInfo(`Route53Domains test setup: probing readiness at ${readinessUrl}`);
184 yield* HttpClient.get(readinessUrl).pipe(
185 Effect.flatMap((response) =>
186 response.status === 200
187 ? Effect.succeed(response)
188 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
189 ),
190 Effect.tapError((error) =>
191 Effect.logWarning(
192 `Route53Domains test setup: fixture not ready yet (${String(error)})`,
193 ),
194 ),
195 Effect.retry({ schedule: readinessPolicy }),
196 );
197 }),
198 { timeout: 240_000 },
199 );
200
201 afterAll(sharedStack.destroy(), { timeout: 120_000 });
202
203 describe("CheckDomainAvailability", () => {
204 test.provider(
205 "returns an availability verdict from inside the Lambda",
206 (_stack) =>
207 Effect.gen(function* () {
208 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/availability`)).pipe(
209 Effect.flatMap((r) => r.json),
210 )) as { availability: string | undefined };
211
212 expect(response.availability).toBeDefined();
213 }),
214 { timeout: 120_000 },
215 );
216 });
217
218 describe("ListDomains", () => {
219 test.provider(
220 "lists the account's registered domains from inside the Lambda",
221 (_stack) =>
222 Effect.gen(function* () {
223 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/domains`)).pipe(
224 Effect.flatMap((r) => r.json),
225 )) as { count: number; names: string[] };
226
227 expect(response.count).toBeGreaterThanOrEqual(0);
228 expect(Array.isArray(response.names)).toBe(true);
229 }),
230 { timeout: 120_000 },
231 );
232 });
233
234 describe("GetDomainDetail", () => {
235 test.provider(
236 "reaches the API from inside the Lambda and gets a typed domain error",
237 (_stack) =>
238 Effect.gen(function* () {
239 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/detail`)).pipe(
240 Effect.flatMap((r) => r.json),
241 )) as { ok: boolean; errorTag?: string };
242
243 // The call must reach the API (proving the IAM grant and the
244 // us-east-1 pin) and fail with a domain-level error — never
245 // AccessDenied. The deployed bundle resolves distilled from the
246 // built lib/, so until the coordinator rebuilds it the runtime
247 // still maps this error to the base InvalidInput instead of the
248 // patched DomainNotFound specialization (the strict typed
249 // assertion is the out-of-band probe above).
250 expect(response.ok).toBe(false);
251 expect(["DomainNotFound", "InvalidInput"]).toContain(response.errorTag);
252 }),
253 { timeout: 120_000 },
254 );
255 });
256
257 describe("CheckDomainTransferability", () => {
258 test.provider(
259 "returns a transferability verdict from inside the Lambda",
260 (_stack) =>
261 Effect.gen(function* () {
262 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/transferability`)).pipe(
263 Effect.flatMap((r) => r.json),
264 )) as { transferable: string | undefined };
265
266 expect(response.transferable).toBeDefined();
267 }),
268 { timeout: 120_000 },
269 );
270 });
271
272 describe("GetDomainSuggestions", () => {
273 test.provider(
274 "returns suggestions from inside the Lambda",
275 (_stack) =>
276 Effect.gen(function* () {
277 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/suggestions`)).pipe(
278 Effect.flatMap((r) => r.json),
279 )) as { count: number };
280
281 expect(response.count).toBeGreaterThanOrEqual(0);
282 }),
283 { timeout: 120_000 },
284 );
285 });
286
287 describe("ListPrices", () => {
288 test.provider(
289 "returns .com pricing from inside the Lambda",
290 (_stack) =>
291 Effect.gen(function* () {
292 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/prices`)).pipe(
293 Effect.flatMap((r) => r.json),
294 )) as { count: number; registrationPrice: number | undefined };
295
296 expect(response.count).toBeGreaterThan(0);
297 expect(response.registrationPrice).toBeGreaterThan(0);
298 }),
299 { timeout: 120_000 },
300 );
301 });
302
303 describe("ListOperations", () => {
304 test.provider(
305 "lists the account's registration operations from inside the Lambda",
306 (_stack) =>
307 Effect.gen(function* () {
308 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/operations`)).pipe(
309 Effect.flatMap((r) => r.json),
310 )) as { count: number };
311
312 expect(response.count).toBeGreaterThanOrEqual(0);
313 }),
314 { timeout: 120_000 },
315 );
316 });
317
318 describe("GetOperationDetail", () => {
319 test.provider(
320 "reaches the API from inside the Lambda and gets a typed error for an unknown operation",
321 (_stack) =>
322 Effect.gen(function* () {
323 const response = (yield* send(
324 HttpClientRequest.get(`${baseUrl}/operation-detail`),
325 ).pipe(Effect.flatMap((r) => r.json))) as { ok: boolean; errorTag?: string };
326
327 expect(response.ok).toBe(false);
328 expect(response.errorTag).toBe("InvalidInput");
329 }),
330 { timeout: 120_000 },
331 );
332 });
333
334 describe("RetrieveDomainAuthCode", () => {
335 test.provider(
336 "reaches the API from inside the Lambda and gets a typed domain error",
337 (_stack) =>
338 Effect.gen(function* () {
339 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/auth-code`)).pipe(
340 Effect.flatMap((r) => r.json),
341 )) as { ok: boolean; errorTag?: string };
342
343 // Same stale-lib caveat as GetDomainDetail: the strict
344 // DomainNotFound assertion is the out-of-band probe above.
345 expect(response.ok).toBe(false);
346 expect(["DomainNotFound", "InvalidInput"]).toContain(response.errorTag);
347 }),
348 { timeout: 120_000 },
349 );
350 });
351
352 describe("UpdateDomainNameservers", () => {
353 test.provider(
354 "reaches the API from inside the Lambda and gets a typed domain error",
355 (_stack) =>
356 Effect.gen(function* () {
357 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/nameservers`)).pipe(
358 Effect.flatMap((r) => r.json),
359 )) as { ok: boolean; errorTag?: string };
360
361 expect(response.ok).toBe(false);
362 expect(["DomainNotFound", "InvalidInput"]).toContain(response.errorTag);
363 }),
364 { timeout: 120_000 },
365 );
366 });
367
368 describe("RenewDomain", () => {
369 test.provider(
370 "reaches the API from inside the Lambda and gets a typed domain error",
371 (_stack) =>
372 Effect.gen(function* () {
373 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/renew`)).pipe(
374 Effect.flatMap((r) => r.json),
375 )) as { ok: boolean; errorTag?: string };
376
377 // example.com is not in the account — the renewal is rejected
378 // before any billing can occur.
379 expect(response.ok).toBe(false);
380 expect(["DomainNotFound", "InvalidInput"]).toContain(response.errorTag);
381 }),
382 { timeout: 120_000 },
383 );
384 });
385
386 describe("RegisterDomain", () => {
387 test.provider(
388 "reaches the API from inside the Lambda and is rejected for an unsupported TLD",
389 (_stack) =>
390 Effect.gen(function* () {
391 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/register`)).pipe(
392 Effect.flatMap((r) => r.json),
393 )) as { ok: boolean; errorTag?: string };
394
395 // The deliberately invalid TLD guarantees rejection before any
396 // registration or billing — this suite NEVER registers a domain.
397 expect(response.ok).toBe(false);
398 expect(["UnsupportedTLD", "InvalidInput"]).toContain(response.errorTag);
399 }),
400 { timeout: 120_000 },
401 );
402 });
403 },
404 );