@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 182 lines 6.61 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Blueprint, DataAutomationProject } 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
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const unredact = (value: string | Redacted.Redacted<string>): string =>
15 Redacted.isRedacted(value) ? Redacted.value(value) : value;
16
17 const documentStandardOutput = (
18 granularity: bda.DocumentExtractionGranularityType[],
19 ): bda.StandardOutputConfiguration => ({
20 document: {
21 extraction: {
22 granularity: { types: granularity },
23 boundingBox: { state: "DISABLED" },
24 },
25 generativeField: { state: "DISABLED" },
26 outputFormat: {
27 textFormat: { types: ["MARKDOWN"] },
28 additionalFileFormat: { state: "DISABLED" },
29 },
30 },
31 });
32
33 const findProject = (projectArn: string) =>
34 bda.getDataAutomationProject({ projectArn }).pipe(
35 Effect.map((r) => r.project),
36 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
37 );
38
39 class ProjectStillExists extends Data.TaggedError("ProjectStillExists")<{
40 readonly projectArn: string;
41 }> {}
42
43 const assertProjectDeleted = (projectArn: string) =>
44 findProject(projectArn).pipe(
45 Effect.flatMap((project) =>
46 project === undefined ? Effect.void : Effect.fail(new ProjectStillExists({ projectArn })),
47 ),
48 Effect.retry({
49 while: (e) => e._tag === "ProjectStillExists",
50 schedule: Schedule.max([Schedule.exponential(1000), Schedule.recurs(8)]),
51 }),
52 );
53
54 // Ungated typed-error probe: prove the distilled error union carries the
55 // not-found tag this provider's read/delete paths depend on.
56 test.provider(
57 "getDataAutomationProject on a nonexistent ARN fails with ResourceNotFoundException",
58 () =>
59 Effect.gen(function* () {
60 const { Account } = yield* sts.getCallerIdentity({});
61 const region = yield* Effect.sync(() => process.env.AWS_REGION ?? "us-west-2");
62 const error = yield* Effect.flip(
63 bda.getDataAutomationProject({
64 projectArn: `arn:aws:bedrock:${region}:${Account}:data-automation-project/nonexistent-alchemy-probe`,
65 }),
66 );
67 expect(error._tag).toBe("ResourceNotFoundException");
68 }),
69 { tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"] },
70 );
71
72 test.provider(
73 "create, update configuration, delete project",
74 (stack) =>
75 Effect.gen(function* () {
76 yield* stack.destroy();
77
78 const project = yield* stack.deploy(
79 Effect.gen(function* () {
80 return yield* DataAutomationProject("TestProject", {
81 projectDescription: "alchemy test project",
82 standardOutputConfiguration: documentStandardOutput(["DOCUMENT", "PAGE"]),
83 tags: { Environment: "test" },
84 });
85 }),
86 );
87
88 expect(project.projectArn).toContain(":data-automation-project/");
89 expect(project.status).toBe("COMPLETED");
90
91 // out-of-band verification via distilled
92 const created = yield* findProject(project.projectArn);
93 expect(created).toBeDefined();
94 expect(unredact(created!.projectName)).toBe(project.projectName);
95 expect(unredact(created!.projectDescription ?? "")).toBe("alchemy test project");
96 expect(
97 created!.standardOutputConfiguration?.document?.extraction?.granularity?.types,
98 ).toEqual(["DOCUMENT", "PAGE"]);
99 const tags = yield* bda
100 .listTagsForResource({ resourceARN: project.projectArn })
101 .pipe(Effect.map((r) => Object.fromEntries((r.tags ?? []).map((t) => [t.key, t.value]))));
102 expect(tags.Environment).toBe("test");
103 expect(tags["alchemy::id"]).toBe("TestProject");
104
105 // update the description + granularity in place — same physical project
106 const updated = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* DataAutomationProject("TestProject", {
109 projectDescription: "alchemy test project v2",
110 standardOutputConfiguration: documentStandardOutput(["DOCUMENT", "PAGE", "ELEMENT"]),
111 tags: { Environment: "test" },
112 });
113 }),
114 );
115 expect(updated.projectArn).toBe(project.projectArn);
116
117 const afterUpdate = yield* findProject(project.projectArn);
118 expect(unredact(afterUpdate!.projectDescription ?? "")).toBe("alchemy test project v2");
119 expect(
120 afterUpdate!.standardOutputConfiguration?.document?.extraction?.granularity?.types,
121 ).toEqual(["DOCUMENT", "PAGE", "ELEMENT"]);
122
123 yield* stack.destroy();
124 yield* assertProjectDeleted(project.projectArn);
125 }),
126 {
127 tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"],
128 timeout: 240_000,
129 },
130 );
131
132 test.provider(
133 "project with custom output driven by a blueprint",
134 (stack) =>
135 Effect.gen(function* () {
136 yield* stack.destroy();
137
138 const { blueprint, project } = yield* stack.deploy(
139 Effect.gen(function* () {
140 const blueprint = yield* Blueprint("CustomBlueprint", {
141 type: "DOCUMENT",
142 schema: JSON.stringify({
143 $schema: "http://json-schema.org/draft-07/schema#",
144 description: "Extract receipt fields",
145 class: "receipt",
146 type: "object",
147 definitions: {},
148 properties: {
149 total_amount: {
150 type: "string",
151 inferenceType: "explicit",
152 instruction: "The total amount on the receipt",
153 },
154 },
155 }),
156 });
157 const project = yield* DataAutomationProject("CustomProject", {
158 standardOutputConfiguration: documentStandardOutput(["DOCUMENT"]),
159 customOutputConfiguration: {
160 blueprints: [{ blueprintArn: blueprint.blueprintArn }],
161 },
162 });
163 return { blueprint, project };
164 }),
165 );
166
167 expect(project.status).toBe("COMPLETED");
168
169 // out-of-band: the project references the blueprint
170 const observed = yield* findProject(project.projectArn);
171 expect(observed?.customOutputConfiguration?.blueprints?.map((b) => b.blueprintArn)).toEqual([
172 blueprint.blueprintArn,
173 ]);
174
175 yield* stack.destroy();
176 yield* assertProjectDeleted(project.projectArn);
177 }),
178 {
179 tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"],
180 timeout: 240_000,
181 },
182 );