The best way to control Claude Code, Codex and more from any computer or phone

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