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

main
ts 206 lines 7.99 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { DataSource, Index } from "@alchemy.run/aws/Kendra";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as kendra from "@distilled.cloud/aws/kendra";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 // Amazon Kendra is closed to new customers: CreateIndex on an account that
12 // never used it fails with "Amazon Kendra is no longer accepting new
13 // customers. For more information, see
14 // https://docs.aws.amazon.com/kendra/latest/dg/kendra-availability-change.html"
15 // (wire NotAuthorizedException, typed as KendraClosedToNewCustomers). The
16 // ungated probes assert the typed not-found errors the provider's read/delete
17 // paths depend on, and the typed closed-to-new-customers rejection. The full
18 // lifecycle (an index takes ~20-30 minutes to provision and bills hourly once
19 // ACTIVE) skipped under `--fast` AND AWS_TEST_KENDRA=1 for an
20 // account that is still an existing Kendra customer.
21 const kendraLifecycleGated = !!process.env.FAST || !process.env.AWS_TEST_KENDRA;
22 describe("AWS.Kendra.Index", { tags: ["provider:aws", "provider:aws:kendra", "live"] }, () => {
23 test.provider(
24 "describeIndex on a nonexistent id yields a typed ResourceNotFoundException",
25 (_stack) =>
26 Effect.gen(function* () {
27 const error = yield* kendra
28 .describeIndex({ Id: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" })
29 .pipe(Effect.flip);
30 expect(error._tag).toBe("ResourceNotFoundException");
31 }),
32 { timeout: 60_000 },
33 );
34
35 test.provider(
36 "describeDataSource on a nonexistent index yields a typed ResourceNotFoundException",
37 (_stack) =>
38 Effect.gen(function* () {
39 const error = yield* kendra
40 .describeDataSource({
41 Id: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
42 IndexId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
43 })
44 .pipe(Effect.flip);
45 expect(error._tag).toBe("ResourceNotFoundException");
46 }),
47 { timeout: 60_000 },
48 );
49
50 // Fails before anything is created: the closed-to-new-customers check
51 // precedes role validation. If this ever starts failing with a different
52 // tag, the account can create indexes again — run with AWS_TEST_KENDRA=1.
53 test.provider(
54 "createIndex is rejected with a typed KendraClosedToNewCustomers",
55 (_stack) =>
56 Effect.gen(function* () {
57 const error = yield* kendra
58 .createIndex({
59 Name: "alchemy-closed-probe",
60 Edition: "DEVELOPER_EDITION",
61 RoleArn: "arn:aws:iam::123456789012:role/alchemy-kendra-probe-nonexistent",
62 })
63 .pipe(Effect.flip);
64 expect(error._tag).toBe("KendraClosedToNewCustomers");
65 }),
66 { timeout: 60_000 },
67 );
68
69 test.provider.skipIf(kendraLifecycleGated)(
70 "create developer index + S3 data source, update, destroy, verify gone",
71 (stack) =>
72 Effect.gen(function* () {
73 yield* stack.destroy();
74
75 const deploy = (props: { description?: string }) =>
76 stack.deploy(
77 Effect.gen(function* () {
78 // Role Kendra assumes to publish CloudWatch metrics/logs.
79 const indexRole = yield* AWS.IAM.Role("KendraIndexRole", {
80 assumeRolePolicyDocument: {
81 Version: "2012-10-17",
82 Statement: [
83 {
84 Effect: "Allow",
85 Principal: { Service: ["kendra.amazonaws.com"] },
86 Action: ["sts:AssumeRole"],
87 },
88 ],
89 },
90 inlinePolicies: {
91 observability: {
92 Version: "2012-10-17",
93 Statement: [
94 {
95 Effect: "Allow",
96 Action: ["cloudwatch:PutMetricData"],
97 Resource: ["*"],
98 Condition: { StringEquals: { "cloudwatch:namespace": "AWS/Kendra" } },
99 },
100 {
101 Effect: "Allow",
102 Action: [
103 "logs:DescribeLogGroups",
104 "logs:CreateLogGroup",
105 "logs:DescribeLogStreams",
106 "logs:CreateLogStream",
107 "logs:PutLogEvents",
108 ],
109 Resource: ["*"],
110 },
111 ],
112 },
113 },
114 });
115
116 const bucket = yield* AWS.S3.Bucket("KendraDocs", { forceDestroy: true });
117
118 // Role Kendra assumes to crawl the S3 bucket.
119 const dataSourceRole = yield* AWS.IAM.Role("KendraDataRole", {
120 assumeRolePolicyDocument: {
121 Version: "2012-10-17",
122 Statement: [
123 {
124 Effect: "Allow",
125 Principal: { Service: ["kendra.amazonaws.com"] },
126 Action: ["sts:AssumeRole"],
127 },
128 ],
129 },
130 inlinePolicies: {
131 s3: {
132 Version: "2012-10-17",
133 Statement: [
134 {
135 Effect: "Allow",
136 Action: ["s3:GetObject", "s3:ListBucket"],
137 Resource: ["*"],
138 },
139 {
140 Effect: "Allow",
141 Action: ["kendra:BatchPutDocument", "kendra:BatchDeleteDocument"],
142 Resource: ["*"],
143 },
144 ],
145 },
146 },
147 });
148
149 const index = yield* Index("Search", {
150 edition: "DEVELOPER_EDITION",
151 roleArn: indexRole.roleArn,
152 description: props.description,
153 tags: { Environment: "test" },
154 });
155
156 const source = yield* DataSource("Docs", {
157 indexId: index.id,
158 type: "S3",
159 roleArn: dataSourceRole.roleArn,
160 configuration: { S3Configuration: { BucketName: bucket.bucketName } },
161 });
162
163 return { index, source };
164 }),
165 );
166
167 // Create — reconcile waits (bounded) for ACTIVE.
168 const { index, source } = yield* deploy({});
169 expect(index.id).toBeDefined();
170 expect(index.status).toBe("ACTIVE");
171 expect(index.edition).toBe("DEVELOPER_EDITION");
172 expect(source.id).toBeDefined();
173 expect(source.status).toBe("ACTIVE");
174 expect(source.type).toBe("S3");
175
176 // Out-of-band verification via distilled.
177 const described = yield* kendra.describeIndex({ Id: index.id });
178 expect(described.Status).toBe("ACTIVE");
179
180 // Update in place — description flows through UpdateIndex.
181 const updated = yield* deploy({ description: "updated by test" });
182 expect(updated.index.id).toBe(index.id);
183 const redescribed = yield* kendra.describeIndex({ Id: index.id });
184 expect(redescribed.Description).toBe("updated by test");
185
186 yield* stack.destroy();
187
188 // Typed wait-until-gone.
189 yield* Effect.gen(function* () {
190 const gone = yield* kendra.describeIndex({ Id: index.id }).pipe(
191 Effect.map((d) => d.Status === "DELETING"),
192 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
193 );
194 if (!gone) {
195 return yield* Effect.fail({ _tag: "StillExists" as const });
196 }
197 }).pipe(
198 Effect.retry({
199 while: (e: { _tag: string }) => e._tag === "StillExists",
200 schedule: Schedule.max([Schedule.spaced("15 seconds"), Schedule.recurs(40)]),
201 }),
202 );
203 }),
204 { tags: ["provider:aws:iam", "provider:aws:s3"], timeout: 3_600_000 },
205 );
206 });