1import * as AWS from "@/AWS";
2import { Namespace, Workgroup } from "@/AWS/RedshiftServerless";
3import * as Test from "@/Test/Alchemy";
4import * as redshiftserverless from "@distilled.cloud/aws/redshift-serverless";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8
9const { test } = Test.make({ providers: AWS.providers() });
10
11// Ungated typed-error probes: prove the distilled error union carries the
12// not-found tag the providers' observe/read/delete paths depend on.
13test.provider(
14 "getWorkgroup on a nonexistent workgroup fails with ResourceNotFoundException",
15 () =>
16 Effect.gen(function* () {
17 const error = yield* Effect.flip(
18 redshiftserverless.getWorkgroup({
19 workgroupName: "alchemy-nonexistent-probe-wg",
20 }),
21 );
22 expect(error._tag).toBe("ResourceNotFoundException");
23 }),
24 { tags: ["provider:aws", "provider:aws:redshiftserverless", "live"] },
25);
26
27test.provider(
28 "getNamespace on a nonexistent namespace fails with ResourceNotFoundException",
29 () =>
30 Effect.gen(function* () {
31 const error = yield* Effect.flip(
32 redshiftserverless.getNamespace({
33 namespaceName: "alchemy-nonexistent-probe-ns",
34 }),
35 );
36 expect(error._tag).toBe("ResourceNotFoundException");
37 }),
38 { tags: ["provider:aws", "provider:aws:redshiftserverless", "live"] },
39);
40
41const assertNamespaceGone = (name: string) =>
42 Effect.gen(function* () {
43 const status = yield* redshiftserverless
44 .getNamespace({ namespaceName: name })
45 .pipe(
46 Effect.map((r) => (r.namespace?.status ?? "UNKNOWN").toUpperCase()),
47 Effect.catchTag("ResourceNotFoundException", () =>
48 Effect.succeed("GONE" as const),
49 ),
50 );
51 if (status !== "GONE") {
52 return yield* Effect.fail(
53 new Error(`Redshift namespace still exists (status: ${status})`),
54 );
55 }
56 }).pipe(
57 Effect.retry({
58 schedule: Schedule.max([
59 Schedule.fixed("5 seconds"),
60 Schedule.recurs(24),
61 ]),
62 }),
63 );
64
65const assertWorkgroupGone = (name: string) =>
66 Effect.gen(function* () {
67 const status = yield* redshiftserverless
68 .getWorkgroup({ workgroupName: name })
69 .pipe(
70 Effect.map((r) => (r.workgroup?.status ?? "UNKNOWN").toUpperCase()),
71 Effect.catchTag("ResourceNotFoundException", () =>
72 Effect.succeed("GONE" as const),
73 ),
74 );
75 if (status !== "GONE") {
76 return yield* Effect.fail(
77 new Error(`Redshift workgroup still exists (status: ${status})`),
78 );
79 }
80 }).pipe(
81 Effect.retry({
82 schedule: Schedule.max([
83 Schedule.fixed("5 seconds"),
84 Schedule.recurs(24),
85 ]),
86 }),
87 );
88
89// A namespace is cheap but a workgroup bills against its RPU floor while it
90// exists, so the full lifecycle is gated behind AWS_TEST_SLOW=1 and always
91// destroys immediately (workgroup, then namespace).
92test.provider.skipIf(!process.env.AWS_TEST_SLOW)(
93 "create a namespace + minimal workgroup, verify AVAILABLE, destroy, verify gone",
94 (stack) =>
95 Effect.gen(function* () {
96 // Clean slate in case a previous run died mid-flight.
97 yield* stack.destroy();
98
99 const { namespace, workgroup } = yield* stack.deploy(
100 Effect.gen(function* () {
101 const namespace = yield* Namespace("AnalyticsNamespace", {
102 namespaceName: "alchemy-test-rs-lifecycle-ns",
103 dbName: "dev",
104 adminUsername: "alchemyadmin",
105 manageAdminPassword: true,
106 });
107 const workgroup = yield* Workgroup("AnalyticsWorkgroup", {
108 workgroupName: "alchemy-test-rs-lifecycle-wg",
109 namespaceName: namespace.namespaceName,
110 baseCapacity: 8,
111 publiclyAccessible: false,
112 });
113 return { namespace, workgroup };
114 }),
115 );
116
117 expect(namespace.namespaceName).toBe("alchemy-test-rs-lifecycle-ns");
118 expect(namespace.namespaceArn).toContain(":namespace/");
119 expect(namespace.status.toUpperCase()).toBe("AVAILABLE");
120 // manageAdminPassword => a Secrets Manager secret is provisioned.
121 expect(namespace.adminPasswordSecretArn).toBeDefined();
122
123 expect(workgroup.workgroupName).toBe("alchemy-test-rs-lifecycle-wg");
124 expect(workgroup.workgroupArn).toContain(":workgroup/");
125 expect(workgroup.status.toUpperCase()).toBe("AVAILABLE");
126 expect(workgroup.endpointAddress).toBeDefined();
127 expect(workgroup.endpointPort).toBe(5439);
128 expect(workgroup.publiclyAccessible).toBe(false);
129
130 // Out-of-band verification via distilled.
131 const observedWg = yield* redshiftserverless.getWorkgroup({
132 workgroupName: workgroup.workgroupName,
133 });
134 expect(observedWg.workgroup?.status?.toUpperCase()).toBe("AVAILABLE");
135 expect(observedWg.workgroup?.baseCapacity).toBe(8);
136 expect(observedWg.workgroup?.namespaceName).toBe(namespace.namespaceName);
137
138 const observedNs = yield* redshiftserverless.getNamespace({
139 namespaceName: namespace.namespaceName,
140 });
141 expect(observedNs.namespace?.status?.toUpperCase()).toBe("AVAILABLE");
142
143 // Destroy immediately — a running workgroup bills its RPU floor.
144 yield* stack.destroy();
145 yield* assertWorkgroupGone(workgroup.workgroupName);
146 yield* assertNamespaceGone(namespace.namespaceName);
147 }),
148 // namespace (~1 min) + workgroup create (~2-5 min) + delete (~3-5 min).
149 {
150 tags: ["provider:aws", "provider:aws:redshiftserverless", "live"],
151 timeout: 1_200_000,
152 },
153);