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

main
ts 276 lines 11.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 MicrosoftTeamsChannelConfiguration,
4 SlackChannelConfiguration,
5 } from "@alchemy.run/aws/Chatbot";
6 import { Role } from "@alchemy.run/aws/IAM/Role";
7 import { expect } from "@alchemy.run/test";
8 import * as Test from "@alchemy.run/test/Alchemy";
9 import * as chatbot from "@distilled.cloud/aws/chatbot";
10 import * as Data from "effect/Data";
11 import * as Effect from "effect/Effect";
12 import * as Schedule from "effect/Schedule";
13
14 const { test } = Test.make({ providers: AWS.providers() });
15
16 // ---------------------------------------------------------------------------
17 // Ungated probes — Slack/Teams channel configurations require a workspace
18 // onboarded via the AWS Chatbot console OAuth flow, which cannot be scripted.
19 // These probes prove the typed error semantics the providers depend on, in
20 // every CI pass, at near-zero cost.
21 // ---------------------------------------------------------------------------
22
23 // Workspace-onboarding gate: creating a configuration against a workspace
24 // that has not been authorized via the console OAuth flow is rejected with
25 // the synthetic SlackWorkspaceNotAuthorized tag (patched in
26 // submodules/distilled/packages/aws/patches/chatbot.json). A failed create still
27 // reserves the configuration name server-side (undeletable tombstone), so
28 // repeated runs of this probe surface the typed ConflictException instead —
29 // both tags prove the create is rejected without onboarding.
30 test.provider(
31 "createSlackChannelConfiguration without an onboarded workspace fails with SlackWorkspaceNotAuthorized",
32 () =>
33 Effect.gen(function* () {
34 const { accountId } = yield* AWS.AWSEnvironment.current;
35 const error = yield* Effect.flip(
36 chatbot.createSlackChannelConfiguration({
37 SlackTeamId: "T0000000000",
38 SlackChannelId: "C0000000000",
39 ConfigurationName: "alchemy-chatbot-probe-slack",
40 IamRoleArn: `arn:aws:iam::${accountId}:role/alchemy-nonexistent-role`,
41 }),
42 );
43 expect(["SlackWorkspaceNotAuthorized", "ConflictException"]).toContain(error._tag);
44 }),
45 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 30_000 },
46 );
47
48 // Team-onboarding gate: the Teams create fails at team validation (before
49 // any name reservation), so this probe is exactly repeatable.
50 test.provider(
51 "createMicrosoftTeamsChannelConfiguration without a configured team fails with MicrosoftTeamsTeamNotConfigured",
52 () =>
53 Effect.gen(function* () {
54 const { accountId } = yield* AWS.AWSEnvironment.current;
55 const error = yield* Effect.flip(
56 chatbot.createMicrosoftTeamsChannelConfiguration({
57 ChannelId: "19%3aalchemyprobe%40thread.tacv2",
58 TeamId: "0a1b2c3d-4e5f-1a2b-3c4d-0a1b2c3d4e5f",
59 TenantId: "1a2b3c4d-5e6f-1a2b-3c4d-1a2b3c4d5e6f",
60 ConfigurationName: "alchemy-chatbot-probe-teams",
61 IamRoleArn: `arn:aws:iam::${accountId}:role/alchemy-nonexistent-role`,
62 }),
63 );
64 expect(error._tag).toBe("MicrosoftTeamsTeamNotConfigured");
65 }),
66 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 30_000 },
67 );
68
69 // Validates the distilled chatbot patch: the wire ResourceNotFoundException
70 // is outside the Smithy model's union for this operation and is added via
71 // patches/chatbot.json. The Teams read endpoint occasionally has >30s tail
72 // latency under a full c128 sweep, so this probe gets a narrow 60s budget;
73 // the typed assertion is unchanged and no service error is masked.
74 test.provider(
75 "getMicrosoftTeamsChannelConfiguration on a nonexistent configuration fails with ResourceNotFoundException",
76 () =>
77 Effect.gen(function* () {
78 const { accountId } = yield* AWS.AWSEnvironment.current;
79 const error = yield* Effect.flip(
80 chatbot.getMicrosoftTeamsChannelConfiguration({
81 ChatConfigurationArn: `arn:aws:chatbot::${accountId}:chat-configuration/microsoft-teams-channel/alchemy-probe-nonexistent`,
82 }),
83 );
84 expect(error._tag).toBe("ResourceNotFoundException");
85 }),
86 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 60_000 },
87 );
88
89 // The Slack read path is a filter — an unknown ARN yields an empty list, not
90 // a not-found error.
91 test.provider(
92 "describeSlackChannelConfigurations with an unknown ARN yields an empty list",
93 () =>
94 Effect.gen(function* () {
95 const { accountId } = yield* AWS.AWSEnvironment.current;
96 const result = yield* chatbot.describeSlackChannelConfigurations({
97 ChatConfigurationArn: `arn:aws:chatbot::${accountId}:chat-configuration/slack-channel/alchemy-probe-nonexistent`,
98 });
99 expect(result.SlackChannelConfigurations).toEqual([]);
100 }),
101 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 30_000 },
102 );
103
104 // ---------------------------------------------------------------------------
105 // Gated live lifecycles — require AWS_TEST_CHATBOT=1 plus an account whose
106 // Slack workspace / Microsoft Teams team has been onboarded via the console
107 // OAuth flow, with the workspace identifiers supplied via env vars.
108 // ---------------------------------------------------------------------------
109
110 const chatbotAssumeRolePolicy: AWS.IAM.PolicyDocument = {
111 Version: "2012-10-17",
112 Statement: [
113 {
114 Effect: "Allow",
115 Principal: { Service: "chatbot.amazonaws.com" },
116 Action: ["sts:AssumeRole"],
117 },
118 ],
119 };
120
121 class ConfigurationStillExists extends Data.TaggedError("ConfigurationStillExists")<{
122 readonly arn: string;
123 }> {}
124
125 const assertSlackConfigurationDeleted = (arn: string) =>
126 chatbot.describeSlackChannelConfigurations({ ChatConfigurationArn: arn }).pipe(
127 Effect.flatMap((r) =>
128 (r.SlackChannelConfigurations ?? []).length === 0
129 ? Effect.void
130 : Effect.fail(new ConfigurationStillExists({ arn })),
131 ),
132 Effect.retry({
133 while: (e) => e._tag === "ConfigurationStillExists",
134 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
135 }),
136 );
137
138 test.provider.skipIf(
139 !process.env.AWS_TEST_CHATBOT ||
140 !process.env.AWS_CHATBOT_SLACK_TEAM_ID ||
141 !process.env.AWS_CHATBOT_SLACK_CHANNEL_ID,
142 )(
143 "create, update, delete Slack channel configuration",
144 (stack) =>
145 Effect.gen(function* () {
146 yield* stack.destroy();
147
148 const slackTeamId = process.env.AWS_CHATBOT_SLACK_TEAM_ID!;
149 const slackChannelId = process.env.AWS_CHATBOT_SLACK_CHANNEL_ID!;
150
151 const deploy = (loggingLevel: "ERROR" | "INFO") =>
152 stack.deploy(
153 Effect.gen(function* () {
154 const role = yield* Role("ChatbotSlackRole", {
155 assumeRolePolicyDocument: chatbotAssumeRolePolicy,
156 managedPolicyArns: ["arn:aws:iam::aws:policy/ReadOnlyAccess"],
157 });
158 const config = yield* SlackChannelConfiguration("Config", {
159 slackTeamId,
160 slackChannelId,
161 iamRoleArn: role.roleArn,
162 loggingLevel,
163 tags: { Environment: "test" },
164 });
165 return { config };
166 }),
167 );
168
169 const { config } = yield* deploy("ERROR");
170 expect(config.chatConfigurationArn).toContain(":chat-configuration/slack-channel/");
171 expect(config.slackTeamId).toBe(slackTeamId);
172 expect(config.slackChannelId).toBe(slackChannelId);
173
174 // out-of-band verification via distilled
175 const observed = yield* chatbot
176 .describeSlackChannelConfigurations({
177 ChatConfigurationArn: config.chatConfigurationArn,
178 })
179 .pipe(Effect.map((r) => r.SlackChannelConfigurations?.[0]));
180 expect(observed?.SlackChannelId).toBe(slackChannelId);
181 expect(observed?.LoggingLevel).toBe("ERROR");
182
183 // update the logging level in place
184 const { config: updated } = yield* deploy("INFO");
185 expect(updated.chatConfigurationArn).toBe(config.chatConfigurationArn);
186 const afterUpdate = yield* chatbot
187 .describeSlackChannelConfigurations({
188 ChatConfigurationArn: config.chatConfigurationArn,
189 })
190 .pipe(Effect.map((r) => r.SlackChannelConfigurations?.[0]));
191 expect(afterUpdate?.LoggingLevel).toBe("INFO");
192
193 yield* stack.destroy();
194 yield* assertSlackConfigurationDeleted(config.chatConfigurationArn);
195 }),
196 {
197 tags: ["provider:aws", "provider:aws:chatbot", "provider:aws:iam", "live"],
198 timeout: 300_000,
199 },
200 );
201
202 const findTeamsConfiguration = (arn: string) =>
203 chatbot.getMicrosoftTeamsChannelConfiguration({ ChatConfigurationArn: arn }).pipe(
204 Effect.map((r) => r.ChannelConfiguration),
205 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
206 );
207
208 const assertTeamsConfigurationDeleted = (arn: string) =>
209 findTeamsConfiguration(arn).pipe(
210 Effect.flatMap((config) =>
211 config === undefined ? Effect.void : Effect.fail(new ConfigurationStillExists({ arn })),
212 ),
213 Effect.retry({
214 while: (e) => e._tag === "ConfigurationStillExists",
215 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
216 }),
217 );
218
219 test.provider.skipIf(
220 !process.env.AWS_TEST_CHATBOT ||
221 !process.env.AWS_CHATBOT_TEAMS_TEAM_ID ||
222 !process.env.AWS_CHATBOT_TEAMS_TENANT_ID ||
223 !process.env.AWS_CHATBOT_TEAMS_CHANNEL_ID,
224 )(
225 "create, update, delete Microsoft Teams channel configuration",
226 (stack) =>
227 Effect.gen(function* () {
228 yield* stack.destroy();
229
230 const teamId = process.env.AWS_CHATBOT_TEAMS_TEAM_ID!;
231 const tenantId = process.env.AWS_CHATBOT_TEAMS_TENANT_ID!;
232 const teamsChannelId = process.env.AWS_CHATBOT_TEAMS_CHANNEL_ID!;
233
234 const deploy = (loggingLevel: "ERROR" | "INFO") =>
235 stack.deploy(
236 Effect.gen(function* () {
237 const role = yield* Role("ChatbotTeamsRole", {
238 assumeRolePolicyDocument: chatbotAssumeRolePolicy,
239 managedPolicyArns: ["arn:aws:iam::aws:policy/ReadOnlyAccess"],
240 });
241 const config = yield* MicrosoftTeamsChannelConfiguration("Config", {
242 teamId,
243 tenantId,
244 teamsChannelId,
245 iamRoleArn: role.roleArn,
246 loggingLevel,
247 tags: { Environment: "test" },
248 });
249 return { config };
250 }),
251 );
252
253 const { config } = yield* deploy("ERROR");
254 expect(config.chatConfigurationArn).toContain(":chat-configuration/microsoft-teams-channel/");
255 expect(config.teamId).toBe(teamId);
256 expect(config.tenantId).toBe(tenantId);
257
258 // out-of-band verification via distilled
259 const observed = yield* findTeamsConfiguration(config.chatConfigurationArn);
260 expect(observed?.ChannelId).toBe(teamsChannelId);
261 expect(observed?.LoggingLevel).toBe("ERROR");
262
263 // update the logging level in place
264 const { config: updated } = yield* deploy("INFO");
265 expect(updated.chatConfigurationArn).toBe(config.chatConfigurationArn);
266 const afterUpdate = yield* findTeamsConfiguration(config.chatConfigurationArn);
267 expect(afterUpdate?.LoggingLevel).toBe("INFO");
268
269 yield* stack.destroy();
270 yield* assertTeamsConfigurationDeleted(config.chatConfigurationArn);
271 }),
272 {
273 tags: ["provider:aws", "provider:aws:chatbot", "provider:aws:iam", "live"],
274 timeout: 300_000,
275 },
276 );