@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 133 lines 4.73 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 CurTestFunctionLive, { CurTestFunction, REPORT_NAME } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "CurBindings");
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 const getJson = (path: string) =>
49 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
50
51 describe.sequential(
52 "CostAndUsageReport Bindings",
53 {
54 tags: [
55 "provider:aws",
56 "provider:aws:costandusagereport",
57 "provider:aws:lambda",
58 "provider:aws:s3",
59 "live",
60 ],
61 },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* Effect.logInfo("CUR test setup: destroying previous resources");
66 yield* sharedStack.destroy();
67
68 yield* Effect.logInfo("CUR test setup: deploying fixture");
69 const { functionUrl } = yield* sharedStack.deploy(
70 Effect.gen(function* () {
71 return yield* CurTestFunction;
72 }).pipe(Effect.provide(CurTestFunctionLive)),
73 );
74
75 expect(functionUrl).toBeTruthy();
76 baseUrl = functionUrl!.replace(/\/+$/, "");
77
78 const readinessUrl = `${baseUrl}/bindings`;
79 yield* Effect.logInfo(`CUR 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(`CUR 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: 180_000 });
96
97 describe("binding registration", () => {
98 test.provider("both capabilities initialize in the runtime", (_stack) =>
99 Effect.gen(function* () {
100 const response = yield* getJson("/bindings");
101 expect((response as any).bound).toEqual(["describeReportDefinitions", "listReportTags"]);
102 }),
103 );
104 });
105
106 describe("DescribeReportDefinitions", () => {
107 test.provider(
108 "lists the fixture report definition from the runtime",
109 (_stack) =>
110 Effect.gen(function* () {
111 const response = (yield* getJson("/reports")) as any;
112 expect(response.count).toBeGreaterThan(0);
113 expect(response.names).toContain(REPORT_NAME);
114 }),
115 { timeout: 60_000 },
116 );
117 });
118
119 describe("ListTagsForResource", () => {
120 test.provider(
121 "reads the fixture report's tags (proving ReportName injection + the report-scoped grant)",
122 (_stack) =>
123 Effect.gen(function* () {
124 const response = (yield* getJson("/report-tags")) as any;
125 expect(response.tags.fixture).toBe("cur-bindings");
126 // The provider brands the report with internal alchemy tags.
127 expect(Object.keys(response.tags).some((k) => k.startsWith("alchemy:"))).toBe(true);
128 }),
129 { timeout: 60_000 },
130 );
131 });
132 },
133 );