1import * as AWS from "@/AWS";
2import * as Core from "@/Test/Core";
3import * as Test from "@/Test/Alchemy";
4import { describe, expect } from "alchemy-test";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import * as HttpClient from "effect/http/HttpClient";
9import * as HttpClientRequest from "effect/http/HttpClientRequest";
10import BCMDataExportsTestFunctionLive, {
11 BCMDataExportsTestFunction,
12 fixtureExportName,
13} from "./handler";
14
15const testOptions = { providers: AWS.providers() };
16const { test, beforeAll, afterAll } = Test.make(testOptions);
17const sharedStack = Core.scratchStack(testOptions, "BCMDataExportsBindings");
18
19// Lambda function URL cold-start (DNS, IAM propagation, init) can take well
20// over 60s on a fresh deploy.
21const readinessPolicy = Schedule.max([
22 Schedule.fixed("2 seconds"),
23 Schedule.recurs(75),
24]);
25
26let baseUrl: string;
27
28class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
29 readonly status: number;
30 readonly body: string;
31}> {}
32
33// The shared Lambda fixture occasionally answers a transient 5xx under load
34// (cold re-init, IAM propagation on the freshly attached policy that the
35// handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
36// 4xx/assertion failure surfaces immediately.
37const send = (request: HttpClientRequest.HttpClientRequest) =>
38 HttpClient.execute(request).pipe(
39 Effect.flatMap((response) =>
40 response.status >= 500
41 ? response.text.pipe(
42 Effect.flatMap((body) =>
43 Effect.fail(
44 new TransientUpstream({ status: response.status, body }),
45 ),
46 ),
47 )
48 : Effect.succeed(response),
49 ),
50 Effect.retry({
51 while: (e) => e._tag === "TransientUpstream",
52 schedule: Schedule.max([
53 Schedule.exponential("500 millis"),
54 Schedule.recurs(6),
55 ]),
56 }),
57 );
58
59describe.sequential(
60 "BCMDataExports Bindings",
61 {
62 tags: [
63 "provider:aws",
64 "provider:aws:bcmdataexports",
65 "provider:aws:lambda",
66 "provider:aws:s3",
67 "live",
68 ],
69 },
70 () => {
71 beforeAll(
72 Effect.gen(function* () {
73 yield* Effect.logInfo(
74 "BCMDataExports test setup: destroying previous resources",
75 );
76 yield* sharedStack.destroy();
77
78 yield* Effect.logInfo("BCMDataExports test setup: deploying fixture");
79 const { functionUrl } = yield* sharedStack.deploy(
80 Effect.gen(function* () {
81 return yield* BCMDataExportsTestFunction;
82 }).pipe(Effect.provide(BCMDataExportsTestFunctionLive)),
83 );
84
85 expect(functionUrl).toBeTruthy();
86 baseUrl = functionUrl!.replace(/\/+$/, "");
87
88 const readinessUrl = `${baseUrl}/bindings`;
89 yield* Effect.logInfo(
90 `BCMDataExports test setup: probing readiness at ${readinessUrl}`,
91 );
92 yield* HttpClient.get(readinessUrl).pipe(
93 Effect.flatMap((response) =>
94 response.status === 200
95 ? Effect.succeed(response)
96 : Effect.fail(
97 new Error(`Function not ready: ${response.status}`),
98 ),
99 ),
100 Effect.tapError((error) =>
101 Effect.logWarning(
102 `BCMDataExports test setup: fixture not ready yet (${String(error)})`,
103 ),
104 ),
105 Effect.retry({ schedule: readinessPolicy }),
106 );
107 }),
108 { timeout: 240_000 },
109 );
110
111 afterAll(sharedStack.destroy(), { timeout: 120_000 });
112
113 describe("binding registration", () => {
114 test.provider("all 6 capabilities initialize in the runtime", (_stack) =>
115 Effect.gen(function* () {
116 const response = yield* send(
117 HttpClientRequest.get(`${baseUrl}/bindings`),
118 ).pipe(Effect.flatMap((r) => r.json));
119 expect((response as any).bound).toHaveLength(6);
120 }),
121 );
122 });
123
124 describe("GetExport", () => {
125 test.provider("reads the fixture export's definition", (_stack) =>
126 Effect.gen(function* () {
127 const response = yield* send(
128 HttpClientRequest.get(`${baseUrl}/export`),
129 ).pipe(Effect.flatMap((r) => r.json));
130 expect((response as any).name).toBe(fixtureExportName);
131 expect((response as any).prefix).toBe("bindings");
132 expect((response as any).frequency).toBe("SYNCHRONOUS");
133 }),
134 );
135 });
136
137 describe("ListExports", () => {
138 test.provider(
139 "lists the fixture export among the account's exports",
140 (_stack) =>
141 Effect.gen(function* () {
142 const response = yield* send(
143 HttpClientRequest.get(`${baseUrl}/exports`),
144 ).pipe(Effect.flatMap((r) => r.json));
145 expect((response as any).names).toContain(fixtureExportName);
146 }),
147 );
148 });
149
150 describe("ListExecutions", () => {
151 test.provider(
152 "lists the export's executions (fresh export: >= 0)",
153 (_stack) =>
154 Effect.gen(function* () {
155 const response = yield* send(
156 HttpClientRequest.get(`${baseUrl}/executions`),
157 ).pipe(Effect.flatMap((r) => r.json));
158 expect(typeof (response as any).count).toBe("number");
159 expect((response as any).count).toBeGreaterThanOrEqual(0);
160 }),
161 );
162 });
163
164 describe("GetExecution", () => {
165 test.provider(
166 "surfaces a typed error for a nonexistent execution (proving the grant)",
167 (_stack) =>
168 Effect.gen(function* () {
169 const response = yield* send(
170 HttpClientRequest.get(`${baseUrl}/execution-not-found`),
171 ).pipe(Effect.flatMap((r) => r.json));
172 expect([
173 "ResourceNotFoundException",
174 "ValidationException",
175 ]).toContain((response as any).tag);
176 }),
177 );
178 });
179
180 describe("GetTable", () => {
181 test.provider("reads the COST_AND_USAGE_REPORT table schema", (_stack) =>
182 Effect.gen(function* () {
183 const response = yield* send(
184 HttpClientRequest.get(`${baseUrl}/table`),
185 ).pipe(Effect.flatMap((r) => r.json));
186 expect((response as any).tableName).toBe("COST_AND_USAGE_REPORT");
187 expect((response as any).columnCount).toBeGreaterThan(0);
188 }),
189 );
190 });
191
192 describe("ListTables", () => {
193 test.provider("lists the table dictionary", (_stack) =>
194 Effect.gen(function* () {
195 const response = yield* send(
196 HttpClientRequest.get(`${baseUrl}/tables`),
197 ).pipe(Effect.flatMap((r) => r.json));
198 expect((response as any).names).toContain("COST_AND_USAGE_REPORT");
199 }),
200 );
201 });
202 },
203);