@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 617 lines 20.9 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 qbusiness from "@distilled.cloud/aws/qbusiness";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import QBusinessTestFunctionLive, { QBusinessTestFunction } from "./handler";
10
11 const testOptions = { providers: AWS.providers() };
12 const { test } = Test.make(testOptions);
13
14 // A well-formed-but-nonexistent application/index/data-source id the ungated
15 // probes are driven against.
16 const NONEXISTENT = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
17
18 // ---------------------------------------------------------------------------
19 // Ungated typed-error probes: every operation the thirty-six bindings wrap is
20 // exercised directly through distilled against a nonexistent application, 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 minutes-long, hourly-billed
24 // application + index provisioning.
25 // ---------------------------------------------------------------------------
26
27 describe(
28 "QBusiness binding operations (typed-error probes)",
29 { tags: ["provider:aws", "provider:aws:qbusiness", "live"] },
30 () => {
31 const expectTag = (error: { _tag: string }, tags: readonly string[]) =>
32 expect(tags).toContain(error._tag);
33 const NOT_FOUND = ["ResourceNotFoundException"] as const;
34 // Operations whose body preconditions may be validated before the
35 // application lookup surface either tag.
36 const NOT_FOUND_OR_INVALID = ["ResourceNotFoundException", "ValidationException"] as const;
37 // Chat operations may reject on licensing/authorization before resolving
38 // the application.
39 const CHAT_TAGS = [
40 "ResourceNotFoundException",
41 "AccessDeniedException",
42 "LicenseNotFoundException",
43 "ValidationException",
44 ] as const;
45
46 test.provider("chatSync yields a typed error", () =>
47 Effect.gen(function* () {
48 const error = yield* Effect.flip(
49 qbusiness.chatSync({ applicationId: NONEXISTENT, userMessage: "probe" }),
50 );
51 expectTag(error, CHAT_TAGS);
52 }),
53 );
54
55 test.provider("searchRelevantContent yields a typed error", () =>
56 Effect.gen(function* () {
57 const error = yield* Effect.flip(
58 qbusiness.searchRelevantContent({
59 applicationId: NONEXISTENT,
60 queryText: "probe",
61 contentSource: { retriever: { retrieverId: NONEXISTENT } },
62 }),
63 );
64 expectTag(error, CHAT_TAGS);
65 }),
66 );
67
68 test.provider("putFeedback yields a typed error", () =>
69 Effect.gen(function* () {
70 const error = yield* Effect.flip(
71 qbusiness.putFeedback({
72 applicationId: NONEXISTENT,
73 conversationId: NONEXISTENT,
74 messageId: NONEXISTENT,
75 messageUsefulness: { usefulness: "USEFUL", submittedAt: new Date() },
76 }),
77 );
78 expectTag(error, NOT_FOUND_OR_INVALID);
79 }),
80 );
81
82 test.provider("getChatControlsConfiguration yields a typed not-found error", () =>
83 Effect.gen(function* () {
84 const error = yield* Effect.flip(
85 qbusiness.getChatControlsConfiguration({ applicationId: NONEXISTENT }),
86 );
87 expectTag(error, NOT_FOUND);
88 }),
89 );
90
91 test.provider("updateChatControlsConfiguration yields a typed not-found error", () =>
92 Effect.gen(function* () {
93 const error = yield* Effect.flip(
94 qbusiness.updateChatControlsConfiguration({
95 applicationId: NONEXISTENT,
96 responseScope: "ENTERPRISE_CONTENT_ONLY",
97 }),
98 );
99 expectTag(error, NOT_FOUND);
100 }),
101 );
102
103 test.provider("deleteChatControlsConfiguration yields a typed not-found error", () =>
104 Effect.gen(function* () {
105 const error = yield* Effect.flip(
106 qbusiness.deleteChatControlsConfiguration({ applicationId: NONEXISTENT }),
107 );
108 expectTag(error, NOT_FOUND);
109 }),
110 );
111
112 test.provider("listConversations yields a typed not-found error", () =>
113 Effect.gen(function* () {
114 const error = yield* Effect.flip(
115 qbusiness.listConversations({ applicationId: NONEXISTENT }),
116 );
117 expectTag(error, NOT_FOUND);
118 }),
119 );
120
121 test.provider("deleteConversation yields a typed not-found error", () =>
122 Effect.gen(function* () {
123 const error = yield* Effect.flip(
124 qbusiness.deleteConversation({ applicationId: NONEXISTENT, conversationId: NONEXISTENT }),
125 );
126 expectTag(error, NOT_FOUND);
127 }),
128 );
129
130 test.provider("listMessages yields a typed not-found error", () =>
131 Effect.gen(function* () {
132 const error = yield* Effect.flip(
133 qbusiness.listMessages({ applicationId: NONEXISTENT, conversationId: NONEXISTENT }),
134 );
135 expectTag(error, NOT_FOUND);
136 }),
137 );
138
139 test.provider("listAttachments yields a typed not-found error", () =>
140 Effect.gen(function* () {
141 const error = yield* Effect.flip(qbusiness.listAttachments({ applicationId: NONEXISTENT }));
142 expectTag(error, NOT_FOUND);
143 }),
144 );
145
146 test.provider("deleteAttachment yields a typed not-found error", () =>
147 Effect.gen(function* () {
148 const error = yield* Effect.flip(
149 qbusiness.deleteAttachment({
150 applicationId: NONEXISTENT,
151 conversationId: NONEXISTENT,
152 attachmentId: NONEXISTENT,
153 }),
154 );
155 expectTag(error, NOT_FOUND);
156 }),
157 );
158
159 test.provider("getMedia yields a typed not-found error", () =>
160 Effect.gen(function* () {
161 const error = yield* Effect.flip(
162 qbusiness.getMedia({
163 applicationId: NONEXISTENT,
164 conversationId: NONEXISTENT,
165 messageId: NONEXISTENT,
166 mediaId: NONEXISTENT,
167 }),
168 );
169 expectTag(error, NOT_FOUND);
170 }),
171 );
172
173 test.provider("createUser yields a typed not-found error", () =>
174 Effect.gen(function* () {
175 const error = yield* Effect.flip(
176 qbusiness.createUser({ applicationId: NONEXISTENT, userId: "probe@example.com" }),
177 );
178 expectTag(error, NOT_FOUND);
179 }),
180 );
181
182 test.provider("getUser yields a typed not-found error", () =>
183 Effect.gen(function* () {
184 const error = yield* Effect.flip(
185 qbusiness.getUser({ applicationId: NONEXISTENT, userId: "probe@example.com" }),
186 );
187 expectTag(error, NOT_FOUND);
188 }),
189 );
190
191 test.provider("updateUser yields a typed not-found error", () =>
192 Effect.gen(function* () {
193 const error = yield* Effect.flip(
194 qbusiness.updateUser({ applicationId: NONEXISTENT, userId: "probe@example.com" }),
195 );
196 expectTag(error, NOT_FOUND);
197 }),
198 );
199
200 test.provider("deleteUser yields a typed not-found error", () =>
201 Effect.gen(function* () {
202 const error = yield* Effect.flip(
203 qbusiness.deleteUser({ applicationId: NONEXISTENT, userId: "probe@example.com" }),
204 );
205 expectTag(error, NOT_FOUND);
206 }),
207 );
208
209 test.provider("getPolicy yields a typed not-found error", () =>
210 Effect.gen(function* () {
211 const error = yield* Effect.flip(qbusiness.getPolicy({ applicationId: NONEXISTENT }));
212 expectTag(error, NOT_FOUND);
213 }),
214 );
215
216 test.provider("associatePermission yields a typed error", () =>
217 Effect.gen(function* () {
218 const error = yield* Effect.flip(
219 qbusiness.associatePermission({
220 applicationId: NONEXISTENT,
221 statementId: "probe",
222 actions: ["qbusiness:SearchRelevantContent"],
223 principal: "arn:aws:iam::123456789012:role/AlchemyProbeRole",
224 }),
225 );
226 expectTag(error, NOT_FOUND_OR_INVALID);
227 }),
228 );
229
230 test.provider("disassociatePermission yields a typed error", () =>
231 Effect.gen(function* () {
232 const error = yield* Effect.flip(
233 qbusiness.disassociatePermission({ applicationId: NONEXISTENT, statementId: "probe" }),
234 );
235 expectTag(error, NOT_FOUND_OR_INVALID);
236 }),
237 );
238
239 test.provider("createSubscription yields a typed error", () =>
240 Effect.gen(function* () {
241 const error = yield* Effect.flip(
242 qbusiness.createSubscription({
243 applicationId: NONEXISTENT,
244 principal: { user: NONEXISTENT },
245 type: "Q_LITE",
246 }),
247 );
248 expectTag(error, NOT_FOUND_OR_INVALID);
249 }),
250 );
251
252 test.provider("updateSubscription yields a typed error", () =>
253 Effect.gen(function* () {
254 const error = yield* Effect.flip(
255 qbusiness.updateSubscription({
256 applicationId: NONEXISTENT,
257 subscriptionId: NONEXISTENT,
258 type: "Q_LITE",
259 }),
260 );
261 expectTag(error, NOT_FOUND_OR_INVALID);
262 }),
263 );
264
265 test.provider("cancelSubscription yields a typed error", () =>
266 Effect.gen(function* () {
267 const error = yield* Effect.flip(
268 qbusiness.cancelSubscription({ applicationId: NONEXISTENT, subscriptionId: NONEXISTENT }),
269 );
270 expectTag(error, NOT_FOUND_OR_INVALID);
271 }),
272 );
273
274 // listSubscriptions does NOT 404 a nonexistent application — the live API
275 // answers with an empty page, which the probe pins down.
276 test.provider("listSubscriptions answers an empty page", () =>
277 Effect.gen(function* () {
278 const response = yield* qbusiness.listSubscriptions({ applicationId: NONEXISTENT });
279 expect(response.subscriptions ?? []).toHaveLength(0);
280 }),
281 );
282
283 test.provider("batchPutDocument yields a typed error", () =>
284 Effect.gen(function* () {
285 const error = yield* Effect.flip(
286 qbusiness.batchPutDocument({
287 applicationId: NONEXISTENT,
288 indexId: NONEXISTENT,
289 documents: [
290 {
291 id: "probe",
292 content: { blob: new TextEncoder().encode("probe") },
293 contentType: "PLAIN_TEXT",
294 },
295 ],
296 }),
297 );
298 expectTag(error, NOT_FOUND_OR_INVALID);
299 }),
300 );
301
302 test.provider("batchDeleteDocument yields a typed not-found error", () =>
303 Effect.gen(function* () {
304 const error = yield* Effect.flip(
305 qbusiness.batchDeleteDocument({
306 applicationId: NONEXISTENT,
307 indexId: NONEXISTENT,
308 documents: [{ documentId: "probe" }],
309 }),
310 );
311 expectTag(error, NOT_FOUND);
312 }),
313 );
314
315 test.provider("listDocuments yields a typed not-found error", () =>
316 Effect.gen(function* () {
317 const error = yield* Effect.flip(
318 qbusiness.listDocuments({ applicationId: NONEXISTENT, indexId: NONEXISTENT }),
319 );
320 expectTag(error, NOT_FOUND);
321 }),
322 );
323
324 test.provider("getDocumentContent yields a typed not-found error", () =>
325 Effect.gen(function* () {
326 const error = yield* Effect.flip(
327 qbusiness.getDocumentContent({
328 applicationId: NONEXISTENT,
329 indexId: NONEXISTENT,
330 documentId: "probe",
331 }),
332 );
333 expectTag(error, NOT_FOUND);
334 }),
335 );
336
337 test.provider("checkDocumentAccess yields a typed error", () =>
338 Effect.gen(function* () {
339 const error = yield* Effect.flip(
340 qbusiness.checkDocumentAccess({
341 applicationId: NONEXISTENT,
342 indexId: NONEXISTENT,
343 userId: "probe@example.com",
344 documentId: "probe",
345 }),
346 );
347 expectTag(error, NOT_FOUND_OR_INVALID);
348 }),
349 );
350
351 test.provider("putGroup yields a typed not-found error", () =>
352 Effect.gen(function* () {
353 const error = yield* Effect.flip(
354 qbusiness.putGroup({
355 applicationId: NONEXISTENT,
356 indexId: NONEXISTENT,
357 groupName: "probe",
358 type: "INDEX",
359 groupMembers: { memberUsers: [{ userId: "probe@example.com", type: "INDEX" }] },
360 }),
361 );
362 expectTag(error, NOT_FOUND);
363 }),
364 );
365
366 test.provider("getGroup yields a typed not-found error", () =>
367 Effect.gen(function* () {
368 const error = yield* Effect.flip(
369 qbusiness.getGroup({
370 applicationId: NONEXISTENT,
371 indexId: NONEXISTENT,
372 groupName: "probe",
373 }),
374 );
375 expectTag(error, NOT_FOUND);
376 }),
377 );
378
379 test.provider("deleteGroup yields a typed not-found error", () =>
380 Effect.gen(function* () {
381 const error = yield* Effect.flip(
382 qbusiness.deleteGroup({
383 applicationId: NONEXISTENT,
384 indexId: NONEXISTENT,
385 groupName: "probe",
386 }),
387 );
388 expectTag(error, NOT_FOUND);
389 }),
390 );
391
392 test.provider("listGroups yields a typed not-found error", () =>
393 Effect.gen(function* () {
394 const error = yield* Effect.flip(
395 qbusiness.listGroups({
396 applicationId: NONEXISTENT,
397 indexId: NONEXISTENT,
398 updatedEarlierThan: new Date(),
399 }),
400 );
401 expectTag(error, NOT_FOUND);
402 }),
403 );
404
405 test.provider("startDataSourceSyncJob yields a typed not-found error", () =>
406 Effect.gen(function* () {
407 const error = yield* Effect.flip(
408 qbusiness.startDataSourceSyncJob({
409 applicationId: NONEXISTENT,
410 indexId: NONEXISTENT,
411 dataSourceId: NONEXISTENT,
412 }),
413 );
414 expectTag(error, NOT_FOUND);
415 }),
416 );
417
418 test.provider("stopDataSourceSyncJob yields a typed not-found error", () =>
419 Effect.gen(function* () {
420 const error = yield* Effect.flip(
421 qbusiness.stopDataSourceSyncJob({
422 applicationId: NONEXISTENT,
423 indexId: NONEXISTENT,
424 dataSourceId: NONEXISTENT,
425 }),
426 );
427 expectTag(error, NOT_FOUND);
428 }),
429 );
430
431 test.provider("listDataSourceSyncJobs yields a typed not-found error", () =>
432 Effect.gen(function* () {
433 const error = yield* Effect.flip(
434 qbusiness.listDataSourceSyncJobs({
435 applicationId: NONEXISTENT,
436 indexId: NONEXISTENT,
437 dataSourceId: NONEXISTENT,
438 }),
439 );
440 expectTag(error, NOT_FOUND);
441 }),
442 );
443
444 test.provider("createAnonymousWebExperienceUrl yields a typed not-found error", () =>
445 Effect.gen(function* () {
446 const error = yield* Effect.flip(
447 qbusiness.createAnonymousWebExperienceUrl({
448 applicationId: NONEXISTENT,
449 webExperienceId: NONEXISTENT,
450 }),
451 );
452 expectTag(error, NOT_FOUND);
453 }),
454 );
455 },
456 );
457
458 // ---------------------------------------------------------------------------
459 // Full runtime fixture: a Lambda bound to all thirty-six bindings against a
460 // live ANONYMOUS-identity application + STARTER index + CUSTOM data source +
461 // native retriever + web experience. The application/index take minutes to
462 // provision and the index bills hourly once ACTIVE, so this is gated behind
463 // AWS_TEST_QBUSINESS=1 and always destroys what it created.
464 // ---------------------------------------------------------------------------
465
466 const sharedStack = Core.scratchStack(testOptions, "QBusinessBindings");
467
468 test.provider.skipIf(!process.env.AWS_TEST_QBUSINESS)(
469 "all bindings against a live anonymous application",
470 () =>
471 Effect.gen(function* () {
472 yield* sharedStack.destroy();
473
474 yield* Effect.gen(function* () {
475 const { functionUrl } = yield* sharedStack.deploy(
476 Effect.gen(function* () {
477 return yield* QBusinessTestFunction;
478 }).pipe(Effect.provide(QBusinessTestFunctionLive)),
479 );
480 expect(functionUrl).toBeTruthy();
481 const baseUrl = functionUrl!.replace(/\/+$/, "");
482
483 const getJson = (path: string) =>
484 HttpClient.get(`${baseUrl}${path}`).pipe(
485 Effect.flatMap((response) =>
486 response.status >= 500
487 ? Effect.fail(new Error(`transient upstream ${response.status}`))
488 : Effect.succeed(response),
489 ),
490 Effect.retry({
491 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
492 }),
493 Effect.flatMap((r) => r.json),
494 );
495
496 // All thirty-six capabilities initialized in the runtime.
497 const bindings = (yield* getJson("/bindings")) as { bound: string[] };
498 expect(bindings.bound).toHaveLength(36);
499
500 // CUSTOM connector sync cycle — proves the three data-source
501 // bindings and the id injection.
502 const sync = (yield* getJson("/sync-cycle")) as {
503 executionId?: string | null;
504 jobs?: number | string;
505 stopped?: boolean;
506 errorTag?: string;
507 };
508 expect(sync.errorTag).toBeUndefined();
509 expect(sync.executionId).toBeTruthy();
510
511 // BatchPutDocument — proves applicationId/indexId injection + IAM.
512 const put = (yield* getJson("/put-documents")) as { failed?: number; errorTag?: string };
513 expect(put.errorTag).toBeUndefined();
514 expect(put.failed).toBe(0);
515
516 // ListDocuments — poll (bounded) until the pushed document leaves
517 // the processing states.
518 const statuses = yield* getJson("/documents").pipe(
519 Effect.map((r) => (r as { statuses?: string[] }).statuses ?? []),
520 Effect.repeat({
521 schedule: Schedule.spaced("10 seconds"),
522 until: (s): boolean => s.includes("INDEXED") || s.includes("FAILED"),
523 times: 30,
524 }),
525 );
526 expect(statuses).toContain("INDEXED");
527
528 // GetDocumentContent + CheckDocumentAccess over the pushed document.
529 const content = (yield* getJson("/document-content")) as {
530 hasUrl?: boolean;
531 errorTag?: string;
532 };
533 expect(content.errorTag).toBeUndefined();
534 expect(content.hasUrl).toBe(true);
535
536 // ChatSync — the flagship data-plane call answers over the indexed
537 // document.
538 const chat = (yield* getJson("/chat?q=What is the passphrase?")) as {
539 conversationId?: string;
540 messageId?: string;
541 hasMessage?: boolean;
542 errorTag?: string;
543 };
544 expect(chat.errorTag).toBeUndefined();
545 expect(chat.hasMessage).toBe(true);
546
547 // SearchRelevantContent through the native retriever.
548 const search = (yield* getJson("/search?q=zanzibar passphrase")) as {
549 count?: number;
550 errorTag?: string;
551 };
552 expect(search.errorTag).toBeUndefined();
553
554 // Chat controls read + update round trip.
555 const controls = (yield* getJson("/controls")) as {
556 responseScope?: string | null;
557 errorTag?: string;
558 };
559 expect(controls.errorTag).toBeUndefined();
560 const updateControls = (yield* getJson("/update-controls")) as {
561 ok?: boolean;
562 errorTag?: string;
563 };
564 expect(updateControls.errorTag).toBeUndefined();
565
566 // Application policy read.
567 const policy = (yield* getJson("/policy")) as { policy?: string | null; errorTag?: string };
568 expect(policy.errorTag).toBeUndefined();
569
570 // Anonymous URL minting (ANONYMOUS identity type).
571 const anonymousUrl = (yield* getJson("/anonymous-url")) as {
572 hasUrl?: boolean;
573 errorTag?: string;
574 };
575 expect(anonymousUrl.errorTag).toBeUndefined();
576 expect(anonymousUrl.hasUrl).toBe(true);
577
578 // The remaining routes must answer 200 with either concrete fields
579 // or a TYPED error tag (an ANONYMOUS application rejects the user
580 // store / subscription plane with typed errors) — an untyped error
581 // would crash the route into a 500 and fail getJson.
582 for (const path of [
583 "/conversations",
584 "/attachments",
585 "/delete-attachment",
586 "/media",
587 "/users",
588 "/groups",
589 "/subscriptions",
590 "/subscription-cycle",
591 "/associate-permission",
592 "/disassociate-permission",
593 "/check-access",
594 ]) {
595 const result = (yield* getJson(path)) as Record<string, unknown>;
596 expect(result).toBeTruthy();
597 }
598
599 // BatchDeleteDocument cleanup.
600 const deleted = (yield* getJson("/delete-documents")) as {
601 failed?: number;
602 errorTag?: string;
603 };
604 expect(deleted.errorTag).toBeUndefined();
605 }).pipe(Effect.ensuring(sharedStack.destroy().pipe(Effect.orDie)));
606 }),
607 {
608 tags: [
609 "provider:aws",
610 "provider:aws:batch",
611 "provider:aws:lambda",
612 "provider:aws:qbusiness",
613 "live",
614 ],
615 timeout: 3_600_000,
616 },
617 );