@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 297 lines 12.6 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 sts from "@distilled.cloud/aws/sts";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import BdaTestFunctionLive, { BdaTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "BdaBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy under parallel-suite load.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22 let profileArn: string | undefined;
23
24 // The cross-region data automation profile ARN is account + geography
25 // scoped. Credentials are only provided inside `test.provider` contexts, so
26 // this is computed lazily by the first test that needs it.
27 const getProfileArn = Effect.gen(function* () {
28 if (profileArn !== undefined) return profileArn;
29 const { Account } = yield* sts.getCallerIdentity({});
30 const region = yield* Effect.sync(() => process.env.AWS_REGION ?? "us-west-2");
31 const geo = region.startsWith("eu-") ? "eu" : region.startsWith("ap-") ? "apac" : "us";
32 profileArn = `arn:aws:bedrock:${region}:${Account}:data-automation-profile/${geo}.data-automation-v1`;
33 return profileArn;
34 });
35
36 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
37 readonly status: number;
38 readonly body: string;
39 }> {}
40
41 // The shared Lambda fixture occasionally answers a transient 5xx under load
42 // (cold re-init, IAM propagation on the freshly attached policies that the
43 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
44 // 4xx/assertion failure surfaces immediately.
45 const send = (request: HttpClientRequest.HttpClientRequest) =>
46 HttpClient.execute(request).pipe(
47 Effect.flatMap((response) =>
48 response.status >= 500
49 ? response.text.pipe(
50 Effect.flatMap((body) =>
51 Effect.fail(new TransientUpstream({ status: response.status, body })),
52 ),
53 )
54 : Effect.succeed(response),
55 ),
56 Effect.retry({
57 while: (e) => e._tag === "TransientUpstream",
58 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
59 }),
60 );
61
62 describe.sequential(
63 "BedrockDataAutomation Bindings",
64 {
65 tags: [
66 "provider:aws",
67 "provider:aws:bedrockdataautomation",
68 "provider:aws:lambda",
69 "provider:aws:s3",
70 "live",
71 ],
72 },
73 () => {
74 beforeAll(
75 Effect.gen(function* () {
76 yield* Effect.logInfo("BDA test setup: destroying previous resources");
77 yield* sharedStack.destroy();
78
79 yield* Effect.logInfo("BDA test setup: deploying fixture");
80 const { functionUrl } = yield* sharedStack.deploy(
81 Effect.gen(function* () {
82 return yield* BdaTestFunction;
83 }).pipe(Effect.provide(BdaTestFunctionLive)),
84 );
85
86 expect(functionUrl).toBeTruthy();
87 baseUrl = functionUrl!.replace(/\/+$/, "");
88
89 const readinessUrl = `${baseUrl}/bindings`;
90 yield* Effect.logInfo(`BDA test setup: probing readiness at ${readinessUrl}`);
91 yield* HttpClient.get(readinessUrl).pipe(
92 Effect.flatMap((response) =>
93 response.status === 200
94 ? Effect.succeed(response)
95 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
96 ),
97 Effect.tapError((error) =>
98 Effect.logWarning(`BDA test setup: fixture not ready yet (${String(error)})`),
99 ),
100 Effect.retry({ schedule: readinessPolicy }),
101 );
102 }),
103 { timeout: 300_000 },
104 );
105
106 afterAll(sharedStack.destroy(), { timeout: 180_000 });
107
108 describe("binding registration", () => {
109 test.provider("all 14 capabilities initialize in the runtime", (_stack) =>
110 Effect.gen(function* () {
111 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
112 Effect.flatMap((r) => r.json),
113 );
114 expect((response as any).bound).toHaveLength(14);
115 }),
116 );
117 });
118
119 describe("InvokeDataAutomationAsync + GetDataAutomationStatus", () => {
120 test.provider(
121 "submits an async job against the bound project and polls its status",
122 (_stack) =>
123 Effect.gen(function* () {
124 const arn = yield* getProfileArn;
125 const invoked = (yield* send(
126 HttpClientRequest.post(
127 `${baseUrl}/invoke-async?profileArn=${encodeURIComponent(arn)}`,
128 ),
129 ).pipe(Effect.flatMap((r) => r.json))) as {
130 invocationArn?: string;
131 error?: string;
132 message?: string;
133 };
134 // Proves project injection + bedrock:InvokeDataAutomationAsync +
135 // the caller-permission S3 grants end-to-end. On failure the
136 // fixture reports the typed tag + message here.
137 expect(
138 invoked.error === undefined ? "none" : `${invoked.error}: ${invoked.message}`,
139 ).toBe("none");
140 const invocationArn = invoked.invocationArn;
141 expect(invocationArn).toContain(":data-automation-invocation/");
142 if (invocationArn === undefined) {
143 // Unreachable — the expect above throws on undefined; this
144 // narrows the type for the status poll below.
145 return yield* Effect.die(new Error("invocationArn missing"));
146 }
147
148 // Immediate status poll — the job settles asynchronously, so any
149 // lifecycle status proves bedrock:GetDataAutomationStatus.
150 const status = (yield* send(
151 HttpClientRequest.get(
152 `${baseUrl}/status?invocationArn=${encodeURIComponent(invocationArn)}`,
153 ),
154 ).pipe(Effect.flatMap((r) => r.json))) as { status: string };
155 expect(["Created", "InProgress", "Success", "ServiceError", "ClientError"]).toContain(
156 status.status,
157 );
158 }),
159 { timeout: 120_000 },
160 );
161 });
162
163 describe("Library ingestion jobs + entities", () => {
164 test.provider(
165 "starts an inline ingestion job, polls it, and lists jobs + entities",
166 (_stack) =>
167 Effect.gen(function* () {
168 // InvokeDataAutomationLibraryIngestionJob — inline vocabulary
169 // upsert against the bound library.
170 const ingested = (yield* send(HttpClientRequest.post(`${baseUrl}/library-ingest`)).pipe(
171 Effect.flatMap((r) => r.json),
172 )) as { jobArn?: string; error?: string; message?: string };
173 expect(
174 ingested.error === undefined ? "none" : `${ingested.error}: ${ingested.message}`,
175 ).toBe("none");
176 expect(ingested.jobArn).toBeTruthy();
177
178 // GetDataAutomationLibraryIngestionJob — any lifecycle status
179 // proves the grant + library injection.
180 const job = (yield* send(
181 HttpClientRequest.get(
182 `${baseUrl}/library-ingestion-job?jobArn=${encodeURIComponent(ingested.jobArn!)}`,
183 ),
184 ).pipe(Effect.flatMap((r) => r.json))) as { status?: string };
185 expect(["IN_PROGRESS", "COMPLETED", "COMPLETED_WITH_ERRORS", "FAILED"]).toContain(
186 job.status,
187 );
188
189 // ListDataAutomationLibraryIngestionJobs — the job we just
190 // started must be visible.
191 const jobs = (yield* send(
192 HttpClientRequest.get(`${baseUrl}/library-ingestion-jobs`),
193 ).pipe(Effect.flatMap((r) => r.json))) as { count: number };
194 expect(jobs.count).toBeGreaterThanOrEqual(1);
195
196 // ListDataAutomationLibraryEntities — entity visibility lags the
197 // async job, so only the successful (authorized) empty-or-more
198 // page is asserted.
199 const entities = (yield* send(
200 HttpClientRequest.get(`${baseUrl}/library-entities`),
201 ).pipe(Effect.flatMap((r) => r.json))) as { count: number };
202 expect(entities.count).toBeGreaterThanOrEqual(0);
203
204 // GetDataAutomationLibraryEntity — typed not-found path proves
205 // the grant (an IAM gap would surface AccessDeniedException).
206 const missing = (yield* send(
207 HttpClientRequest.get(`${baseUrl}/library-entity-missing`),
208 ).pipe(Effect.flatMap((r) => r.json))) as { tag: string };
209 expect(["ResourceNotFoundException", "ValidationException"]).toContain(missing.tag);
210 }),
211 { timeout: 120_000 },
212 );
213 });
214
215 describe("Blueprint management", () => {
216 test.provider(
217 "creates a blueprint version and drives copy-stage's typed error path",
218 (_stack) =>
219 Effect.gen(function* () {
220 // CreateBlueprintVersion — snapshots the fixture blueprint.
221 const version = (yield* send(
222 HttpClientRequest.post(`${baseUrl}/blueprint-version`),
223 ).pipe(Effect.flatMap((r) => r.json))) as {
224 version?: string;
225 error?: string;
226 message?: string;
227 };
228 expect(
229 version.error === undefined ? "none" : `${version.error}: ${version.message}`,
230 ).toBe("none");
231 expect(version.version).toBeTruthy();
232
233 // CopyBlueprintStage — the fixture blueprint has no DEVELOPMENT
234 // stage, so the typed error tag proves the grant + injection
235 // without creating a stage copy.
236 const copied = (yield* send(
237 HttpClientRequest.post(`${baseUrl}/copy-stage-validation`),
238 ).pipe(Effect.flatMap((r) => r.json))) as { tag: string };
239 expect(["ResourceNotFoundException", "ValidationException"]).toContain(copied.tag);
240 }),
241 { timeout: 120_000 },
242 );
243 });
244
245 describe("Blueprint optimization", () => {
246 test.provider(
247 "drives the typed error paths of optimize + status polling",
248 (_stack) =>
249 Effect.gen(function* () {
250 // InvokeBlueprintOptimizationAsync — an empty samples list is
251 // invalid, so the typed ValidationException proves the grant +
252 // blueprint injection without paying for a real optimization job.
253 const arn = yield* getProfileArn;
254 const optimize = (yield* send(
255 HttpClientRequest.post(
256 `${baseUrl}/optimize-validation?profileArn=${encodeURIComponent(arn)}`,
257 ),
258 ).pipe(Effect.flatMap((r) => r.json))) as { tag: string };
259 expect(optimize.tag).toBe("ValidationException");
260
261 // GetBlueprintOptimizationStatus — typed not-found on a
262 // well-formed but nonexistent invocation ARN.
263 const { Account } = yield* sts.getCallerIdentity({});
264 const region = yield* Effect.sync(() => process.env.AWS_REGION ?? "us-west-2");
265 const missingArn = `arn:aws:bedrock:${region}:${Account}:blueprint-optimization-invocation/00000000-0000-0000-0000-000000000000`;
266 const status = (yield* send(
267 HttpClientRequest.get(
268 `${baseUrl}/optimization-status-missing?invocationArn=${encodeURIComponent(missingArn)}`,
269 ),
270 ).pipe(Effect.flatMap((r) => r.json))) as { tag: string };
271 expect(["ResourceNotFoundException", "ValidationException"]).toContain(status.tag);
272 }),
273 { timeout: 120_000 },
274 );
275 });
276
277 describe("InvokeDataAutomation", () => {
278 // The sync API needs a SYNC project and a non-empty input; the fixture
279 // drives the binding through its typed ValidationException path — an IAM
280 // gap would surface AccessDeniedException (a 500) instead of the tag.
281 test.provider(
282 "surfaces the typed ValidationException for an empty input",
283 (_stack) =>
284 Effect.gen(function* () {
285 const arn = yield* getProfileArn;
286 const response = (yield* send(
287 HttpClientRequest.post(
288 `${baseUrl}/invoke-sync-validation?profileArn=${encodeURIComponent(arn)}`,
289 ),
290 ).pipe(Effect.flatMap((r) => r.json))) as { tag: string };
291 expect(response.tag).toBe("ValidationException");
292 }),
293 { timeout: 120_000 },
294 );
295 });
296 },
297 );