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

main
ts 248 lines 9.8 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 appintegrations from "@distilled.cloud/aws/appintegrations";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import AppIntegrationsTestFunctionLive, { AppIntegrationsTestFunction } from "./fixtures/handler";
10
11 const testOptions = { providers: AWS.providers() };
12 const { test, beforeAll, afterAll } = Test.make(testOptions);
13 const sharedStack = Core.scratchStack(testOptions, "AppIntegrationsBindings");
14
15 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
16 // over 60s on a fresh deploy under parallel-suite load.
17 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
18
19 let baseUrl: string;
20
21 const getJson = <T>(pathname: string) =>
22 HttpClient.get(`${baseUrl}${pathname}`).pipe(
23 Effect.flatMap((response) =>
24 response.status === 200
25 ? Effect.succeed(response)
26 : Effect.fail(new Error(`unexpected status ${response.status}`)),
27 ),
28 Effect.flatMap((response) => response.json),
29 Effect.map((body) => body as T),
30 // First requests after deploy can hit IAM-propagation AccessDenied
31 // surfaced as a 500 by the fixture's orDie — bounded retry.
32 Effect.retry({
33 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
34 }),
35 );
36
37 const postJson = <T>(pathname: string) =>
38 HttpClient.post(`${baseUrl}${pathname}`).pipe(
39 Effect.flatMap((response) =>
40 response.status === 200
41 ? Effect.succeed(response)
42 : Effect.fail(new Error(`unexpected status ${response.status}`)),
43 ),
44 Effect.flatMap((response) => response.json),
45 Effect.map((body) => body as T),
46 Effect.retry({
47 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
48 }),
49 );
50
51 describe(
52 "AppIntegrations Bindings",
53 {
54 tags: [
55 "provider:aws",
56 "provider:aws:appintegrations",
57 "provider:aws:kms",
58 "provider:aws:lambda",
59 "provider:aws:s3",
60 "live",
61 ],
62 },
63 () => {
64 beforeAll(
65 Effect.gen(function* () {
66 yield* sharedStack.destroy();
67
68 const { functionUrl } = yield* sharedStack.deploy(
69 Effect.gen(function* () {
70 return yield* AppIntegrationsTestFunction;
71 }).pipe(Effect.provide(AppIntegrationsTestFunctionLive)),
72 );
73
74 expect(functionUrl).toBeTruthy();
75 baseUrl = functionUrl!.replace(/\/+$/, "");
76
77 yield* HttpClient.get(`${baseUrl}/applications`).pipe(
78 Effect.flatMap((response) =>
79 response.status === 200
80 ? Effect.succeed(response)
81 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
82 ),
83 Effect.retry({ schedule: readinessPolicy }),
84 );
85 }),
86 { timeout: 240_000 },
87 );
88
89 afterAll(sharedStack.destroy(), { timeout: 120_000 });
90
91 describe("ListApplications", () => {
92 test.provider("lists the fixture application", () =>
93 Effect.gen(function* () {
94 const body = yield* getJson<{ self: string; applications: { Id?: string }[] }>(
95 "/applications",
96 );
97 expect(Array.isArray(body.applications)).toBe(true);
98 expect(body.applications.map((a) => a.Id)).toContain(body.self);
99 }),
100 );
101 });
102
103 describe("ListApplicationAssociations", () => {
104 test.provider("lists the application's associations (empty)", () =>
105 Effect.gen(function* () {
106 const body = yield* getJson<{ associations: unknown[] }>("/application-associations");
107 // Nothing associates with the fixture application — the call
108 // succeeding with an array proves the binding + IAM grant.
109 expect(Array.isArray(body.associations)).toBe(true);
110 }),
111 );
112 });
113
114 describe("ListDataIntegrations", () => {
115 test.provider("lists the fixture data integration", () =>
116 Effect.gen(function* () {
117 const body = yield* getJson<{
118 self: string;
119 dataIntegrations: { Name?: string; Arn?: string }[];
120 }>("/data-integrations");
121 expect(Array.isArray(body.dataIntegrations)).toBe(true);
122 // The list API returns Arn/Name summaries; resolve self (an id) via
123 // distilled to compare by name.
124 const observed = yield* appintegrations.getDataIntegration({ Identifier: body.self });
125 expect(body.dataIntegrations.map((d) => d.Name)).toContain(observed.Name);
126 }),
127 );
128 });
129
130 describe("ListDataIntegrationAssociations", () => {
131 test.provider("lists the data integration's associations (empty)", () =>
132 Effect.gen(function* () {
133 const body = yield* getJson<{ associations: unknown[] }>(
134 "/data-integration-associations",
135 );
136 expect(Array.isArray(body.associations)).toBe(true);
137 }),
138 );
139 });
140
141 describe("CreateDataIntegrationAssociation / UpdateDataIntegrationAssociation", () => {
142 // AppIntegrations attaches a service-managed resource-based policy to
143 // DataIntegrations that explicitly denies association creation to
144 // principals other than approved client services — Amazon Connect /
145 // Amazon Q create the associations when the integration is configured
146 // there. The ungated probe asserts the typed AccessDeniedException whose
147 // message names the exact action AND the resolved data-integration ARN,
148 // proving the binding wiring, the IAM grant path, and the identifier
149 // injection end-to-end. Run the full lifecycle on an entitled principal
150 // with AWS_TEST_APPINTEGRATIONS_ASSOCIATIONS=1.
151 test.provider.skipIf(!!process.env.AWS_TEST_APPINTEGRATIONS_ASSOCIATIONS)(
152 "probe: the service's resource policy denies association creation",
153 () =>
154 Effect.gen(function* () {
155 const body = yield* postJson<{
156 created: string;
157 message?: string;
158 associationId?: string;
159 updated: string;
160 }>("/data-integration-associations").pipe(
161 Effect.repeat({
162 // Transient IAM-propagation denials lack the "explicit deny"
163 // clause — retry (bounded) until the authoritative denial (or
164 // an unexpected success) is observed.
165 schedule: Schedule.spaced("5 seconds"),
166 until: (b): boolean =>
167 b.created === "ok" || (b.message ?? "").includes("explicit deny"),
168 times: 8,
169 }),
170 );
171 expect(body.created).toBe("AccessDeniedException");
172 expect(body.message).toContain(
173 "app-integrations:CreateDataIntegrationAssociation on resource: arn:aws:app-integrations",
174 );
175 expect(body.message).toContain(":data-integration/");
176 expect(body.message).toContain("explicit deny in a resource-based policy");
177 }),
178 );
179
180 test.provider.skipIf(!process.env.AWS_TEST_APPINTEGRATIONS_ASSOCIATIONS)(
181 "creates an association and reruns its on-demand job",
182 () =>
183 Effect.gen(function* () {
184 const body = yield* postJson<{
185 created: string;
186 message?: string;
187 associationId?: string;
188 updated: string;
189 }>("/data-integration-associations").pipe(
190 Effect.repeat({
191 schedule: Schedule.spaced("5 seconds"),
192 until: (b): boolean => b.created === "ok" && b.updated === "ok",
193 times: 10,
194 }),
195 );
196 expect(body, JSON.stringify(body)).toMatchObject({ created: "ok", updated: "ok" });
197 expect(body.associationId).toBeTruthy();
198
199 // The association is now visible through the list binding.
200 const listed = yield* getJson<{
201 associations: { DataIntegrationAssociationArn?: string }[];
202 }>("/data-integration-associations");
203 expect(
204 listed.associations.map((a) => a.DataIntegrationAssociationArn?.split("/").at(-1)),
205 ).toContain(body.associationId);
206 }),
207 );
208 });
209
210 describe("ListEventIntegrations", () => {
211 test.provider("lists the fixture event integration", () =>
212 Effect.gen(function* () {
213 const body = yield* getJson<{ self: string; eventIntegrations: { Name?: string }[] }>(
214 "/event-integrations",
215 );
216 expect(Array.isArray(body.eventIntegrations)).toBe(true);
217 expect(body.eventIntegrations.map((e) => e.Name)).toContain(body.self);
218 }),
219 );
220 });
221
222 describe("ListEventIntegrationAssociations", () => {
223 test.provider("lists the event integration's associations (empty)", () =>
224 Effect.gen(function* () {
225 const body = yield* getJson<{ associations: unknown[] }>(
226 "/event-integration-associations",
227 );
228 expect(Array.isArray(body.associations)).toBe(true);
229 }),
230 );
231 });
232
233 describe("consumeIntegrationEvents", () => {
234 test.provider("creates the EventIntegration for the event source", () =>
235 Effect.gen(function* () {
236 // The fixture's event source created an EventIntegration on the
237 // default bus — verify it out-of-band via distilled.
238 const body = yield* getJson<{ self: string }>("/event-integrations");
239 const observed = yield* appintegrations.getEventIntegration({ Name: body.self });
240 expect(observed.EventBridgeBus).toBe("default");
241 expect(observed.EventFilter?.Source).toBe(
242 "aws.partner/examplepartner.com/alchemy-bindings",
243 );
244 }),
245 );
246 });
247 },
248 );