@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 148 lines 4.81 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Flow } from "@alchemy.run/aws/AppFlow";
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 appflow from "@distilled.cloud/aws/appflow";
8 import * as s3 from "@distilled.cloud/aws/s3";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Deterministic bucket used as both flow source and destination (distinct
15 // prefixes). AppFlow requires the bucket policy to authorize the service
16 // principal for reads (source) and writes (destination).
17 const BUCKET = "alchemy-test-appflow-flow";
18 const BUCKET_ARN = `arn:aws:s3:::${BUCKET}`;
19
20 const appflowBucketPolicy: PolicyStatement[] = [
21 {
22 Effect: "Allow",
23 Principal: { Service: "appflow.amazonaws.com" },
24 Action: ["s3:GetObject", "s3:ListBucket"],
25 Resource: [BUCKET_ARN, `${BUCKET_ARN}/*`],
26 },
27 {
28 Effect: "Allow",
29 Principal: { Service: "appflow.amazonaws.com" },
30 Action: [
31 "s3:PutObject",
32 "s3:AbortMultipartUpload",
33 "s3:ListMultipartUploadParts",
34 "s3:ListBucketMultipartUploads",
35 "s3:GetBucketAcl",
36 "s3:PutObjectAcl",
37 ],
38 Resource: [BUCKET_ARN, `${BUCKET_ARN}/*`],
39 },
40 ];
41
42 const bucketProgram = Effect.gen(function* () {
43 return yield* Bucket("FlowBucket", {
44 bucketName: BUCKET,
45 forceDestroy: true,
46 policy: appflowBucketPolicy,
47 });
48 });
49
50 const flowProgram = (description: string) =>
51 Effect.gen(function* () {
52 const bucket = yield* bucketProgram;
53 return yield* Flow("CopyFlow", {
54 description,
55 triggerConfig: { triggerType: "OnDemand" },
56 sourceFlowConfig: {
57 connectorType: "S3",
58 sourceConnectorProperties: {
59 S3: { bucketName: bucket.bucketName, bucketPrefix: "input" },
60 },
61 },
62 destinationFlowConfigList: [
63 {
64 connectorType: "S3",
65 destinationConnectorProperties: {
66 S3: { bucketName: bucket.bucketName, bucketPrefix: "output" },
67 },
68 },
69 ],
70 tasks: [
71 {
72 taskType: "Map_all",
73 sourceFields: [],
74 connectorOperator: { S3: "NO_OP" },
75 taskProperties: {},
76 },
77 ],
78 });
79 });
80
81 const assertFlowGone = (flowName: string) =>
82 Effect.gen(function* () {
83 const result = yield* appflow.describeFlow({ flowName }).pipe(
84 Effect.map(() => "present" as const),
85 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)),
86 );
87 if (result === "present") {
88 return yield* Effect.fail(new Error(`Flow '${flowName}' still exists`));
89 }
90 }).pipe(
91 Effect.retry({
92 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]),
93 }),
94 );
95
96 // The S3-to-S3 path is credential-free, so the full lifecycle runs ungated.
97 test.provider(
98 "create, update, and destroy an S3-to-S3 flow",
99 (stack) =>
100 Effect.gen(function* () {
101 yield* stack.destroy();
102
103 // Phase 1: the bucket alone. AppFlow's S3 connector validates the flow
104 // by listing the source prefix at createFlow time — the prefix must
105 // contain at least one object or createFlow fails with
106 // ConnectorServerException.
107 yield* stack.deploy(bucketProgram);
108 yield* s3.putObject({
109 Bucket: BUCKET,
110 Key: "input/data.csv",
111 Body: new TextEncoder().encode("id,name\n1,alpha\n2,beta\n"),
112 ContentType: "text/csv",
113 });
114
115 // Phase 2: create the flow.
116 const created = yield* stack.deploy(flowProgram("initial description"));
117 expect(created.flowName).toBeDefined();
118 expect(created.flowArn).toContain(":appflow:");
119
120 // Out-of-band verification.
121 const described = yield* appflow.describeFlow({
122 flowName: created.flowName,
123 });
124 expect(described.description).toBe("initial description");
125 expect(described.sourceFlowConfig?.connectorType).toBe("S3");
126 expect(described.tags?.["alchemy::id"]).toBe("CopyFlow");
127
128 // No-op redeploy keeps the same ARN.
129 const noop = yield* stack.deploy(flowProgram("initial description"));
130 expect(noop.flowArn).toBe(created.flowArn);
131
132 // Update the description in place.
133 const updated = yield* stack.deploy(flowProgram("updated description"));
134 expect(updated.flowArn).toBe(created.flowArn);
135 const reDescribed = yield* appflow.describeFlow({
136 flowName: created.flowName,
137 });
138 expect(reDescribed.description).toBe("updated description");
139
140 // Destroy and verify.
141 yield* stack.destroy();
142 yield* assertFlowGone(created.flowName);
143 }),
144 {
145 tags: ["provider:aws", "provider:aws:appflow", "provider:aws:s3", "live"],
146 timeout: 150_000,
147 },
148 );