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

main
ts 458 lines 16.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 kendra from "@distilled.cloud/aws/kendra";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import KendraTestFunctionLive, { KendraTestFunction } from "./handler";
10
11 const testOptions = { providers: AWS.providers() };
12 const { test } = Test.make(testOptions);
13
14 // A well-formed-but-nonexistent index/data-source id the ungated probes are
15 // driven against.
16 const NONEXISTENT = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
17
18 // ---------------------------------------------------------------------------
19 // Ungated typed-error probes: every operation the twenty-three bindings wrap
20 // is exercised directly through distilled against a nonexistent index, and
21 // must answer with a typed tag. These prove the distilled error unions (and
22 // request serialization) at near-zero cost on every CI pass, while the full
23 // runtime fixture below is gated behind the 20-30 minute index provisioning.
24 // ---------------------------------------------------------------------------
25
26 describe(
27 "Kendra binding operations (typed-error probes)",
28 { tags: ["provider:aws", "provider:aws:kendra", "live"] },
29 () => {
30 const expectTag = (error: { _tag: string }, tags: readonly string[]) =>
31 expect(tags).toContain(error._tag);
32 const NOT_FOUND = ["ResourceNotFoundException"] as const;
33 // Operations whose body preconditions may be validated before the index
34 // lookup surface either tag.
35 const NOT_FOUND_OR_INVALID = ["ResourceNotFoundException", "ValidationException"] as const;
36
37 test.provider("query yields a typed not-found error", () =>
38 Effect.gen(function* () {
39 const error = yield* Effect.flip(
40 kendra.query({ IndexId: NONEXISTENT, QueryText: "probe" }),
41 );
42 expectTag(error, NOT_FOUND);
43 }),
44 );
45
46 test.provider("retrieve yields a typed not-found error", () =>
47 Effect.gen(function* () {
48 const error = yield* Effect.flip(
49 kendra.retrieve({ IndexId: NONEXISTENT, QueryText: "probe" }),
50 );
51 expectTag(error, NOT_FOUND);
52 }),
53 );
54
55 test.provider("getQuerySuggestions yields a typed not-found error", () =>
56 Effect.gen(function* () {
57 const error = yield* Effect.flip(
58 kendra.getQuerySuggestions({ IndexId: NONEXISTENT, QueryText: "probe" }),
59 );
60 expectTag(error, NOT_FOUND);
61 }),
62 );
63
64 test.provider("submitFeedback yields a typed error", () =>
65 Effect.gen(function* () {
66 const error = yield* Effect.flip(
67 kendra.submitFeedback({
68 IndexId: NONEXISTENT,
69 QueryId: "probe-query-id",
70 ClickFeedbackItems: [{ ResultId: "probe-result", ClickTime: new Date() }],
71 }),
72 );
73 expectTag(error, NOT_FOUND_OR_INVALID);
74 }),
75 );
76
77 test.provider("batchPutDocument yields a typed error", () =>
78 Effect.gen(function* () {
79 const error = yield* Effect.flip(
80 kendra.batchPutDocument({
81 IndexId: NONEXISTENT,
82 Documents: [
83 { Id: "probe", Blob: new TextEncoder().encode("probe"), ContentType: "PLAIN_TEXT" },
84 ],
85 }),
86 );
87 expectTag(error, NOT_FOUND_OR_INVALID);
88 }),
89 );
90
91 test.provider("batchDeleteDocument yields a typed not-found error", () =>
92 Effect.gen(function* () {
93 const error = yield* Effect.flip(
94 kendra.batchDeleteDocument({ IndexId: NONEXISTENT, DocumentIdList: ["probe"] }),
95 );
96 expectTag(error, NOT_FOUND);
97 }),
98 );
99
100 test.provider("batchGetDocumentStatus yields a typed not-found error", () =>
101 Effect.gen(function* () {
102 const error = yield* Effect.flip(
103 kendra.batchGetDocumentStatus({
104 IndexId: NONEXISTENT,
105 DocumentInfoList: [{ DocumentId: "probe" }],
106 }),
107 );
108 expectTag(error, NOT_FOUND);
109 }),
110 );
111
112 test.provider("getSnapshots yields a typed error", () =>
113 Effect.gen(function* () {
114 const error = yield* Effect.flip(
115 kendra.getSnapshots({
116 IndexId: NONEXISTENT,
117 Interval: "ONE_WEEK_AGO",
118 MetricType: "QUERIES_BY_COUNT",
119 }),
120 );
121 expectTag(error, NOT_FOUND_OR_INVALID);
122 }),
123 );
124
125 test.provider("putPrincipalMapping yields a typed error", () =>
126 Effect.gen(function* () {
127 const error = yield* Effect.flip(
128 kendra.putPrincipalMapping({
129 IndexId: NONEXISTENT,
130 GroupId: "probe",
131 GroupMembers: { MemberUsers: [{ UserId: "probe@example.com" }] },
132 }),
133 );
134 expectTag(error, NOT_FOUND_OR_INVALID);
135 }),
136 );
137
138 test.provider("deletePrincipalMapping yields a typed not-found error", () =>
139 Effect.gen(function* () {
140 const error = yield* Effect.flip(
141 kendra.deletePrincipalMapping({ IndexId: NONEXISTENT, GroupId: "probe" }),
142 );
143 expectTag(error, NOT_FOUND);
144 }),
145 );
146
147 test.provider("describePrincipalMapping yields a typed not-found error", () =>
148 Effect.gen(function* () {
149 const error = yield* Effect.flip(
150 kendra.describePrincipalMapping({ IndexId: NONEXISTENT, GroupId: "probe" }),
151 );
152 expectTag(error, NOT_FOUND);
153 }),
154 );
155
156 test.provider("listGroupsOlderThanOrderingId yields a typed not-found error", () =>
157 Effect.gen(function* () {
158 const error = yield* Effect.flip(
159 kendra.listGroupsOlderThanOrderingId({ IndexId: NONEXISTENT, OrderingId: 1 }),
160 );
161 expectTag(error, NOT_FOUND);
162 }),
163 );
164
165 test.provider("clearQuerySuggestions yields a typed not-found error", () =>
166 Effect.gen(function* () {
167 const error = yield* Effect.flip(kendra.clearQuerySuggestions({ IndexId: NONEXISTENT }));
168 expectTag(error, NOT_FOUND);
169 }),
170 );
171
172 test.provider("describeQuerySuggestionsConfig yields a typed not-found error", () =>
173 Effect.gen(function* () {
174 const error = yield* Effect.flip(
175 kendra.describeQuerySuggestionsConfig({ IndexId: NONEXISTENT }),
176 );
177 expectTag(error, NOT_FOUND);
178 }),
179 );
180
181 test.provider("updateQuerySuggestionsConfig yields a typed not-found error", () =>
182 Effect.gen(function* () {
183 const error = yield* Effect.flip(
184 kendra.updateQuerySuggestionsConfig({ IndexId: NONEXISTENT, Mode: "LEARN_ONLY" }),
185 );
186 expectTag(error, NOT_FOUND);
187 }),
188 );
189
190 test.provider("createAccessControlConfiguration yields a typed error", () =>
191 Effect.gen(function* () {
192 const error = yield* Effect.flip(
193 kendra.createAccessControlConfiguration({ IndexId: NONEXISTENT, Name: "probe" }),
194 );
195 expectTag(error, NOT_FOUND_OR_INVALID);
196 }),
197 );
198
199 test.provider("describeAccessControlConfiguration yields a typed not-found error", () =>
200 Effect.gen(function* () {
201 const error = yield* Effect.flip(
202 kendra.describeAccessControlConfiguration({ IndexId: NONEXISTENT, Id: "probe" }),
203 );
204 expectTag(error, NOT_FOUND);
205 }),
206 );
207
208 test.provider("updateAccessControlConfiguration yields a typed not-found error", () =>
209 Effect.gen(function* () {
210 const error = yield* Effect.flip(
211 kendra.updateAccessControlConfiguration({ IndexId: NONEXISTENT, Id: "probe" }),
212 );
213 expectTag(error, NOT_FOUND);
214 }),
215 );
216
217 test.provider("deleteAccessControlConfiguration yields a typed not-found error", () =>
218 Effect.gen(function* () {
219 const error = yield* Effect.flip(
220 kendra.deleteAccessControlConfiguration({ IndexId: NONEXISTENT, Id: "probe" }),
221 );
222 expectTag(error, NOT_FOUND);
223 }),
224 );
225
226 test.provider("listAccessControlConfigurations yields a typed not-found error", () =>
227 Effect.gen(function* () {
228 const error = yield* Effect.flip(
229 kendra.listAccessControlConfigurations({ IndexId: NONEXISTENT }),
230 );
231 expectTag(error, NOT_FOUND);
232 }),
233 );
234
235 test.provider("startDataSourceSyncJob yields a typed not-found error", () =>
236 Effect.gen(function* () {
237 const error = yield* Effect.flip(
238 kendra.startDataSourceSyncJob({ IndexId: NONEXISTENT, Id: NONEXISTENT }),
239 );
240 expectTag(error, NOT_FOUND);
241 }),
242 );
243
244 test.provider("stopDataSourceSyncJob yields a typed not-found error", () =>
245 Effect.gen(function* () {
246 const error = yield* Effect.flip(
247 kendra.stopDataSourceSyncJob({ IndexId: NONEXISTENT, Id: NONEXISTENT }),
248 );
249 expectTag(error, NOT_FOUND);
250 }),
251 );
252
253 test.provider("listDataSourceSyncJobs yields a typed not-found error", () =>
254 Effect.gen(function* () {
255 const error = yield* Effect.flip(
256 kendra.listDataSourceSyncJobs({ IndexId: NONEXISTENT, Id: NONEXISTENT }),
257 );
258 expectTag(error, NOT_FOUND);
259 }),
260 );
261 },
262 );
263
264 // ---------------------------------------------------------------------------
265 // Full runtime fixture: a Lambda bound to all twenty-three bindings against a
266 // live Developer-edition index + S3 data source. An index takes ~20-30
267 // minutes to provision and bills hourly once ACTIVE. Amazon Kendra is also
268 // closed to new customers ("Amazon Kendra is no longer accepting new
269 // customers. For more information, see
270 // https://docs.aws.amazon.com/kendra/latest/dg/kendra-availability-change.html",
271 // typed KendraClosedToNewCustomers — probed in SearchIndex.test.ts), so this
272 // skipped under `--fast` AND AWS_TEST_KENDRA=1 (same gate as the
273 // Index lifecycle test) and always destroys what it created.
274 // ---------------------------------------------------------------------------
275
276 const sharedStack = Core.scratchStack(testOptions, "KendraBindings");
277
278 test.provider.skipIf(!!process.env.FAST || !process.env.AWS_TEST_KENDRA)(
279 "all bindings against a live index + data source",
280 () =>
281 Effect.gen(function* () {
282 yield* sharedStack.destroy();
283
284 yield* Effect.gen(function* () {
285 const { functionUrl } = yield* sharedStack.deploy(
286 Effect.gen(function* () {
287 return yield* KendraTestFunction;
288 }).pipe(Effect.provide(KendraTestFunctionLive)),
289 );
290 expect(functionUrl).toBeTruthy();
291 const baseUrl = functionUrl!.replace(/\/+$/, "");
292
293 const getJson = (path: string) =>
294 HttpClient.get(`${baseUrl}${path}`).pipe(
295 Effect.flatMap((response) =>
296 response.status >= 500
297 ? Effect.fail(new Error(`transient upstream ${response.status}`))
298 : Effect.succeed(response),
299 ),
300 Effect.retry({
301 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
302 }),
303 Effect.flatMap((r) => r.json),
304 );
305
306 // All twenty-three capabilities initialized in the runtime.
307 const bindings = (yield* getJson("/bindings")) as { bound: string[] };
308 expect(bindings.bound).toHaveLength(23);
309
310 // BatchPutDocument — proves IndexId injection + the IAM grant.
311 const put = (yield* getJson("/put-documents")) as { failed?: number; errorTag?: string };
312 expect(put.errorTag).toBeUndefined();
313 expect(put.failed).toBe(0);
314
315 // BatchGetDocumentStatus — poll (bounded) until the pushed document
316 // leaves the processing states.
317 const status = yield* getJson("/document-status").pipe(
318 Effect.map((r) => (r as { status?: string }).status ?? "UNKNOWN"),
319 Effect.repeat({
320 schedule: Schedule.spaced("15 seconds"),
321 until: (s): boolean => s === "INDEXED" || s === "FAILED",
322 times: 40,
323 }),
324 );
325 expect(status).toBe("INDEXED");
326
327 // Query — the pushed document is searchable.
328 const queried = (yield* getJson("/query?q=zanzibar")) as {
329 queryId?: string;
330 resultId?: string;
331 count?: number;
332 errorTag?: string;
333 };
334 expect(queried.errorTag).toBeUndefined();
335 expect(queried.queryId).toBeTruthy();
336 expect(queried.count).toBeGreaterThanOrEqual(1);
337
338 // SubmitFeedback for the query's first result.
339 const feedback = (yield* getJson(
340 `/feedback?queryId=${queried.queryId}&resultId=${queried.resultId}`,
341 )) as { ok?: boolean; errorTag?: string };
342 expect(feedback.errorTag).toBeUndefined();
343
344 // Retrieve — passage retrieval over the same document.
345 const retrieved = (yield* getJson("/retrieve?q=zanzibar")) as {
346 count?: number;
347 errorTag?: string;
348 };
349 expect(retrieved.errorTag).toBeUndefined();
350
351 // GetQuerySuggestions (suggestions build from query history so an
352 // empty list is fine — no typed error is the assertion).
353 const suggested = (yield* getJson("/suggest?q=zanzi")) as {
354 count?: number;
355 errorTag?: string;
356 };
357 expect(suggested.errorTag).toBeUndefined();
358
359 // Query-suggestions config read + Duration-typed update + clear.
360 const config = (yield* getJson("/suggestions-config")) as {
361 mode?: string;
362 errorTag?: string;
363 };
364 expect(config.errorTag).toBeUndefined();
365 expect(config.mode).toBeTruthy();
366 const updatedSuggestions = (yield* getJson("/update-suggestions")) as {
367 ok?: boolean;
368 errorTag?: string;
369 };
370 expect(updatedSuggestions.errorTag).toBeUndefined();
371 const cleared = (yield* getJson("/clear-suggestions")) as {
372 ok?: boolean;
373 errorTag?: string;
374 };
375 expect(cleared.errorTag).toBeUndefined();
376
377 // Access-control configuration create/describe/update/delete round
378 // trip.
379 const acl = (yield* getJson("/access-control")) as {
380 id?: string;
381 name?: string;
382 errorTag?: string;
383 };
384 expect(acl.errorTag).toBeUndefined();
385 expect(acl.id).toBeTruthy();
386 expect(acl.name).toBe("block-departed-users");
387 const acls = (yield* getJson("/access-controls")) as { count?: number; errorTag?: string };
388 expect(acls.errorTag).toBeUndefined();
389
390 // Principal mapping put + describe, then delete.
391 const mapping = (yield* getJson("/principal-mapping")) as {
392 actions?: number;
393 errorTag?: string;
394 };
395 expect(mapping.errorTag).toBeUndefined();
396 expect(mapping.actions).toBeGreaterThanOrEqual(1);
397 const stale = (yield* getJson("/stale-groups")) as { count?: number; errorTag?: string };
398 expect(stale.errorTag).toBeUndefined();
399 const unmapped = (yield* getJson("/delete-principal-mapping")) as {
400 ok?: boolean;
401 errorTag?: string;
402 };
403 expect(unmapped.errorTag).toBeUndefined();
404
405 // Data-source sync trio: start, observe in history, stop.
406 const sync = (yield* getJson("/sync")) as { executionId?: string; errorTag?: string };
407 expect(sync.errorTag).toBeUndefined();
408 expect(sync.executionId).toBeTruthy();
409 const jobs = yield* getJson("/sync-jobs").pipe(
410 Effect.map((r) => (r as { count?: number }).count ?? 0),
411 Effect.repeat({
412 schedule: Schedule.spaced("10 seconds"),
413 until: (count): boolean => count >= 1,
414 times: 18,
415 }),
416 );
417 expect(jobs).toBeGreaterThanOrEqual(1);
418 // Stopping is best-effort: the sync may already have finished, which
419 // Kendra answers with a typed error we accept.
420 const stopped = (yield* getJson("/stop-sync")) as { ok?: boolean; errorTag?: string };
421 if (stopped.errorTag !== undefined) {
422 expect([
423 "ResourceNotFoundException",
424 "ConflictException",
425 "ValidationException",
426 ]).toContain(stopped.errorTag);
427 }
428
429 // GetSnapshots — search analytics for the index.
430 const snapshots = (yield* getJson("/snapshots")) as {
431 header?: string[];
432 errorTag?: string;
433 };
434 expect(snapshots.errorTag).toBeUndefined();
435
436 // BatchDeleteDocument cleanup.
437 const deleted = (yield* getJson("/delete-documents")) as {
438 failed?: number;
439 errorTag?: string;
440 };
441 expect(deleted.errorTag).toBeUndefined();
442 expect(deleted.failed).toBe(0);
443 }).pipe(Effect.ensuring(sharedStack.destroy().pipe(Effect.orDie)));
444 }),
445 // index create (~20-30 min) + document indexing + sync + delete wait.
446 {
447 tags: [
448 "provider:aws",
449 "provider:aws:batch",
450 "provider:aws:iam",
451 "provider:aws:kendra",
452 "provider:aws:lambda",
453 "provider:aws:s3",
454 "live",
455 ],
456 timeout: 5_400_000,
457 },
458 );