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

main
ts 253 lines 9.08 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 Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import ChatbotTestFunctionLive, { ChatbotTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "ChatbotBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The shared Lambda fixture occasionally answers a transient 5xx under load
28 // (cold re-init, IAM propagation on the freshly attached policy). Retry only
29 // 5xx; a genuine 4xx/assertion failure surfaces immediately.
30 const send = (request: HttpClientRequest.HttpClientRequest) =>
31 HttpClient.execute(request).pipe(
32 Effect.flatMap((response) =>
33 response.status >= 500
34 ? response.text.pipe(
35 Effect.flatMap((body) =>
36 Effect.fail(new TransientUpstream({ status: response.status, body })),
37 ),
38 )
39 : Effect.succeed(response),
40 ),
41 Effect.retry({
42 while: (e) => e._tag === "TransientUpstream",
43 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
44 }),
45 );
46
47 const getJson = (path: string) =>
48 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
49
50 const postJson = (path: string) =>
51 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
52
53 // The testing account has no Slack workspace or Teams team onboarded, so the
54 // reads answer real empty lists (a REAL data-plane success through each
55 // binding's IAM grant) and the identity deletes answer typed error tags for
56 // the nonexistent identities.
57 describe.sequential(
58 "Chatbot Bindings",
59 { tags: ["provider:aws", "provider:aws:chatbot", "provider:aws:lambda", "live"] },
60 () => {
61 beforeAll(
62 Effect.gen(function* () {
63 yield* Effect.logInfo("Chatbot test setup: destroying previous resources");
64 yield* sharedStack.destroy();
65
66 yield* Effect.logInfo("Chatbot test setup: deploying fixture");
67 const { functionUrl } = yield* sharedStack.deploy(
68 Effect.gen(function* () {
69 return yield* ChatbotTestFunction;
70 }).pipe(Effect.provide(ChatbotTestFunctionLive)),
71 );
72
73 expect(functionUrl).toBeTruthy();
74 baseUrl = functionUrl!.replace(/\/+$/, "");
75
76 const readinessUrl = `${baseUrl}/bindings`;
77 yield* Effect.logInfo(`Chatbot test setup: probing readiness at ${readinessUrl}`);
78 yield* HttpClient.get(readinessUrl).pipe(
79 Effect.flatMap((response) =>
80 response.status === 200
81 ? Effect.succeed(response)
82 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
83 ),
84 Effect.tapError((error) =>
85 Effect.logWarning(`Chatbot test setup: fixture not ready yet (${String(error)})`),
86 ),
87 Effect.retry({ schedule: readinessPolicy }),
88 );
89 }),
90 { timeout: 240_000 },
91 );
92
93 afterAll(sharedStack.destroy(), { timeout: 120_000 });
94
95 describe("binding registration", () => {
96 test.provider("all 10 account-level capabilities initialize", (_stack) =>
97 Effect.gen(function* () {
98 const response = yield* getJson("/bindings");
99 expect((response as any).bound).toHaveLength(10);
100 }),
101 );
102 });
103
104 describe("GetAccountPreferences", () => {
105 test.provider("succeeds end-to-end through the binding's IAM grant", (_stack) =>
106 Effect.gen(function* () {
107 const response = (yield* getJson("/account-preferences")) as {
108 ok: boolean;
109 hasPreferences: boolean;
110 };
111 expect(response.ok).toBe(true);
112 expect(response.hasPreferences).toBe(true);
113 }),
114 );
115 });
116
117 describe("UpdateAccountPreferences", () => {
118 test.provider("no-op round-trip write succeeds through the binding", (_stack) =>
119 Effect.gen(function* () {
120 const response = (yield* postJson("/account-preferences-roundtrip")) as {
121 ok: boolean;
122 tag?: string;
123 };
124 expect(response.ok).toBe(true);
125 }),
126 );
127 });
128
129 describe("DescribeSlackWorkspaces", () => {
130 test.provider("answers the workspace list", (_stack) =>
131 Effect.gen(function* () {
132 const response = (yield* getJson("/slack-workspaces")) as { ok: boolean; count: number };
133 expect(response.ok).toBe(true);
134 expect(response.count).toBeGreaterThanOrEqual(0);
135 }),
136 );
137 });
138
139 describe("DescribeSlackUserIdentities", () => {
140 test.provider("answers the identity list", (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* getJson("/slack-user-identities")) as {
143 ok: boolean;
144 count: number;
145 };
146 expect(response.ok).toBe(true);
147 expect(response.count).toBeGreaterThanOrEqual(0);
148 }),
149 );
150 });
151
152 describe("ListMicrosoftTeamsConfiguredTeams", () => {
153 test.provider("answers the configured team list", (_stack) =>
154 Effect.gen(function* () {
155 const response = (yield* getJson("/teams-configured-teams")) as {
156 ok: boolean;
157 count: number;
158 };
159 expect(response.ok).toBe(true);
160 expect(response.count).toBeGreaterThanOrEqual(0);
161 }),
162 );
163 });
164
165 describe("ListMicrosoftTeamsUserIdentities", () => {
166 test.provider("answers the identity list", (_stack) =>
167 Effect.gen(function* () {
168 const response = (yield* getJson("/teams-user-identities")) as {
169 ok: boolean;
170 count: number;
171 };
172 expect(response.ok).toBe(true);
173 expect(response.count).toBeGreaterThanOrEqual(0);
174 }),
175 );
176 });
177
178 describe("DeleteSlackUserIdentity", () => {
179 test.provider("answers a typed tag for a nonexistent identity", (_stack) =>
180 Effect.gen(function* () {
181 const { accountId } = yield* AWS.AWSEnvironment.current;
182 const response = (yield* postJson(
183 `/delete-slack-user-identity?account=${accountId}`,
184 )) as { ok: boolean; tag?: string };
185 expect(response.ok).toBe(false);
186 expect([
187 "DeleteSlackUserIdentityException",
188 "InvalidParameterException",
189 "ResourceNotFoundException",
190 ]).toContain(response.tag);
191 }),
192 );
193 });
194
195 describe("DeleteSlackWorkspaceAuthorization", () => {
196 test.provider(
197 "answers idempotently or with a typed tag for a workspace that was never onboarded",
198 (_stack) =>
199 Effect.gen(function* () {
200 const response = (yield* postJson("/delete-slack-workspace-authorization")) as {
201 ok: boolean;
202 tag?: string;
203 };
204 if (!response.ok) {
205 expect([
206 "DeleteSlackWorkspaceAuthorizationFault",
207 "InvalidParameterException",
208 ]).toContain(response.tag);
209 }
210 }),
211 );
212 });
213
214 describe("DeleteMicrosoftTeamsUserIdentity", () => {
215 test.provider("answers a typed tag for a nonexistent identity", (_stack) =>
216 Effect.gen(function* () {
217 const { accountId } = yield* AWS.AWSEnvironment.current;
218 const response = (yield* postJson(
219 `/delete-teams-user-identity?account=${accountId}`,
220 )) as { ok: boolean; tag?: string };
221 expect(response.ok).toBe(false);
222 expect([
223 "DeleteMicrosoftTeamsUserIdentityException",
224 "InvalidParameterException",
225 "ResourceNotFoundException",
226 ]).toContain(response.tag);
227 }),
228 );
229 });
230
231 describe("DeleteMicrosoftTeamsConfiguredTeam", () => {
232 test.provider(
233 "answers the typed not-found tag for a team that was never onboarded",
234 (_stack) =>
235 Effect.gen(function* () {
236 // ResourceNotFoundException is outside the Smithy model's union
237 // for this operation — patched in distilled patches/chatbot.json.
238 const response = (yield* postJson("/delete-teams-configured-team")) as {
239 ok: boolean;
240 tag?: string;
241 };
242 if (!response.ok) {
243 expect([
244 "DeleteTeamsConfiguredTeamException",
245 "InvalidParameterException",
246 "ResourceNotFoundException",
247 ]).toContain(response.tag);
248 }
249 }),
250 );
251 });
252 },
253 );