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

main
ts 201 lines 7.56 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { CustomAction } from "@alchemy.run/aws/Chatbot";
3 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
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 { Region } from "@distilled.cloud/aws/Region";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 import * as Schedule from "effect/Schedule";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 const findAction = (arn: string) =>
16 chatbot.getCustomAction({ CustomActionArn: arn }).pipe(
17 Effect.map((r) => r.CustomAction),
18 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
19 );
20
21 class CustomActionStillExists extends Data.TaggedError("CustomActionStillExists")<{
22 readonly arn: string;
23 }> {}
24
25 const assertActionDeleted = (arn: string) =>
26 findAction(arn).pipe(
27 Effect.flatMap((action) =>
28 action === undefined ? Effect.void : Effect.fail(new CustomActionStillExists({ arn })),
29 ),
30 Effect.retry({
31 while: (e) => e._tag === "CustomActionStillExists",
32 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
33 }),
34 );
35
36 // Ungated typed-error probe: prove the distilled error union carries the
37 // not-found tag this provider's read/delete paths depend on.
38 test.provider(
39 "getCustomAction on a nonexistent action fails with ResourceNotFoundException",
40 () =>
41 Effect.gen(function* () {
42 const { accountId } = yield* AWS.AWSEnvironment.current;
43 const error = yield* Effect.flip(
44 chatbot.getCustomAction({
45 CustomActionArn: `arn:aws:chatbot::${accountId}:custom-action/alchemy-probe-nonexistent`,
46 }),
47 );
48 expect(error._tag).toBe("ResourceNotFoundException");
49 }),
50 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 30_000 },
51 );
52
53 test.provider(
54 "create, update definition + alias + attachments + tags, delete",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const action = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* CustomAction("TestAction", {
62 commandText: "aws lambda list-functions",
63 tags: { Environment: "test" },
64 });
65 }),
66 );
67
68 expect(action.actionName).toBeDefined();
69 expect(action.customActionArn).toContain(":custom-action/");
70
71 // out-of-band verification via distilled
72 const created = yield* findAction(action.customActionArn);
73 expect(created?.Definition.CommandText).toBe("aws lambda list-functions");
74 expect(created?.AliasName).toBeUndefined();
75 const tags = yield* chatbot
76 .listTagsForResource({ ResourceARN: action.customActionArn })
77 .pipe(
78 Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.TagKey, t.TagValue]))),
79 );
80 expect(tags.Environment).toBe("test");
81 expect(tags["alchemy::id"]).toBe("TestAction");
82
83 // update the command, add an alias + a notification attachment, and
84 // change the user tags
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* CustomAction("TestAction", {
88 commandText: "aws cloudwatch describe-alarms --alarm-names $AlarmName",
89 aliasName: "alchemy-test-describe-alarm",
90 attachments: [
91 {
92 // NotificationType is required by the live API even though
93 // the model marks it optional (patched in distilled).
94 notificationType: "CloudWatch",
95 buttonText: "Describe alarm",
96 criteria: [{ operator: "HAS_VALUE", variableName: "AlarmName" }],
97 },
98 ],
99 tags: { Environment: "production" },
100 });
101 }),
102 );
103 expect(updated.actionName).toBe(action.actionName);
104 expect(updated.customActionArn).toBe(action.customActionArn);
105
106 const afterUpdate = yield* findAction(action.customActionArn);
107 expect(afterUpdate?.Definition.CommandText).toBe(
108 "aws cloudwatch describe-alarms --alarm-names $AlarmName",
109 );
110 expect(afterUpdate?.AliasName).toBe("alchemy-test-describe-alarm");
111 expect(afterUpdate?.Attachments?.[0]?.ButtonText).toBe("Describe alarm");
112 expect(afterUpdate?.Attachments?.[0]?.Criteria?.[0]?.VariableName).toBe("AlarmName");
113 const updatedTags = yield* chatbot
114 .listTagsForResource({ ResourceARN: action.customActionArn })
115 .pipe(
116 Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.TagKey, t.TagValue]))),
117 );
118 expect(updatedTags.Environment).toBe("production");
119
120 yield* stack.destroy();
121 yield* assertActionDeleted(action.customActionArn);
122 }),
123 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 120_000 },
124 );
125
126 test.provider(
127 "custom action name and replacement on rename",
128 (stack) =>
129 Effect.gen(function* () {
130 yield* stack.destroy();
131
132 const first = yield* stack.deploy(
133 Effect.gen(function* () {
134 return yield* CustomAction("NamedAction", {
135 actionName: "alchemy-test-action-a",
136 commandText: "aws s3 ls",
137 });
138 }),
139 );
140 expect(first.actionName).toBe("alchemy-test-action-a");
141 expect(first.customActionArn).toContain(":custom-action/alchemy-test-action-a");
142
143 // renaming triggers a replacement: new physical action, old one gone
144 const second = yield* stack.deploy(
145 Effect.gen(function* () {
146 return yield* CustomAction("NamedAction", {
147 actionName: "alchemy-test-action-b",
148 commandText: "aws s3 ls",
149 });
150 }),
151 );
152 expect(second.actionName).toBe("alchemy-test-action-b");
153
154 const observed = yield* findAction(second.customActionArn);
155 expect(observed?.Definition.CommandText).toBe("aws s3 ls");
156 yield* assertActionDeleted(first.customActionArn);
157
158 yield* stack.destroy();
159 yield* assertActionDeleted(second.customActionArn);
160 }),
161 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 120_000 },
162 );
163
164 // Chatbot has endpoints in only four regions. A stack in any other region
165 // (ap-northeast-1 here) must still deploy: calls fall back to us-east-2.
166 const unsupportedRegion = Test.make({
167 providers: AWS.providers().pipe(
168 Layer.provide(
169 Layer.effect(
170 AWSEnvironment,
171 Effect.gen(function* () {
172 const profileEnvironment = yield* AWSEnvironment;
173 return Effect.map(profileEnvironment, (environment) => ({
174 ...environment,
175 region: "ap-northeast-1",
176 }));
177 }),
178 ).pipe(Layer.provide(AWS.providers())),
179 ),
180 ),
181 });
182
183 unsupportedRegion.test.provider(
184 "deploys from a region without a Chatbot endpoint",
185 (stack) =>
186 Effect.gen(function* () {
187 yield* stack.destroy();
188 const action = yield* stack.deploy(
189 CustomAction("RegionFallbackAction", { commandText: "aws sts get-caller-identity" }),
190 );
191 // Out-of-band through a supported endpoint: the action exists.
192 const inUsEast2 = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
193 effect.pipe(Effect.provideService(Region, Effect.succeed("us-east-2")));
194 expect((yield* inUsEast2(findAction(action.customActionArn)))?.CustomActionArn).toBe(
195 action.customActionArn,
196 );
197 yield* stack.destroy();
198 yield* inUsEast2(assertActionDeleted(action.customActionArn));
199 }),
200 { tags: ["provider:aws", "provider:aws:chatbot", "live"], timeout: 120_000 },
201 );