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 RbinTestFunctionLive, { RbinTestFunction } from "./handler";
11
12const testOptions = { providers: AWS.providers() };
13const { test, beforeAll, afterAll } = Test.make(testOptions);
14const sharedStack = Core.scratchStack(testOptions, "RbinBindings");
15
16// Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17// over 60s on a fresh deploy.
18const readinessPolicy = Schedule.max([
19 Schedule.fixed("2 seconds"),
20 Schedule.recurs(75),
21]);
22
23let baseUrl: string;
24
25class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28}> {}
29
30// The Lambda fixture occasionally answers a transient 5xx under load (cold
31// re-init, IAM propagation on the freshly attached policy that the handler's
32// `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion
33// failure surfaces immediately.
34const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(
41 new TransientUpstream({ status: response.status, body }),
42 ),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([
50 Schedule.exponential("500 millis"),
51 Schedule.recurs(6),
52 ]),
53 }),
54 );
55
56const getJson = (path: string) =>
57 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
58 Effect.flatMap((r) => r.json),
59 );
60
61describe.sequential(
62 "Rbin Bindings",
63 {
64 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:rbin", "live"],
65 },
66 () => {
67 beforeAll(
68 Effect.gen(function* () {
69 yield* Effect.logInfo("Rbin test setup: destroying previous resources");
70 yield* sharedStack.destroy();
71
72 yield* Effect.logInfo("Rbin test setup: deploying fixture");
73 const attrs = yield* sharedStack.deploy(
74 Effect.gen(function* () {
75 return yield* RbinTestFunction;
76 }).pipe(Effect.provide(RbinTestFunctionLive)),
77 );
78
79 expect(attrs.functionUrl).toBeTruthy();
80 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
81
82 const readinessUrl = `${baseUrl}/bindings`;
83 yield* Effect.logInfo(
84 `Rbin test setup: probing readiness at ${readinessUrl}`,
85 );
86 yield* HttpClient.get(readinessUrl).pipe(
87 Effect.flatMap((response) =>
88 response.status === 200
89 ? Effect.succeed(response)
90 : Effect.fail(
91 new Error(`Function not ready: ${response.status}`),
92 ),
93 ),
94 Effect.tapError((error) =>
95 Effect.logWarning(
96 `Rbin test setup: fixture not ready yet (${String(error)})`,
97 ),
98 ),
99 Effect.retry({ schedule: readinessPolicy }),
100 );
101 }),
102 { timeout: 240_000 },
103 );
104
105 afterAll(sharedStack.destroy(), { timeout: 120_000 });
106
107 describe("binding registration", () => {
108 test.provider("both capabilities initialize in the runtime", (_stack) =>
109 Effect.gen(function* () {
110 const response = (yield* getJson("/bindings")) as { bound: string[] };
111 expect(response.bound).toEqual(["getRule", "listRules"]);
112 }),
113 );
114 });
115
116 describe("GetRule", () => {
117 test.provider(
118 "reads the bound rule's detail (injected identifier)",
119 (_stack) =>
120 Effect.gen(function* () {
121 const response = (yield* getJson("/rule")) as {
122 identifier: string;
123 resourceType: string;
124 retentionDays: number;
125 description: string;
126 };
127 expect(response.identifier).toBeTruthy();
128 expect(response.resourceType).toBe("EBS_SNAPSHOT");
129 expect(response.retentionDays).toBe(7);
130 expect(response.description).toBe(
131 "alchemy rbin bindings fixture rule",
132 );
133 }),
134 );
135 });
136
137 describe("ListRules", () => {
138 test.provider(
139 "enumerates the Region's rules including the fixture's",
140 (_stack) =>
141 Effect.gen(function* () {
142 const rule = (yield* getJson("/rule")) as { identifier: string };
143 const response = (yield* getJson("/rules")) as { ids: string[] };
144 expect(response.ids).toContain(rule.identifier);
145 }),
146 );
147 });
148 },
149);