The best way to control Claude Code, Codex and more from any computer or phone

main
ts 309 lines 11.1 KB
Raw
1 /**
2 * Mode-scoped local dev for the AWS serverless data plane.
3 *
4 * `Test.make({ dev: true })` alone (no profile stub, no endpoint config)
5 * must route the dualized data-plane resources to the floci emulator: each
6 * `flociDual` registration in `AWS/Providers.ts` builds the SAME live
7 * provider code with every lifecycle method wrapped in the floci override
8 * context (endpoint `http://localhost:4566`, dummy credentials, account
9 * `000000000000`, region `us-east-1`).
10 *
11 * Proof structure:
12 * - attrs carry the dummy account (`:000000000000:` ARNs) and localhost
13 * queue URL — the live cloud can never produce these;
14 * - persisted state rows are stamped `providerMode: "local"` (the
15 * stamping/replacement law of ProviderLayer.dual then guarantees a live
16 * deploy plans replacements — pinned by test/provider-mode.test.ts);
17 * - out-of-band raw HTTP against the emulator gateway finds the
18 * resources (and finds them gone after destroy);
19 * - `Alchemy.remote()` opts a resource back to the live provider even in
20 * dev: real-account identity, out-of-band verification through the real
21 * cloud API, and a live delete on destroy.
22 *
23 * Requires Docker (floci runs as a container); skipped when unavailable.
24 */
25 import * as AWS from "@alchemy.run/aws";
26 import { expect } from "@alchemy.run/test";
27 import * as Test from "@alchemy.run/test/Alchemy";
28 import * as SSM from "@distilled.cloud/aws/ssm";
29 import * as Alchemy from "alchemy";
30 import { Stack } from "alchemy/Stack";
31 import { State, type ResourceState } from "alchemy/State";
32 import * as Effect from "effect/Effect";
33 import * as Redacted from "effect/Redacted";
34 import { liveContext } from "./fixtures/live.ts";
35 import { rawAwsJson, rawAwsQuery, rawS3GetBucket, regionOfArn } from "./fixtures/raw.ts";
36
37 const { test } = Test.make({ providers: AWS.providers(), dev: true });
38
39 const getState = Effect.fn(function* (fqn: string) {
40 const state = yield* yield* State;
41 const stk = yield* Stack;
42 return (yield* state.get({
43 stack: stk.name,
44 stage: stk.stage,
45 fqn,
46 })) as ResourceState | undefined;
47 });
48
49 const assumeRolePolicy = {
50 Version: "2012-10-17" as const,
51 Statement: [
52 {
53 Effect: "Allow" as const,
54 Principal: { Service: "lambda.amazonaws.com" },
55 Action: ["sts:AssumeRole"],
56 },
57 ],
58 };
59
60 test.provider(
61 "dev mode routes the serverless data plane to floci and stamps local mode",
62 (stack) =>
63 Effect.gen(function* () {
64 yield* stack.destroy();
65
66 const outputs = yield* stack.deploy(
67 Effect.gen(function* () {
68 const bucket = yield* AWS.S3.Bucket("DataBucket");
69 const queue = yield* AWS.SQS.Queue("DataQueue");
70 const table = yield* AWS.DynamoDB.Table("DataTable", {
71 partitionKey: "id",
72 attributes: { id: "S" },
73 });
74 // Requires the alchemy floci fork ≥ 1.6.0-alchemy.2: the Topic
75 // reconciler observes SNS GetDataProtectionPolicy unconditionally
76 // on standard topics, which stock floci 1.6.0 rejects.
77 const topic = yield* AWS.SNS.Topic("DataTopic");
78 const secret = yield* AWS.SecretsManager.Secret("DataSecret", {
79 secretString: Redacted.make("local-secret-value"),
80 });
81 const parameter = yield* AWS.SSM.Parameter("DataParameter", {
82 value: "local-parameter-value",
83 });
84 const role = yield* AWS.IAM.Role("DataRole", {
85 assumeRolePolicyDocument: assumeRolePolicy,
86 });
87 const bus = yield* AWS.EventBridge.EventBus("DataBus");
88 const rule = yield* AWS.EventBridge.Rule("DataRule", {
89 eventBusName: bus.eventBusName,
90 eventPattern: { source: ["alchemy.local-test"] },
91 });
92 return {
93 bucket,
94 queue,
95 table,
96 topic,
97 secret,
98 parameter,
99 role,
100 bus,
101 rule,
102 };
103 }),
104 );
105
106 // --- Dummy-account identities: the live cloud can never mint these.
107 expect(outputs.queue.queueArn).toContain(":000000000000:");
108 expect(outputs.queue.queueUrl).toContain("localhost");
109 expect(outputs.table.tableArn).toContain(":000000000000:");
110 expect(outputs.topic.topicArn).toContain(":000000000000:");
111 expect(outputs.secret.secretArn).toContain(":000000000000:");
112 expect(outputs.parameter.parameterArn).toContain(":000000000000:");
113 expect(outputs.role.roleArn).toContain("::000000000000:");
114 expect(outputs.bus.eventBusArn).toContain(":000000000000:");
115 expect(outputs.rule.ruleArn).toContain(":000000000000:");
116 expect(outputs.bucket.bucketArn).toContain(outputs.bucket.bucketName);
117
118 // --- Stamped-mode proof: every dualized row commits providerMode
119 // "local" (the replacement-on-mode-switch law is pinned by
120 // provider-mode.test.ts on exactly this stamp).
121 for (const fqn of [
122 "DataBucket",
123 "DataQueue",
124 "DataTable",
125 "DataTopic",
126 "DataSecret",
127 "DataParameter",
128 "DataRole",
129 "DataBus",
130 "DataRule",
131 ]) {
132 const row = yield* getState(fqn);
133 expect(`${fqn}:${row?.status}`).toBe(`${fqn}:created`);
134 expect(`${fqn}:${row?.providerMode}`).toBe(`${fqn}:local`);
135 }
136
137 const region = regionOfArn(outputs.queue.queueArn);
138
139 // --- Out-of-band: raw HTTP against the emulator gateway (no distilled
140 // SDK, no alchemy layers). Real-cloud resources would not be here.
141 const listQueues = yield* rawAwsJson({
142 service: "sqs",
143 region,
144 target: "AmazonSQS.ListQueues",
145 contentType: "application/x-amz-json-1.0",
146 body: {},
147 });
148 expect(listQueues.status).toBe(200);
149 const queues = (yield* listQueues.json) as { QueueUrls?: string[] };
150 expect(queues.QueueUrls?.some((url) => url.endsWith(`/${outputs.queue.queueName}`))).toBe(
151 true,
152 );
153
154 const describeTable = yield* rawAwsJson({
155 service: "dynamodb",
156 region,
157 target: "DynamoDB_20120810.DescribeTable",
158 contentType: "application/x-amz-json-1.0",
159 body: { TableName: outputs.table.tableName },
160 });
161 expect(describeTable.status).toBe(200);
162
163 const getBucket = yield* rawS3GetBucket(outputs.bucket.bucketName);
164 expect(getBucket.status).toBe(200);
165
166 const getTopicAttrs = yield* rawAwsQuery({
167 service: "sns",
168 region,
169 params: {
170 Action: "GetTopicAttributes",
171 Version: "2010-03-31",
172 TopicArn: outputs.topic.topicArn,
173 },
174 });
175 expect(getTopicAttrs.status).toBe(200);
176
177 const getParameter = yield* rawAwsJson({
178 service: "ssm",
179 region,
180 target: "AmazonSSM.GetParameter",
181 contentType: "application/x-amz-json-1.1",
182 body: { Name: outputs.parameter.parameterName },
183 });
184 expect(getParameter.status).toBe(200);
185 const param = (yield* getParameter.json) as {
186 Parameter?: { Value?: string };
187 };
188 expect(param.Parameter?.Value).toBe("local-parameter-value");
189
190 const describeSecret = yield* rawAwsJson({
191 service: "secretsmanager",
192 region,
193 target: "secretsmanager.DescribeSecret",
194 contentType: "application/x-amz-json-1.1",
195 body: { SecretId: outputs.secret.secretArn },
196 });
197 expect(describeSecret.status).toBe(200);
198
199 // --- Destroy and verify gone from the emulator.
200 yield* stack.destroy();
201
202 const queuesAfter = (yield* (yield* rawAwsJson({
203 service: "sqs",
204 region,
205 target: "AmazonSQS.ListQueues",
206 contentType: "application/x-amz-json-1.0",
207 body: {},
208 })).json) as { QueueUrls?: string[] };
209 expect(
210 queuesAfter.QueueUrls?.some((url) => url.endsWith(`/${outputs.queue.queueName}`)) ?? false,
211 ).toBe(false);
212
213 const tableAfter = yield* rawAwsJson({
214 service: "dynamodb",
215 region,
216 target: "DynamoDB_20120810.DescribeTable",
217 contentType: "application/x-amz-json-1.0",
218 body: { TableName: outputs.table.tableName },
219 });
220 expect(tableAfter.status).toBe(400); // ResourceNotFoundException
221
222 const bucketAfter = yield* rawS3GetBucket(outputs.bucket.bucketName);
223 expect(bucketAfter.status).toBe(404); // NoSuchBucket
224
225 const topicAfter = yield* rawAwsQuery({
226 service: "sns",
227 region,
228 params: {
229 Action: "GetTopicAttributes",
230 Version: "2010-03-31",
231 TopicArn: outputs.topic.topicArn,
232 },
233 });
234 expect(topicAfter.status).toBe(404); // NotFoundException
235 }),
236 {
237 tags: [
238 "provider:aws",
239 "provider:aws:dynamodb",
240 "provider:aws:eventbridge",
241 "provider:aws:iam",
242 "provider:aws:s3",
243 "provider:aws:secretsmanager",
244 "provider:aws:sns",
245 "provider:aws:sqs",
246 "provider:aws:ssm",
247 "local",
248 ],
249 timeout: 300_000,
250 },
251 );
252
253 /**
254 * `Alchemy.remote()` opts a resource OUT of local emulation: even in a dev
255 * run the parameter is created on real AWS by the live provider variant
256 * (testing-profile credentials), its state row is stamped `live`, and the
257 * destroy deletes it from the real cloud.
258 */
259 test.provider(
260 "Alchemy.remote() parameter runs live in dev alongside a local one",
261 (stack) =>
262 Effect.gen(function* () {
263 yield* stack.destroy();
264
265 const outputs = yield* stack.deploy(
266 Effect.gen(function* () {
267 const localParam = yield* AWS.SSM.Parameter("MixedLocalParam", {
268 value: "local-value",
269 });
270 const liveParam = yield* AWS.SSM.Parameter("MixedLiveParam", {
271 value: "live-value",
272 }).pipe(Alchemy.remote());
273 return { localParam, liveParam };
274 }),
275 );
276
277 // The default parameter is emulated; the remote() one is real.
278 expect(outputs.localParam.parameterArn).toContain(":000000000000:");
279 expect(outputs.liveParam.parameterArn).not.toContain(":000000000000:");
280 expect((yield* getState("MixedLocalParam"))?.providerMode).toBe("local");
281 expect((yield* getState("MixedLiveParam"))?.providerMode).toBe("live");
282
283 // Out-of-band: read the REAL cloud explicitly (the ambient
284 // environment in a dev run is the emulator) — the remote() parameter
285 // must be there with its value.
286 const live = yield* SSM.getParameter({
287 Name: outputs.liveParam.parameterName,
288 }).pipe(Effect.provide(liveContext));
289 const liveValue = live.Parameter?.Value;
290 expect(typeof liveValue === "string" ? liveValue : Redacted.value(liveValue!)).toBe(
291 "live-value",
292 );
293
294 yield* stack.destroy();
295
296 // The live parameter was deleted from the real cloud on destroy (its
297 // state row is stamped live, so the live provider handles the delete
298 // even in a dev run).
299 const gone = yield* SSM.getParameter({
300 Name: outputs.liveParam.parameterName,
301 }).pipe(
302 Effect.as(false),
303 Effect.catchTag("ParameterNotFound", () => Effect.succeed(true)),
304 Effect.provide(liveContext),
305 );
306 expect(gone).toBe(true);
307 }),
308 { tags: ["provider:aws", "provider:aws:ssm", "live"], timeout: 300_000 },
309 );