@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 158 lines 5.79 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Blueprint } 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 // A deterministic, valid BDA blueprint schema (checked-in constant fixture).
18 const invoiceSchema = (instruction: string) =>
19 JSON.stringify({
20 $schema: "http://json-schema.org/draft-07/schema#",
21 description: "Extract invoice fields",
22 class: "invoice",
23 type: "object",
24 definitions: {},
25 properties: { invoice_number: { type: "string", inferenceType: "explicit", instruction } },
26 });
27
28 const findBlueprint = (blueprintArn: string) =>
29 bda.getBlueprint({ blueprintArn }).pipe(
30 Effect.map((r) => r.blueprint),
31 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
32 );
33
34 class BlueprintStillExists extends Data.TaggedError("BlueprintStillExists")<{
35 readonly blueprintArn: string;
36 }> {}
37
38 const assertBlueprintDeleted = (blueprintArn: string) =>
39 findBlueprint(blueprintArn).pipe(
40 Effect.flatMap((blueprint) =>
41 blueprint === undefined
42 ? Effect.void
43 : Effect.fail(new BlueprintStillExists({ blueprintArn })),
44 ),
45 Effect.retry({
46 while: (e) => e._tag === "BlueprintStillExists",
47 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
48 }),
49 );
50
51 // Ungated typed-error probe: prove the distilled error union carries the
52 // not-found tag this provider's read/delete paths depend on.
53 test.provider(
54 "getBlueprint on a nonexistent ARN fails with ResourceNotFoundException",
55 () =>
56 Effect.gen(function* () {
57 const { Account } = yield* sts.getCallerIdentity({});
58 const region = yield* Effect.sync(() => process.env.AWS_REGION ?? "us-west-2");
59 const error = yield* Effect.flip(
60 bda.getBlueprint({
61 blueprintArn: `arn:aws:bedrock:${region}:${Account}:blueprint/nonexistent-alchemy-probe`,
62 }),
63 );
64 expect(error._tag).toBe("ResourceNotFoundException");
65 }),
66 { tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"] },
67 );
68
69 test.provider(
70 "create, update schema, delete blueprint",
71 (stack) =>
72 Effect.gen(function* () {
73 yield* stack.destroy();
74
75 const blueprint = yield* stack.deploy(
76 Effect.gen(function* () {
77 return yield* Blueprint("TestBlueprint", {
78 type: "DOCUMENT",
79 schema: invoiceSchema("The invoice number"),
80 tags: { Environment: "test" },
81 });
82 }),
83 );
84
85 expect(blueprint.blueprintArn).toContain(":blueprint/");
86 expect(blueprint.type).toBe("DOCUMENT");
87
88 // out-of-band verification via distilled
89 const created = yield* findBlueprint(blueprint.blueprintArn);
90 expect(created).toBeDefined();
91 expect(unredact(created!.blueprintName)).toBe(blueprint.blueprintName);
92 expect(JSON.parse(unredact(created!.schema)).properties.invoice_number).toBeDefined();
93 const tags = yield* bda
94 .listTagsForResource({ resourceARN: blueprint.blueprintArn })
95 .pipe(Effect.map((r) => Object.fromEntries((r.tags ?? []).map((t) => [t.key, t.value]))));
96 expect(tags.Environment).toBe("test");
97 expect(tags["alchemy::id"]).toBe("TestBlueprint");
98
99 // update the schema in place — same physical blueprint
100 const updated = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* Blueprint("TestBlueprint", {
103 type: "DOCUMENT",
104 schema: invoiceSchema("The unique invoice number on the header"),
105 tags: { Environment: "test" },
106 });
107 }),
108 );
109 expect(updated.blueprintArn).toBe(blueprint.blueprintArn);
110
111 const afterUpdate = yield* findBlueprint(blueprint.blueprintArn);
112 expect(JSON.parse(unredact(afterUpdate!.schema)).properties.invoice_number.instruction).toBe(
113 "The unique invoice number on the header",
114 );
115
116 yield* stack.destroy();
117 yield* assertBlueprintDeleted(blueprint.blueprintArn);
118 }),
119 { tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"], timeout: 120_000 },
120 );
121
122 test.provider(
123 "custom blueprint name and replacement on rename",
124 (stack) =>
125 Effect.gen(function* () {
126 yield* stack.destroy();
127
128 const first = yield* stack.deploy(
129 Effect.gen(function* () {
130 return yield* Blueprint("NamedBlueprint", {
131 blueprintName: "alchemy-test-blueprint-a",
132 type: "DOCUMENT",
133 schema: invoiceSchema("The invoice number"),
134 });
135 }),
136 );
137 expect(first.blueprintName).toBe("alchemy-test-blueprint-a");
138
139 // renaming triggers a replacement: new physical blueprint, old one gone
140 const second = yield* stack.deploy(
141 Effect.gen(function* () {
142 return yield* Blueprint("NamedBlueprint", {
143 blueprintName: "alchemy-test-blueprint-b",
144 type: "DOCUMENT",
145 schema: invoiceSchema("The invoice number"),
146 });
147 }),
148 );
149 expect(second.blueprintName).toBe("alchemy-test-blueprint-b");
150 expect(second.blueprintArn).not.toBe(first.blueprintArn);
151
152 yield* assertBlueprintDeleted(first.blueprintArn);
153
154 yield* stack.destroy();
155 yield* assertBlueprintDeleted(second.blueprintArn);
156 }),
157 { tags: ["provider:aws", "provider:aws:bedrockdataautomation", "live"], timeout: 120_000 },
158 );