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

main
ts 241 lines 9.71 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 Application,
4 DataSource,
5 Index,
6 Retriever,
7 WebExperience,
8 } from "@alchemy.run/aws/QBusiness";
9 import { describe, expect } from "@alchemy.run/test";
10 import * as Test from "@alchemy.run/test/Alchemy";
11 import * as qbusiness from "@distilled.cloud/aws/qbusiness";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 // Amazon Q Business applications require an IAM Identity Center (or
18 // QuickSight/IAM IdP) identity source, which the shared testing account does
19 // not provision. The ungated probes assert the distilled wiring surfaces the
20 // typed errors the provider's read/delete and create paths depend on; the
21 // full lifecycle is gated behind AWS_TEST_QBUSINESS=1 (an account with an
22 // Identity Center instance, passed as QBUSINESS_IDC_INSTANCE_ARN).
23 describe(
24 "AWS.QBusiness.Application",
25 { tags: ["provider:aws", "provider:aws:qbusiness", "live"] },
26 () => {
27 test.provider(
28 "getApplication on a nonexistent id yields a typed ResourceNotFoundException",
29 (_stack) =>
30 Effect.gen(function* () {
31 const error = yield* qbusiness
32 .getApplication({ applicationId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" })
33 .pipe(Effect.flip);
34 expect(error._tag).toBe("ResourceNotFoundException");
35 }),
36 { timeout: 60_000 },
37 );
38
39 test.provider(
40 "getIndex on a nonexistent application yields a typed ResourceNotFoundException",
41 (_stack) =>
42 Effect.gen(function* () {
43 const error = yield* qbusiness
44 .getIndex({
45 applicationId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
46 indexId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
47 })
48 .pipe(Effect.flip);
49 expect(error._tag).toBe("ResourceNotFoundException");
50 }),
51 { timeout: 60_000 },
52 );
53
54 // createApplication does NOT validate the Identity Center instance ARN
55 // synchronously — the application is created and converges asynchronously
56 // to the terminal FAILED status with an explanatory error detail. This
57 // probe proves the exact create → observe-FAILED → delete flow the
58 // provider's reconcile/delete paths depend on, without needing a real
59 // Identity Center instance. The application never becomes ACTIVE, so
60 // nothing bills.
61 test.provider(
62 "createApplication with a nonexistent Identity Center instance converges to FAILED and deletes cleanly",
63 (_stack) =>
64 Effect.gen(function* () {
65 const displayName = "alchemy-qbusiness-probe";
66 // Tolerate a leaked probe app from an interrupted prior run: a
67 // duplicate create conflicts, so fall back to the existing one.
68 const created = yield* qbusiness
69 .createApplication({
70 displayName,
71 identityCenterInstanceArn: "arn:aws:sso:::instance/ssoins-0000000000000000",
72 })
73 .pipe(
74 Effect.map((r) => r.applicationId),
75 Effect.catchTag("ConflictException", () =>
76 qbusiness
77 .listApplications({})
78 .pipe(
79 Effect.map(
80 (r) =>
81 r.applications?.find(
82 (a) => a.displayName === displayName && a.status !== "DELETING",
83 )?.applicationId,
84 ),
85 ),
86 ),
87 );
88 if (created === undefined) {
89 return yield* Effect.die(new Error("no probe applicationId available"));
90 }
91 const applicationId = created;
92
93 yield* Effect.gen(function* () {
94 // Converges to FAILED with a typed error detail.
95 const failed = yield* qbusiness.getApplication({ applicationId }).pipe(
96 Effect.repeat({
97 schedule: Schedule.spaced("5 seconds"),
98 until: (r) => r.status === "FAILED",
99 times: 24,
100 }),
101 );
102 expect(failed.status).toBe("FAILED");
103 expect(failed.error?.errorMessage).toContain("Identity Center");
104 }).pipe(
105 // Always delete the probe application, even if assertions fail.
106 Effect.ensuring(
107 qbusiness.deleteApplication({ applicationId }).pipe(
108 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed({})),
109 Effect.ignore,
110 ),
111 ),
112 );
113
114 // Deletion initiated — DELETING or gone.
115 const after = yield* qbusiness.getApplication({ applicationId }).pipe(
116 Effect.map((r) => r.status ?? "gone"),
117 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
118 );
119 expect(["DELETING", "gone"]).toContain(after);
120 }),
121 { timeout: 180_000 },
122 );
123
124 test.provider.skipIf(!process.env.AWS_TEST_QBUSINESS)(
125 "create application + index + retriever + web experience, update, destroy, verify gone",
126 (stack) =>
127 Effect.gen(function* () {
128 yield* stack.destroy();
129
130 const identityCenterInstanceArn = process.env.QBUSINESS_IDC_INSTANCE_ARN;
131 if (!identityCenterInstanceArn) {
132 return yield* Effect.die(
133 new Error("AWS_TEST_QBUSINESS runs require QBUSINESS_IDC_INSTANCE_ARN"),
134 );
135 }
136
137 const deploy = (props: { description?: string }) =>
138 stack.deploy(
139 Effect.gen(function* () {
140 const app = yield* Application("Assistant", {
141 identityCenterInstanceArn,
142 description: props.description,
143 tags: { Environment: "test" },
144 });
145
146 const index = yield* Index("Docs", { applicationId: app.applicationId });
147
148 const retriever = yield* Retriever("Docs", {
149 applicationId: app.applicationId,
150 type: "NATIVE_INDEX",
151 configuration: { nativeIndexConfiguration: { indexId: index.indexId } },
152 });
153
154 // Role the data source connector assumes; CUSTOM sources need
155 // index write access only.
156 const dataRole = yield* AWS.IAM.Role("QBusinessDataRole", {
157 assumeRolePolicyDocument: {
158 Version: "2012-10-17",
159 Statement: [
160 {
161 Effect: "Allow",
162 Principal: { Service: ["qbusiness.amazonaws.com"] },
163 Action: ["sts:AssumeRole"],
164 },
165 ],
166 },
167 inlinePolicies: {
168 ingest: {
169 Version: "2012-10-17",
170 Statement: [
171 {
172 Effect: "Allow",
173 Action: ["qbusiness:BatchPutDocument", "qbusiness:BatchDeleteDocument"],
174 Resource: ["*"],
175 },
176 ],
177 },
178 },
179 });
180
181 const source = yield* DataSource("Custom", {
182 applicationId: app.applicationId,
183 indexId: index.indexId,
184 roleArn: dataRole.roleArn,
185 configuration: { type: "CUSTOM", version: "1.0.0" },
186 });
187
188 const web = yield* WebExperience("Chat", {
189 applicationId: app.applicationId,
190 title: "Alchemy Test Assistant",
191 });
192
193 return { app, index, retriever, source, web };
194 }),
195 );
196
197 // Create — reconcile waits (bounded) for ACTIVE.
198 const { app, index, retriever, source, web } = yield* deploy({});
199 expect(app.applicationId).toBeDefined();
200 expect(app.status).toBe("ACTIVE");
201 expect(index.indexId).toBeDefined();
202 expect(index.status).toBe("ACTIVE");
203 expect(retriever.retrieverId).toBeDefined();
204 expect(retriever.status).toBe("ACTIVE");
205 expect(source.dataSourceId).toBeDefined();
206 expect(source.status).toBe("ACTIVE");
207 expect(web.webExperienceId).toBeDefined();
208 expect(web.defaultEndpoint).toBeDefined();
209
210 // Out-of-band verification via distilled.
211 const described = yield* qbusiness.getApplication({ applicationId: app.applicationId });
212 expect(described.status).toBe("ACTIVE");
213
214 // Update in place — description flows through UpdateApplication.
215 const updated = yield* deploy({ description: "updated by test" });
216 expect(updated.app.applicationId).toBe(app.applicationId);
217 const redescribed = yield* qbusiness.getApplication({ applicationId: app.applicationId });
218 expect(redescribed.description).toBe("updated by test");
219
220 yield* stack.destroy();
221
222 // Typed wait-until-gone.
223 yield* Effect.gen(function* () {
224 const gone = yield* qbusiness.getApplication({ applicationId: app.applicationId }).pipe(
225 Effect.map((d) => d.status === "DELETING"),
226 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
227 );
228 if (!gone) {
229 return yield* Effect.fail({ _tag: "StillExists" as const });
230 }
231 }).pipe(
232 Effect.retry({
233 while: (e: { _tag: string }) => e._tag === "StillExists",
234 schedule: Schedule.max([Schedule.spaced("15 seconds"), Schedule.recurs(40)]),
235 }),
236 );
237 }),
238 { tags: ["provider:aws:iam"], timeout: 3_600_000 },
239 );
240 },
241 );