@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 160 lines 6.75 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 qapps from "@distilled.cloud/aws/qapps";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import QAppsTestFunctionLive, { QAppsTestFunction } from "./bindings-handler.ts";
10
11 const testOptions = { providers: AWS.providers() };
12 const { test } = Test.make(testOptions);
13
14 // A well-formed-but-nonexistent Q Business application environment instance
15 // the ungated probes are driven against.
16 const NONEXISTENT_INSTANCE = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
17 const NONEXISTENT_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
18
19 // Without a Q Business instance, Amazon Q Apps rejects at the door with a
20 // typed UnauthorizedException ("Unauthorized") — the caller is not an IAM
21 // Identity Center user of any instance. An entitled account with bogus ids
22 // surfaces ResourceNotFoundException / AccessDeniedException /
23 // ValidationException instead; all are typed tags in the distilled unions.
24 const TYPED_REJECTIONS = [
25 "UnauthorizedException",
26 "ResourceNotFoundException",
27 "AccessDeniedException",
28 "ValidationException",
29 ];
30
31 // ---------------------------------------------------------------------------
32 // Ungated typed-error probes: representative operations behind each binding
33 // group are exercised directly through distilled against a nonexistent
34 // instance and must answer with a typed tag. These prove the distilled error
35 // unions and request serialization (instance-id header, query/body shapes)
36 // at near-zero cost on every CI pass; the full runtime fixture below is
37 // gated behind an entitled Q Business instance.
38 // ---------------------------------------------------------------------------
39
40 describe(
41 "QApps data-plane operations (typed-error probes)",
42 { tags: ["provider:aws", "provider:aws:qapps", "live"] },
43 () => {
44 const probes: Record<string, Effect.Effect<unknown, { _tag: string }, any>> = {
45 startQAppSession: qapps.startQAppSession({
46 instanceId: NONEXISTENT_INSTANCE,
47 appId: NONEXISTENT_ID,
48 appVersion: 1,
49 }),
50 getQAppSession: qapps.getQAppSession({
51 instanceId: NONEXISTENT_INSTANCE,
52 sessionId: NONEXISTENT_ID,
53 }),
54 listQApps: qapps.listQApps({ instanceId: NONEXISTENT_INSTANCE }),
55 listCategories: qapps.listCategories({ instanceId: NONEXISTENT_INSTANCE }),
56 batchCreateCategory: qapps.batchCreateCategory({
57 instanceId: NONEXISTENT_INSTANCE,
58 categories: [{ title: "alchemy-probe" }],
59 }),
60 createLibraryItem: qapps.createLibraryItem({
61 instanceId: NONEXISTENT_INSTANCE,
62 appId: NONEXISTENT_ID,
63 appVersion: 1,
64 categories: [],
65 }),
66 describeQAppPermissions: qapps.describeQAppPermissions({
67 instanceId: NONEXISTENT_INSTANCE,
68 appId: NONEXISTENT_ID,
69 }),
70 predictQApp: qapps.predictQApp({ instanceId: NONEXISTENT_INSTANCE }),
71 };
72
73 for (const [name, probe] of Object.entries(probes)) {
74 test.provider(
75 `${name} against a nonexistent instance yields a typed error`,
76 () =>
77 Effect.gen(function* () {
78 const error = yield* Effect.flip(probe);
79 expect(TYPED_REJECTIONS).toContain(error._tag);
80 }),
81 { timeout: 60_000 },
82 );
83 }
84 },
85 );
86
87 // ---------------------------------------------------------------------------
88 // Full runtime fixture: a Q App + a Lambda bound to six Q Apps bindings
89 // (sessions, permissions, inventory, categories — covering both the
90 // app/session-scoped and the instance-level builders). Amazon Q Apps lives
91 // inside a Q Business application environment instance (IAM Identity
92 // Center required), which the shared testing account does not have, so this
93 // is gated behind AWS_TEST_QAPPS=1 + QAPPS_INSTANCE_ID and always destroys
94 // what it created. Note: the Q Apps data plane authorizes the calling
95 // identity as an Identity Center user of the instance — an entitled runner
96 // must grant the Lambda's role access to the instance.
97 // ---------------------------------------------------------------------------
98
99 const sharedStack = Core.scratchStack(testOptions, "QAppsBindings");
100
101 test.provider.skipIf(!process.env.AWS_TEST_QAPPS)(
102 "session, inventory, category, and permission bindings against a live instance",
103 () =>
104 Effect.gen(function* () {
105 yield* sharedStack.destroy();
106
107 yield* Effect.gen(function* () {
108 const { functionUrl } = yield* sharedStack.deploy(
109 Effect.gen(function* () {
110 return yield* QAppsTestFunction;
111 }).pipe(Effect.provide(QAppsTestFunctionLive)),
112 );
113 expect(functionUrl).toBeTruthy();
114 const baseUrl = functionUrl!.replace(/\/+$/, "");
115
116 const getJson = (path: string) =>
117 HttpClient.get(`${baseUrl}${path}`).pipe(
118 Effect.flatMap((response) =>
119 response.status >= 500
120 ? Effect.fail(new Error(`transient upstream ${response.status}`))
121 : Effect.succeed(response),
122 ),
123 Effect.retry({
124 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
125 }),
126 Effect.flatMap((r) => r.json),
127 );
128
129 // All six capabilities initialized in the runtime.
130 const bindings = (yield* getJson("/bindings")) as { bound: string[] };
131 expect(bindings.bound).toHaveLength(6);
132
133 // ListQApps — instance-level builder, instance-id header injection.
134 const apps = (yield* getJson("/apps")) as { count?: number; errorTag?: string };
135 expect(apps.errorTag).toBeUndefined();
136 expect(apps.count).toBeGreaterThanOrEqual(1);
137
138 // ListCategories — instance-level builder.
139 const categories = (yield* getJson("/categories")) as { count?: number; errorTag?: string };
140 expect(categories.errorTag).toBeUndefined();
141
142 // DescribeQAppPermissions — app-scoped builder, app-id injection.
143 const permissions = (yield* getJson("/permissions")) as {
144 resourceArn?: string;
145 errorTag?: string;
146 };
147 expect(permissions.errorTag).toBeUndefined();
148
149 // Session flow — start → get → stop against the deployed app.
150 const session = (yield* getJson("/session")) as {
151 sessionId?: string;
152 status?: string;
153 errorTag?: string;
154 };
155 expect(session.errorTag).toBeUndefined();
156 expect(session.sessionId).toBeTruthy();
157 }).pipe(Effect.ensuring(sharedStack.destroy().pipe(Effect.orDie)));
158 }),
159 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:qapps", "live"], timeout: 600_000 },
160 );