@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 197 lines 7.25 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 BudgetsTestFunctionLive, { BudgetsTestFunction, fixtureBudgetName } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "BudgetsBindings");
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 describe.sequential(
49 "Budgets Bindings",
50 {
51 tags: [
52 "provider:aws",
53 "provider:aws:budgets",
54 "provider:aws:iam",
55 "provider:aws:lambda",
56 "live",
57 ],
58 },
59 () => {
60 beforeAll(
61 Effect.gen(function* () {
62 yield* Effect.logInfo("Budgets test setup: destroying previous resources");
63 yield* sharedStack.destroy();
64
65 yield* Effect.logInfo("Budgets test setup: deploying fixture");
66 const { functionUrl } = yield* sharedStack.deploy(
67 Effect.gen(function* () {
68 return yield* BudgetsTestFunction;
69 }).pipe(Effect.provide(BudgetsTestFunctionLive)),
70 );
71
72 expect(functionUrl).toBeTruthy();
73 baseUrl = functionUrl!.replace(/\/+$/, "");
74
75 const readinessUrl = `${baseUrl}/bindings`;
76 yield* Effect.logInfo(`Budgets test setup: probing readiness at ${readinessUrl}`);
77 yield* HttpClient.get(readinessUrl).pipe(
78 Effect.flatMap((response) =>
79 response.status === 200
80 ? Effect.succeed(response)
81 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
82 ),
83 Effect.tapError((error) =>
84 Effect.logWarning(`Budgets test setup: fixture not ready yet (${String(error)})`),
85 ),
86 Effect.retry({ schedule: readinessPolicy }),
87 );
88 }),
89 { timeout: 300_000 },
90 );
91
92 afterAll(sharedStack.destroy(), { timeout: 180_000 });
93
94 describe("binding registration", () => {
95 test.provider("all 7 capabilities initialize in the runtime", (_stack) =>
96 Effect.gen(function* () {
97 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
98 Effect.flatMap((r) => r.json),
99 );
100 expect((response as any).bound).toHaveLength(7);
101 }),
102 );
103 });
104
105 describe("DescribeBudget", () => {
106 test.provider("reads the fixture budget's definition", (_stack) =>
107 Effect.gen(function* () {
108 const response = yield* send(HttpClientRequest.get(`${baseUrl}/budget`)).pipe(
109 Effect.flatMap((r) => r.json),
110 );
111 expect((response as any).name).toBe(fixtureBudgetName);
112 expect(Number((response as any).limitAmount)).toBe(1_000_000);
113 expect((response as any).limitUnit).toBe("USD");
114 expect((response as any).timeUnit).toBe("MONTHLY");
115 }),
116 );
117 });
118
119 describe("DescribeBudgetPerformanceHistory", () => {
120 test.provider("reads the budget's history (fresh budget: >= 0 periods)", (_stack) =>
121 Effect.gen(function* () {
122 const response = yield* send(HttpClientRequest.get(`${baseUrl}/history`)).pipe(
123 Effect.flatMap((r) => r.json),
124 );
125 expect(typeof (response as any).periods).toBe("number");
126 expect((response as any).periods).toBeGreaterThanOrEqual(0);
127 }),
128 );
129 });
130
131 describe("DescribeNotificationsForBudget", () => {
132 test.provider("lists the fixture notification threshold", (_stack) =>
133 Effect.gen(function* () {
134 const response = yield* send(HttpClientRequest.get(`${baseUrl}/notifications`)).pipe(
135 Effect.flatMap((r) => r.json),
136 );
137 expect((response as any).thresholds).toContain(80);
138 }),
139 );
140 });
141
142 describe("DescribeSubscribersForNotification", () => {
143 test.provider("lists the fixture notification's subscriber", (_stack) =>
144 Effect.gen(function* () {
145 const response = yield* send(HttpClientRequest.get(`${baseUrl}/subscribers`)).pipe(
146 Effect.flatMap((r) => r.json),
147 );
148 expect((response as any).count).toBe(1);
149 expect((response as any).subscriptionTypes).toContain("EMAIL");
150 }),
151 );
152 });
153
154 describe("DescribeBudgetActionsForBudget", () => {
155 test.provider("lists the fixture action in STANDBY", (_stack) =>
156 Effect.gen(function* () {
157 const response = yield* send(HttpClientRequest.get(`${baseUrl}/actions`)).pipe(
158 Effect.flatMap((r) => r.json),
159 );
160 const actions = (response as any).actions as {
161 actionId: string;
162 status: string;
163 actionType: string;
164 }[];
165 expect(actions.length).toBeGreaterThanOrEqual(1);
166 expect(actions[0]!.actionType).toBe("APPLY_IAM_POLICY");
167 expect(actions[0]!.status).toBe("STANDBY");
168 }),
169 );
170 });
171
172 describe("DescribeBudgetActionHistories", () => {
173 test.provider("reads the action's event history (>= CREATE_ACTION)", (_stack) =>
174 Effect.gen(function* () {
175 const response = yield* send(HttpClientRequest.get(`${baseUrl}/action-histories`)).pipe(
176 Effect.flatMap((r) => r.json),
177 );
178 expect((response as any).count).toBeGreaterThanOrEqual(1);
179 expect((response as any).eventTypes).toContain("CREATE_ACTION");
180 }),
181 );
182 });
183
184 describe("ExecuteBudgetAction", () => {
185 test.provider("the grant works — execute returns a typed budgets outcome", (_stack) =>
186 Effect.gen(function* () {
187 const response = yield* send(HttpClientRequest.get(`${baseUrl}/execute-reset`)).pipe(
188 Effect.flatMap((r) => r.json),
189 );
190 expect(["ok", "ResourceLockedException", "InvalidParameterException"]).toContain(
191 (response as any).outcome,
192 );
193 }),
194 );
195 });
196 },
197 );