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

main
ts 163 lines 5.84 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { DataAutomationLibrary } from "@alchemy.run/aws/BedrockDataAutomation";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as bda from "@distilled.cloud/aws/bedrock-data-automation";
6 import * as sts from "@distilled.cloud/aws/sts";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 const unredact = (value: string | Redacted.Redacted<string>): string =>
16 Redacted.isRedacted(value) ? Redacted.value(value) : value;
17
18 const findLibrary = (libraryArn: string) =>
19 bda.getDataAutomationLibrary({ libraryArn }).pipe(
20 Effect.map((r) => r.library),
21 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
22 );
23
24 class LibraryStillExists extends Data.TaggedError("LibraryStillExists")<{
25 readonly libraryArn: string;
26 }> {}
27
28 const assertLibraryDeleted = (libraryArn: string) =>
29 findLibrary(libraryArn).pipe(
30 Effect.flatMap((library) =>
31 library === undefined ? Effect.void : Effect.fail(new LibraryStillExists({ libraryArn })),
32 ),
33 Effect.retry({
34 while: (e) => e._tag === "LibraryStillExists",
35 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
36 }),
37 );
38
39 // Sweep libraries leaked by interrupted probe/dev runs (deterministic names
40 // only — never touches libraries owned by other suites).
41 const sweepLeakedProbeLibraries = Effect.gen(function* () {
42 const summaries = yield* bda.listDataAutomationLibraries.items({}).pipe(Stream.runCollect);
43 yield* Effect.forEach(
44 Array.from(summaries).filter(
45 (s) => s.libraryName !== undefined && unredact(s.libraryName) === "alchemy-probe-lib",
46 ),
47 (s) =>
48 bda
49 .deleteDataAutomationLibrary({ libraryArn: s.libraryArn })
50 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)),
51 { concurrency: 1 },
52 );
53 });
54
55 // Ungated typed-error probe: prove the distilled error union carries the
56 // not-found tag this provider's read/delete paths depend on.
57 test.provider(
58 "getDataAutomationLibrary on a nonexistent ARN fails with ResourceNotFoundException",
59 () =>
60 Effect.gen(function* () {
61 const { Account } = yield* sts.getCallerIdentity({});
62 const region = yield* Effect.sync(() => process.env.AWS_REGION ?? "us-west-2");
63 const error = yield* Effect.flip(
64 bda.getDataAutomationLibrary({
65 libraryArn: `arn:aws:bedrock:${region}:${Account}:data-automation-library/nonexistentalchemyprobe0`,
66 }),
67 );
68 expect(error._tag).toBe("ResourceNotFoundException");
69 }),
70 { tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"] },
71 );
72
73 test.provider(
74 "create, update description, delete library",
75 (stack) =>
76 Effect.gen(function* () {
77 yield* sweepLeakedProbeLibraries;
78 yield* stack.destroy();
79
80 const library = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* DataAutomationLibrary("TestLibrary", {
83 libraryDescription: "alchemy test library",
84 tags: { Environment: "test" },
85 });
86 }),
87 );
88
89 expect(library.libraryArn).toContain(":data-automation-library/");
90 expect(library.status).toBe("ACTIVE");
91
92 // out-of-band verification via distilled
93 const created = yield* findLibrary(library.libraryArn);
94 expect(created).toBeDefined();
95 expect(unredact(created!.libraryName)).toBe(library.libraryName);
96 expect(unredact(created!.libraryDescription ?? "")).toBe("alchemy test library");
97 const tags = yield* bda
98 .listTagsForResource({ resourceARN: library.libraryArn })
99 .pipe(Effect.map((r) => Object.fromEntries((r.tags ?? []).map((t) => [t.key, t.value]))));
100 expect(tags.Environment).toBe("test");
101 expect(tags["alchemy::id"]).toBe("TestLibrary");
102
103 // update the description in place — same physical library
104 const updated = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* DataAutomationLibrary("TestLibrary", {
107 libraryDescription: "alchemy test library (updated)",
108 tags: { Environment: "test" },
109 });
110 }),
111 );
112 expect(updated.libraryArn).toBe(library.libraryArn);
113
114 const afterUpdate = yield* findLibrary(library.libraryArn);
115 expect(unredact(afterUpdate!.libraryDescription ?? "")).toBe(
116 "alchemy test library (updated)",
117 );
118
119 yield* stack.destroy();
120 yield* assertLibraryDeleted(library.libraryArn);
121 }),
122 {
123 tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"],
124 timeout: 120_000,
125 },
126 );
127
128 test.provider(
129 "custom library name and replacement on rename",
130 (stack) =>
131 Effect.gen(function* () {
132 yield* stack.destroy();
133
134 const first = yield* stack.deploy(
135 Effect.gen(function* () {
136 return yield* DataAutomationLibrary("NamedLibrary", {
137 libraryName: "alchemy-test-library-a",
138 });
139 }),
140 );
141 expect(first.libraryName).toBe("alchemy-test-library-a");
142
143 // renaming triggers a replacement: new physical library, old one gone
144 const second = yield* stack.deploy(
145 Effect.gen(function* () {
146 return yield* DataAutomationLibrary("NamedLibrary", {
147 libraryName: "alchemy-test-library-b",
148 });
149 }),
150 );
151 expect(second.libraryName).toBe("alchemy-test-library-b");
152 expect(second.libraryArn).not.toBe(first.libraryArn);
153
154 yield* assertLibraryDeleted(first.libraryArn);
155
156 yield* stack.destroy();
157 yield* assertLibraryDeleted(second.libraryArn);
158 }),
159 {
160 tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"],
161 timeout: 120_000,
162 },
163 );