1import * as AWS from "@/AWS";
2import * as Core from "@/Test/Core";
3import * as Test from "@/Test/Alchemy";
4import * as repostspace from "@distilled.cloud/aws/repostspace";
5import { describe, expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import * as HttpClient from "effect/http/HttpClient";
9import RePostSpaceBindingsFunctionLive, {
10 RePostSpaceBindingsFunction,
11} from "./bindings-handler";
12
13const testOptions = { providers: AWS.providers() };
14const { test, beforeAll, afterAll } = Test.make(testOptions);
15
16// re:Post Private is a paid tier (Basic/Standard) that requires IAM Identity
17// Center and provisions a space asynchronously (~30 minutes), so the live
18// Lambda E2E is gated behind AWS_TEST_REPOSTSPACE=1. The ungated probes
19// below prove the typed error unions the bindings depend on on every
20// account at near-zero cost.
21const RUN_LIVE = !!process.env.AWS_TEST_REPOSTSPACE;
22
23const BOGUS_SPACE_ID = "SPalchemynonexistentprobe0";
24
25test.provider(
26 "getChannel on a nonexistent space fails with ResourceNotFoundException",
27 () =>
28 Effect.gen(function* () {
29 const error = yield* Effect.flip(
30 repostspace.getChannel({
31 spaceId: BOGUS_SPACE_ID,
32 channelId: "CHalchemynonexistentprobe0",
33 }),
34 );
35 expect(error._tag).toBe("ResourceNotFoundException");
36 }),
37 { tags: ["provider:aws", "provider:aws:repostspace", "live"] },
38);
39
40test.provider(
41 "listChannels on a nonexistent space fails with ResourceNotFoundException",
42 () =>
43 Effect.gen(function* () {
44 const error = yield* Effect.flip(
45 repostspace.listChannels({ spaceId: BOGUS_SPACE_ID }),
46 );
47 expect(error._tag).toBe("ResourceNotFoundException");
48 }),
49 { tags: ["provider:aws", "provider:aws:repostspace", "live"] },
50);
51
52test.provider(
53 "sendInvites on a nonexistent space fails with a typed tag",
54 () =>
55 Effect.gen(function* () {
56 const error = yield* Effect.flip(
57 repostspace.sendInvites({
58 spaceId: BOGUS_SPACE_ID,
59 accessorIds: ["00000000-0000-0000-0000-000000000000"],
60 title: "alchemy probe",
61 body: "alchemy probe",
62 }),
63 );
64 expect(["ResourceNotFoundException", "ValidationException"]).toContain(
65 error._tag,
66 );
67 }),
68 { tags: ["provider:aws", "provider:aws:repostspace", "live"] },
69);
70
71test.provider(
72 "batchAddRole on a nonexistent space fails with a typed tag",
73 () =>
74 Effect.gen(function* () {
75 const error = yield* Effect.flip(
76 repostspace.batchAddRole({
77 spaceId: BOGUS_SPACE_ID,
78 accessorIds: ["00000000-0000-0000-0000-000000000000"],
79 role: "EXPERT",
80 }),
81 );
82 expect(["ResourceNotFoundException", "ValidationException"]).toContain(
83 error._tag,
84 );
85 }),
86 { tags: ["provider:aws", "provider:aws:repostspace", "live"] },
87);
88
89const sharedStack = Core.scratchStack(testOptions, "RePostSpaceBindings");
90
91let baseUrl: string;
92
93const get = (path: string) =>
94 HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
95const post = (path: string) =>
96 HttpClient.post(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
97
98describe(
99 "RePostSpace Bindings (E2E)",
100 {
101 tags: [
102 "provider:aws",
103 "provider:aws:batch",
104 "provider:aws:lambda",
105 "provider:aws:repostspace",
106 "live",
107 ],
108 },
109 () => {
110 beforeAll(
111 Effect.gen(function* () {
112 if (!RUN_LIVE) return;
113 yield* Effect.logInfo("RePostSpace E2E setup: destroying previous run");
114 yield* sharedStack.destroy();
115
116 yield* Effect.logInfo(
117 "RePostSpace E2E setup: deploying space + Lambda (~30 min)",
118 );
119 const { functionUrl } = yield* sharedStack.deploy(
120 Effect.gen(function* () {
121 return yield* RePostSpaceBindingsFunction;
122 }).pipe(Effect.provide(RePostSpaceBindingsFunctionLive)),
123 );
124
125 expect(functionUrl).toBeTruthy();
126 baseUrl = functionUrl!.replace(/\/+$/, "");
127
128 // Readiness probe — fresh function URLs take seconds to serve 200s.
129 yield* HttpClient.get(`${baseUrl}/bindings`).pipe(
130 Effect.flatMap((response) =>
131 response.status === 200
132 ? Effect.succeed(response)
133 : Effect.fail(
134 new Error(`Function not ready: ${response.status}`),
135 ),
136 ),
137 Effect.retry({
138 schedule: Schedule.max([
139 Schedule.fixed("2 seconds"),
140 Schedule.recurs(60),
141 ]),
142 }),
143 );
144 }),
145 // space provisioning is asynchronous and can take ~30 minutes.
146 { timeout: 3_600_000 },
147 );
148 afterAll(
149 Effect.gen(function* () {
150 if (!RUN_LIVE) return;
151 // spaces bill while they exist — always destroy.
152 yield* sharedStack.destroy();
153 }),
154 { timeout: 600_000 },
155 );
156
157 test.provider.skipIf(!RUN_LIVE)(
158 "all 11 capabilities initialize in the runtime",
159 () =>
160 Effect.gen(function* () {
161 const response = (yield* get("/bindings")) as any;
162 expect(response.bound).toHaveLength(11);
163 }),
164 );
165
166 test.provider.skipIf(!RUN_LIVE)(
167 "CreateChannel + GetChannel + UpdateChannel + ListChannels round-trip",
168 () =>
169 Effect.gen(function* () {
170 const created = (yield* post("/channels")) as any;
171 expect(typeof created.channelId).toBe("string");
172 expect(created.name).toBe("alchemy-bindings-channel");
173 expect(created.renamed).toBe("alchemy-bindings-channel-renamed");
174
175 const listed = (yield* get("/channels")) as any;
176 expect(listed.count).toBeGreaterThanOrEqual(1);
177
178 // Channel-role mutations against the real channel with a bogus
179 // accessor — the API reports per-accessor errors (or a typed 400),
180 // either proves the grant + injection end-to-end.
181 const roles = (yield* post(
182 `/channel-roles/bogus?channelId=${created.channelId}`,
183 )) as any;
184 expect(roles.added).toBeDefined();
185 expect(roles.removed).toBeDefined();
186 }),
187 { timeout: 120_000 },
188 );
189
190 test.provider.skipIf(!RUN_LIVE)(
191 "space-role and admin bindings surface typed results for bogus accessors",
192 () =>
193 Effect.gen(function* () {
194 const roles = (yield* post("/roles/bogus")) as any;
195 expect(roles.added).toBeDefined();
196 expect(roles.removed).toBeDefined();
197
198 const admins = (yield* post("/admins/bogus")) as any;
199 expect(admins.register).toBeDefined();
200 expect(admins.deregister).toBeDefined();
201
202 const invites = (yield* post("/invites/bogus")) as any;
203 expect(invites).toBeDefined();
204 }),
205 { timeout: 120_000 },
206 );
207 },
208);