@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 200 lines 7.48 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 AccountTestFunctionLive, { AccountTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "AccountBindings");
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 that the
29 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
30 // 4xx/assertion failure surfaces immediately.
31 const send = (request: HttpClientRequest.HttpClientRequest) =>
32 HttpClient.execute(request).pipe(
33 Effect.flatMap((response) =>
34 response.status >= 500
35 ? response.text.pipe(
36 Effect.flatMap((body) =>
37 Effect.fail(new TransientUpstream({ status: response.status, body })),
38 ),
39 )
40 : Effect.succeed(response),
41 ),
42 Effect.retry({
43 while: (e) => e._tag === "TransientUpstream",
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
45 }),
46 );
47
48 // The freshly attached IAM policy can take ~10-30s to propagate to the
49 // Lambda's role; until it does, granted calls surface a transient
50 // AccessDeniedException. Poll (bounded) until the response is no longer
51 // access-denied so grant-proof assertions see steady-state behavior.
52 const getJson = (path: string) =>
53 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
54 Effect.flatMap((r) => r.json),
55 Effect.repeat({
56 schedule: Schedule.spaced("3 seconds"),
57 until: (response): boolean => (response as { tag?: string }).tag !== "AccessDeniedException",
58 times: 10,
59 }),
60 );
61
62 describe.sequential(
63 "Account Bindings",
64 { tags: ["provider:aws", "provider:aws:account", "provider:aws:lambda", "live"] },
65 () => {
66 beforeAll(
67 Effect.gen(function* () {
68 yield* Effect.logInfo("Account test setup: destroying previous resources");
69 yield* sharedStack.destroy();
70
71 yield* Effect.logInfo("Account test setup: deploying fixture");
72 const attrs = yield* sharedStack.deploy(
73 Effect.gen(function* () {
74 return yield* AccountTestFunction;
75 }).pipe(Effect.provide(AccountTestFunctionLive)),
76 );
77
78 expect(attrs.functionUrl).toBeTruthy();
79 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
80
81 const readinessUrl = `${baseUrl}/bindings`;
82 yield* Effect.logInfo(`Account test setup: probing readiness at ${readinessUrl}`);
83 yield* HttpClient.get(readinessUrl).pipe(
84 Effect.flatMap((response) =>
85 response.status === 200
86 ? Effect.succeed(response)
87 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
88 ),
89 Effect.tapError((error) =>
90 Effect.logWarning(`Account test setup: fixture not ready yet (${String(error)})`),
91 ),
92 Effect.retry({ schedule: readinessPolicy }),
93 );
94 }),
95 { timeout: 240_000 },
96 );
97
98 afterAll(sharedStack.destroy(), { timeout: 120_000 });
99
100 describe("binding registration", () => {
101 test.provider("all 5 capabilities initialize in the runtime", (_stack) =>
102 Effect.gen(function* () {
103 const response = (yield* getJson("/bindings")) as { bound: string[] };
104 expect(response.bound).toEqual([
105 "getAccountInformation",
106 "getContactInformation",
107 "getAlternateContact",
108 "listRegions",
109 "getRegionOptStatus",
110 ]);
111 }),
112 );
113 });
114
115 describe("GetAccountInformation", () => {
116 test.provider("reads the account's metadata", (_stack) =>
117 Effect.gen(function* () {
118 const response = (yield* getJson("/account-info")) as
119 | {
120 ok: true;
121 accountId: string | null;
122 accountState: string | null;
123 hasAccountName: boolean;
124 }
125 | { ok: false; tag: string };
126 expect(response.ok).toBe(true);
127 if (response.ok) {
128 expect(response.accountId).toMatch(/^\d{12}$/);
129 expect(response.accountState).toBe("ACTIVE");
130 }
131 }),
132 );
133 });
134
135 describe("GetContactInformation", () => {
136 test.provider("reads the account's primary contact", (_stack) =>
137 Effect.gen(function* () {
138 const response = (yield* getJson("/contact-info")) as
139 | { ok: true; hasFullName: boolean; hasCountryCode: boolean }
140 | { ok: false; tag: string };
141 expect(response.ok).toBe(true);
142 if (response.ok) {
143 // Every account has a primary contact with a name and country.
144 expect(response.hasFullName).toBe(true);
145 expect(response.hasCountryCode).toBe(true);
146 }
147 }),
148 );
149 });
150
151 describe("GetAlternateContact", () => {
152 test.provider("reads the billing contact or surfaces the typed not-found tag", (_stack) =>
153 Effect.gen(function* () {
154 const response = (yield* getJson("/alternate-contact")) as
155 | { ok: true; contactType: string | null }
156 | { ok: false; tag: string };
157 if (response.ok) {
158 expect(response.contactType).toBe("BILLING");
159 } else {
160 // The BILLING alternate contact isn't set on the account — the
161 // typed tag proves the grant (an IAM gap would surface
162 // AccessDeniedException, which getJson polls away and would
163 // fail the assertion below).
164 expect(response.tag).toBe("ResourceNotFoundException");
165 }
166 }),
167 );
168 });
169
170 describe("ListRegions", () => {
171 test.provider("lists the account's regions with opt statuses", (_stack) =>
172 Effect.gen(function* () {
173 const response = (yield* getJson("/regions")) as
174 | { ok: true; count: number; regionNames: string[] }
175 | { ok: false; tag: string };
176 expect(response.ok).toBe(true);
177 if (response.ok) {
178 expect(response.count).toBeGreaterThanOrEqual(1);
179 expect(response.regionNames).toContain("us-east-1");
180 }
181 }),
182 );
183 });
184
185 describe("GetRegionOptStatus", () => {
186 test.provider("reads us-east-1's opt status", (_stack) =>
187 Effect.gen(function* () {
188 const response = (yield* getJson("/region-opt-status")) as
189 | { ok: true; regionName: string | null; regionOptStatus: string | null }
190 | { ok: false; tag: string };
191 expect(response.ok).toBe(true);
192 if (response.ok) {
193 expect(response.regionName).toBe("us-east-1");
194 expect(response.regionOptStatus).toBe("ENABLED_BY_DEFAULT");
195 }
196 }),
197 );
198 });
199 },
200 );