@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 189 lines 7.43 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as pipes from "@distilled.cloud/aws/pipes";
5 import * as Effect from "effect/Effect";
6 import * as Redacted from "effect/Redacted";
7
8 const { test } = Test.make({ providers: AWS.providers() });
9
10 const unwrap = (value: string | Redacted.Redacted<string> | undefined): string | undefined =>
11 value === undefined ? undefined : Redacted.isRedacted(value) ? Redacted.value(value) : value;
12
13 const firstFilterPattern = (described: pipes.DescribePipeResponse): string | undefined =>
14 unwrap(described.SourceParameters?.FilterCriteria?.Filters?.[0]?.Pattern);
15
16 // Read/receive statements the pipes.amazonaws.com role needs on the source
17 // queue, plus send on the target queue.
18 const pipeRole = (source: AWS.SQS.Queue, target: AWS.SQS.Queue) =>
19 AWS.IAM.Role("LifecyclePipeRole", {
20 assumeRolePolicyDocument: {
21 Version: "2012-10-17",
22 Statement: [
23 {
24 Effect: "Allow",
25 Principal: { Service: "pipes.amazonaws.com" },
26 Action: ["sts:AssumeRole"],
27 },
28 ],
29 },
30 inlinePolicies: {
31 PipeSource: {
32 Version: "2012-10-17",
33 Statement: [
34 {
35 Effect: "Allow",
36 Action: ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"],
37 Resource: [source.queueArn],
38 },
39 ],
40 },
41 PipeTarget: {
42 Version: "2012-10-17",
43 Statement: [{ Effect: "Allow", Action: ["sqs:SendMessage"], Resource: [target.queueArn] }],
44 },
45 },
46 });
47
48 describe(
49 "AWS.Pipes.Pipe",
50 { tags: ["provider:aws", "provider:aws:iam", "provider:aws:pipes", "provider:aws:sqs", "live"] },
51 () => {
52 test.provider(
53 "lifecycle: create SQS→SQS pipe with filter, update filter, destroy",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const filterA = JSON.stringify({ body: { type: ["order.created"] } });
59 const filterB = JSON.stringify({ body: { type: ["order.updated"] } });
60
61 const deployPipe = (pattern: string) =>
62 stack.deploy(
63 Effect.gen(function* () {
64 const source = yield* AWS.SQS.Queue("LifecycleSourceQueue");
65 const target = yield* AWS.SQS.Queue("LifecycleTargetQueue");
66 const role = yield* pipeRole(source, target);
67 return yield* AWS.Pipes.Pipe("LifecyclePipe", {
68 source: source.queueArn,
69 target: target.queueArn,
70 roleArn: role.roleArn,
71 sourceParameters: {
72 FilterCriteria: { Filters: [{ Pattern: pattern }] },
73 SqsQueueParameters: { BatchSize: 1 },
74 },
75 });
76 }),
77 );
78
79 // Create — the provider waits (bounded) for RUNNING before
80 // returning, so the deployed attributes already reflect it.
81 const created = yield* deployPipe(filterA);
82 expect(created.currentState).toEqual("RUNNING");
83
84 // Out-of-band verification via distilled.
85 const describedA = yield* pipes.describePipe({ Name: created.pipeName });
86 expect(describedA.CurrentState).toEqual("RUNNING");
87 expect(firstFilterPattern(describedA)).toEqual(filterA);
88 expect(describedA.SourceParameters?.SqsQueueParameters?.BatchSize).toEqual(1);
89
90 // Update the filter criteria in place (same source → no replace).
91 const updated = yield* deployPipe(filterB);
92 expect(updated.pipeArn).toEqual(created.pipeArn);
93 const describedB = yield* pipes.describePipe({ Name: updated.pipeName });
94 expect(firstFilterPattern(describedB)).toEqual(filterB);
95 expect(describedB.CurrentState).toEqual("RUNNING");
96
97 // Destroy — the provider's delete waits until describePipe reports
98 // NotFound, so a single typed check suffices here.
99 yield* stack.destroy();
100 const gone = yield* pipes.describePipe({ Name: created.pipeName }).pipe(
101 Effect.map(() => false),
102 Effect.catchTag("NotFoundException", () => Effect.succeed(true)),
103 );
104 expect(gone).toBe(true);
105 }),
106 { timeout: 240_000 },
107 );
108
109 test.provider(
110 "replacement: changing the source replaces the pipe",
111 (stack) =>
112 Effect.gen(function* () {
113 yield* stack.destroy();
114
115 // Keep BOTH candidate source queues deployed across every step —
116 // replacing the pipe while simultaneously removing its old source
117 // dependency deadlocks the engine.
118 const deployPipe = (useSecondSource: boolean) =>
119 stack.deploy(
120 Effect.gen(function* () {
121 const sourceA = yield* AWS.SQS.Queue("ReplaceSourceQueueA");
122 const sourceB = yield* AWS.SQS.Queue("ReplaceSourceQueueB");
123 const target = yield* AWS.SQS.Queue("ReplaceTargetQueue");
124 const role = yield* AWS.IAM.Role("ReplacePipeRole", {
125 assumeRolePolicyDocument: {
126 Version: "2012-10-17",
127 Statement: [
128 {
129 Effect: "Allow",
130 Principal: { Service: "pipes.amazonaws.com" },
131 Action: ["sts:AssumeRole"],
132 },
133 ],
134 },
135 inlinePolicies: {
136 PipeAccess: {
137 Version: "2012-10-17",
138 Statement: [
139 {
140 Effect: "Allow",
141 Action: [
142 "sqs:ReceiveMessage",
143 "sqs:DeleteMessage",
144 "sqs:GetQueueAttributes",
145 ],
146 Resource: [sourceA.queueArn, sourceB.queueArn],
147 },
148 {
149 Effect: "Allow",
150 Action: ["sqs:SendMessage"],
151 Resource: [target.queueArn],
152 },
153 ],
154 },
155 },
156 });
157 const active = useSecondSource ? sourceB : sourceA;
158 const pipe = yield* AWS.Pipes.Pipe("ReplacePipe", {
159 source: active.queueArn,
160 target: target.queueArn,
161 roleArn: role.roleArn,
162 });
163 return { pipe, sourceA, sourceB };
164 }),
165 );
166
167 const first = yield* deployPipe(false);
168 expect(first.pipe.currentState).toEqual("RUNNING");
169
170 const second = yield* deployPipe(true);
171 // Replacement mints a new instance: new physical name + ARN.
172 expect(second.pipe.pipeArn).not.toEqual(first.pipe.pipeArn);
173
174 const described = yield* pipes.describePipe({ Name: second.pipe.pipeName });
175 expect(described.Source).toEqual(second.sourceB.queueArn);
176
177 // The replaced pipe is deleted.
178 const oldGone = yield* pipes.describePipe({ Name: first.pipe.pipeName }).pipe(
179 Effect.map(() => false),
180 Effect.catchTag("NotFoundException", () => Effect.succeed(true)),
181 );
182 expect(oldGone).toBe(true);
183
184 yield* stack.destroy();
185 }),
186 { timeout: 240_000 },
187 );
188 },
189 );