@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 166 lines 7 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Association, CustomAction, SlackChannelConfiguration } from "@alchemy.run/aws/Chatbot";
3 import { Role } from "@alchemy.run/aws/IAM/Role";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as chatbot from "@distilled.cloud/aws/chatbot";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // ---------------------------------------------------------------------------
15 // Ungated typed-error probes — a full association lifecycle needs a Slack or
16 // Teams channel configuration, which requires a workspace onboarded via the
17 // AWS Chatbot console OAuth flow (cannot be scripted). These probes prove the
18 // typed error semantics the Association provider's observe/associate paths
19 // depend on, in every CI pass, at near-zero cost.
20 // ---------------------------------------------------------------------------
21
22 // The observe path the provider depends on: listAssociations against an
23 // unknown (well-formed) configuration ARN yields an empty list rather than a
24 // not-found error, so observation reads it as "no association".
25 test.provider(
26 "listAssociations with an unknown configuration ARN yields an empty list",
27 () =>
28 Effect.gen(function* () {
29 const { accountId } = yield* AWS.AWSEnvironment.current;
30 const listings = yield* chatbot.listAssociations
31 .items({
32 ChatConfiguration: `arn:aws:chatbot::${accountId}:chat-configuration/slack-channel/alchemy-probe-nonexistent`,
33 })
34 .pipe(Stream.runCollect, Effect.timeout("45 seconds"));
35 expect(Array.from(listings)).toEqual([]);
36 }),
37 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 60_000 },
38 );
39
40 // Validates the distilled chatbot patch: associateToConfiguration against an
41 // unknown configuration ARN is rejected with the wire
42 // ResourceNotFoundException ("Channel Arn ... does not exist!") rather than
43 // creating a dangling association.
44 test.provider(
45 "associateToConfiguration with an unknown configuration ARN fails with ResourceNotFoundException",
46 () =>
47 Effect.gen(function* () {
48 const { accountId } = yield* AWS.AWSEnvironment.current;
49 const error = yield* Effect.flip(
50 chatbot.associateToConfiguration({
51 ChatConfiguration: `arn:aws:chatbot::${accountId}:chat-configuration/slack-channel/alchemy-probe-nonexistent`,
52 Resource: `arn:aws:chatbot::${accountId}:custom-action/alchemy-probe-nonexistent`,
53 }),
54 );
55 expect(error._tag).toBe("ResourceNotFoundException");
56 }),
57 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 30_000 },
58 );
59
60 // ---------------------------------------------------------------------------
61 // Gated live lifecycle — requires AWS_TEST_CHATBOT=1 plus an account whose
62 // Slack workspace has been onboarded via the console OAuth flow, with the
63 // workspace identifiers supplied via env vars.
64 // ---------------------------------------------------------------------------
65
66 class AssociationMismatch extends Data.TaggedError("AssociationMismatch")<{
67 readonly configuration: string;
68 readonly expected: readonly string[];
69 }> {}
70
71 const listAssociatedResources = (configuration: string) =>
72 chatbot.listAssociations.items({ ChatConfiguration: configuration }).pipe(
73 Stream.runCollect,
74 Effect.map((listings) => Array.from(listings).map((listing) => listing.Resource)),
75 Effect.catchTag("InvalidRequestException", () => Effect.succeed([] as string[])),
76 );
77
78 // Bounded wait until the configuration's association list equals `expected`.
79 const assertAssociations = (configuration: string, expected: readonly string[]) =>
80 listAssociatedResources(configuration).pipe(
81 Effect.flatMap((resources) =>
82 JSON.stringify([...resources].sort()) === JSON.stringify([...expected].sort())
83 ? Effect.void
84 : Effect.fail(new AssociationMismatch({ configuration, expected })),
85 ),
86 Effect.retry({
87 while: (e): boolean => e._tag === "AssociationMismatch",
88 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
89 }),
90 );
91
92 const chatbotAssumeRolePolicy: AWS.IAM.PolicyDocument = {
93 Version: "2012-10-17",
94 Statement: [
95 {
96 Effect: "Allow",
97 Principal: { Service: "chatbot.amazonaws.com" },
98 Action: ["sts:AssumeRole"],
99 },
100 ],
101 };
102
103 test.provider.skipIf(
104 !process.env.AWS_TEST_CHATBOT ||
105 !process.env.AWS_CHATBOT_SLACK_TEAM_ID ||
106 !process.env.AWS_CHATBOT_SLACK_CHANNEL_ID,
107 )(
108 "create, replace (re-point to a second action), delete association",
109 (stack) =>
110 Effect.gen(function* () {
111 yield* stack.destroy();
112
113 const slackTeamId = process.env.AWS_CHATBOT_SLACK_TEAM_ID!;
114 const slackChannelId = process.env.AWS_CHATBOT_SLACK_CHANNEL_ID!;
115
116 // Both custom actions stay deployed across the replacement step — a
117 // deploy that replaces a resource while simultaneously removing its old
118 // dependency deadlocks the engine.
119 const deploy = (target: "a" | "b") =>
120 stack.deploy(
121 Effect.gen(function* () {
122 const role = yield* Role("AssocChatbotRole", {
123 assumeRolePolicyDocument: chatbotAssumeRolePolicy,
124 managedPolicyArns: ["arn:aws:iam::aws:policy/ReadOnlyAccess"],
125 });
126 const config = yield* SlackChannelConfiguration("AssocConfig", {
127 slackTeamId,
128 slackChannelId,
129 iamRoleArn: role.roleArn,
130 });
131 const actionA = yield* CustomAction("AssocActionA", {
132 commandText: "aws lambda list-functions",
133 });
134 const actionB = yield* CustomAction("AssocActionB", {
135 commandText: "aws s3 ls",
136 });
137 const association = yield* Association("Assoc", {
138 chatConfiguration: config.chatConfigurationArn,
139 resource: target === "a" ? actionA.customActionArn : actionB.customActionArn,
140 });
141 return { config, actionA, actionB, association };
142 }),
143 );
144
145 const first = yield* deploy("a");
146 expect(first.association.chatConfigurationArn).toBe(first.config.chatConfigurationArn);
147 expect(first.association.resourceArn).toBe(first.actionA.customActionArn);
148
149 // out-of-band verification via distilled
150 yield* assertAssociations(first.config.chatConfigurationArn, [first.actionA.customActionArn]);
151
152 // re-pointing the association to a different resource is a replacement:
153 // the new pair is associated and the old one disassociated
154 const second = yield* deploy("b");
155 expect(second.association.resourceArn).toBe(second.actionB.customActionArn);
156 yield* assertAssociations(second.config.chatConfigurationArn, [
157 second.actionB.customActionArn,
158 ]);
159
160 yield* stack.destroy();
161 }),
162 {
163 tags: ["provider:aws", "provider:aws:chatbot", "provider:aws:iam", "live"],
164 timeout: 300_000,
165 },
166 );