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 Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import * as HttpClient from "effect/http/HttpClient";
8import RedshiftBindingsTestFunctionLive, {
9 RedshiftBindingsTestFunction,
10} from "./fixtures/bindings-handler";
11
12const testOptions = { providers: AWS.providers() };
13const { test, beforeAll, afterAll } = Test.make(testOptions);
14const sharedStack = Core.scratchStack(testOptions, "RedshiftBindings");
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
25const getJson = (path: string) =>
26 HttpClient.get(`${baseUrl}${path}`).pipe(
27 Effect.flatMap((response) =>
28 response.status >= 500
29 ? Effect.fail(new Error(`transient upstream ${response.status}`))
30 : Effect.succeed(response),
31 ),
32 Effect.retry({
33 schedule: Schedule.max([
34 Schedule.exponential("500 millis"),
35 Schedule.recurs(6),
36 ]),
37 }),
38 Effect.flatMap((r) => r.json),
39 );
40
41describe.sequential(
42 "Redshift Bindings",
43 {
44 tags: [
45 "provider:aws",
46 "provider:aws:lambda",
47 "provider:aws:redshift",
48 "live",
49 ],
50 },
51 () => {
52 beforeAll(
53 Effect.gen(function* () {
54 yield* Effect.logInfo(
55 "Redshift bindings setup: destroying previous resources",
56 );
57 yield* sharedStack.destroy();
58
59 yield* Effect.logInfo("Redshift bindings setup: deploying fixture");
60 const { functionUrl } = yield* sharedStack.deploy(
61 Effect.gen(function* () {
62 return yield* RedshiftBindingsTestFunction;
63 }).pipe(Effect.provide(RedshiftBindingsTestFunctionLive)),
64 );
65
66 expect(functionUrl).toBeTruthy();
67 baseUrl = functionUrl!.replace(/\/+$/, "");
68
69 const readinessUrl = `${baseUrl}/bindings`;
70 yield* HttpClient.get(readinessUrl).pipe(
71 Effect.flatMap((response) =>
72 response.status === 200
73 ? Effect.succeed(response)
74 : Effect.fail(
75 new Error(`Function not ready: ${response.status}`),
76 ),
77 ),
78 Effect.retry({ schedule: readinessPolicy }),
79 );
80 }),
81 { timeout: 240_000 },
82 );
83
84 afterAll(sharedStack.destroy(), { timeout: 120_000 });
85
86 describe("binding registration", () => {
87 test.provider("all capabilities initialize in the runtime", (_stack) =>
88 Effect.gen(function* () {
89 const response = yield* getJson("/bindings");
90 expect((response as any).bound).toHaveLength(5);
91 }),
92 );
93 });
94
95 describe("DescribeClusters", () => {
96 test.provider(
97 "surfaces the typed not-found tag for a nonexistent cluster",
98 (_stack) =>
99 Effect.gen(function* () {
100 const response = yield* getJson("/clusters");
101 expect((response as any).tag).toBe("ClusterNotFoundFault");
102 }),
103 );
104 });
105
106 describe("DescribeClusterSnapshots", () => {
107 test.provider("lists the account's manual snapshots", (_stack) =>
108 Effect.gen(function* () {
109 const response = yield* getJson("/snapshots");
110 expect((response as any).count).toBeGreaterThanOrEqual(0);
111 }),
112 );
113 });
114
115 describe("DescribeEvents", () => {
116 test.provider("lists the account's recent Redshift events", (_stack) =>
117 Effect.gen(function* () {
118 const response = yield* getJson("/events");
119 expect((response as any).count).toBeGreaterThanOrEqual(0);
120 }),
121 );
122 });
123
124 describe("DeleteClusterSnapshot", () => {
125 test.provider(
126 "surfaces the typed not-found tag for a nonexistent snapshot",
127 (_stack) =>
128 Effect.gen(function* () {
129 const response = yield* getJson("/delete-snapshot-probe");
130 expect((response as any).tag).toBe("ClusterSnapshotNotFoundFault");
131 }),
132 );
133 });
134
135 describe("CopyClusterSnapshot", () => {
136 test.provider(
137 "surfaces the typed not-found tag for a nonexistent source snapshot",
138 (_stack) =>
139 Effect.gen(function* () {
140 const response = yield* getJson("/copy-snapshot-probe");
141 expect((response as any).tag).toBe("ClusterSnapshotNotFoundFault");
142 }),
143 );
144 });
145 },
146);