@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 125 lines 4.08 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Capability, Transformer } from "@alchemy.run/aws/B2BI";
3 import type { PolicyStatement } from "@alchemy.run/aws/IAM";
4 import { Bucket } from "@alchemy.run/aws/S3";
5 import { expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as b2bi from "@distilled.cloud/aws/b2bi";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Deterministic bucket for the capability's EDI input/output locations.
14 // B2BI accesses the bucket as the service principal, authorized by policy.
15 const BUCKET = "alchemy-test-b2bi-capability";
16 const BUCKET_ARN = `arn:aws:s3:::${BUCKET}`;
17
18 const b2biBucketPolicy: PolicyStatement[] = [
19 {
20 Effect: "Allow",
21 Principal: { Service: "b2bi.amazonaws.com" },
22 Action: [
23 "s3:GetObject",
24 "s3:PutObject",
25 "s3:DeleteObject",
26 "s3:ListBucket",
27 "s3:GetBucketLocation",
28 "s3:AbortMultipartUpload",
29 ],
30 Resource: [BUCKET_ARN, `${BUCKET_ARN}/*`],
31 },
32 ];
33
34 const capabilityProgram = (direction: "INBOUND" | "OUTBOUND") =>
35 Effect.gen(function* () {
36 const bucket = yield* Bucket("CapabilityBucket", {
37 bucketName: BUCKET,
38 forceDestroy: true,
39 policy: b2biBucketPolicy,
40 });
41 const transformer = yield* Transformer("OrdersTransformer", {
42 name: "alchemy-b2bi-cap-transformer",
43 status: "active",
44 inputConversion: {
45 fromFormat: "X12",
46 formatOptions: {
47 x12: { transactionSet: "X12_850", version: "VERSION_4010" },
48 },
49 },
50 mapping: {
51 templateLanguage: "JSONATA",
52 template: '{ "orderId": "test" }',
53 },
54 });
55 return yield* Capability("Orders", {
56 name: "alchemy-b2bi-capability",
57 configuration: {
58 edi: {
59 capabilityDirection: direction,
60 type: {
61 x12Details: {
62 transactionSet: "X12_850",
63 version: "VERSION_4010",
64 },
65 },
66 inputLocation: { bucketName: bucket.bucketName, key: "inbound/" },
67 outputLocation: {
68 bucketName: bucket.bucketName,
69 key: "processed/",
70 },
71 transformerId: transformer.transformerId,
72 },
73 },
74 });
75 });
76
77 const assertCapabilityGone = (capabilityId: string) =>
78 Effect.gen(function* () {
79 const result = yield* b2bi.getCapability({ capabilityId }).pipe(
80 Effect.map(() => "present" as const),
81 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
82 );
83 if (result === "present") {
84 return yield* Effect.fail(new Error(`Capability '${capabilityId}' still exists`));
85 }
86 }).pipe(
87 Effect.retry({
88 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]),
89 }),
90 );
91
92 // Capabilities are credential-free (bucket + transformer are self-service),
93 // so the full lifecycle runs ungated.
94 test.provider(
95 "create, update, and destroy a B2BI capability",
96 (stack) =>
97 Effect.gen(function* () {
98 yield* stack.destroy();
99
100 // Create an inbound EDI capability.
101 const created = yield* stack.deploy(capabilityProgram("INBOUND"));
102 expect(created.capabilityId).toMatch(/^ca-/);
103 expect(created.name).toBe("alchemy-b2bi-capability");
104 expect(created.type).toBe("edi");
105
106 // Out-of-band verification.
107 const described = yield* b2bi.getCapability({
108 capabilityId: created.capabilityId,
109 });
110 expect(described.configuration.edi?.capabilityDirection).toBe("INBOUND");
111 expect(described.configuration.edi?.inputLocation.key).toBe("inbound/");
112
113 // No-op redeploy keeps the same id.
114 const noop = yield* stack.deploy(capabilityProgram("INBOUND"));
115 expect(noop.capabilityId).toBe(created.capabilityId);
116
117 // Destroy and verify (capability, transformer, bucket all removed).
118 yield* stack.destroy();
119 yield* assertCapabilityGone(created.capabilityId);
120 }),
121 {
122 tags: ["provider:aws", "provider:aws:b2bi", "provider:aws:s3", "live"],
123 timeout: 150_000,
124 },
125 );