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

main
ts 11,345 lines 451 KB
Raw
1 import { assert, describe, expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import { Data, Layer } from "effect";
4 import * as Cause from "effect/Cause";
5 import * as Context from "effect/Context";
6 import * as Deferred from "effect/Deferred";
7 import * as Duration from "effect/Duration";
8 import * as Effect from "effect/Effect";
9 import * as Exit from "effect/Exit";
10 import * as Fiber from "effect/Fiber";
11 import * as Option from "effect/Option";
12 import * as Redacted from "effect/Redacted";
13 import { Action } from "@/Action";
14 import { adopt, AdoptPolicy, OwnedBySomeoneElse, Unowned, Unresolved } from "@/AdoptPolicy";
15 import { apply, DestroyError } from "@/Apply";
16 import { isResolved } from "@/Diff";
17 import * as ProviderLayer from "@/Local/ProviderLayer";
18 import * as Namespace from "@/Namespace.ts";
19 import * as Output from "@/Output";
20 import * as Plan from "@/Plan";
21 import * as Provider from "@/Provider";
22 import { remote } from "@/ProviderMode.ts";
23 import * as RemovalPolicy from "@/RemovalPolicy.ts";
24 import { renamedFrom } from "@/Rename.ts";
25 import { Cli } from "@/Report.ts";
26 import { Resource } from "@/Resource";
27 import { Stack, make as makeStack } from "@/Stack";
28 import { Stage } from "@/Stage";
29 import {
30 type ActionState,
31 type CreatingResourceState,
32 type ReplacedResourceState,
33 type ReplacingResourceState,
34 type ResourceState,
35 State,
36 StateStoreError,
37 } from "@/State";
38 import {
39 AliasedWidget,
40 aliasedWidgetDeletes,
41 aliasedWidgetProvider,
42 ArtifactProbe,
43 BindingTarget,
44 capturedConfigHost,
45 CollisionRegistry,
46 DeleteFirstResource,
47 DeletedBindingRegressionTarget,
48 DurationResource,
49 FqnProbe,
50 Function,
51 inDev,
52 KindStablesResource,
53 ModalResource,
54 modalCalls,
55 type ModalResourceProps,
56 PhasedTarget,
57 ProbeBinding,
58 StaticStablesResource,
59 TestLayers,
60 TestResource,
61 TestResourceHooks,
62 type TestResourceProps,
63 } from "./test.resources.ts";
64
65 const { test } = Test.make({ providers: TestLayers() });
66
67 const getState = Effect.fn(function* <S = ResourceState>(resourceId: string) {
68 const state = yield* yield* State;
69 const stk = yield* Stack;
70 return (yield* state.get({ stack: stk.name, stage: stk.stage, fqn: resourceId })) as S;
71 });
72 /** The planned action for a logical id, or `undefined` if it isn't planned. */
73 const actionOfPlan = (plan: any, logicalId: string) =>
74 (Object.values(plan.resources) as any[]).find(
75 (node: any) => node.resource.LogicalId === logicalId,
76 )?.action;
77
78 const listState = Effect.fn(function* () {
79 const state = yield* yield* State;
80 const stk = yield* Stack;
81 return yield* state.list({ stack: stk.name, stage: stk.stage });
82 });
83
84 const recordingCli = (events: Array<{ id: string; status: string }>) =>
85 Cli.of({
86 startPlanningSession: () =>
87 Effect.succeed({
88 update: () => Effect.void,
89 succeed: () => Effect.void,
90 fail: () => Effect.void,
91 close: Effect.void,
92 }),
93 approvePlan: () => Effect.succeed(true),
94 displayPlan: () => Effect.void,
95 startApplySession: () =>
96 Effect.succeed({
97 done: () => Effect.void,
98 emit: (event) =>
99 Effect.sync(() => {
100 if (event.type === "apply.resource.status") {
101 events.push({ id: event.id, status: event.status });
102 }
103 }),
104 }),
105 });
106
107 const expectConvergedStatus = (status: ResourceState["status"] | undefined) => {
108 expect(["created", "updated"]).toContain(status);
109 };
110
111 // Graceful failure handling means downstream resources of a failed upstream
112 // may have committed an intermediate "creating"/"replacing" status before
113 // their `waitForDeps` discovered the upstream failure - or may have fully
114 // converged using a stable previous output of the failed upstream (e.g. a
115 // replacement whose old generation is still live). This helper tolerates any
116 // of those outcomes; the corresponding recovery deploy validates terminal
117 // state.
118 const expectNotStarted = (state: ResourceState | undefined) => {
119 expect([undefined, "creating", "replacing", "created", "updated"]).toContain(state?.status);
120 };
121
122 export class ResourceFailure extends Data.TaggedError("ResourceFailure")<{ message: string }> {
123 constructor() {
124 super({ message: `Failed to create` });
125 }
126 }
127
128 const hook =
129 (hooks?: {
130 create?: (id: string, props: TestResourceProps) => Effect.Effect<void, any>;
131 update?: (id: string, props: TestResourceProps) => Effect.Effect<void, any>;
132 delete?: (id: string) => Effect.Effect<void, any>;
133 read?: (id: string) => Effect.Effect<any, any>;
134 }) =>
135 <A, Err, Req>(test: Effect.Effect<A, Err, Req>) =>
136 test.pipe(
137 Effect.provide(
138 Layer.succeed(
139 TestResourceHooks,
140 hooks ?? {
141 create: () => Effect.fail(new ResourceFailure()),
142 update: () => Effect.fail(new ResourceFailure()),
143 delete: () => Effect.fail(new ResourceFailure()),
144 read: () => Effect.succeed(undefined),
145 },
146 ),
147 ),
148 // Phase-1 (create/update) failures surface as the raw ResourceFailure;
149 // Phase-2 (GC/destroy) delete failures are aggregated into DestroyError.
150 // @ts-expect-error - catchTag changes the return type
151 Effect.catchTag(["ResourceFailure", "DestroyError"], () => Effect.succeed(true)),
152 ) as Effect.Effect<A, Err, Req | State>;
153
154 // Helper to fail on specific resource IDs
155 const failOn = (
156 resourceId: string,
157 hook: "create" | "update" | "delete",
158 ): {
159 create?: (id: string, props: TestResourceProps) => Effect.Effect<void, any>;
160 update?: (id: string, props: TestResourceProps) => Effect.Effect<void, any>;
161 delete?: (id: string) => Effect.Effect<void, any>;
162 } => ({
163 [hook]: (id: string) =>
164 id === resourceId ? Effect.fail(new ResourceFailure()) : Effect.succeed(undefined),
165 });
166
167 // Helper to fail on multiple resource IDs for different hooks
168 const failOnMultiple = (
169 failures: Array<{ id: string; hook: "create" | "update" | "delete" }>,
170 ): {
171 create?: (id: string, props: TestResourceProps) => Effect.Effect<void, any>;
172 update?: (id: string, props: TestResourceProps) => Effect.Effect<void, any>;
173 delete?: (id: string) => Effect.Effect<void, any>;
174 } => {
175 const createFailures = failures.filter((f) => f.hook === "create").map((f) => f.id);
176 const updateFailures = failures.filter((f) => f.hook === "update").map((f) => f.id);
177 const deleteFailures = failures.filter((f) => f.hook === "delete").map((f) => f.id);
178
179 return {
180 create: (id: string) =>
181 createFailures.includes(id) ? Effect.fail(new ResourceFailure()) : Effect.succeed(undefined),
182 update: (id: string) =>
183 updateFailures.includes(id) ? Effect.fail(new ResourceFailure()) : Effect.succeed(undefined),
184 delete: (id: string) =>
185 deleteFailures.includes(id) ? Effect.fail(new ResourceFailure()) : Effect.succeed(undefined),
186 };
187 };
188
189 describe("Action output convergence", { tags: ["unit", "local"] }, () => {
190 for (const consumer of ["prop", "binding"] as const) {
191 test.provider(
192 `unchanged Action ${consumer} consumers stop reconciling after the first deploy`,
193 (stack) =>
194 Effect.gen(function* () {
195 let runs = 0;
196 const reconciled: string[] = [];
197 const Compute = Action("Compute", (_: {}) =>
198 Effect.sync(() => {
199 runs++;
200 return { value: "v1" };
201 }),
202 );
203 const program = Effect.gen(function* () {
204 const result = yield* Compute({});
205 if (consumer === "prop") {
206 const host = yield* TestResource("Host", { string: result.value });
207 return host.string;
208 }
209 const host = yield* BindingTarget("Host", {});
210 yield* host.bind("Result", { env: { RESULT: result.value } });
211 return host.env.RESULT;
212 });
213 yield* Effect.gen(function* () {
214 for (const _ of [1, 2, 3]) {
215 expect(yield* stack.deploy(program)).toBe("v1");
216 }
217 expect(runs).toBe(1);
218 expect(reconciled).toEqual(["create"]);
219 const plan = yield* stack.plan(program);
220 expect(actionOfPlan(plan, "Host")).toBe("noop");
221 }).pipe(
222 Effect.provideService(TestResourceHooks, {
223 create: () =>
224 Effect.sync(() => {
225 reconciled.push("create");
226 }),
227 update: () =>
228 Effect.sync(() => {
229 reconciled.push("update");
230 }),
231 }),
232 );
233 }),
234 );
235 }
236
237 for (const sameOutput of [false, true]) {
238 test.provider(
239 `changed Action input ${sameOutput ? "with the same output" : "with a fresh output"} converges after the rerun`,
240 (stack) =>
241 Effect.gen(function* () {
242 let runs = 0;
243 const reconciled: (string | undefined)[] = [];
244 const Compute = Action("Compute", (input: { revision: string }) =>
245 Effect.sync(() => {
246 runs++;
247 return { value: sameOutput ? "constant" : input.revision };
248 }),
249 );
250 const program = (revision: string) =>
251 Effect.gen(function* () {
252 const result = yield* Compute({ revision });
253 const host = yield* TestResource("Host", { string: result.value });
254 return host.string;
255 });
256 const record = (_: string, props: TestResourceProps) =>
257 Effect.sync(() => {
258 reconciled.push(props.string);
259 });
260 yield* Effect.gen(function* () {
261 expect(yield* stack.deploy(program("v1"))).toBe(sameOutput ? "constant" : "v1");
262 expect(yield* stack.deploy(program("v2"))).toBe(sameOutput ? "constant" : "v2");
263 expect(runs).toBe(2);
264 if (sameOutput) {
265 expect([1, 2]).toContain(reconciled.length);
266 expect(reconciled.every((value) => value === "constant")).toBe(true);
267 } else {
268 expect(reconciled).toEqual(["v1", "v2"]);
269 }
270 const settledCount = reconciled.length;
271 expect(actionOfPlan(yield* stack.plan(program("v2")), "Host")).toBe("noop");
272 yield* stack.deploy(program("v2"));
273 expect(runs).toBe(2);
274 expect(reconciled).toHaveLength(settledCount);
275 }).pipe(Effect.provideService(TestResourceHooks, { create: record, update: record }));
276 }),
277 );
278 }
279
280 test.provider(
281 "changed Action binding data reaches the host before subsequent deploys converge",
282 (stack) =>
283 Effect.gen(function* () {
284 let runs = 0;
285 let reconciles = 0;
286 const Compute = Action("Compute", (input: { value: string }) =>
287 Effect.sync(() => {
288 runs++;
289 return input;
290 }),
291 );
292 const program = (value: string) =>
293 Effect.gen(function* () {
294 const result = yield* Compute({ value });
295 const host = yield* BindingTarget("Host", {});
296 yield* host.bind("Result", { env: { RESULT: result.value } });
297 return host.env.RESULT;
298 });
299 const record = () =>
300 Effect.sync(() => {
301 reconciles++;
302 });
303 yield* Effect.gen(function* () {
304 expect(yield* stack.deploy(program("v1"))).toBe("v1");
305 expect(yield* stack.deploy(program("v2"))).toBe("v2");
306 expect(runs).toBe(2);
307 expect(reconciles).toBe(2);
308 expect(actionOfPlan(yield* stack.plan(program("v2")), "Host")).toBe("noop");
309 expect(yield* stack.deploy(program("v2"))).toBe("v2");
310 expect(runs).toBe(2);
311 expect(reconciles).toBe(2);
312 }).pipe(Effect.provideService(TestResourceHooks, { create: record, update: record }));
313 }),
314 );
315
316 test.provider(
317 "Action chains propagate fresh outputs and stop rerunning once unchanged",
318 (stack) =>
319 Effect.gen(function* () {
320 const runs: string[] = [];
321 const Compute = Action("Compute", (input: { value: string }) =>
322 Effect.sync(() => {
323 runs.push(input.value);
324 return { value: `${input.value}!` };
325 }),
326 );
327 const program = (value: string) =>
328 Effect.gen(function* () {
329 const first = yield* Compute("First", { value });
330 const second = yield* Compute("Second", { value: first.value });
331 const host = yield* Function("Host", { env: { RESULT: second.value } });
332 return host.env.RESULT;
333 });
334 expect(yield* stack.deploy(program("v1"))).toBe("v1!!");
335 expect(yield* stack.deploy(program("v2"))).toBe("v2!!");
336 expect(yield* stack.deploy(program("v2"))).toBe("v2!!");
337 expect(runs).toEqual(["v1", "v1!", "v2", "v2!"]);
338 expect(actionOfPlan(yield* stack.plan(program("v2")), "Host")).toBe("noop");
339 }),
340 );
341
342 test.provider(
343 "binding-only changes to an upstream resource invalidate Action inputs and consumer outputs",
344 (stack) =>
345 Effect.gen(function* () {
346 const seen: string[] = [];
347 const Compute = Action("Compute", (input: { value: string }) =>
348 Effect.sync(() => {
349 seen.push(input.value);
350 return input;
351 }),
352 );
353 const program = (value: string) =>
354 Effect.gen(function* () {
355 const source = yield* BindingTarget("Source", {});
356 yield* source.bind("Value", { env: { RESULT: value } });
357 const result = yield* Compute({ value: source.env.RESULT });
358 const host = yield* Function("Host", { env: { RESULT: result.value } });
359 return host.env.RESULT;
360 });
361 expect(yield* stack.deploy(program("v1"))).toBe("v1");
362 const changed = yield* stack.plan(program("v2"));
363 expect(changed.resources.Source.action).toBe("update");
364 expect(changed.actions.Compute.action).toBe("run");
365 expect(changed.resources.Host.action).toBe("update");
366 expect(yield* stack.deploy(program("v2"))).toBe("v2");
367 expect(yield* stack.deploy(program("v2"))).toBe("v2");
368 expect(seen).toEqual(["v1", "v2"]);
369 expect(actionOfPlan(yield* stack.plan(program("v2")), "Host")).toBe("noop");
370 }),
371 );
372
373 test.provider(
374 "an Action planned to run skips its body when upstream resolves to the same input",
375 (stack) =>
376 Effect.gen(function* () {
377 let firstRuns = 0;
378 let secondRuns = 0;
379 const First = Action("First", (_: { revision: string }) =>
380 Effect.sync(() => {
381 firstRuns++;
382 return { value: "constant" };
383 }),
384 );
385 const Second = Action("Second", (input: { value: string }) =>
386 Effect.sync(() => {
387 secondRuns++;
388 return { value: `${input.value}!` };
389 }),
390 );
391 const program = (revision: string) =>
392 Effect.gen(function* () {
393 const first = yield* First({ revision });
394 const second = yield* Second({ value: first.value });
395 const host = yield* Function("Host", { env: { RESULT: second.value } });
396 return host.env.RESULT;
397 });
398 expect(yield* stack.deploy(program("v1"))).toBe("constant!");
399 const changed = yield* stack.plan(program("v2"));
400 expect(changed.actions.First.action).toBe("run");
401 expect(changed.actions.Second.action).toBe("run");
402 expect(yield* stack.deploy(program("v2"))).toBe("constant!");
403 expect(firstRuns).toBe(2);
404 expect(secondRuns).toBe(1);
405 const forced = yield* program("v2").pipe(
406 makeStack({ name: stack.name, providers: TestLayers(), state: stack.state }),
407 Effect.flatMap((spec) =>
408 Plan.make(spec, { force: true }).pipe(
409 Effect.flatMap(apply),
410 Effect.provide(spec.services),
411 ),
412 ),
413 Effect.provideService(Stage, stack.stage),
414 );
415 expect(forced).toBe("constant!");
416 expect(firstRuns).toBe(3);
417 expect(secondRuns).toBe(2);
418 expect(actionOfPlan(yield* stack.plan(program("v2")), "Host")).toBe("noop");
419 }),
420 );
421
422 test.provider(
423 "changed Action output replaces the whole environment without retaining removed keys",
424 (stack) =>
425 Effect.gen(function* () {
426 const Compute = Action("Compute", (input: { revision: number }) =>
427 Effect.succeed<Record<string, string>>(
428 input.revision === 1 ? { KEEP: "old", REMOVE: "old" } : { KEEP: "new" },
429 ),
430 );
431 const program = (revision: number) =>
432 Effect.gen(function* () {
433 const env = yield* Compute({ revision });
434 const host = yield* Function("Host", { env });
435 return host.env;
436 });
437 expect(yield* stack.deploy(program(1))).toEqual({ KEEP: "old", REMOVE: "old" });
438 expect(yield* stack.deploy(program(2))).toEqual({ KEEP: "new" });
439 expect(actionOfPlan(yield* stack.plan(program(2)), "Host")).toBe("noop");
440 }),
441 );
442
443 test.provider(
444 "a failed Action rerun blocks its value consumer and recovery uses the new output",
445 (stack) =>
446 Effect.gen(function* () {
447 let fail = false;
448 const reconciled: (string | undefined)[] = [];
449 const Compute = Action("Compute", (input: { revision: string }) =>
450 Effect.gen(function* () {
451 if (fail) return yield* Effect.fail(new ResourceFailure());
452 return { value: input.revision };
453 }),
454 );
455 const program = (revision: string) =>
456 Effect.gen(function* () {
457 const result = yield* Compute({ revision });
458 const host = yield* TestResource("Host", { string: result.value });
459 return host.string;
460 });
461 const record = (_: string, props: TestResourceProps) =>
462 Effect.sync(() => {
463 reconciled.push(props.string);
464 });
465 yield* Effect.gen(function* () {
466 expect(yield* stack.deploy(program("v1"))).toBe("v1");
467 fail = true;
468 const failed = yield* Effect.exit(stack.deploy(program("v2")));
469 assert(Exit.isFailure(failed));
470 expect(failed.cause.reasons.find(Cause.isFailReason)?.error).toBeInstanceOf(
471 ResourceFailure,
472 );
473 expect(reconciled).toEqual(["v1"]);
474 fail = false;
475 expect(yield* stack.deploy(program("v2"))).toBe("v2");
476 expect(reconciled).toEqual(["v1", "v2"]);
477 }).pipe(Effect.provideService(TestResourceHooks, { create: record, update: record }));
478 }),
479 );
480
481 for (const value of [undefined, null, false, 0, ""] as const) {
482 test.provider(
483 `persisted ${String(value)} Action output remains reusable after apply`,
484 (stack) =>
485 Effect.gen(function* () {
486 let runs = 0;
487 const Compute = Action("Compute", (_: {}) =>
488 Effect.sync(() => {
489 runs++;
490 return value;
491 }),
492 );
493 const program = Effect.gen(function* () {
494 const result = yield* Compute({});
495 const host = yield* Function("Host", { env: { RESULT: Output.map(result, String) } });
496 return host.env.RESULT;
497 });
498 expect(yield* stack.deploy(program)).toBe(String(value));
499 expect(yield* stack.deploy(program)).toBe(String(value));
500 expect(runs).toBe(1);
501 expect(actionOfPlan(yield* stack.plan(program), "Host")).toBe("noop");
502 }),
503 );
504 }
505
506 test.provider(
507 "a forced Action rerun publishes fresh output instead of its persisted result",
508 (stack) =>
509 Effect.gen(function* () {
510 let runs = 0;
511 const Compute = Action("Compute", (_: {}) =>
512 Effect.sync(() => ({ value: String(++runs) })),
513 );
514 const program = Effect.gen(function* () {
515 const result = yield* Compute({});
516 const host = yield* Function("Host", { env: { RESULT: result.value } });
517 return host.env.RESULT;
518 });
519 expect(yield* stack.deploy(program)).toBe("1");
520 const forced = yield* program.pipe(
521 makeStack({ name: stack.name, providers: TestLayers(), state: stack.state }),
522 Effect.flatMap((spec) =>
523 Plan.make(spec, { force: true }).pipe(
524 Effect.flatMap(apply),
525 Effect.provide(spec.services),
526 ),
527 ),
528 Effect.provideService(Stage, stack.stage),
529 );
530 expect(forced).toBe("2");
531 expect(yield* stack.deploy(program)).toBe("2");
532 expect(runs).toBe(2);
533 }),
534 );
535
536 test.provider(
537 "an Action migration marker in the environment converges without removing its dependency",
538 (stack) =>
539 Effect.gen(function* () {
540 let runs = 0;
541 const Compute = Action("Compute", (_: {}) =>
542 Effect.sync(() => {
543 runs++;
544 return { migrated: true };
545 }),
546 );
547 const program = Effect.gen(function* () {
548 const result = yield* Compute({});
549 const host = yield* Function("Host", {
550 name: "host",
551 env: { MIGRATION: Output.map(result, JSON.stringify) },
552 });
553 return host.name;
554 });
555 expect(yield* stack.deploy(program)).toBe("host");
556 expect(yield* stack.deploy(program)).toBe("host");
557 expect(runs).toBe(1);
558 const plan = yield* stack.plan(program);
559 expect(plan.actions.Compute.action).toBe("noop");
560 expect(actionOfPlan(plan, "Host")).toBe("noop");
561 }),
562 );
563
564 for (const operation of ["create", "update", "replace"] as const) {
565 test.provider(`Action completion precedes bound host ${operation}`, (stack) =>
566 Effect.gen(function* () {
567 const events: string[] = [];
568 const Compute = Action("Compute", (_: { revision: string }) =>
569 Effect.gen(function* () {
570 yield* Effect.yieldNow;
571 events.push("action completed");
572 return { migrated: true };
573 }),
574 );
575 const program = (revision: string) =>
576 Effect.gen(function* () {
577 const result = yield* Compute({ revision });
578 const host = yield* BindingTarget("Host", {
579 string: revision,
580 replaceString: operation === "replace" ? revision : "fixed",
581 });
582 yield* host.bind("Migration", {
583 env: { MIGRATION: Output.map(result, JSON.stringify) },
584 });
585 return host.string;
586 });
587 const record = () =>
588 Effect.sync(() => {
589 events.push("host reconciled");
590 });
591 yield* Effect.gen(function* () {
592 if (operation !== "create") yield* stack.deploy(program("v1"));
593 events.length = 0;
594 expect(actionOfPlan(yield* stack.plan(program("v2")), "Host")).toBe(operation);
595 expect(yield* stack.deploy(program("v2"))).toBe("v2");
596 expect(events).toEqual(["action completed", "host reconciled"]);
597 }).pipe(Effect.provideService(TestResourceHooks, { create: record, update: record }));
598 }),
599 );
600
601 test.provider(`Action failure prevents bound host ${operation}`, (stack) =>
602 Effect.gen(function* () {
603 let fail = false;
604 const reconciled: string[] = [];
605 const Compute = Action("Compute", (_: { revision: string }) =>
606 fail ? Effect.fail(new ResourceFailure()) : Effect.succeed("done"),
607 );
608 const program = (revision: string) =>
609 Effect.gen(function* () {
610 const result = yield* Compute({ revision });
611 const host = yield* BindingTarget("Host", {
612 string: revision,
613 replaceString: operation === "replace" ? revision : "fixed",
614 });
615 yield* host.bind("Migration", {
616 env: { MIGRATION: Output.map(result, JSON.stringify) },
617 });
618 return host.string;
619 });
620 const record = (id: string) =>
621 Effect.sync(() => {
622 reconciled.push(id);
623 });
624 yield* Effect.gen(function* () {
625 if (operation !== "create") yield* stack.deploy(program("v1"));
626 reconciled.length = 0;
627 fail = true;
628 expect(actionOfPlan(yield* stack.plan(program("v2")), "Host")).toBe(operation);
629 const failed = yield* Effect.exit(stack.deploy(program("v2")));
630 assert(Exit.isFailure(failed));
631 expect(failed.cause.reasons.find(Cause.isFailReason)?.error).toBeInstanceOf(
632 ResourceFailure,
633 );
634 expect(reconciled).toEqual([]);
635 }).pipe(Effect.provideService(TestResourceHooks, { create: record, update: record }));
636 }),
637 );
638 }
639 });
640
641 // #1831: a changed value read by a Platform's Init must reach the provider,
642 // even though `CapturedConfigHost`'s diff always returns `noop`.
643 describe("Platform Init-captured config", { tags: ["unit", "local"] }, () => {
644 test.provider("a changed captured value deploys and converges", (stack) =>
645 Effect.gen(function* () {
646 expect((yield* stack.deploy(capturedConfigHost("a"))).mode).toBe("a");
647 expect(actionOfPlan(yield* stack.plan(capturedConfigHost("a")), "Host")).toBe("noop");
648 expect((yield* stack.deploy(capturedConfigHost("b"))).mode).toBe("b");
649 expect(actionOfPlan(yield* stack.plan(capturedConfigHost("b")), "Host")).toBe("noop");
650 }),
651 );
652 });
653
654 describe("basic operations", { tags: ["unit", "local"] }, () => {
655 test.provider("should create, update, and delete resources", (stack) =>
656 Effect.gen(function* () {
657 expect(
658 yield* Effect.gen(function* () {
659 const A = yield* TestResource("A", { string: "test-string" });
660 return A.string;
661 }).pipe(stack.deploy),
662 ).toEqual("test-string");
663
664 expect(
665 yield* Effect.gen(function* () {
666 const A = yield* TestResource("A", { string: "test-string-new" });
667 return A.string;
668 }).pipe(stack.deploy),
669 ).toEqual("test-string-new");
670
671 yield* stack.destroy();
672
673 expect(yield* getState("A")).toBeUndefined();
674 expect(yield* listState()).toEqual([]);
675 }),
676 );
677
678 test.provider("should resolve output properties", (stack) =>
679 Effect.gen(function* () {
680 expect(
681 yield* Effect.gen(function* () {
682 const A = yield* TestResource("A", {
683 string: "test-string",
684 stringArray: ["test-string-array"],
685 });
686 const B = yield* TestResource("B", { string: A.string });
687 return B.string;
688 }).pipe(stack.deploy),
689 ).toEqual("test-string");
690
691 expect(
692 yield* Effect.gen(function* () {
693 const A = yield* TestResource("A", {
694 string: "test-string",
695 stringArray: ["test-string-array"],
696 });
697 const B = yield* TestResource("B", {
698 string: A.string.pipe(Output.map((string) => string.toUpperCase())),
699 });
700 return B.string;
701 }).pipe(stack.deploy),
702 ).toEqual("TEST-STRING");
703
704 expect(
705 yield* Effect.gen(function* () {
706 const A = yield* TestResource("A", {
707 string: "test-string",
708 stringArray: ["test-string-array"],
709 });
710 const B = yield* TestResource("B", {
711 string: A.string.pipe(Output.map((string) => string.toUpperCase() + "-NEW")),
712 });
713 return B.string;
714 }).pipe(stack.deploy),
715 ).toEqual("TEST-STRING-NEW");
716
717 expect(
718 yield* Effect.gen(function* () {
719 const A = yield* TestResource("A", {
720 string: "test-string",
721 stringArray: ["test-string-array"],
722 });
723 const B = yield* TestResource("B", {
724 string: A.string.pipe(
725 Output.flatMap((string) => Output.literal(string.toUpperCase() + "-FLAT")),
726 ),
727 });
728 return B.string;
729 }).pipe(stack.deploy),
730 ).toEqual("TEST-STRING-FLAT");
731 }),
732 );
733
734 test.provider(
735 "should apply downstream resources when a stable kind shadows an output discriminator",
736 (stack) =>
737 Effect.gen(function* () {
738 yield* KindStablesResource("Database", { value: "v1" }).pipe(stack.deploy);
739
740 const output = yield* Effect.gen(function* () {
741 const database = yield* KindStablesResource("Database", { value: "v2" });
742 const role = yield* KindStablesResource("Role", { value: "role", upstream: database });
743 return { database, role };
744 }).pipe(stack.deploy);
745
746 expect(output.database.value).toBe("v2");
747 expect(output.role.upstreamKind).toBe("postgresql");
748 }),
749 );
750
751 test.provider("should resolve bindings inside constructs using namespaced resources", (stack) =>
752 Effect.gen(function* () {
753 const Site = (id: string, _props: {}) =>
754 Effect.gen(function* () {
755 const bucket = yield* BindingTarget("Bucket", { string: "bucket-value" });
756 const distribution = yield* BindingTarget("Distribution", {
757 string: "distribution-value",
758 });
759
760 yield* bucket.bind("Policy", {
761 env: { BUCKET: bucket.string, DISTRIBUTION: distribution.string },
762 });
763
764 return { bucket, distribution };
765 }).pipe(Namespace.push(id));
766
767 const output = yield* Site("MarketingSite", {}).pipe(stack.deploy);
768
769 expect(output.bucket.env).toEqual({
770 BUCKET: "bucket-value",
771 DISTRIBUTION: "distribution-value",
772 });
773 expectConvergedStatus((yield* getState("MarketingSite/Bucket"))?.status);
774 expect((yield* getState("MarketingSite/Distribution"))?.status).toEqual("created");
775 }),
776 );
777
778 test.provider("should exclude deleted bindings before provider updates", (stack) =>
779 Effect.gen(function* () {
780 const created = yield* stack.deploy(
781 Effect.gen(function* () {
782 const target = yield* DeletedBindingRegressionTarget("A", { name: "target" });
783 yield* target.bind("TestBinding", { env: { FEATURE_FLAG: "on" } });
784 return target;
785 }),
786 );
787
788 expect(created.env).toEqual({ FEATURE_FLAG: "on" });
789
790 const updated = yield* stack.deploy(
791 Effect.gen(function* () {
792 return yield* DeletedBindingRegressionTarget("A", { name: "target" });
793 }),
794 );
795
796 expect(updated.env).toEqual({});
797 expect(yield* getState("A")).toMatchObject({ bindings: [], attr: { env: {} } });
798 }),
799 );
800
801 // #874: terminal commits must persist the RESOLVED binding payload the
802 // provider reconciled with, not the raw plan-time expressions. Raw
803 // `node.bindings` hold unresolved Outputs (silently dropped by JSON state
804 // stores), so persisting them makes every later plan's `diffBindings`
805 // compare a lossy stored shape against fully-resolved data — a phantom
806 // binding update on every plan. Exercises the create, update, and replace
807 // commit sites.
808 test.provider("terminal commits persist resolved binding payloads", (stack) =>
809 Effect.gen(function* () {
810 const program = (opts: { source: string; replaceString?: string }) =>
811 Effect.gen(function* () {
812 const source = yield* BindingTarget("BindSource", { string: opts.source });
813 const host = yield* BindingTarget("BindHost", {
814 name: "host",
815 replaceString: opts.replaceString,
816 });
817 // `source.string` is an unresolved Output at plan time.
818 yield* host.bind("FromSource", { env: { VALUE: source.string } });
819 return { source, host };
820 });
821
822 const actionOf = (plan: any, logicalId: string) =>
823 (Object.values(plan.resources) as any[]).find(
824 (node: any) => node.resource.LogicalId === logicalId,
825 )?.action;
826
827 const expectHostBindings = Effect.fn(function* (value: string) {
828 const hostState = yield* getState("BindHost");
829 expect(hostState?.bindings).toEqual([
830 { sid: "FromSource", data: { env: { VALUE: value } } },
831 ]);
832 });
833
834 // ── create commit ──
835 yield* stack.deploy(program({ source: "v1" }));
836 yield* expectHostBindings("v1");
837 const created = yield* stack.plan(program({ source: "v1" }));
838 expect(actionOf(created, "BindSource")).toBe("noop");
839 expect(actionOf(created, "BindHost")).toBe("noop");
840
841 // ── update commit ──
842 yield* stack.deploy(program({ source: "v2" }));
843 yield* expectHostBindings("v2");
844 const updated = yield* stack.plan(program({ source: "v2" }));
845 expect(actionOf(updated, "BindSource")).toBe("noop");
846 expect(actionOf(updated, "BindHost")).toBe("noop");
847
848 // ── replace commit ──
849 yield* stack.deploy(program({ source: "v2", replaceString: "flip" }));
850 yield* expectHostBindings("v2");
851 const replaced = yield* stack.plan(program({ source: "v2", replaceString: "flip" }));
852 expect(actionOf(replaced, "BindSource")).toBe("noop");
853 expect(actionOf(replaced, "BindHost")).toBe("noop");
854 }),
855 );
856
857 test.provider(
858 "persists resolved binding data so an unchanged redeploy plans binding noops",
859 (stack) =>
860 Effect.gen(function* () {
861 // Binding data references an output of a resource created in the
862 // SAME deploy — unresolved at plan time, resolved during apply.
863 const program = Effect.gen(function* () {
864 const upstream = yield* BindingTarget("Upstream", { string: "upstream-value" });
865 const target = yield* BindingTarget("Target", { string: "t" });
866 yield* target.bind("Cap", { env: { UPSTREAM: upstream.string } });
867 return target;
868 });
869
870 const created = yield* stack.deploy(program);
871 expect(created.env).toEqual({ UPSTREAM: "upstream-value" });
872
873 // The persisted binding data must hold the RESOLVED value (what
874 // `reconcile` received) — not the raw plan-time data, whose Output
875 // proxies JSON state stores silently drop.
876 expect(yield* getState("Target")).toMatchObject({
877 status: "created",
878 bindings: [{ sid: "Cap", data: { env: { UPSTREAM: "upstream-value" } } }],
879 });
880
881 // An unchanged redeploy must plan the binding as a noop. Before the
882 // fix, the truncated persisted data diffed against the now-resolved
883 // value and forced a spurious update on every deploy after a create.
884 yield* stack.deploy(program);
885 expect((yield* getState("Target"))?.status).toEqual("created");
886 }),
887 );
888
889 test.provider(
890 "should update a surviving consumer before deleting a removed dependency",
891 (stack) =>
892 Effect.gen(function* () {
893 const created = yield* stack.deploy(
894 Effect.gen(function* () {
895 const secret = yield* TestResource("Secret", { string: "secret-value" });
896 const worker = yield* Function("Worker", {
897 name: "worker",
898 env: { SECRET: secret.string },
899 });
900 return { secret, worker };
901 }),
902 );
903
904 expect(created.worker.env).toEqual({ SECRET: "secret-value" });
905 expect((yield* getState("Secret"))?.status).toEqual("created");
906 expect((yield* getState("Worker"))?.status).toEqual("created");
907
908 const updated = yield* stack.deploy(
909 Effect.gen(function* () {
910 return yield* Function("Worker", { name: "worker" });
911 }),
912 );
913
914 expect(updated.env).toEqual({});
915 expect(yield* getState("Secret")).toBeUndefined();
916 expect((yield* getState("Worker"))?.status).toEqual("updated");
917 }),
918 );
919
920 test.provider(
921 "should create a resource with a binding that references its own output",
922 (stack) =>
923 Effect.gen(function* () {
924 const created = yield* stack.deploy(
925 Effect.gen(function* () {
926 const target = yield* DeletedBindingRegressionTarget("A", { name: "target" });
927 yield* target.bind("SelfBinding", { env: { SELF_NAME: target.name } });
928 return target;
929 }),
930 );
931
932 expect(created.env).toEqual({ SELF_NAME: "target" });
933 }),
934 { timeout: 10_000 },
935 );
936 });
937
938 // Regression: a logical ID may legitimately contain the FQN separator ("/").
939 // The GitHub event source registers a webhook keyed by `${owner}/${repository}`
940 // (e.g. "alchemy-run/alchemy"). During destroy the deletion path used to
941 // recompute the state key via `toFqn(namespace, logicalId)`, but `logicalId`
942 // came from `parseFqn` which splits on "/" and keeps only the last segment
943 // ("alchemy"). The recomputed key missed the real state row, so the
944 // resource was deleted from the cloud yet never removed from state — resurfacing
945 // as an orphan deletion on every subsequent destroy, forever.
946 describe("FQN separator in logical ID", { tags: ["unit", "local"] }, () => {
947 test.provider("destroy clears state for a top-level logical ID containing '/'", (stack) =>
948 Effect.gen(function* () {
949 const fqn = "alchemy-run/alchemy";
950
951 yield* stack.deploy(
952 Effect.gen(function* () {
953 return yield* TestResource(fqn, { string: "v1" });
954 }),
955 );
956
957 // The row is persisted under the full FQN (separator and all).
958 expect((yield* getState(fqn))?.status).toEqual("created");
959
960 const deleted: string[] = [];
961 yield* stack.destroy().pipe(
962 hook({
963 delete: (id: string) =>
964 Effect.sync(() => {
965 deleted.push(id);
966 }),
967 }),
968 );
969
970 // provider.delete ran exactly once, AND the state row was removed
971 // (the pre-fix bug deleted the cloud resource but missed the row).
972 expect(deleted).toHaveLength(1);
973 expect(yield* getState(fqn)).toBeUndefined();
974 expect(yield* listState()).toEqual([]);
975 }),
976 );
977
978 test.provider("destroy clears state for a namespaced logical ID containing '/'", (stack) =>
979 Effect.gen(function* () {
980 // Mirrors the GitHub webhook: a host construct (the Worker) whose
981 // child resource's logical ID is "owner/repo".
982 const fqn = "ReleaseService/alchemy-run/alchemy";
983
984 yield* stack.deploy(
985 Effect.gen(function* () {
986 return yield* TestResource("alchemy-run/alchemy", { string: "v1" });
987 }).pipe(Namespace.push("ReleaseService")),
988 );
989
990 expect((yield* getState(fqn))?.status).toEqual("created");
991
992 yield* stack.destroy();
993
994 expect(yield* getState(fqn)).toBeUndefined();
995 expect(yield* listState()).toEqual([]);
996 }),
997 );
998 });
999
1000 // A ref to a resource the same program declares (current stack and stage) is
1001 // a dependency like a direct reference. Each case slows the side a missing
1002 // edge would let run first, so a race shows up as a wrong order instead of
1003 // passing by luck.
1004 describe("same-stack refs to declared resources", { tags: ["unit", "local"] }, () => {
1005 const program = (shared: TestResourceProps, withConsumer = true) =>
1006 Effect.gen(function* () {
1007 const declared = yield* TestResource("Shared", shared);
1008 if (!withConsumer) return { shared: declared, consumer: undefined };
1009 const ref = yield* TestResource.ref("Shared");
1010 const consumer = yield* TestResource("Consumer", { string: ref.string });
1011 return { shared: declared, consumer };
1012 });
1013
1014 // Target first, then the dependent: a ref that already resolves from state,
1015 // so the later assertions isolate ordering and freshness from the
1016 // same-deploy create case.
1017 const deployInPhases = (stack: Test.ScratchStack, shared: TestResourceProps) =>
1018 Effect.gen(function* () {
1019 yield* stack.deploy(program(shared, false));
1020 return yield* stack.deploy(program(shared));
1021 });
1022
1023 const recorder = () => {
1024 const events: string[] = [];
1025 const record = (event: string, slow?: string) => (id: string) =>
1026 Effect.gen(function* () {
1027 if (id === slow) yield* Effect.sleep("100 millis");
1028 events.push(`${event} ${id}`);
1029 });
1030 return { events, record };
1031 };
1032
1033 test.provider("deploys a ref to a resource created in the same deploy, target first", (stack) =>
1034 Effect.gen(function* () {
1035 const { events, record } = recorder();
1036 const output = yield* program({ string: "v1" }).pipe(
1037 stack.deploy,
1038 hook({ create: record("create", "Shared") }),
1039 );
1040
1041 expect(output.consumer!.string).toEqual("v1");
1042 expect(events).toEqual(["create Shared", "create Consumer"]);
1043 // The edge is persisted, so a later destroy orders deletes without the program.
1044 expect((yield* getState("Shared"))?.downstream).toContain("Consumer");
1045 }),
1046 );
1047
1048 test.provider("destroy deletes the ref's dependent before its target", (stack) =>
1049 Effect.gen(function* () {
1050 yield* deployInPhases(stack, { string: "v1" });
1051
1052 const { events, record } = recorder();
1053 yield* stack.destroy().pipe(hook({ delete: record("delete", "Consumer") }));
1054
1055 expect(events).toEqual(["delete Consumer", "delete Shared"]);
1056 expect(yield* listState()).toEqual([]);
1057 }),
1058 );
1059
1060 test.provider("removing both from the program deletes the dependent first", (stack) =>
1061 Effect.gen(function* () {
1062 yield* deployInPhases(stack, { string: "v1" });
1063
1064 const { events, record } = recorder();
1065 yield* stack
1066 .deploy(Effect.gen(function* () {}))
1067 .pipe(hook({ delete: record("delete", "Consumer") }));
1068
1069 expect(events).toEqual(["delete Consumer", "delete Shared"]);
1070 }),
1071 );
1072
1073 test.provider("removing only the dependent keeps the target", (stack) =>
1074 Effect.gen(function* () {
1075 yield* deployInPhases(stack, { string: "v1" });
1076
1077 const { events, record } = recorder();
1078 yield* stack
1079 .deploy(program({ string: "v1" }, false))
1080 .pipe(hook({ delete: record("delete") }));
1081
1082 expect(events).toEqual(["delete Consumer"]);
1083 expect((yield* getState("Shared"))?.status).toEqual("created");
1084 expect((yield* getState("Shared"))?.downstream ?? []).not.toContain("Consumer");
1085 }),
1086 );
1087
1088 test.provider("an update to the target reaches the dependent fresh, never stale", (stack) =>
1089 Effect.gen(function* () {
1090 yield* deployInPhases(stack, { string: "v1" });
1091
1092 const seen: string[] = [];
1093 const output = yield* program({ string: "v2" }).pipe(
1094 stack.deploy,
1095 hook({
1096 update: (id, props) =>
1097 Effect.gen(function* () {
1098 if (id === "Shared") yield* Effect.sleep("100 millis");
1099 if (id === "Consumer" && typeof props.string === "string") seen.push(props.string);
1100 }),
1101 }),
1102 );
1103
1104 expect(output.consumer!.string).toEqual("v2");
1105 expect(seen).toEqual(["v2"]);
1106 }),
1107 );
1108
1109 test.provider("a replaced target hands the dependent its new generation", (stack) =>
1110 Effect.gen(function* () {
1111 yield* deployInPhases(stack, { string: "v1", replaceString: "a" });
1112
1113 const { events, record } = recorder();
1114 const output = yield* program({ string: "v2", replaceString: "b" }).pipe(
1115 stack.deploy,
1116 hook({
1117 create: record("create", "Shared"),
1118 update: record("update"),
1119 delete: record("delete"),
1120 }),
1121 );
1122
1123 expect(output.shared.replaceString).toEqual("b");
1124 expect(output.consumer!.string).toEqual("v2");
1125 // Create-first replacement: new generation, then the dependent, then
1126 // the old generation is retired.
1127 expect(events).toEqual(["create Shared", "update Consumer", "delete Shared"]);
1128 }),
1129 );
1130
1131 test.provider(
1132 "a resource's ref to itself reads its persisted state without waiting on itself",
1133 (stack) =>
1134 Effect.gen(function* () {
1135 yield* stack.deploy(TestResource("Self", { string: "v1" }));
1136
1137 const output = yield* stack.deploy(
1138 Effect.gen(function* () {
1139 const self = yield* TestResource.ref("Self");
1140 return yield* TestResource("Self", { string: "v2", stringArray: [self.string] });
1141 }),
1142 );
1143
1144 expect(output.string).toEqual("v2");
1145 expect(output.stringArray).toEqual(["v1"]);
1146 expect((yield* getState("Self"))?.downstream ?? []).not.toContain("Self");
1147 }),
1148 );
1149
1150 test.provider("a ref used only in a binding persists the binding edge", (stack) =>
1151 Effect.gen(function* () {
1152 yield* stack.deploy(TestResource("Shared", { string: "v1" }));
1153 const output = yield* stack.deploy(
1154 Effect.gen(function* () {
1155 yield* TestResource("Shared", { string: "v1" });
1156 const ref = yield* TestResource.ref("Shared");
1157 const host = yield* BindingTarget("Host", { name: "host" });
1158 yield* host.bind("Shared", { env: { SHARED: ref.string } });
1159 return host;
1160 }),
1161 );
1162
1163 expect(output.env.SHARED).toEqual("v1");
1164 expect((yield* getState("Shared"))?.downstream).toContain("Host");
1165 }),
1166 );
1167 });
1168
1169 describe("linear update propagation", { tags: ["unit", "local"] }, () => {
1170 // Regression: in a linear chain (A -> B with no cycle), an update to A
1171 // followed by an update to B must let B see A's *post-update* attr, never
1172 // the stale prior attr. Before the cycle-gating change, A would publish
1173 // its prior attr early and B's update would race against the live value,
1174 // sometimes deploying with stale data (e.g. a Worker reading a Build's
1175 // outdir/hash before the build finished).
1176 test.provider("downstream update receives upstream's post-update attr", (stack) =>
1177 Effect.gen(function* () {
1178 yield* stack.deploy(
1179 Effect.gen(function* () {
1180 const A = yield* TestResource("A", { string: "v1" });
1181 const B = yield* TestResource("B", { string: A.string });
1182 return { A, B };
1183 }),
1184 );
1185
1186 const sawByB: string[] = [];
1187 const captureBHooks = {
1188 create: () => Effect.succeed(undefined),
1189 update: (id: string, props: TestResourceProps) =>
1190 Effect.sync(() => {
1191 if (id === "B" && typeof props.string === "string") {
1192 sawByB.push(props.string);
1193 }
1194 }),
1195 delete: () => Effect.succeed(undefined),
1196 read: () => Effect.succeed(undefined),
1197 };
1198
1199 const output = yield* Effect.gen(function* () {
1200 const A = yield* TestResource("A", { string: "v2" });
1201 const B = yield* TestResource("B", { string: A.string });
1202 return { A, B };
1203 }).pipe(stack.deploy, hook(captureBHooks));
1204
1205 expect(output.A.string).toEqual("v2");
1206 expect(output.B.string).toEqual("v2");
1207 // B.update must have observed the fresh upstream value, never the
1208 // stale "v1". A single fresh-only call is the ideal; we accept any
1209 // sequence as long as no stale value leaked through.
1210 expect(sawByB.length).toBeGreaterThan(0);
1211 expect(sawByB.every((v) => v === "v2")).toBe(true);
1212 }),
1213 );
1214
1215 // Regression: a dependent that repins from upstream A to upstream B updates
1216 // *itself*, while A and B are both noops. The noop pass must still persist
1217 // their new `downstream` — delete ordering reads it from the persisted row,
1218 // so a stale list would delete B concurrently with the dependent that still
1219 // references it (and needlessly wait on A).
1220 test.provider("noop upstreams persist a repinned dependent's downstream edge", (stack) =>
1221 Effect.gen(function* () {
1222 const program = (pin: "A" | "B") =>
1223 Effect.gen(function* () {
1224 const A = yield* TestResource("A", { string: "a" });
1225 const B = yield* TestResource("B", { string: "b" });
1226 const C = yield* TestResource("C", { string: (pin === "A" ? A : B).string });
1227 return { A, B, C };
1228 });
1229
1230 yield* stack.deploy(program("A"));
1231 expect((yield* getState("A"))?.downstream).toEqual(["C"]);
1232 expect((yield* getState("B"))?.downstream).toEqual([]);
1233
1234 yield* stack.deploy(program("B"));
1235 expect((yield* getState("A"))?.downstream).toEqual([]);
1236 expect((yield* getState("B"))?.downstream).toEqual(["C"]);
1237 }),
1238 );
1239 });
1240
1241 // Regression: `deleteFirst` on a `replace` diff was plumbed from the provider
1242 // all the way into persisted state but never *read* — every replacement was
1243 // create-first, with the old generation reclaimed afterwards by Phase-2 GC.
1244 // That silently broke any resource whose replacement can't coexist with the
1245 // original (fixed physical name, singleton): the create collided with the
1246 // not-yet-deleted original. These tests pin both orderings.
1247 describe("deleteFirst replacements", { tags: ["unit", "local"] }, () => {
1248 test.provider("deletes the old generation BEFORE creating the replacement", (stack) =>
1249 Effect.gen(function* () {
1250 yield* stack.deploy(
1251 Effect.gen(function* () {
1252 return yield* DeleteFirstResource("R", { replaceString: "v1" });
1253 }),
1254 );
1255
1256 const order: string[] = [];
1257 const recordHooks = {
1258 create: () =>
1259 Effect.sync(() => {
1260 order.push("create");
1261 }),
1262 update: () => Effect.succeed(undefined),
1263 delete: () =>
1264 Effect.sync(() => {
1265 order.push("delete");
1266 }),
1267 };
1268
1269 yield* Effect.gen(function* () {
1270 return yield* DeleteFirstResource("R", { replaceString: "v2" });
1271 }).pipe(stack.deploy, hook(recordHooks));
1272
1273 // The whole point: delete-old precedes create-new.
1274 expect(order).toEqual(["delete", "create"]);
1275
1276 // The resource collapses straight to a terminal `created` state with
1277 // no leftover replacement chain for GC to drain.
1278 const state = yield* getState("R");
1279 expect(state?.status).toEqual("created");
1280 expect((state as { old?: unknown }).old).toBeUndefined();
1281 expect(yield* listState()).toHaveLength(1);
1282 }),
1283 );
1284
1285 test.provider("default (non-deleteFirst) replacement still creates BEFORE deleting", (stack) =>
1286 Effect.gen(function* () {
1287 // `TestResource` returns a plain `{ action: "replace" }` (deleteFirst
1288 // defaults to false), so the engine must stay create-first.
1289 yield* stack.deploy(
1290 Effect.gen(function* () {
1291 return yield* TestResource("R", { replaceString: "v1" });
1292 }),
1293 );
1294
1295 const order: string[] = [];
1296 yield* Effect.gen(function* () {
1297 return yield* TestResource("R", { replaceString: "v2" });
1298 }).pipe(
1299 stack.deploy,
1300 hook({
1301 create: () =>
1302 Effect.sync(() => {
1303 order.push("create");
1304 }),
1305 update: () => Effect.succeed(undefined),
1306 delete: () =>
1307 Effect.sync(() => {
1308 order.push("delete");
1309 }),
1310 }),
1311 );
1312
1313 expect(order).toEqual(["create", "delete"]);
1314 }),
1315 );
1316
1317 test.provider(
1318 "lets a same-identity replacement succeed where create-first would collide",
1319 (stack) =>
1320 Effect.gen(function* () {
1321 // A shared registry of live physical names. The provider's create
1322 // fails if the (fixed) name is still live — exactly the failure mode
1323 // of a real fixed-name resource (Docker network "already exists",
1324 // no-op `volume create`) when create runs before the old is deleted.
1325 const registry = { live: new Set<string>() };
1326 const withRegistry = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
1327 effect.pipe(Effect.provide(Layer.succeed(CollisionRegistry, registry)));
1328
1329 yield* stack
1330 .deploy(
1331 Effect.gen(function* () {
1332 return yield* DeleteFirstResource("R", { name: "singleton", replaceString: "v1" });
1333 }),
1334 )
1335 .pipe(withRegistry);
1336 expect(registry.live.has("singleton")).toBe(true);
1337
1338 // Before the fix this deploy died with a CollisionError because the
1339 // create of the new "singleton" ran while the old one was still live.
1340 const result = yield* stack
1341 .deploy(
1342 Effect.gen(function* () {
1343 return yield* DeleteFirstResource("R", { name: "singleton", replaceString: "v2" });
1344 }),
1345 )
1346 .pipe(withRegistry);
1347
1348 expect(result.name).toEqual("singleton");
1349 expect(result.replaceString).toEqual("v2");
1350 // Exactly one live instance remains (old torn down, new created).
1351 expect(registry.live.size).toBe(1);
1352 expect(registry.live.has("singleton")).toBe(true);
1353
1354 const state = yield* getState("R");
1355 expect(state?.status).toEqual("created");
1356 }),
1357 );
1358 });
1359
1360 describe("circularity via bindings", { tags: ["unit", "local"] }, () => {
1361 const selfBoundStack = (props: { string: string; replaceString?: string; includeD?: boolean }) =>
1362 Effect.gen(function* () {
1363 const A = yield* BindingTarget("A", {
1364 name: "a",
1365 string: props.string,
1366 replaceString: props.replaceString,
1367 });
1368 yield* A.bind("SelfBinding", { env: { SELF: A.string } });
1369 const B = yield* TestResource("B", { string: A.string });
1370 if (props.includeD) {
1371 const D = yield* TestResource("D", { string: B.string });
1372 return { A, B, D };
1373 }
1374 return { A, B };
1375 });
1376
1377 const mutualBindingStack = (props: {
1378 aString: string;
1379 aReplaceString?: string;
1380 bString?: string;
1381 includeD?: boolean;
1382 }) =>
1383 Effect.gen(function* () {
1384 const A = yield* BindingTarget("A", {
1385 name: "a",
1386 string: props.aString,
1387 replaceString: props.aReplaceString,
1388 });
1389 const B = yield* BindingTarget("B", { name: "b", string: props.bString ?? "b-value" });
1390 yield* A.bind("FromB", { env: { PEER: B.string } });
1391 yield* B.bind("FromA", { env: { PEER: A.string } });
1392 if (props.includeD) {
1393 const D = yield* TestResource("D", { string: Output.interpolate`${A.string}-${B.string}` });
1394 return { A, B, D };
1395 }
1396 return { A, B };
1397 });
1398
1399 const propAndBindingCycleStack = () =>
1400 Effect.gen(function* () {
1401 const A = yield* BindingTarget("A", { name: "a", string: "a-value" });
1402 const B = yield* TestResource("B", { string: A.string });
1403 yield* A.bind("FromB", { env: { PEER: B.string } });
1404 return { A, B };
1405 });
1406
1407 test.provider(
1408 "create succeeds when props use precreate output and bindings use downstream output",
1409 (stack) =>
1410 Effect.gen(function* () {
1411 const output = yield* stack.deploy(propAndBindingCycleStack());
1412
1413 expect(output.A.env).toEqual({ PEER: "a-value" });
1414 expect(output.B.string).toEqual("a-value");
1415 expectConvergedStatus((yield* getState("A"))?.status);
1416 expectConvergedStatus((yield* getState("B"))?.status);
1417 }),
1418 { timeout: 10_000 },
1419 );
1420
1421 describe("self-referential bindings", () => {
1422 test.provider("create succeeds with self binding", (stack) =>
1423 Effect.gen(function* () {
1424 const output = yield* stack.deploy(
1425 selfBoundStack({ string: "a-value", replaceString: "original" }),
1426 );
1427
1428 expect(output.A.env).toEqual({ SELF: "a-value" });
1429 expect(output.B.string).toEqual("a-value");
1430 expectConvergedStatus((yield* getState("A"))?.status);
1431 expectConvergedStatus((yield* getState("B"))?.status);
1432 }),
1433 );
1434
1435 test.provider(
1436 "replacing state noop replay recovers and creates downstream resources",
1437 (stack) =>
1438 Effect.gen(function* () {
1439 yield* selfBoundStack({ string: "a-value", replaceString: "original" }).pipe(
1440 stack.deploy,
1441 );
1442
1443 const program = selfBoundStack({
1444 string: "a-value-replaced",
1445 replaceString: "changed",
1446 includeD: true,
1447 });
1448
1449 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
1450
1451 expect((yield* getState<ReplacingResourceState>("A"))?.status).toEqual("replacing");
1452 expectConvergedStatus((yield* getState("B"))?.status);
1453 expectNotStarted(yield* getState("D"));
1454
1455 const output = yield* program.pipe(stack.deploy);
1456 expectConvergedStatus((yield* getState("A"))?.status);
1457 expect((yield* getState("B"))?.status).toEqual("updated");
1458 expectConvergedStatus((yield* getState("D"))?.status);
1459 expect(output.A.env).toEqual({ SELF: "a-value-replaced" });
1460 expect(output.D!.string).toEqual("a-value-replaced");
1461 }),
1462 );
1463
1464 test.provider(
1465 "replacing state update replay updates replacement and creates downstream resources",
1466 (stack) =>
1467 Effect.gen(function* () {
1468 yield* selfBoundStack({ string: "a-value", replaceString: "original" }).pipe(
1469 stack.deploy,
1470 );
1471
1472 yield* selfBoundStack({
1473 string: "a-value-replaced",
1474 replaceString: "changed",
1475 includeD: true,
1476 }).pipe(stack.deploy, hook(failOn("A", "create")));
1477
1478 expect((yield* getState<ReplacingResourceState>("A"))?.status).toEqual("replacing");
1479 expectConvergedStatus((yield* getState("B"))?.status);
1480 expectNotStarted(yield* getState("D"));
1481
1482 const output = yield* selfBoundStack({
1483 string: "a-value-updated-during-recovery",
1484 replaceString: "changed",
1485 includeD: true,
1486 }).pipe(stack.deploy);
1487
1488 expectConvergedStatus((yield* getState("A"))?.status);
1489 expect((yield* getState("B"))?.status).toEqual("updated");
1490 expectConvergedStatus((yield* getState("D"))?.status);
1491 expect(output.A.env).toEqual({ SELF: "a-value-updated-during-recovery" });
1492 expect(output.D!.string).toEqual("a-value-updated-during-recovery");
1493 }),
1494 );
1495
1496 test.provider(
1497 "replaced state noop replay finishes cleanup and creates downstream resources",
1498 (stack) =>
1499 Effect.gen(function* () {
1500 yield* selfBoundStack({ string: "a-value", replaceString: "original" }).pipe(
1501 stack.deploy,
1502 );
1503
1504 const program = selfBoundStack({
1505 string: "a-value-replaced",
1506 replaceString: "changed",
1507 includeD: true,
1508 });
1509
1510 yield* program.pipe(stack.deploy, hook(failOn("B", "update")));
1511
1512 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
1513 expect((yield* getState("B"))?.status).toEqual("updating");
1514 expectNotStarted(yield* getState("D"));
1515
1516 const output = yield* program.pipe(stack.deploy);
1517 expectConvergedStatus((yield* getState("A"))?.status);
1518 expect((yield* getState("B"))?.status).toEqual("updated");
1519 expectConvergedStatus((yield* getState("D"))?.status);
1520 expect(output.A.env).toEqual({ SELF: "a-value-replaced" });
1521 expect(output.D!.string).toEqual("a-value-replaced");
1522 }),
1523 );
1524
1525 test.provider(
1526 "replaced state update replay updates replacement and downstream resources",
1527 (stack) =>
1528 Effect.gen(function* () {
1529 yield* selfBoundStack({ string: "a-value", replaceString: "original" }).pipe(
1530 stack.deploy,
1531 );
1532
1533 yield* selfBoundStack({
1534 string: "a-value-replaced",
1535 replaceString: "changed",
1536 includeD: true,
1537 }).pipe(stack.deploy, hook(failOn("B", "update")));
1538
1539 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
1540 expect((yield* getState("B"))?.status).toEqual("updating");
1541 expectNotStarted(yield* getState("D"));
1542
1543 const output = yield* selfBoundStack({
1544 string: "a-value-updated-after-replace",
1545 replaceString: "changed",
1546 includeD: true,
1547 }).pipe(stack.deploy);
1548
1549 expectConvergedStatus((yield* getState("A"))?.status);
1550 expect((yield* getState("B"))?.status).toEqual("updated");
1551 expectConvergedStatus((yield* getState("D"))?.status);
1552 expect(output.A.env).toEqual({ SELF: "a-value-updated-after-replace" });
1553 expect(output.D!.string).toEqual("a-value-updated-after-replace");
1554 }),
1555 );
1556 });
1557
1558 describe("mutual A <-> B bindings", () => {
1559 test.provider("create succeeds with mutual bindings", (stack) =>
1560 Effect.gen(function* () {
1561 const output = yield* stack.deploy(mutualBindingStack({ aString: "a-value" }));
1562
1563 expect(output.A.env).toEqual({ PEER: "b-value" });
1564 expect(output.B.env).toEqual({ PEER: "a-value" });
1565 expectConvergedStatus((yield* getState("A"))?.status);
1566 expectConvergedStatus((yield* getState("B"))?.status);
1567 }),
1568 );
1569
1570 test.provider("destroy succeeds with mutual bindings", (stack) =>
1571 Effect.gen(function* () {
1572 yield* mutualBindingStack({ aString: "a-value" }).pipe(stack.deploy);
1573
1574 yield* stack.destroy();
1575
1576 expect(yield* getState("A")).toBeUndefined();
1577 expectNotStarted(yield* getState("B"));
1578 }),
1579 );
1580
1581 describe("from replacing state", () => {
1582 test.provider("replacing noop recovery creates downstream resources", (stack) =>
1583 Effect.gen(function* () {
1584 yield* mutualBindingStack({ aString: "a-value", aReplaceString: "original" }).pipe(
1585 stack.deploy,
1586 );
1587
1588 const program = mutualBindingStack({
1589 aString: "a-value-replaced",
1590 aReplaceString: "changed",
1591 includeD: true,
1592 });
1593
1594 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
1595
1596 expect((yield* getState<ReplacingResourceState>("A"))?.status).toEqual("replacing");
1597 expectConvergedStatus((yield* getState("B"))?.status);
1598 expectNotStarted(yield* getState("D"));
1599
1600 const output = yield* program.pipe(stack.deploy);
1601 expectConvergedStatus((yield* getState("A"))?.status);
1602 expect((yield* getState("B"))?.status).toEqual("updated");
1603 expectConvergedStatus((yield* getState("D"))?.status);
1604 expect(output.A.env).toEqual({ PEER: "b-value" });
1605 expect(output.B.env).toEqual({ PEER: "a-value-replaced" });
1606 expect(output.D!.string).toEqual("a-value-replaced-b-value");
1607 }),
1608 );
1609
1610 test.provider("replacing update recovery creates downstream resources", (stack) =>
1611 Effect.gen(function* () {
1612 yield* mutualBindingStack({ aString: "a-value", aReplaceString: "original" }).pipe(
1613 stack.deploy,
1614 );
1615
1616 yield* mutualBindingStack({
1617 aString: "a-value-replaced",
1618 aReplaceString: "changed",
1619 includeD: true,
1620 }).pipe(stack.deploy, hook(failOn("A", "create")));
1621
1622 expect((yield* getState<ReplacingResourceState>("A"))?.status).toEqual("replacing");
1623 expectConvergedStatus((yield* getState("B"))?.status);
1624 expectNotStarted(yield* getState("D"));
1625
1626 const output = yield* mutualBindingStack({
1627 aString: "a-value-updated-during-recovery",
1628 aReplaceString: "changed",
1629 includeD: true,
1630 }).pipe(stack.deploy);
1631
1632 expectConvergedStatus((yield* getState("A"))?.status);
1633 expect((yield* getState("B"))?.status).toEqual("updated");
1634 expectConvergedStatus((yield* getState("D"))?.status);
1635 expect(output.A.env).toEqual({ PEER: "b-value" });
1636 expect(output.B.env).toEqual({ PEER: "a-value-updated-during-recovery" });
1637 expect(output.D!.string).toEqual("a-value-updated-during-recovery-b-value");
1638 }),
1639 );
1640
1641 test.provider("replacing replace recovery nests another replacement", (stack) =>
1642 Effect.gen(function* () {
1643 yield* mutualBindingStack({ aString: "a-value", aReplaceString: "original" }).pipe(
1644 stack.deploy,
1645 );
1646
1647 yield* mutualBindingStack({
1648 aString: "a-value-replaced",
1649 aReplaceString: "changed",
1650 includeD: true,
1651 }).pipe(stack.deploy, hook(failOn("A", "create")));
1652
1653 const output = yield* mutualBindingStack({
1654 aString: "a-value-another-replacement",
1655 aReplaceString: "another-change",
1656 includeD: true,
1657 }).pipe(stack.deploy);
1658
1659 expectConvergedStatus((yield* getState("A"))?.status);
1660 expect((yield* getState("B"))?.status).toEqual("updated");
1661 expectConvergedStatus((yield* getState("D"))?.status);
1662 expect(output.B.env).toEqual({ PEER: "a-value-another-replacement" });
1663 }),
1664 );
1665 });
1666
1667 describe("from replaced state", () => {
1668 test.provider(
1669 "replaced noop recovery updates downstream then creates downstream resources",
1670 (stack) =>
1671 Effect.gen(function* () {
1672 yield* mutualBindingStack({ aString: "a-value", aReplaceString: "original" }).pipe(
1673 stack.deploy,
1674 );
1675
1676 const program = mutualBindingStack({
1677 aString: "a-value-replaced",
1678 aReplaceString: "changed",
1679 includeD: true,
1680 });
1681
1682 yield* program.pipe(stack.deploy, hook(failOn("B", "update")));
1683
1684 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
1685 expect((yield* getState("B"))?.status).toEqual("updating");
1686 expectNotStarted(yield* getState("D"));
1687
1688 const output = yield* program.pipe(stack.deploy);
1689 expect((yield* getState("A"))?.status).toEqual("created");
1690 expect((yield* getState("B"))?.status).toEqual("updated");
1691 expectConvergedStatus((yield* getState("D"))?.status);
1692 expect(output.A.env).toEqual({ PEER: "b-value" });
1693 expect(output.B.env).toEqual({ PEER: "a-value-replaced" });
1694 expect(output.D!.string).toEqual("a-value-replaced-b-value");
1695 }),
1696 );
1697
1698 test.provider(
1699 "replaced with update recovery updates replacement and downstream resources",
1700 (stack) =>
1701 Effect.gen(function* () {
1702 yield* mutualBindingStack({ aString: "a-value", aReplaceString: "original" }).pipe(
1703 stack.deploy,
1704 );
1705
1706 yield* mutualBindingStack({
1707 aString: "a-value-replaced",
1708 aReplaceString: "changed",
1709 includeD: true,
1710 }).pipe(stack.deploy, hook(failOn("B", "update")));
1711
1712 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
1713 expect((yield* getState("B"))?.status).toEqual("updating");
1714 expectNotStarted(yield* getState("D"));
1715
1716 const output = yield* mutualBindingStack({
1717 aString: "a-value-updated-after-replace",
1718 aReplaceString: "changed",
1719 includeD: true,
1720 }).pipe(stack.deploy);
1721
1722 expect((yield* getState("A"))?.status).toEqual("created");
1723 expect((yield* getState("B"))?.status).toEqual("updated");
1724 expectConvergedStatus((yield* getState("D"))?.status);
1725 expect(output.A.env).toEqual({ PEER: "b-value" });
1726 expect(output.B.env).toEqual({ PEER: "a-value-updated-after-replace" });
1727 expect(output.D!.string).toEqual("a-value-updated-after-replace-b-value");
1728 }),
1729 );
1730
1731 test.provider("replaced replace recovery nests another replacement", (stack) =>
1732 Effect.gen(function* () {
1733 yield* mutualBindingStack({ aString: "a-value", aReplaceString: "original" }).pipe(
1734 stack.deploy,
1735 );
1736
1737 yield* mutualBindingStack({
1738 aString: "a-value-replaced",
1739 aReplaceString: "changed",
1740 includeD: true,
1741 }).pipe(stack.deploy, hook(failOn("B", "update")));
1742
1743 const output = yield* mutualBindingStack({
1744 aString: "a-value-another-replacement",
1745 aReplaceString: "another-change",
1746 includeD: true,
1747 }).pipe(stack.deploy);
1748
1749 expectConvergedStatus((yield* getState("A"))?.status);
1750 expect((yield* getState("B"))?.status).toEqual("updated");
1751 expectConvergedStatus((yield* getState("D"))?.status);
1752 expect(output.B.env).toEqual({ PEER: "a-value-another-replacement" });
1753 }),
1754 );
1755 });
1756 });
1757 });
1758
1759 describe("prop-flow convergence", { tags: ["unit", "local"] }, () => {
1760 test.provider(
1761 "downstream outside a cycle waits for precreated upstream reconciliation",
1762 (stack) =>
1763 Effect.gen(function* () {
1764 let reconciled = false;
1765 const observations: boolean[] = [];
1766 yield* Effect.gen(function* () {
1767 const worker = yield* PhasedTarget("Worker", {
1768 desired: "queue-handler",
1769 replaceKey: "v1",
1770 });
1771 const binding = yield* TestResource("Binding", { string: worker.stableId });
1772 yield* worker.bind("Feedback", { env: { B: binding.string } });
1773 // Like a Queue Consumer: the script name is available from the
1774 // stub, but attaching requires the real Worker upload to finish.
1775 return yield* TestResource("Consumer", { string: worker.stableId });
1776 }).pipe(
1777 stack.deploy,
1778 hook({
1779 create: (id) =>
1780 Effect.gen(function* () {
1781 if (id === "Worker") {
1782 yield* Effect.sleep("50 millis");
1783 reconciled = true;
1784 } else if (id === "Consumer") {
1785 observations.push(reconciled);
1786 }
1787 }),
1788 }),
1789 );
1790 expect(observations).toEqual([true]);
1791 }),
1792 );
1793
1794 const phasedCycleStack = (props: {
1795 desired: string;
1796 replaceKey?: string;
1797 use: "stableId" | "value";
1798 includeC?: boolean;
1799 }) =>
1800 Effect.gen(function* () {
1801 const A = yield* PhasedTarget("A", { desired: props.desired, replaceKey: props.replaceKey });
1802 const selected = props.use === "stableId" ? A.stableId : A.value;
1803 const B = yield* TestResource("B", { string: selected });
1804 yield* A.bind("FromB", { env: { B: B.string } });
1805
1806 if (props.includeC) {
1807 const C = yield* TestResource("C", { string: B.string });
1808 return { A, B, C };
1809 }
1810
1811 return { A, B };
1812 });
1813
1814 test.provider("fresh circular create may use a stable precreate identifier", (stack) =>
1815 Effect.gen(function* () {
1816 const output = yield* phasedCycleStack({
1817 desired: "final-a",
1818 replaceKey: "v1",
1819 use: "stableId",
1820 }).pipe(stack.deploy);
1821
1822 expect(output.A.value).toEqual("final-a");
1823 expect(output.B.string).toEqual("stable:v1");
1824 }),
1825 );
1826
1827 test.provider("fresh circular create should converge downstream props to final values", (stack) =>
1828 Effect.gen(function* () {
1829 const output = yield* phasedCycleStack({
1830 desired: "final-a",
1831 replaceKey: "v1",
1832 use: "value",
1833 }).pipe(stack.deploy);
1834
1835 expect(output.A.value).toEqual("final-a");
1836 expect(output.B.string).toEqual("final-a");
1837 }),
1838 );
1839
1840 test.provider(
1841 "fresh replacement should converge newly created downstream props to replacement values",
1842 (stack) =>
1843 Effect.gen(function* () {
1844 yield* phasedCycleStack({ desired: "old-a", replaceKey: "v1", use: "value" }).pipe(
1845 stack.deploy,
1846 );
1847
1848 const output = yield* phasedCycleStack({
1849 desired: "new-a",
1850 replaceKey: "v2",
1851 use: "value",
1852 }).pipe(stack.deploy);
1853
1854 expect(output.A.value).toEqual("new-a");
1855 expect(output.B.string).toEqual("new-a");
1856 }),
1857 );
1858
1859 test.provider("stale precreate values should not propagate transitively", (stack) =>
1860 Effect.gen(function* () {
1861 const output = yield* phasedCycleStack({
1862 desired: "final-a",
1863 replaceKey: "v1",
1864 use: "value",
1865 includeC: true,
1866 }).pipe(stack.deploy);
1867
1868 expect(output.A.value).toEqual("final-a");
1869 expect(output.B.string).toEqual("final-a");
1870 expect(output.C!.string).toEqual("final-a");
1871 }),
1872 );
1873
1874 test.provider("binding feedback converges across an A -> B -> A fixed point", (stack) =>
1875 Effect.gen(function* () {
1876 const output = yield* Effect.gen(function* () {
1877 const A = yield* PhasedTarget("A", { desired: "final-a", replaceKey: "v1" });
1878 const B = yield* TestResource("B", { string: A.value });
1879 yield* A.bind("FromB", { env: { B: B.string } });
1880 return { A, B };
1881 }).pipe(stack.deploy);
1882
1883 expect(output.A.value).toEqual("final-a");
1884 expect(output.B.string).toEqual("final-a");
1885 expect(output.A.env).toEqual({ B: "final-a" });
1886 }),
1887 );
1888
1889 test.provider(
1890 "terminal created or updated status is delayed until fixed-point convergence finishes",
1891 (stack) =>
1892 Effect.gen(function* () {
1893 const events: Array<{ id: string; status: string }> = [];
1894 const cli = Cli.of({
1895 startPlanningSession: () =>
1896 Effect.succeed({
1897 update: () => Effect.void,
1898 succeed: () => Effect.void,
1899 fail: () => Effect.void,
1900 close: Effect.void,
1901 }),
1902 approvePlan: () => Effect.succeed(true),
1903 displayPlan: () => Effect.void,
1904 startApplySession: () =>
1905 Effect.succeed({
1906 done: () => Effect.void,
1907 emit: (event) =>
1908 Effect.sync(() => {
1909 if (event.type === "apply.resource.status") {
1910 events.push({ id: event.id, status: event.status });
1911 }
1912 }),
1913 }),
1914 });
1915
1916 const output = yield* Effect.gen(function* () {
1917 const A = yield* PhasedTarget("A", { desired: "final-a", replaceKey: "v1" });
1918 const B = yield* TestResource("B", { string: A.value });
1919 yield* A.bind("FromB", { env: { B: B.string } });
1920 return { A, B };
1921 }).pipe(stack.deploy, Effect.provide(Layer.succeed(Cli, cli)));
1922
1923 expect(output.A.env).toEqual({ B: "final-a" });
1924
1925 const statusesById = events.reduce(
1926 (acc, event: { id: string; status: string }) => {
1927 (acc[event.id] ??= []).push(event);
1928 return acc;
1929 },
1930 {} as Record<string, Array<{ id: string; status: string }>>,
1931 );
1932 const terminal = (id: string) =>
1933 (statusesById[id] ?? [])
1934 .map((event: { id: string; status: string }) => event.status)
1935 .filter((status: string) => status === "created" || status === "updated");
1936
1937 expect(terminal("A")).toEqual(["updated"]);
1938 expect(terminal("B")).toEqual(["updated"]);
1939 }),
1940 );
1941
1942 test.provider("apply sessions finalize after a resource failure", (stack) =>
1943 Effect.gen(function* () {
1944 let finalized = 0;
1945 const cli = Cli.of({
1946 startPlanningSession: () =>
1947 Effect.succeed({
1948 update: () => Effect.void,
1949 succeed: () => Effect.void,
1950 fail: () => Effect.void,
1951 close: Effect.void,
1952 }),
1953 approvePlan: () => Effect.succeed(true),
1954 displayPlan: () => Effect.void,
1955 startApplySession: () =>
1956 Effect.succeed({
1957 done: () =>
1958 Effect.sync(() => {
1959 finalized += 1;
1960 }),
1961 emit: () => Effect.void,
1962 }),
1963 });
1964
1965 yield* TestResource("A", { string: "value" }).pipe(
1966 stack.deploy,
1967 hook(failOn("A", "create")),
1968 Effect.provide(Layer.succeed(Cli, cli)),
1969 );
1970
1971 expect(finalized).toBe(1);
1972 }),
1973 );
1974
1975 // Regression: a resource with `precreate` (e.g. Cloudflare Worker) resolves
1976 // its early `ready` signal before its real `reconcile` runs. A non-cyclic
1977 // downstream must still wait for the upstream's TERMINAL output, so that an
1978 // upstream `reconcile` failure interrupts the downstream instead of letting
1979 // it proceed off the precreate stub. Before the fix the downstream raced
1980 // ahead on the precreate identifier and fully created itself even though the
1981 // upstream failed.
1982 test.provider(
1983 "precreate upstream reconcile failure interrupts non-cyclic downstream (stable id dep)",
1984 (stack) =>
1985 Effect.gen(function* () {
1986 const program = Effect.gen(function* () {
1987 const A = yield* PhasedTarget("A", { desired: "a-value", replaceKey: "v1" });
1988 // Only cycle members precreate; the self-binding puts A in one.
1989 yield* A.bind("Self", { env: { SELF: A.stableId } });
1990 // B depends on A.stableId — a value already available from A's
1991 // precreate stub — yet must still be gated on A's reconcile.
1992 const B = yield* TestResource("B", { string: A.stableId });
1993 return { A, B };
1994 });
1995
1996 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
1997
1998 // A's reconcile failed after committing "creating".
1999 expect((yield* getState("A"))?.status).toEqual("creating");
2000 // B must NOT have reached its own reconcile. It may have committed an
2001 // intermediate "creating" while waiting on deps, but it must never be
2002 // "created" — that would mean the upstream failure was ignored.
2003 expect((yield* getState("B"))?.status).not.toEqual("created");
2004
2005 // Recovery deploy converges both.
2006 const output = yield* program.pipe(stack.deploy);
2007 expectConvergedStatus((yield* getState("A"))?.status);
2008 expectConvergedStatus((yield* getState("B"))?.status);
2009 expect(output.B.string).toEqual("stable:v1");
2010 }),
2011 );
2012
2013 test.provider(
2014 "precreate upstream reconcile failure interrupts non-cyclic downstream (value dep)",
2015 (stack) =>
2016 Effect.gen(function* () {
2017 const program = Effect.gen(function* () {
2018 const A = yield* PhasedTarget("A", { desired: "a-value", replaceKey: "v1" });
2019 // Only cycle members precreate; the self-binding puts A in one.
2020 yield* A.bind("Self", { env: { SELF: A.stableId } });
2021 const B = yield* TestResource("B", { string: A.value });
2022 const C = yield* TestResource("C", { string: B.string });
2023 return { A, B, C };
2024 });
2025
2026 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
2027
2028 expect((yield* getState("A"))?.status).toEqual("creating");
2029 expect((yield* getState("B"))?.status).not.toEqual("created");
2030 // Transitive downstream never starts either.
2031 expectNotStarted(yield* getState("C"));
2032
2033 const output = yield* program.pipe(stack.deploy);
2034 expectConvergedStatus((yield* getState("A"))?.status);
2035 expectConvergedStatus((yield* getState("B"))?.status);
2036 expectConvergedStatus((yield* getState("C"))?.status);
2037 expect(output.C.string).toEqual("a-value");
2038 }),
2039 );
2040 });
2041
2042 describe("from created state", { tags: ["unit", "local"] }, () => {
2043 test.provider("noop when props unchanged", (stack) =>
2044 Effect.gen(function* () {
2045 const program = Effect.gen(function* () {
2046 const A = yield* TestResource("A", { string: "test-string" });
2047 return A.string;
2048 });
2049
2050 let output = yield* stack.deploy(program);
2051 expect(output).toEqual("test-string");
2052
2053 expect((yield* getState("A"))?.status).toEqual("created");
2054 output = yield* stack.deploy(program);
2055
2056 // Re-apply with same props - should be noop
2057 expect((yield* getState("A"))?.status).toEqual("created");
2058 expect(output).toEqual("test-string");
2059 }),
2060 );
2061
2062 test.provider("replace when props trigger replacement", (stack) =>
2063 Effect.gen(function* () {
2064 yield* stack.deploy(
2065 Effect.gen(function* () {
2066 const A = yield* TestResource("A", { replaceString: "original" });
2067 return A.replaceString;
2068 }),
2069 );
2070 expect((yield* getState("A"))?.status).toEqual("created");
2071
2072 // Change props that trigger replacement
2073
2074 const output = yield* stack.deploy(
2075 Effect.gen(function* () {
2076 const A = yield* TestResource("A", { replaceString: "new" });
2077 return A.replaceString;
2078 }),
2079 );
2080 expect((yield* getState("A"))?.status).toEqual("created");
2081 expect(output).toEqual("new");
2082 }),
2083 );
2084 });
2085
2086 describe("from updated state", { tags: ["unit", "local"] }, () => {
2087 test.provider("noop when props unchanged", (stack) =>
2088 Effect.gen(function* () {
2089 yield* stack.deploy(
2090 Effect.gen(function* () {
2091 yield* TestResource("A", { string: "test-string" });
2092 }),
2093 );
2094 expect((yield* getState("A"))?.status).toEqual("created");
2095
2096 // Update to get to updated state
2097 yield* stack.deploy(
2098 Effect.gen(function* () {
2099 yield* TestResource("A", { string: "test-string-changed" });
2100 }),
2101 );
2102 expect((yield* getState("A"))?.status).toEqual("updated");
2103
2104 // Re-apply with same props - should be noop
2105 const output = yield* stack.deploy(
2106 Effect.gen(function* () {
2107 const A = yield* TestResource("A", { string: "test-string-changed" });
2108 return A.string;
2109 }),
2110 );
2111 expect((yield* getState("A"))?.status).toEqual("updated");
2112 expect(output).toEqual("test-string-changed");
2113 }),
2114 );
2115
2116 test.provider("replace when props trigger replacement", (stack) =>
2117 Effect.gen(function* () {
2118 yield* stack.deploy(
2119 Effect.gen(function* () {
2120 yield* TestResource("A", { string: "test-string", replaceString: "original" });
2121 }),
2122 );
2123 expect((yield* getState("A"))?.status).toEqual("created");
2124
2125 // Update to get to updated state
2126 yield* stack.deploy(
2127 Effect.gen(function* () {
2128 yield* TestResource("A", { string: "test-string-changed", replaceString: "original" });
2129 }),
2130 );
2131 expect((yield* getState("A"))?.status).toEqual("updated");
2132
2133 // Change props that trigger replacement
2134 const output = yield* stack.deploy(
2135 Effect.gen(function* () {
2136 const A = yield* TestResource("A", {
2137 string: "test-string-changed",
2138 replaceString: "new",
2139 });
2140 return A.replaceString;
2141 }),
2142 );
2143 expect((yield* getState("A"))?.status).toEqual("created");
2144 expect(output).toEqual("new");
2145 }),
2146 );
2147 });
2148
2149 describe("from creating state", { tags: ["unit", "local"] }, () => {
2150 test.provider("continue creating when props unchanged", (stack) =>
2151 Effect.gen(function* () {
2152 yield* Effect.gen(function* () {
2153 yield* TestResource("A", { string: "test-string" });
2154 }).pipe(stack.deploy, hook());
2155 expect((yield* getState("A"))?.status).toEqual("creating");
2156
2157 const output = yield* Effect.gen(function* () {
2158 const A = yield* TestResource("A", { string: "test-string" });
2159 return A.string;
2160 }).pipe(stack.deploy);
2161 expect((yield* getState("A"))?.status).toEqual("created");
2162 expect(output).toEqual("test-string");
2163 }),
2164 );
2165
2166 test.provider("continue creating when props have updatable changes", (stack) =>
2167 Effect.gen(function* () {
2168 yield* Effect.gen(function* () {
2169 yield* TestResource("A", { string: "test-string" });
2170 }).pipe(stack.deploy, hook());
2171 expect((yield* getState("A"))?.status).toEqual("creating");
2172
2173 const output = yield* Effect.gen(function* () {
2174 const A = yield* TestResource("A", { string: "test-string-changed" });
2175 return A.string;
2176 }).pipe(stack.deploy);
2177 expect(output).toEqual("test-string-changed");
2178 expect((yield* getState("A"))?.status).toEqual("created");
2179 }),
2180 );
2181
2182 test.provider("replace when props trigger replacement", (stack) =>
2183 Effect.gen(function* () {
2184 yield* Effect.gen(function* () {
2185 yield* TestResource("A", { replaceString: "test-string" });
2186 }).pipe(stack.deploy, hook());
2187 expect((yield* getState("A"))?.status).toEqual("creating");
2188
2189 const output = yield* Effect.gen(function* () {
2190 const A = yield* TestResource("A", { replaceString: "test-string-changed" });
2191 return A.replaceString;
2192 }).pipe(stack.deploy);
2193 expect(output).toEqual("test-string-changed");
2194 expect((yield* getState("A"))?.status).toEqual("created");
2195 }),
2196 );
2197
2198 test.provider("destroy should handle creating state with no attributes", (stack) =>
2199 Effect.gen(function* () {
2200 // 1. Create a resource but fail - this leaves state in "creating" with no attr
2201 yield* Effect.gen(function* () {
2202 yield* TestResource("A", { string: "test-string" });
2203 }).pipe(stack.deploy, hook());
2204 expect((yield* getState("A"))?.status).toEqual("creating");
2205 expect((yield* getState("A"))?.attr).toBeUndefined();
2206
2207 // 2. Call destroy - this triggers collectGarbage which tries to delete
2208 // the orphaned resource. The bug is that output is undefined in the
2209 // delete call when the resource never completed creation.
2210 yield* stack.destroy();
2211
2212 // Resource should be cleaned up
2213 expect(yield* getState("A")).toBeUndefined();
2214 }),
2215 );
2216
2217 test.provider("destroy should handle creating state when attributes can be recovered", (stack) =>
2218 Effect.gen(function* () {
2219 yield* Effect.gen(function* () {
2220 yield* TestResource("A", { string: "test-string" });
2221 }).pipe(stack.deploy, hook());
2222 expect((yield* getState("A"))?.status).toEqual("creating");
2223 expect((yield* getState("A"))?.attr).toBeUndefined();
2224
2225 yield* stack.destroy().pipe(
2226 hook({
2227 delete: () => Effect.fail(new ResourceFailure()),
2228 read: () => Effect.succeed({ string: "test-string" }),
2229 }),
2230 );
2231
2232 // Resource should be cleaned up
2233 expect((yield* getState("A"))?.status).toEqual("deleting");
2234
2235 // actually delete this time
2236 yield* stack.destroy().pipe(hook({ read: () => Effect.succeed({ string: "test-string" }) }));
2237
2238 expect(yield* getState("A")).toBeUndefined();
2239 }),
2240 );
2241
2242 test.provider(
2243 "destroy should handle replacing state when old resource has no attributes",
2244 (stack) =>
2245 Effect.gen(function* () {
2246 // 1. Create a resource but fail - this leaves state in "creating" with no attr
2247 yield* Effect.gen(function* () {
2248 yield* TestResource("A", { replaceString: "original" });
2249 }).pipe(stack.deploy, hook());
2250 expect((yield* getState("A"))?.status).toEqual("creating");
2251 expect((yield* getState("A"))?.attr).toBeUndefined();
2252
2253 // 2. Trigger replacement but also fail during create - this leaves state in "replacing"
2254 // with old.attr being undefined
2255 yield* Effect.gen(function* () {
2256 yield* TestResource("A", { replaceString: "new" });
2257 }).pipe(stack.deploy, hook());
2258 const state = yield* getState<ReplacingResourceState>("A");
2259 expect(state?.status).toEqual("replacing");
2260 expect(state?.old?.attr).toBeUndefined();
2261
2262 // 3. Call destroy - this triggers collectGarbage which tries to delete
2263 // the resource. The bug is that old.attr is undefined.
2264 yield* stack
2265 .destroy()
2266 .pipe(hook({ read: () => Effect.succeed({ replaceString: "original" }) }));
2267
2268 // Resource should be cleaned up
2269 expect(yield* getState("A")).toBeUndefined();
2270 }),
2271 );
2272
2273 // ── attr-less `creating` rows must not orphan the physical resource ──
2274 //
2275 // A create can be interrupted AFTER the cloud-side call succeeded but
2276 // BEFORE reconcile returned Attributes, leaving a `creating` row with
2277 // `attr === undefined`. Destroy used to skip `provider.delete` entirely
2278 // for such rows and drop the state, silently orphaning the physical
2279 // resource. The engine now read-then-deletes: `provider.read` recovers
2280 // the attributes from the persisted props (deterministic physical name),
2281 // and only a confirmed-missing or Unowned resource skips the delete.
2282
2283 test.provider(
2284 "destroy deletes the recovered physical resource of an attr-less creating row",
2285 (stack) =>
2286 Effect.gen(function* () {
2287 // Interrupted create: cloud-side create "succeeded" but no attrs
2288 // were ever persisted.
2289 yield* Effect.gen(function* () {
2290 yield* TestResource("A", { string: "test-string" });
2291 }).pipe(stack.deploy, hook());
2292 expect((yield* getState("A"))?.status).toEqual("creating");
2293 expect((yield* getState("A"))?.attr).toBeUndefined();
2294
2295 const deleted: string[] = [];
2296 yield* stack.destroy().pipe(
2297 hook({
2298 read: () => Effect.succeed({ string: "test-string" }),
2299 delete: (id) => Effect.sync(() => void deleted.push(id)),
2300 }),
2301 );
2302
2303 // The physical resource recovered by `read` was actually deleted —
2304 // not silently orphaned.
2305 expect(deleted).toEqual(["A"]);
2306 expect(yield* getState("A")).toBeUndefined();
2307 }),
2308 );
2309
2310 test.provider("destroy never deletes a recovered resource that is Unowned", (stack) =>
2311 Effect.gen(function* () {
2312 yield* Effect.gen(function* () {
2313 yield* TestResource("A", { string: "test-string" });
2314 }).pipe(stack.deploy, hook());
2315 expect((yield* getState("A"))?.status).toEqual("creating");
2316
2317 const deleted: string[] = [];
2318 yield* stack.destroy().pipe(
2319 hook({
2320 // The physical name exists but belongs to someone else — our
2321 // interrupted create actually lost a name race (or died before
2322 // stamping ownership).
2323 read: () => Effect.succeed(Unowned({ string: "foreign" })),
2324 delete: (id) => Effect.sync(() => void deleted.push(id)),
2325 }),
2326 );
2327
2328 // Foreign resources are left in place; only our state is dropped.
2329 expect(deleted).toEqual([]);
2330 expect(yield* getState("A")).toBeUndefined();
2331 }),
2332 );
2333
2334 test.provider("destroy tolerates not-found for an attr-less creating row", (stack) =>
2335 Effect.gen(function* () {
2336 yield* Effect.gen(function* () {
2337 yield* TestResource("A", { string: "test-string" });
2338 }).pipe(stack.deploy, hook());
2339 expect((yield* getState("A"))?.status).toEqual("creating");
2340
2341 const deleted: string[] = [];
2342 yield* stack.destroy().pipe(
2343 hook({
2344 read: () => Effect.succeed(undefined),
2345 delete: (id) => Effect.sync(() => void deleted.push(id)),
2346 }),
2347 );
2348
2349 // Nothing exists cloud-side — delete is not invoked, state is dropped.
2350 expect(deleted).toEqual([]);
2351 expect(yield* getState("A")).toBeUndefined();
2352 }),
2353 );
2354
2355 test.provider(
2356 "destroy survives a recovery read that crashes on degraded creating props",
2357 (stack) =>
2358 Effect.gen(function* () {
2359 // An interrupted create can persist `creating` props whose
2360 // unresolved Outputs were stripped to holes; a provider read that
2361 // dereferences one crashes with a defect (e.g. a SchemaError deep
2362 // in its SDK client, see #995). Destroy must degrade to "nothing
2363 // recovered" and drop the row instead of bricking the stage.
2364 yield* Effect.gen(function* () {
2365 yield* TestResource("A", { string: "test-string" });
2366 }).pipe(stack.deploy, hook());
2367 expect((yield* getState("A"))?.status).toEqual("creating");
2368
2369 const deleted: string[] = [];
2370 yield* stack.destroy().pipe(
2371 hook({
2372 read: () => Effect.die(new Error("SchemaError: Expected string, got undefined")),
2373 delete: (id) => Effect.sync(() => void deleted.push(id)),
2374 }),
2375 );
2376
2377 // Recovery failed — delete is not invoked, state is still dropped.
2378 expect(deleted).toEqual([]);
2379 expect(yield* getState("A")).toBeUndefined();
2380 }),
2381 );
2382
2383 test.provider("destroy drops an attr-less creating row when the provider has no read", (stack) =>
2384 Effect.gen(function* () {
2385 // Manufacture the interrupted-create row directly: Test.Queue's
2386 // provider implements no `read`, so recovery is impossible and the
2387 // engine can only drop the row (surfacing a note, not crashing).
2388 const state = yield* yield* State;
2389 const stk = yield* Stack;
2390 yield* state.set({
2391 stack: stk.name,
2392 stage: stk.stage,
2393 fqn: "Q",
2394 value: {
2395 kind: "resource",
2396 status: "creating",
2397 resourceType: "Test.Queue",
2398 namespace: undefined,
2399 fqn: "Q",
2400 logicalId: "Q",
2401 instanceId: "q-instance",
2402 providerVersion: 0,
2403 downstream: [],
2404 bindings: [],
2405 props: { name: "q" },
2406 } satisfies CreatingResourceState,
2407 });
2408
2409 yield* stack.destroy();
2410 expect(yield* getState("Q")).toBeUndefined();
2411 }),
2412 );
2413
2414 test.provider("replacement drain recovers and deletes an attr-less old generation", (stack) =>
2415 Effect.gen(function* () {
2416 // 1. Interrupted create — `creating` row with no attr.
2417 yield* Effect.gen(function* () {
2418 yield* TestResource("A", { replaceString: "original" });
2419 }).pipe(stack.deploy, hook());
2420 expect((yield* getState("A"))?.status).toEqual("creating");
2421 expect((yield* getState("A"))?.attr).toBeUndefined();
2422
2423 // 2. Deploy a replacement. The first `read` (plan-time create-resume
2424 // probe) reports not-found so the engine plans a replacement instead
2425 // of resuming the create; the drain-time `read` then discovers the
2426 // physical resource the interrupted create actually made, and the
2427 // engine must delete it while draining the replaced old generation.
2428 const deleted: string[] = [];
2429 let reads = 0;
2430 yield* Effect.gen(function* () {
2431 yield* TestResource("A", { replaceString: "new" });
2432 }).pipe(
2433 stack.deploy,
2434 hook({
2435 read: () =>
2436 Effect.sync(() => ++reads).pipe(
2437 Effect.map((n) => (n === 1 ? undefined : { replaceString: "original" })),
2438 ),
2439 delete: (id) => Effect.sync(() => void deleted.push(id)),
2440 }),
2441 );
2442
2443 // The old generation's physical resource was recovered and deleted,
2444 // and the replacement collapsed to a stable `created` row.
2445 expect(deleted).toEqual(["A"]);
2446 expect((yield* getState("A"))?.status).toEqual("created");
2447 expect(((yield* getState("A"))?.attr as TestResourceProps)?.replaceString).toEqual("new");
2448 }),
2449 );
2450
2451 test.provider(
2452 "resuming an interrupted create fails loudly when the recovered resource is Unowned",
2453 (stack) =>
2454 Effect.gen(function* () {
2455 yield* Effect.gen(function* () {
2456 yield* TestResource("A", { string: "test-string" });
2457 }).pipe(stack.deploy, hook());
2458 expect((yield* getState("A"))?.status).toEqual("creating");
2459
2460 const exit = yield* Effect.gen(function* () {
2461 yield* TestResource("A", { string: "test-string" });
2462 }).pipe(
2463 stack.deploy,
2464 hook({ read: () => Effect.succeed(Unowned({ string: "foreign" })) }),
2465 Effect.exit,
2466 );
2467
2468 expect(Exit.isFailure(exit)).toBe(true);
2469 if (Exit.isFailure(exit)) {
2470 const reason = exit.cause.reasons.find(Cause.isFailReason);
2471 expect((reason?.error as any)?._tag).toBe("OwnedBySomeoneElse");
2472 }
2473 // The row is untouched — the user can re-run with --adopt.
2474 expect((yield* getState("A"))?.status).toEqual("creating");
2475 }),
2476 );
2477
2478 test.provider(
2479 "resuming an interrupted create with adopt(true) takes over an Unowned resource",
2480 (stack) =>
2481 Effect.gen(function* () {
2482 yield* Effect.gen(function* () {
2483 yield* TestResource("A", { string: "test-string" });
2484 }).pipe(stack.deploy, hook());
2485 expect((yield* getState("A"))?.status).toEqual("creating");
2486
2487 yield* Effect.gen(function* () {
2488 yield* TestResource("A", { string: "test-string" });
2489 }).pipe(
2490 adopt(true),
2491 stack.deploy,
2492 hook({ read: () => Effect.succeed(Unowned({ string: "test-string" })) }),
2493 );
2494
2495 const persisted = yield* getState("A");
2496 expect(persisted?.status).toEqual("created");
2497 // The Unowned brand never reaches persisted state.
2498 expect(Unowned.is(persisted?.attr)).toBe(false);
2499 }),
2500 );
2501 });
2502
2503 describe("from updating state", { tags: ["unit", "local"] }, () => {
2504 test.provider("continue updating when props unchanged", (stack) =>
2505 Effect.gen(function* () {
2506 yield* Effect.gen(function* () {
2507 yield* TestResource("A", { string: "test-string" });
2508 }).pipe(stack.deploy);
2509 expect((yield* getState("A"))?.status).toEqual("created");
2510
2511 yield* Effect.gen(function* () {
2512 yield* TestResource("A", { string: "test-string-changed" });
2513 }).pipe(stack.deploy, hook({ update: () => Effect.fail(new ResourceFailure()) }));
2514 expect((yield* getState("A"))?.status).toEqual("updating");
2515
2516 const output = yield* Effect.gen(function* () {
2517 const A = yield* TestResource("A", { string: "test-string-changed" });
2518 return A.string;
2519 }).pipe(stack.deploy);
2520 expect((yield* getState("A"))?.status).toEqual("updated");
2521 expect(output).toEqual("test-string-changed");
2522 }),
2523 );
2524
2525 test.provider("continue updating when props have updatable changes", (stack) =>
2526 Effect.gen(function* () {
2527 yield* Effect.gen(function* () {
2528 yield* TestResource("A", { string: "test-string" });
2529 }).pipe(stack.deploy);
2530 expect((yield* getState("A"))?.status).toEqual("created");
2531
2532 yield* Effect.gen(function* () {
2533 yield* TestResource("A", { string: "test-string-changed" });
2534 }).pipe(stack.deploy, hook({ update: () => Effect.fail(new ResourceFailure()) }));
2535 expect((yield* getState("A"))?.status).toEqual("updating");
2536
2537 const output = yield* Effect.gen(function* () {
2538 const A = yield* TestResource("A", { string: "test-string-changed-again" });
2539 return A.string;
2540 }).pipe(stack.deploy);
2541 expect((yield* getState("A"))?.status).toEqual("updated");
2542 expect(output).toEqual("test-string-changed-again");
2543 }),
2544 );
2545
2546 test.provider("replace when props trigger replacement", (stack) =>
2547 Effect.gen(function* () {
2548 yield* Effect.gen(function* () {
2549 yield* TestResource("A", { string: "test-string", replaceString: "original" });
2550 }).pipe(stack.deploy);
2551 expect((yield* getState("A"))?.status).toEqual("created");
2552
2553 yield* Effect.gen(function* () {
2554 yield* TestResource("A", { string: "test-string-changed", replaceString: "original" });
2555 }).pipe(stack.deploy, hook({ update: () => Effect.fail(new ResourceFailure()) }));
2556 expect((yield* getState("A"))?.status).toEqual("updating");
2557
2558 const output = yield* Effect.gen(function* () {
2559 const A = yield* TestResource("A", {
2560 string: "test-string-changed",
2561 replaceString: "changed",
2562 });
2563 return A.replaceString;
2564 }).pipe(stack.deploy);
2565 expect((yield* getState("A"))?.status).toEqual("created");
2566 expect(output).toEqual("changed");
2567 }),
2568 );
2569 });
2570
2571 describe("from replacing state", { tags: ["unit", "local"] }, () => {
2572 test.provider("continue replacement when props unchanged", (stack) =>
2573 Effect.gen(function* () {
2574 // 1. Create initial resource
2575 yield* Effect.gen(function* () {
2576 yield* TestResource("A", { replaceString: "original" });
2577 }).pipe(stack.deploy);
2578 expect((yield* getState("A"))?.status).toEqual("created");
2579
2580 // 2. Trigger replacement but fail during create of replacement
2581 yield* Effect.gen(function* () {
2582 yield* TestResource("A", { replaceString: "new" });
2583 }).pipe(stack.deploy, hook({ create: () => Effect.fail(new ResourceFailure()) }));
2584 const state = yield* getState<ReplacingResourceState>("A");
2585 expect(state?.status).toEqual("replacing");
2586 expect(state?.old?.status).toEqual("created");
2587
2588 // 3. Re-apply with same props - should continue replacement
2589 const output = yield* Effect.gen(function* () {
2590 const A = yield* TestResource("A", { replaceString: "new" });
2591 return A.replaceString;
2592 }).pipe(stack.deploy);
2593 expect((yield* getState("A"))?.status).toEqual("created");
2594 expect(output).toEqual("new");
2595 }),
2596 );
2597
2598 test.provider("continue replacement when props have updatable changes", (stack) =>
2599 Effect.gen(function* () {
2600 // 1. Create initial resource
2601 yield* Effect.gen(function* () {
2602 yield* TestResource("A", { replaceString: "original", string: "initial" });
2603 }).pipe(stack.deploy);
2604 expect((yield* getState("A"))?.status).toEqual("created");
2605
2606 // 2. Trigger replacement but fail during create
2607 yield* Effect.gen(function* () {
2608 yield* TestResource("A", { replaceString: "new", string: "initial" });
2609 }).pipe(stack.deploy, hook({ create: () => Effect.fail(new ResourceFailure()) }));
2610 expect((yield* getState("A"))?.status).toEqual("replacing");
2611
2612 // 3. Re-apply with changed props (updatable) - should continue replacement with new props
2613 const output = yield* Effect.gen(function* () {
2614 const A = yield* TestResource("A", { replaceString: "new", string: "changed" });
2615 return { replaceString: A.replaceString, string: A.string };
2616 }).pipe(stack.deploy);
2617 expect((yield* getState("A"))?.status).toEqual("created");
2618 expect(output.replaceString).toEqual("new");
2619 expect(output.string).toEqual("changed");
2620 }),
2621 );
2622
2623 test.provider("continue replacement when props trigger another replacement", (stack) =>
2624 Effect.gen(function* () {
2625 // 1. Create initial resource
2626 yield* Effect.gen(function* () {
2627 yield* TestResource("A", { replaceString: "original" });
2628 }).pipe(stack.deploy);
2629 expect((yield* getState("A"))?.status).toEqual("created");
2630
2631 // 2. Trigger replacement but fail during create
2632 yield* Effect.gen(function* () {
2633 yield* TestResource("A", { replaceString: "new" });
2634 }).pipe(stack.deploy, hook({ create: () => Effect.fail(new ResourceFailure()) }));
2635 expect((yield* getState("A"))?.status).toEqual("replacing");
2636
2637 // 3. Replace again with another replacement - should converge
2638 const output = yield* Effect.gen(function* () {
2639 const A = yield* TestResource("A", { replaceString: "another-replacement" });
2640 return A.replaceString;
2641 }).pipe(stack.deploy);
2642 expectConvergedStatus((yield* getState("A"))?.status);
2643 expect(output).toEqual("another-replacement");
2644 }),
2645 );
2646 });
2647
2648 describe("from replaced state", { tags: ["unit", "local"] }, () => {
2649 test.provider("continue cleanup when props unchanged", (stack) =>
2650 Effect.gen(function* () {
2651 yield* Effect.gen(function* () {
2652 yield* TestResource("A", { replaceString: "test-string" });
2653 }).pipe(stack.deploy);
2654 expect((yield* getState("A"))?.status).toEqual("created");
2655
2656 yield* Effect.gen(function* () {
2657 yield* TestResource("A", { replaceString: "test-string-changed" });
2658 }).pipe(stack.deploy, hook({ delete: () => Effect.fail(new ResourceFailure()) }));
2659 const AState = yield* getState<ReplacedResourceState>("A");
2660 expect(AState?.status).toEqual("replaced");
2661 expect(AState?.old).toMatchObject({
2662 status: "created",
2663 props: { replaceString: "test-string" },
2664 });
2665
2666 yield* Effect.gen(function* () {
2667 yield* TestResource("A", { replaceString: "test-string-changed" });
2668 }).pipe(stack.deploy);
2669 expect((yield* getState("A"))?.status).toEqual("created");
2670 }),
2671 );
2672
2673 test.provider("update replacement then cleanup when props have updatable changes", (stack) =>
2674 Effect.gen(function* () {
2675 // 1. Create initial resource
2676 yield* Effect.gen(function* () {
2677 yield* TestResource("A", { replaceString: "original", string: "initial" });
2678 }).pipe(stack.deploy);
2679 expect((yield* getState("A"))?.status).toEqual("created");
2680
2681 // 2. Trigger replacement and fail during delete of old resource
2682 yield* Effect.gen(function* () {
2683 yield* TestResource("A", { replaceString: "new", string: "initial" });
2684 }).pipe(stack.deploy, hook({ delete: () => Effect.fail(new ResourceFailure()) }));
2685 const state = yield* getState<ReplacedResourceState>("A");
2686 expect(state?.status).toEqual("replaced");
2687 expect(state?.old?.status).toEqual("created");
2688
2689 // 3. Change props again (updatable change) - should update the replacement then cleanup
2690 const output = yield* Effect.gen(function* () {
2691 const A = yield* TestResource("A", { replaceString: "new", string: "changed" });
2692 return { replaceString: A.replaceString, string: A.string };
2693 }).pipe(stack.deploy);
2694 expect((yield* getState("A"))?.status).toEqual("created");
2695 expect(output.replaceString).toEqual("new");
2696 expect(output.string).toEqual("changed");
2697 }),
2698 );
2699
2700 test.provider("continue cleanup when props trigger another replacement", (stack) =>
2701 Effect.gen(function* () {
2702 // 1. Create initial resource
2703 yield* Effect.gen(function* () {
2704 yield* TestResource("A", { replaceString: "original" });
2705 }).pipe(stack.deploy);
2706 expect((yield* getState("A"))?.status).toEqual("created");
2707
2708 // 2. Trigger replacement and fail during delete of old resource
2709 yield* Effect.gen(function* () {
2710 yield* TestResource("A", { replaceString: "new" });
2711 }).pipe(stack.deploy, hook({ delete: () => Effect.fail(new ResourceFailure()) }));
2712 expect((yield* getState("A"))?.status).toEqual("replaced");
2713
2714 // 3. Replace again and continue cleanup of the older generations
2715 const output = yield* Effect.gen(function* () {
2716 const A = yield* TestResource("A", { replaceString: "another-replacement" });
2717 return A.replaceString;
2718 }).pipe(stack.deploy);
2719 expectConvergedStatus((yield* getState("A"))?.status);
2720 expect(output).toEqual("another-replacement");
2721 }),
2722 );
2723 });
2724
2725 describe("retain removal policy on replace", { tags: ["unit", "local"] }, () => {
2726 test.provider("replace with retain does not delete the old generation", (stack) =>
2727 Effect.gen(function* () {
2728 const deleted: string[] = [];
2729
2730 // 1. Create initial resource with a retain removal policy.
2731 yield* Effect.gen(function* () {
2732 yield* TestResource("A", { replaceString: "v1" }).pipe(RemovalPolicy.retain(true));
2733 }).pipe(stack.deploy);
2734
2735 const before = yield* getState("A");
2736 expect(before?.status).toEqual("created");
2737 const oldInstanceId = before?.instanceId;
2738
2739 // 2. Trigger a replacement (replaceString change). The old generation
2740 // must NOT be deleted because the resource is retained.
2741 const output = yield* Effect.gen(function* () {
2742 const A = yield* TestResource("A", { replaceString: "v2" }).pipe(
2743 RemovalPolicy.retain(true),
2744 );
2745 return A.replaceString;
2746 }).pipe(
2747 stack.deploy,
2748 hook({
2749 delete: (id) =>
2750 Effect.sync(() => {
2751 deleted.push(id);
2752 }),
2753 }),
2754 );
2755
2756 expect(output).toEqual("v2");
2757 // provider.delete must never fire for the retained old generation.
2758 expect(deleted).not.toContain("A");
2759
2760 const after = yield* getState("A");
2761 // Resource was genuinely replaced (fresh instance id) and the old
2762 // chain drained back to a terminal `created` state.
2763 expect(after?.status).toEqual("created");
2764 expect(after?.instanceId).not.toEqual(oldInstanceId);
2765 }),
2766 );
2767
2768 test.provider("replace without retain deletes the old generation exactly once", (stack) =>
2769 Effect.gen(function* () {
2770 const deleted: string[] = [];
2771
2772 // 1. Create initial resource with the default (destroy) policy.
2773 yield* Effect.gen(function* () {
2774 yield* TestResource("A", { replaceString: "v1" });
2775 }).pipe(stack.deploy);
2776 expect((yield* getState("A"))?.status).toEqual("created");
2777
2778 // 2. Trigger a replacement. The old generation must be deleted since
2779 // the resource is not retained — guards the retain patch against
2780 // disabling normal replacement GC.
2781 const output = yield* Effect.gen(function* () {
2782 const A = yield* TestResource("A", { replaceString: "v2" });
2783 return A.replaceString;
2784 }).pipe(
2785 stack.deploy,
2786 hook({
2787 delete: (id) =>
2788 Effect.sync(() => {
2789 deleted.push(id);
2790 }),
2791 }),
2792 );
2793
2794 expect(output).toEqual("v2");
2795 expect(deleted.filter((id) => id === "A")).toHaveLength(1);
2796 expect((yield* getState("A"))?.status).toEqual("created");
2797 }),
2798 );
2799
2800 test.provider("nested replacement chain with retain never deletes old generations", (stack) =>
2801 Effect.gen(function* () {
2802 const deleted: string[] = [];
2803
2804 // 1. Create with retain.
2805 yield* Effect.gen(function* () {
2806 yield* TestResource("A", { replaceString: "v1" }).pipe(RemovalPolicy.retain(true));
2807 }).pipe(stack.deploy);
2808 expect((yield* getState("A"))?.status).toEqual("created");
2809
2810 // 2. Trigger a replacement but fail mid-create so a replacement chain
2811 // forms (replacing, with old=created still live).
2812 yield* Effect.gen(function* () {
2813 yield* TestResource("A", { replaceString: "v2" }).pipe(RemovalPolicy.retain(true));
2814 }).pipe(stack.deploy, hook({ create: () => Effect.fail(new ResourceFailure()) }));
2815 expect((yield* getState("A"))?.status).toEqual("replacing");
2816
2817 // 3. Replace again — converges and drains the entire old chain. Every
2818 // old generation must be retained (no provider.delete calls).
2819 const output = yield* Effect.gen(function* () {
2820 const A = yield* TestResource("A", { replaceString: "v3" }).pipe(
2821 RemovalPolicy.retain(true),
2822 );
2823 return A.replaceString;
2824 }).pipe(
2825 stack.deploy,
2826 hook({
2827 delete: (id) =>
2828 Effect.sync(() => {
2829 deleted.push(id);
2830 }),
2831 }),
2832 );
2833
2834 expect(output).toEqual("v3");
2835 expect(deleted).not.toContain("A");
2836 expect((yield* getState("A"))?.status).toEqual("created");
2837 }),
2838 );
2839
2840 test.provider("orphan delete still honors retain (regression guard)", (stack) =>
2841 Effect.gen(function* () {
2842 const deleted: string[] = [];
2843 const events: Array<{ id: string; status: string }> = [];
2844
2845 yield* Effect.gen(function* () {
2846 yield* TestResource("A", { string: "v1" }).pipe(RemovalPolicy.retain(true));
2847 }).pipe(stack.deploy);
2848 expect((yield* getState("A"))?.status).toEqual("created");
2849
2850 const plan = yield* Effect.void.pipe(stack.plan);
2851 expect(plan.deletions.A?.action).toBe("orphaned");
2852
2853 // Destroy removes the resource from the stack. The explicit orphaned
2854 // action must skip provider.delete and just drop state.
2855 yield* stack.destroy().pipe(
2856 hook({
2857 delete: (id) =>
2858 Effect.sync(() => {
2859 deleted.push(id);
2860 }),
2861 }),
2862 Effect.provide(Layer.succeed(Cli, recordingCli(events))),
2863 );
2864
2865 expect(deleted).not.toContain("A");
2866 expect(yield* getState("A")).toBeUndefined();
2867 expect(events.filter((event) => event.id === "A").map((event) => event.status)).toEqual([
2868 "orphaning",
2869 "orphaned",
2870 ]);
2871 }),
2872 );
2873
2874 test.provider(
2875 "retain added to an already-created resource is persisted by the noop deploy",
2876 (stack) =>
2877 Effect.gen(function* () {
2878 // 1. Create with the default (destroy) policy.
2879 yield* Effect.gen(function* () {
2880 yield* TestResource("A", { string: "v1" });
2881 }).pipe(stack.deploy);
2882 expect((yield* getState("A"))?.removalPolicy).toEqual("destroy");
2883
2884 // 2. Add `retain` — props are otherwise identical, so the resource
2885 // plans as a noop. The policy is a declaration decoration, not a
2886 // prop, so nothing about it can produce a diff; the noop path is
2887 // the only pass that ever sees the change.
2888 const declaration = Effect.gen(function* () {
2889 yield* TestResource("A", { string: "v1" }).pipe(RemovalPolicy.retain(true));
2890 });
2891 const plan = yield* declaration.pipe(stack.plan);
2892 expect(actionOfPlan(plan, "A")).toEqual("noop");
2893 yield* declaration.pipe(stack.deploy);
2894 expect((yield* getState("A"))?.removalPolicy).toEqual("retain");
2895
2896 // 3. Remove the declaration — the orphan sweep reads the policy from
2897 // state, so the provider's delete must never fire.
2898 const deleted: string[] = [];
2899 yield* stack.destroy().pipe(
2900 hook({
2901 delete: (id) =>
2902 Effect.sync(() => {
2903 deleted.push(id);
2904 }),
2905 }),
2906 );
2907 expect(deleted).not.toContain("A");
2908 expect(yield* getState("A")).toBeUndefined();
2909 }),
2910 );
2911
2912 test.provider(
2913 "retain removed from an already-created resource is persisted by the noop deploy",
2914 (stack) =>
2915 Effect.gen(function* () {
2916 // The inverse direction: a resource that was retained and is now
2917 // declared `destroy` must actually be deleted by the orphan sweep.
2918 yield* Effect.gen(function* () {
2919 yield* TestResource("A", { string: "v1" }).pipe(RemovalPolicy.retain(true));
2920 }).pipe(stack.deploy);
2921 expect((yield* getState("A"))?.removalPolicy).toEqual("retain");
2922
2923 yield* Effect.gen(function* () {
2924 yield* TestResource("A", { string: "v1" });
2925 }).pipe(stack.deploy);
2926 expect((yield* getState("A"))?.removalPolicy).toEqual("destroy");
2927
2928 const deleted: string[] = [];
2929 yield* stack.destroy().pipe(
2930 hook({
2931 delete: (id) =>
2932 Effect.sync(() => {
2933 deleted.push(id);
2934 }),
2935 }),
2936 );
2937 expect(deleted).toContain("A");
2938 expect(yield* getState("A")).toBeUndefined();
2939 }),
2940 );
2941 });
2942
2943 describe("replaced generation whose delete is in progress", { tags: ["unit", "local"] }, () => {
2944 const inProgress = () =>
2945 Effect.fail(new Provider.DeleteInProgress({ message: "still draining" }));
2946
2947 test.provider("does not fail the apply and is deleted on the next apply", (stack) =>
2948 Effect.gen(function* () {
2949 yield* Effect.gen(function* () {
2950 yield* TestResource("A", { replaceString: "v1" });
2951 }).pipe(stack.deploy);
2952
2953 const output = yield* Effect.gen(function* () {
2954 const A = yield* TestResource("A", { replaceString: "v2" });
2955 return A.replaceString;
2956 }).pipe(stack.deploy, Effect.provideService(TestResourceHooks, { delete: inProgress }));
2957 expect(output).toEqual("v2");
2958 const pending = yield* getState<ReplacedResourceState>("A");
2959 expect(pending.status).toEqual("replaced");
2960 expect(pending.old.attr?.replaceString).toEqual("v1");
2961
2962 const deleted: string[] = [];
2963 yield* Effect.gen(function* () {
2964 yield* TestResource("A", { replaceString: "v2" });
2965 }).pipe(
2966 stack.deploy,
2967 Effect.provideService(TestResourceHooks, {
2968 delete: (id) => Effect.sync(() => void deleted.push(id)),
2969 }),
2970 );
2971 expect(deleted).toEqual(["A"]);
2972 expect((yield* getState("A"))?.status).toEqual("created");
2973 }),
2974 );
2975
2976 test.provider("still deletes older generations behind it", (stack) =>
2977 Effect.gen(function* () {
2978 yield* Effect.gen(function* () {
2979 yield* TestResource("A", { replaceString: "v1" });
2980 }).pipe(stack.deploy);
2981 yield* Effect.gen(function* () {
2982 yield* TestResource("A", { replaceString: "v2" });
2983 }).pipe(stack.deploy, Effect.provideService(TestResourceHooks, { delete: inProgress }));
2984
2985 // v2 is newest and still draining, so v1 behind it must be the one deleted.
2986 let deletes = 0;
2987 yield* Effect.gen(function* () {
2988 yield* TestResource("A", { replaceString: "v3" });
2989 }).pipe(
2990 stack.deploy,
2991 Effect.provideService(TestResourceHooks, {
2992 delete: () => (deletes++ === 0 ? inProgress() : Effect.void),
2993 }),
2994 );
2995 expect(deletes).toEqual(2);
2996 const state = yield* getState<ReplacedResourceState>("A");
2997 expect(state.status).toEqual("replaced");
2998 expect(state.attr.replaceString).toEqual("v3");
2999 expect(state.old.status).toEqual("created");
3000 expect(state.old.attr?.replaceString).toEqual("v2");
3001
3002 yield* Effect.gen(function* () {
3003 yield* TestResource("A", { replaceString: "v3" });
3004 }).pipe(stack.deploy);
3005 expect((yield* getState("A"))?.status).toEqual("created");
3006 }),
3007 );
3008
3009 test.provider("fails destroy and keeps the resource in state", (stack) =>
3010 Effect.gen(function* () {
3011 yield* Effect.gen(function* () {
3012 yield* TestResource("A", { replaceString: "v1" });
3013 }).pipe(stack.deploy);
3014 yield* Effect.gen(function* () {
3015 yield* TestResource("A", { replaceString: "v2" });
3016 }).pipe(stack.deploy, Effect.provideService(TestResourceHooks, { delete: inProgress }));
3017
3018 const error = yield* stack
3019 .destroy()
3020 .pipe(Effect.provideService(TestResourceHooks, { delete: inProgress }), Effect.flip);
3021 expect(error).toBeInstanceOf(DestroyError);
3022 expect((yield* getState("A"))?.status).toEqual("replaced");
3023 }),
3024 );
3025 });
3026
3027 describe("from deleting state", { tags: ["unit", "local"] }, () => {
3028 test.provider("create when props unchanged or have updatable changes", (stack) =>
3029 Effect.gen(function* () {
3030 yield* Effect.gen(function* () {
3031 yield* TestResource("A", { string: "test-string" });
3032 }).pipe(stack.deploy);
3033 expect((yield* getState("A"))?.status).toEqual("created");
3034
3035 yield* stack.destroy().pipe(hook({ delete: () => Effect.fail(new ResourceFailure()) }));
3036 expect((yield* getState("A"))?.status).toEqual("deleting");
3037
3038 // Now re-apply with the same props - should create the resource again
3039 const output = yield* Effect.gen(function* () {
3040 const A = yield* TestResource("A", { string: "test-string" });
3041 return A.string;
3042 }).pipe(stack.deploy);
3043 expect((yield* getState("A"))?.status).toEqual("created");
3044 expect(output).toEqual("test-string");
3045 }),
3046 );
3047
3048 // A destroy interrupted while `provider.delete` is still in flight (the
3049 // shape of a live delete stuck in a long provisioning wait — e.g.
3050 // CloudFront's disable→wait→delete — when the test runner's timeout fires
3051 // and teardown is abandoned) must keep the resource's state row. Deletes
3052 // are idempotent and resumable: the engine commits a `deleting` row BEFORE
3053 // calling `provider.delete` and only drops it after success, so the next
3054 // destroy sees the row and drains it. Losing the row here is an invisible
3055 // orphan — the next destroy plans "no changes" and the cloud resource
3056 // leaks forever.
3057 test.provider("interrupting a destroy mid-delete keeps a resumable deleting row", (stack) =>
3058 Effect.gen(function* () {
3059 yield* Effect.gen(function* () {
3060 yield* TestResource("A", { string: "v1" });
3061 }).pipe(stack.deploy);
3062 expect((yield* getState("A"))?.status).toEqual("created");
3063
3064 // Destroy with a delete that signals entry and then never resolves,
3065 // then interrupt the destroy once the delete is in flight —
3066 // simulating the runner's timeout + teardown abandonment.
3067 const deleteStarted = yield* Deferred.make<void>();
3068 const fiber = yield* stack.destroy().pipe(
3069 hook({
3070 delete: () => Deferred.succeed(deleteStarted, void 0).pipe(Effect.andThen(Effect.never)),
3071 }),
3072 Effect.forkChild,
3073 );
3074 yield* Deferred.await(deleteStarted);
3075 yield* Fiber.interrupt(fiber);
3076
3077 // The row survives the interruption, parked at `deleting`.
3078 expect((yield* getState("A"))?.status).toEqual("deleting");
3079
3080 // The next destroy resumes the delete and drains the row.
3081 yield* stack.destroy();
3082 expect(yield* getState("A")).toBeUndefined();
3083 expect(yield* listState()).toEqual([]);
3084 }),
3085 );
3086
3087 test.provider("create when props trigger replacement", (stack) =>
3088 Effect.gen(function* () {
3089 // 1. Create initial resource
3090 yield* Effect.gen(function* () {
3091 yield* TestResource("A", { replaceString: "original" });
3092 }).pipe(stack.deploy);
3093 expect((yield* getState("A"))?.status).toEqual("created");
3094
3095 // 2. Try to delete but fail
3096 yield* stack.destroy().pipe(hook({ delete: () => Effect.fail(new ResourceFailure()) }));
3097 expect((yield* getState("A"))?.status).toEqual("deleting");
3098
3099 // 3. Re-apply with props that trigger replacement - should recreate
3100 const output = yield* Effect.gen(function* () {
3101 const A = yield* TestResource("A", { replaceString: "new" });
3102 return A.replaceString;
3103 }).pipe(stack.deploy);
3104 expect((yield* getState("A"))?.status).toEqual("created");
3105 expect(output).toEqual("new");
3106 }),
3107 );
3108 });
3109
3110 // =============================================================================
3111 // DEPENDENT RESOURCES (A -> B where B depends on A.string)
3112 // =============================================================================
3113
3114 describe("dependent resources (A -> B)", { tags: ["unit", "local"] }, () => {
3115 describe("happy path", () => {
3116 test.provider("create A then B where B uses A.string", (stack) =>
3117 Effect.gen(function* () {
3118 const output = yield* Effect.gen(function* () {
3119 const A = yield* TestResource("A", { string: "a-value" });
3120 const B = yield* TestResource("B", { string: A.string });
3121 return { A, B };
3122 }).pipe(stack.deploy);
3123
3124 expect((yield* getState("A"))?.status).toEqual("created");
3125 expect((yield* getState("B"))?.status).toEqual("created");
3126 expect(output.A.string).toEqual("a-value");
3127 expect(output.B.string).toEqual("a-value");
3128 }),
3129 );
3130
3131 test.provider("update A propagates to B", (stack) =>
3132 Effect.gen(function* () {
3133 yield* Effect.gen(function* () {
3134 const A = yield* TestResource("A", { string: "a-value" });
3135 yield* TestResource("B", { string: A.string });
3136 }).pipe(stack.deploy);
3137 expect((yield* getState("A"))?.status).toEqual("created");
3138 expect((yield* getState("B"))?.status).toEqual("created");
3139
3140 // Update A's string - B should update with the new value
3141 const output = yield* Effect.gen(function* () {
3142 const A = yield* TestResource("A", { string: "a-value-updated" });
3143 const B = yield* TestResource("B", { string: A.string });
3144 return { A, B };
3145 }).pipe(stack.deploy);
3146
3147 expect((yield* getState("A"))?.status).toEqual("updated");
3148 expect((yield* getState("B"))?.status).toEqual("updated");
3149 expect(output.A.string).toEqual("a-value-updated");
3150 expect(output.B.string).toEqual("a-value-updated");
3151 }),
3152 );
3153
3154 test.provider("replace A, B updates to new A's output", (stack) =>
3155 Effect.gen(function* () {
3156 yield* Effect.gen(function* () {
3157 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3158 yield* TestResource("B", { string: A.string });
3159 }).pipe(stack.deploy);
3160 expect((yield* getState("A"))?.status).toEqual("created");
3161 expect((yield* getState("B"))?.status).toEqual("created");
3162
3163 // Replace A - B should update to point to new A's output
3164 const output = yield* Effect.gen(function* () {
3165 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3166 const B = yield* TestResource("B", { string: A.string });
3167 return { A, B };
3168 }).pipe(stack.deploy);
3169
3170 expect((yield* getState("A"))?.status).toEqual("created");
3171 expect((yield* getState("B"))?.status).toEqual("updated");
3172 expect(output.A.string).toEqual("a-value-new");
3173 expect(output.B.string).toEqual("a-value-new");
3174 }),
3175 );
3176
3177 test.provider("delete both resources (B deleted first, then A)", (stack) =>
3178 Effect.gen(function* () {
3179 yield* Effect.gen(function* () {
3180 const A = yield* TestResource("A", { string: "a-value" });
3181 yield* TestResource("B", { string: A.string });
3182 }).pipe(stack.deploy);
3183 expect((yield* getState("A"))?.status).toEqual("created");
3184 expect((yield* getState("B"))?.status).toEqual("created");
3185
3186 yield* stack.destroy();
3187
3188 expect(yield* getState("A")).toBeUndefined();
3189 expectNotStarted(yield* getState("B"));
3190 expect(yield* listState()).toEqual([]);
3191 }),
3192 );
3193 });
3194
3195 describe("failures during expandAndPivot", () => {
3196 test.provider("A create fails, B never starts - recovery creates both", (stack) =>
3197 Effect.gen(function* () {
3198 // A fails to create - B should never start
3199 yield* Effect.gen(function* () {
3200 const A = yield* TestResource("A", { string: "a-value" });
3201 yield* TestResource("B", { string: A.string });
3202 }).pipe(stack.deploy, hook(failOn("A", "create")));
3203
3204 expect((yield* getState("A"))?.status).toEqual("creating");
3205 expectNotStarted(yield* getState("B"));
3206
3207 // Recovery: re-apply should create both
3208 const output = yield* Effect.gen(function* () {
3209 const A = yield* TestResource("A", { string: "a-value" });
3210 const B = yield* TestResource("B", { string: A.string });
3211 return { A, B };
3212 }).pipe(stack.deploy);
3213
3214 expect((yield* getState("A"))?.status).toEqual("created");
3215 expect((yield* getState("B"))?.status).toEqual("created");
3216 expect(output.A.string).toEqual("a-value");
3217 expect(output.B.string).toEqual("a-value");
3218 }),
3219 );
3220
3221 test.provider("A creates, B create fails - recovery creates B", (stack) =>
3222 Effect.gen(function* () {
3223 // A succeeds, B fails to create
3224 yield* Effect.gen(function* () {
3225 const A = yield* TestResource("A", { string: "a-value" });
3226 yield* TestResource("B", { string: A.string });
3227 }).pipe(stack.deploy, hook(failOn("B", "create")));
3228
3229 expect((yield* getState("A"))?.status).toEqual("created");
3230 expect((yield* getState("B"))?.status).toEqual("creating");
3231
3232 // Recovery: re-apply should noop A and create B
3233 const output = yield* Effect.gen(function* () {
3234 const A = yield* TestResource("A", { string: "a-value" });
3235 const B = yield* TestResource("B", { string: A.string });
3236 return { A, B };
3237 }).pipe(stack.deploy);
3238
3239 expect((yield* getState("A"))?.status).toEqual("created");
3240 expect((yield* getState("B"))?.status).toEqual("created");
3241 expect(output.B.string).toEqual("a-value");
3242 }),
3243 );
3244
3245 test.provider("A update fails - recovery updates both", (stack) =>
3246 Effect.gen(function* () {
3247 yield* Effect.gen(function* () {
3248 const A = yield* TestResource("A", { string: "a-value" });
3249 yield* TestResource("B", { string: A.string });
3250 }).pipe(stack.deploy);
3251
3252 const program = Effect.gen(function* () {
3253 const A = yield* TestResource("A", { string: "a-value-updated" });
3254 const B = yield* TestResource("B", { string: A.string });
3255 return { A, B };
3256 });
3257
3258 // A fails to update - B should not start updating
3259 yield* program.pipe(stack.deploy, hook(failOn("A", "update")));
3260
3261 expect((yield* getState("A"))?.status).toEqual("updating");
3262 expect((yield* getState("B"))?.status).toEqual("created");
3263
3264 // Recovery: re-apply should update both
3265 const output = yield* program.pipe(stack.deploy);
3266
3267 expect((yield* getState("A"))?.status).toEqual("updated");
3268 expect((yield* getState("B"))?.status).toEqual("updated");
3269 expect(output.A.string).toEqual("a-value-updated");
3270 expect(output.B.string).toEqual("a-value-updated");
3271 }),
3272 );
3273
3274 test.provider("A updates, B update fails - recovery updates B", (stack) =>
3275 Effect.gen(function* () {
3276 yield* Effect.gen(function* () {
3277 const A = yield* TestResource("A", { string: "a-value" });
3278 yield* TestResource("B", { string: A.string });
3279 }).pipe(stack.deploy);
3280
3281 const program = Effect.gen(function* () {
3282 const A = yield* TestResource("A", { string: "a-value-updated" });
3283 const B = yield* TestResource("B", { string: A.string });
3284 return { A, B };
3285 });
3286
3287 // A succeeds, B fails to update
3288 yield* program.pipe(stack.deploy, hook(failOn("B", "update")));
3289
3290 expect((yield* getState("A"))?.status).toEqual("updated");
3291 expect((yield* getState("B"))?.status).toEqual("updating");
3292
3293 // Recovery: re-apply should noop A and update B
3294 const output = yield* program.pipe(stack.deploy);
3295
3296 expect((yield* getState("A"))?.status).toEqual("updated");
3297 expect((yield* getState("B"))?.status).toEqual("updated");
3298 expect(output.B.string).toEqual("a-value-updated");
3299 }),
3300 );
3301
3302 test.provider("A replacement fails - recovery replaces A and updates B", (stack) =>
3303 Effect.gen(function* () {
3304 yield* Effect.gen(function* () {
3305 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3306 yield* TestResource("B", { string: A.string });
3307 }).pipe(stack.deploy);
3308
3309 const program = Effect.gen(function* () {
3310 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3311 const B = yield* TestResource("B", { string: A.string });
3312 return { A, B };
3313 });
3314
3315 // A replacement fails (during create of new A) - B should not start
3316 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
3317
3318 expect((yield* getState<ReplacingResourceState>("A"))?.status).toEqual("replacing");
3319 expect((yield* getState("B"))?.status).toEqual("created");
3320
3321 // Recovery: re-apply should complete A replacement and update B
3322 const output = yield* program.pipe(stack.deploy);
3323
3324 expect((yield* getState("A"))?.status).toEqual("created");
3325 expect((yield* getState("B"))?.status).toEqual("updated");
3326 expect(output.A.string).toEqual("a-value-new");
3327 expect(output.B.string).toEqual("a-value-new");
3328 }),
3329 );
3330
3331 test.provider("A replaced, B update fails - recovery updates B then cleans up", (stack) =>
3332 Effect.gen(function* () {
3333 yield* Effect.gen(function* () {
3334 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3335 yield* TestResource("B", { string: A.string });
3336 }).pipe(stack.deploy);
3337
3338 const program = Effect.gen(function* () {
3339 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3340 const B = yield* TestResource("B", { string: A.string });
3341 return { A, B };
3342 });
3343
3344 // A replacement succeeds, B fails to update
3345 yield* program.pipe(stack.deploy, hook(failOn("B", "update")));
3346
3347 // A should be in replaced state (new A created, old A pending cleanup)
3348 // B should be in updating state
3349 const aState = yield* getState<ReplacedResourceState>("A");
3350 expect(aState?.status).toEqual("replaced");
3351 expect((yield* getState("B"))?.status).toEqual("updating");
3352
3353 // Recovery: re-apply should update B and clean up old A
3354 const output = yield* program.pipe(stack.deploy);
3355
3356 expect((yield* getState("A"))?.status).toEqual("created");
3357 expect((yield* getState("B"))?.status).toEqual("updated");
3358 expect(output.B.string).toEqual("a-value-new");
3359 }),
3360 );
3361 });
3362
3363 describe("failures during collectGarbage", () => {
3364 test.provider("A replaced, B updated, old A delete fails - recovery cleans up", (stack) =>
3365 Effect.gen(function* () {
3366 yield* Effect.gen(function* () {
3367 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3368 yield* TestResource("B", { string: A.string });
3369 }).pipe(stack.deploy);
3370
3371 const program = Effect.gen(function* () {
3372 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3373 const B = yield* TestResource("B", { string: A.string });
3374 return { A, B };
3375 });
3376
3377 // A replacement and B update succeed, but old A delete fails
3378 yield* program.pipe(stack.deploy, hook(failOn("A", "delete")));
3379
3380 // A should be in replaced state (delete of old A failed)
3381 // B should have been updated successfully
3382 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
3383 expect((yield* getState("B"))?.status).toEqual("updated");
3384
3385 // Recovery: re-apply should clean up old A
3386 const output = yield* program.pipe(stack.deploy);
3387
3388 expect((yield* getState("A"))?.status).toEqual("created");
3389 expect((yield* getState("B"))?.status).toEqual("updated");
3390 expect(output.A.string).toEqual("a-value-new");
3391 }),
3392 );
3393
3394 test.provider("orphan B delete fails - recovery deletes B then A", (stack) =>
3395 Effect.gen(function* () {
3396 yield* Effect.gen(function* () {
3397 const A = yield* TestResource("A", { string: "a-value" });
3398 yield* TestResource("B", { string: A.string });
3399 }).pipe(stack.deploy);
3400 expect((yield* getState("A"))?.status).toEqual("created");
3401 expect((yield* getState("B"))?.status).toEqual("created");
3402
3403 // Orphan deletion: B delete fails
3404 yield* stack.destroy().pipe(hook(failOn("B", "delete")));
3405
3406 // B should be in deleting state, A should still be created (waiting for B)
3407 expect((yield* getState("B"))?.status).toEqual("deleting");
3408 expect((yield* getState("A"))?.status).toEqual("created");
3409
3410 // Recovery: re-apply destroy should delete B then A
3411 yield* stack.destroy();
3412
3413 expect(yield* getState("A")).toBeUndefined();
3414 expectNotStarted(yield* getState("B"));
3415 }),
3416 );
3417
3418 test.provider("orphan A delete fails after B deleted - recovery deletes A", (stack) =>
3419 Effect.gen(function* () {
3420 yield* Effect.gen(function* () {
3421 const A = yield* TestResource("A", { string: "a-value" });
3422 yield* TestResource("B", { string: A.string });
3423 }).pipe(stack.deploy);
3424 expect((yield* getState("A"))?.status).toEqual("created");
3425 expect((yield* getState("B"))?.status).toEqual("created");
3426
3427 // Orphan deletion: B succeeds, A fails
3428 yield* stack.destroy().pipe(hook(failOn("A", "delete")));
3429
3430 // B should be deleted, A should be in deleting state
3431 expectNotStarted(yield* getState("B"));
3432 expect((yield* getState("A"))?.status).toEqual("deleting");
3433
3434 // Recovery: re-apply destroy should delete A
3435 yield* stack.destroy();
3436
3437 expect(yield* getState("A")).toBeUndefined();
3438 }),
3439 );
3440 });
3441 });
3442
3443 // =============================================================================
3444 // THREE-LEVEL DEPENDENCY CHAIN (A -> B -> C where C depends on B, B depends on A)
3445 // =============================================================================
3446
3447 describe("three-level dependency chain (A -> B -> C)", { tags: ["unit", "local"] }, () => {
3448 describe("happy path", () => {
3449 test.provider("create A then B then C", (stack) =>
3450 Effect.gen(function* () {
3451 const output = yield* Effect.gen(function* () {
3452 const A = yield* TestResource("A", { string: "a-value" });
3453 const B = yield* TestResource("B", { string: A.string });
3454 const C = yield* TestResource("C", { string: B.string });
3455 return { A, B, C };
3456 }).pipe(stack.deploy);
3457
3458 expect((yield* getState("A"))?.status).toEqual("created");
3459 expect((yield* getState("B"))?.status).toEqual("created");
3460 expect((yield* getState("C"))?.status).toEqual("created");
3461 expect(output.A.string).toEqual("a-value");
3462 expect(output.B.string).toEqual("a-value");
3463 expect(output.C.string).toEqual("a-value");
3464 }),
3465 );
3466
3467 test.provider("update A propagates through B to C", (stack) =>
3468 Effect.gen(function* () {
3469 yield* Effect.gen(function* () {
3470 const A = yield* TestResource("A", { string: "a-value" });
3471 const B = yield* TestResource("B", { string: A.string });
3472 yield* TestResource("C", { string: B.string });
3473 }).pipe(stack.deploy);
3474
3475 const output = yield* Effect.gen(function* () {
3476 const A = yield* TestResource("A", { string: "a-value-updated" });
3477 const B = yield* TestResource("B", { string: A.string });
3478 const C = yield* TestResource("C", { string: B.string });
3479 return { A, B, C };
3480 }).pipe(stack.deploy);
3481
3482 expect((yield* getState("A"))?.status).toEqual("updated");
3483 expect((yield* getState("B"))?.status).toEqual("updated");
3484 expect((yield* getState("C"))?.status).toEqual("updated");
3485 expect(output.C.string).toEqual("a-value-updated");
3486 }),
3487 );
3488
3489 test.provider("replace A propagates through B to C", (stack) =>
3490 Effect.gen(function* () {
3491 yield* Effect.gen(function* () {
3492 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3493 const B = yield* TestResource("B", { string: A.string });
3494 yield* TestResource("C", { string: B.string });
3495 }).pipe(stack.deploy);
3496
3497 const output = yield* Effect.gen(function* () {
3498 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3499 const B = yield* TestResource("B", { string: A.string });
3500 const C = yield* TestResource("C", { string: B.string });
3501 return { A, B, C };
3502 }).pipe(stack.deploy);
3503
3504 expect((yield* getState("A"))?.status).toEqual("created");
3505 expect((yield* getState("B"))?.status).toEqual("updated");
3506 expect((yield* getState("C"))?.status).toEqual("updated");
3507 expect(output.C.string).toEqual("a-value-new");
3508 }),
3509 );
3510
3511 test.provider("delete all three (C first, then B, then A)", (stack) =>
3512 Effect.gen(function* () {
3513 yield* Effect.gen(function* () {
3514 const A = yield* TestResource("A", { string: "a-value" });
3515 const B = yield* TestResource("B", { string: A.string });
3516 yield* TestResource("C", { string: B.string });
3517 }).pipe(stack.deploy);
3518
3519 yield* stack.destroy();
3520
3521 expect(yield* getState("A")).toBeUndefined();
3522 expectNotStarted(yield* getState("B"));
3523 expectNotStarted(yield* getState("C"));
3524 expect(yield* listState()).toEqual([]);
3525 }),
3526 );
3527 });
3528
3529 describe("creation failures", () => {
3530 test.provider("A create fails - B and C never start", (stack) =>
3531 Effect.gen(function* () {
3532 const program = Effect.gen(function* () {
3533 const A = yield* TestResource("A", { string: "a-value" });
3534 const B = yield* TestResource("B", { string: A.string });
3535 const C = yield* TestResource("C", { string: B.string });
3536 return { A, B, C };
3537 });
3538
3539 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
3540
3541 expect((yield* getState("A"))?.status).toEqual("creating");
3542 expectNotStarted(yield* getState("B"));
3543 expectNotStarted(yield* getState("C"));
3544
3545 // Recovery
3546 const output = yield* program.pipe(stack.deploy);
3547 expect((yield* getState("A"))?.status).toEqual("created");
3548 expect((yield* getState("B"))?.status).toEqual("created");
3549 expect((yield* getState("C"))?.status).toEqual("created");
3550 expect(output.C.string).toEqual("a-value");
3551 }),
3552 );
3553
3554 test.provider("A creates, B create fails - C never starts", (stack) =>
3555 Effect.gen(function* () {
3556 const program = Effect.gen(function* () {
3557 const A = yield* TestResource("A", { string: "a-value" });
3558 const B = yield* TestResource("B", { string: A.string });
3559 const C = yield* TestResource("C", { string: B.string });
3560 return { A, B, C };
3561 });
3562
3563 yield* program.pipe(stack.deploy, hook(failOn("B", "create")));
3564
3565 expect((yield* getState("A"))?.status).toEqual("created");
3566 expect((yield* getState("B"))?.status).toEqual("creating");
3567 expectNotStarted(yield* getState("C"));
3568
3569 // Recovery
3570 const output = yield* program.pipe(stack.deploy);
3571 expect((yield* getState("A"))?.status).toEqual("created");
3572 expect((yield* getState("B"))?.status).toEqual("created");
3573 expect((yield* getState("C"))?.status).toEqual("created");
3574 expect(output.C.string).toEqual("a-value");
3575 }),
3576 );
3577
3578 test.provider("A and B create, C create fails", (stack) =>
3579 Effect.gen(function* () {
3580 const program = Effect.gen(function* () {
3581 const A = yield* TestResource("A", { string: "a-value" });
3582 const B = yield* TestResource("B", { string: A.string });
3583 const C = yield* TestResource("C", { string: B.string });
3584 return { A, B, C };
3585 });
3586
3587 yield* program.pipe(stack.deploy, hook(failOn("C", "create")));
3588
3589 expect((yield* getState("A"))?.status).toEqual("created");
3590 expect((yield* getState("B"))?.status).toEqual("created");
3591 expect((yield* getState("C"))?.status).toEqual("creating");
3592
3593 // Recovery
3594 const output = yield* program.pipe(stack.deploy);
3595 expect((yield* getState("A"))?.status).toEqual("created");
3596 expect((yield* getState("B"))?.status).toEqual("created");
3597 expect((yield* getState("C"))?.status).toEqual("created");
3598 expect(output.C.string).toEqual("a-value");
3599 }),
3600 );
3601 });
3602
3603 describe("update failures", () => {
3604 test.provider("A update fails - B and C remain stable", (stack) =>
3605 Effect.gen(function* () {
3606 yield* Effect.gen(function* () {
3607 const A = yield* TestResource("A", { string: "a-value" });
3608 const B = yield* TestResource("B", { string: A.string });
3609 yield* TestResource("C", { string: B.string });
3610 }).pipe(stack.deploy);
3611
3612 const program = Effect.gen(function* () {
3613 const A = yield* TestResource("A", { string: "a-value-updated" });
3614 const B = yield* TestResource("B", { string: A.string });
3615 const C = yield* TestResource("C", { string: B.string });
3616 return { A, B, C };
3617 });
3618
3619 yield* program.pipe(stack.deploy, hook(failOn("A", "update")));
3620
3621 expect((yield* getState("A"))?.status).toEqual("updating");
3622 expect((yield* getState("B"))?.status).toEqual("created");
3623 expect((yield* getState("C"))?.status).toEqual("created");
3624
3625 // Recovery
3626 const output = yield* program.pipe(stack.deploy);
3627 expect((yield* getState("A"))?.status).toEqual("updated");
3628 expect((yield* getState("B"))?.status).toEqual("updated");
3629 expect((yield* getState("C"))?.status).toEqual("updated");
3630 expect(output.C.string).toEqual("a-value-updated");
3631 }),
3632 );
3633
3634 test.provider("A updates, B update fails - C remains stable", (stack) =>
3635 Effect.gen(function* () {
3636 yield* Effect.gen(function* () {
3637 const A = yield* TestResource("A", { string: "a-value" });
3638 const B = yield* TestResource("B", { string: A.string });
3639 yield* TestResource("C", { string: B.string });
3640 }).pipe(stack.deploy);
3641
3642 const program = Effect.gen(function* () {
3643 const A = yield* TestResource("A", { string: "a-value-updated" });
3644 const B = yield* TestResource("B", { string: A.string });
3645 const C = yield* TestResource("C", { string: B.string });
3646 return { A, B, C };
3647 });
3648
3649 yield* program.pipe(stack.deploy, hook(failOn("B", "update")));
3650
3651 expect((yield* getState("A"))?.status).toEqual("updated");
3652 expect((yield* getState("B"))?.status).toEqual("updating");
3653 expect((yield* getState("C"))?.status).toEqual("created");
3654
3655 // Recovery
3656 const output = yield* program.pipe(stack.deploy);
3657 expect((yield* getState("A"))?.status).toEqual("updated");
3658 expect((yield* getState("B"))?.status).toEqual("updated");
3659 expect((yield* getState("C"))?.status).toEqual("updated");
3660 expect(output.C.string).toEqual("a-value-updated");
3661 }),
3662 );
3663
3664 test.provider("A and B update, C update fails", (stack) =>
3665 Effect.gen(function* () {
3666 yield* Effect.gen(function* () {
3667 const A = yield* TestResource("A", { string: "a-value" });
3668 const B = yield* TestResource("B", { string: A.string });
3669 yield* TestResource("C", { string: B.string });
3670 }).pipe(stack.deploy);
3671
3672 const program = Effect.gen(function* () {
3673 const A = yield* TestResource("A", { string: "a-value-updated" });
3674 const B = yield* TestResource("B", { string: A.string });
3675 const C = yield* TestResource("C", { string: B.string });
3676 return { A, B, C };
3677 });
3678
3679 yield* program.pipe(stack.deploy, hook(failOn("C", "update")));
3680
3681 expect((yield* getState("A"))?.status).toEqual("updated");
3682 expect((yield* getState("B"))?.status).toEqual("updated");
3683 expect((yield* getState("C"))?.status).toEqual("updating");
3684
3685 // Recovery
3686 const output = yield* program.pipe(stack.deploy);
3687 expect((yield* getState("A"))?.status).toEqual("updated");
3688 expect((yield* getState("B"))?.status).toEqual("updated");
3689 expect((yield* getState("C"))?.status).toEqual("updated");
3690 expect(output.C.string).toEqual("a-value-updated");
3691 }),
3692 );
3693 });
3694
3695 describe("replace cascade failures", () => {
3696 test.provider("A replace fails - B and C remain stable", (stack) =>
3697 Effect.gen(function* () {
3698 yield* Effect.gen(function* () {
3699 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3700 const B = yield* TestResource("B", { string: A.string });
3701 yield* TestResource("C", { string: B.string });
3702 }).pipe(stack.deploy);
3703
3704 const program = Effect.gen(function* () {
3705 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3706 const B = yield* TestResource("B", { string: A.string });
3707 const C = yield* TestResource("C", { string: B.string });
3708 return { A, B, C };
3709 });
3710
3711 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
3712
3713 expect((yield* getState<ReplacingResourceState>("A"))?.status).toEqual("replacing");
3714 expect((yield* getState("B"))?.status).toEqual("created");
3715 expect((yield* getState("C"))?.status).toEqual("created");
3716
3717 // Recovery
3718 const output = yield* program.pipe(stack.deploy);
3719 expect((yield* getState("A"))?.status).toEqual("created");
3720 expect((yield* getState("B"))?.status).toEqual("updated");
3721 expect((yield* getState("C"))?.status).toEqual("updated");
3722 expect(output.C.string).toEqual("a-value-new");
3723 }),
3724 );
3725
3726 test.provider("A replaced, B update fails - C remains stable", (stack) =>
3727 Effect.gen(function* () {
3728 yield* Effect.gen(function* () {
3729 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3730 const B = yield* TestResource("B", { string: A.string });
3731 yield* TestResource("C", { string: B.string });
3732 }).pipe(stack.deploy);
3733
3734 const program = Effect.gen(function* () {
3735 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3736 const B = yield* TestResource("B", { string: A.string });
3737 const C = yield* TestResource("C", { string: B.string });
3738 return { A, B, C };
3739 });
3740
3741 yield* program.pipe(stack.deploy, hook(failOn("B", "update")));
3742
3743 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
3744 expect((yield* getState("B"))?.status).toEqual("updating");
3745 expect((yield* getState("C"))?.status).toEqual("created");
3746
3747 // Recovery
3748 const output = yield* program.pipe(stack.deploy);
3749 expect((yield* getState("A"))?.status).toEqual("created");
3750 expect((yield* getState("B"))?.status).toEqual("updated");
3751 expect((yield* getState("C"))?.status).toEqual("updated");
3752 expect(output.C.string).toEqual("a-value-new");
3753 }),
3754 );
3755
3756 test.provider("A replaced, B updated, C update fails", (stack) =>
3757 Effect.gen(function* () {
3758 yield* Effect.gen(function* () {
3759 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3760 const B = yield* TestResource("B", { string: A.string });
3761 yield* TestResource("C", { string: B.string });
3762 }).pipe(stack.deploy);
3763
3764 const program = Effect.gen(function* () {
3765 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3766 const B = yield* TestResource("B", { string: A.string });
3767 const C = yield* TestResource("C", { string: B.string });
3768 return { A, B, C };
3769 });
3770
3771 yield* program.pipe(stack.deploy, hook(failOn("C", "update")));
3772
3773 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
3774 expect((yield* getState("B"))?.status).toEqual("updated");
3775 expect((yield* getState("C"))?.status).toEqual("updating");
3776
3777 // Recovery
3778 const output = yield* program.pipe(stack.deploy);
3779 expect((yield* getState("A"))?.status).toEqual("created");
3780 expect((yield* getState("B"))?.status).toEqual("updated");
3781 expect((yield* getState("C"))?.status).toEqual("updated");
3782 expect(output.C.string).toEqual("a-value-new");
3783 }),
3784 );
3785
3786 test.provider("A replaced, B and C updated, old A delete fails - recovery cleans up", (stack) =>
3787 Effect.gen(function* () {
3788 yield* Effect.gen(function* () {
3789 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
3790 const B = yield* TestResource("B", { string: A.string });
3791 yield* TestResource("C", { string: B.string });
3792 }).pipe(stack.deploy);
3793
3794 const program = Effect.gen(function* () {
3795 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
3796 const B = yield* TestResource("B", { string: A.string });
3797 const C = yield* TestResource("C", { string: B.string });
3798 return { A, B, C };
3799 });
3800
3801 yield* program.pipe(stack.deploy, hook(failOn("A", "delete")));
3802
3803 expect((yield* getState<ReplacedResourceState>("A"))?.status).toEqual("replaced");
3804 expect((yield* getState("B"))?.status).toEqual("updated");
3805 expect((yield* getState("C"))?.status).toEqual("updated");
3806
3807 // Recovery
3808 const output = yield* program.pipe(stack.deploy);
3809 expect((yield* getState("A"))?.status).toEqual("created");
3810 expect((yield* getState("B"))?.status).toEqual("updated");
3811 expect((yield* getState("C"))?.status).toEqual("updated");
3812 expect(output.C.string).toEqual("a-value-new");
3813 }),
3814 );
3815 });
3816
3817 describe("delete order failures", () => {
3818 test.provider("C delete fails - A and B waiting", (stack) =>
3819 Effect.gen(function* () {
3820 yield* Effect.gen(function* () {
3821 const A = yield* TestResource("A", { string: "a-value" });
3822 const B = yield* TestResource("B", { string: A.string });
3823 yield* TestResource("C", { string: B.string });
3824 }).pipe(stack.deploy);
3825
3826 yield* stack.destroy().pipe(hook(failOn("C", "delete")));
3827
3828 expect((yield* getState("C"))?.status).toEqual("deleting");
3829 expect((yield* getState("B"))?.status).toEqual("created");
3830 expect((yield* getState("A"))?.status).toEqual("created");
3831
3832 // Recovery
3833 yield* stack.destroy();
3834 expect(yield* getState("A")).toBeUndefined();
3835 expectNotStarted(yield* getState("B"));
3836 expectNotStarted(yield* getState("C"));
3837 }),
3838 );
3839
3840 test.provider("C deleted, B delete fails - A waiting", (stack) =>
3841 Effect.gen(function* () {
3842 yield* Effect.gen(function* () {
3843 const A = yield* TestResource("A", { string: "a-value" });
3844 const B = yield* TestResource("B", { string: A.string });
3845 yield* TestResource("C", { string: B.string });
3846 }).pipe(stack.deploy);
3847
3848 yield* stack.destroy().pipe(hook(failOn("B", "delete")));
3849
3850 expectNotStarted(yield* getState("C"));
3851 expect((yield* getState("B"))?.status).toEqual("deleting");
3852 expect((yield* getState("A"))?.status).toEqual("created");
3853
3854 // Recovery
3855 yield* stack.destroy();
3856 expect(yield* getState("A")).toBeUndefined();
3857 expectNotStarted(yield* getState("B"));
3858 }),
3859 );
3860
3861 test.provider("C and B deleted, A delete fails", (stack) =>
3862 Effect.gen(function* () {
3863 yield* Effect.gen(function* () {
3864 const A = yield* TestResource("A", { string: "a-value" });
3865 const B = yield* TestResource("B", { string: A.string });
3866 yield* TestResource("C", { string: B.string });
3867 }).pipe(stack.deploy);
3868
3869 yield* stack.destroy().pipe(hook(failOn("A", "delete")));
3870
3871 expectNotStarted(yield* getState("C"));
3872 expectNotStarted(yield* getState("B"));
3873 expect((yield* getState("A"))?.status).toEqual("deleting");
3874
3875 // Recovery
3876 yield* stack.destroy();
3877 expect(yield* getState("A")).toBeUndefined();
3878 }),
3879 );
3880 });
3881 });
3882
3883 // =============================================================================
3884 // DIAMOND DEPENDENCIES (D depends on B and C, both depend on A)
3885 // A
3886 // / \
3887 // B C
3888 // \ /
3889 // D
3890 // =============================================================================
3891
3892 describe("diamond dependencies (A -> B,C -> D)", { tags: ["unit", "local"] }, () => {
3893 describe("happy path", () => {
3894 test.provider("create all four resources", (stack) =>
3895 Effect.gen(function* () {
3896 const output = yield* Effect.gen(function* () {
3897 const A = yield* TestResource("A", { string: "a-value" });
3898 const B = yield* TestResource("B", { string: A.string });
3899 const C = yield* TestResource("C", { string: A.string });
3900 const D = yield* TestResource("D", {
3901 string: Output.interpolate`${B.string}-${C.string}`,
3902 });
3903 return { A, B, C, D };
3904 }).pipe(stack.deploy);
3905
3906 expect((yield* getState("A"))?.status).toEqual("created");
3907 expect((yield* getState("B"))?.status).toEqual("created");
3908 expect((yield* getState("C"))?.status).toEqual("created");
3909 expect((yield* getState("D"))?.status).toEqual("created");
3910 expect(output.D.string).toEqual("a-value-a-value");
3911 }),
3912 );
3913
3914 test.provider("update A propagates to B, C, and D", (stack) =>
3915 Effect.gen(function* () {
3916 yield* Effect.gen(function* () {
3917 const A = yield* TestResource("A", { string: "a-value" });
3918 const B = yield* TestResource("B", { string: A.string });
3919 const C = yield* TestResource("C", { string: A.string });
3920 yield* TestResource("D", { string: Output.interpolate`${B.string}-${C.string}` });
3921 }).pipe(stack.deploy);
3922
3923 const output = yield* Effect.gen(function* () {
3924 const A = yield* TestResource("A", { string: "updated" });
3925 const B = yield* TestResource("B", { string: A.string });
3926 const C = yield* TestResource("C", { string: A.string });
3927 const D = yield* TestResource("D", {
3928 string: Output.interpolate`${B.string}-${C.string}`,
3929 });
3930 return { A, B, C, D };
3931 }).pipe(stack.deploy);
3932
3933 expect((yield* getState("A"))?.status).toEqual("updated");
3934 expect((yield* getState("B"))?.status).toEqual("updated");
3935 expect((yield* getState("C"))?.status).toEqual("updated");
3936 expect((yield* getState("D"))?.status).toEqual("updated");
3937 expect(output.D.string).toEqual("updated-updated");
3938 }),
3939 );
3940
3941 test.provider("add D while B replaces and C noops", (stack) =>
3942 Effect.gen(function* () {
3943 yield* Effect.gen(function* () {
3944 const A = yield* TestResource("A", { string: "a-value" });
3945 yield* TestResource("B", {
3946 string: Output.interpolate`${A.string}-b`,
3947 replaceString: "b-original",
3948 });
3949 yield* TestResource("C", {
3950 string: Output.interpolate`${A.string}-c`,
3951 replaceString: "c-original",
3952 });
3953 }).pipe(stack.deploy);
3954
3955 const output = yield* Effect.gen(function* () {
3956 const A = yield* TestResource("A", { string: "a-value" });
3957 const B = yield* TestResource("B", {
3958 string: Output.interpolate`${A.string}-b-replaced`,
3959 replaceString: "b-changed",
3960 });
3961 const C = yield* TestResource("C", {
3962 string: Output.interpolate`${A.string}-c`,
3963 replaceString: "c-original",
3964 });
3965 const D = yield* TestResource("D", {
3966 string: Output.interpolate`${B.string}-${C.string}`,
3967 });
3968 return { A, B, C, D };
3969 }).pipe(stack.deploy);
3970
3971 expect((yield* getState("A"))?.status).toEqual("created");
3972 expect((yield* getState("B"))?.status).toEqual("created");
3973 expect((yield* getState("C"))?.status).toEqual("created");
3974 expect((yield* getState("D"))?.status).toEqual("created");
3975 expect(output.B.replaceString).toEqual("b-changed");
3976 expect(output.C.replaceString).toEqual("c-original");
3977 expect(output.D.string).toEqual("a-value-b-replaced-a-value-c");
3978 }),
3979 );
3980
3981 test.provider("add D while C replaces and B noops", (stack) =>
3982 Effect.gen(function* () {
3983 yield* Effect.gen(function* () {
3984 const A = yield* TestResource("A", { string: "a-value" });
3985 yield* TestResource("B", {
3986 string: Output.interpolate`${A.string}-b`,
3987 replaceString: "b-original",
3988 });
3989 yield* TestResource("C", {
3990 string: Output.interpolate`${A.string}-c`,
3991 replaceString: "c-original",
3992 });
3993 }).pipe(stack.deploy);
3994
3995 const output = yield* Effect.gen(function* () {
3996 const A = yield* TestResource("A", { string: "a-value" });
3997 const B = yield* TestResource("B", {
3998 string: Output.interpolate`${A.string}-b`,
3999 replaceString: "b-original",
4000 });
4001 const C = yield* TestResource("C", {
4002 string: Output.interpolate`${A.string}-c-replaced`,
4003 replaceString: "c-changed",
4004 });
4005 const D = yield* TestResource("D", {
4006 string: Output.interpolate`${B.string}-${C.string}`,
4007 });
4008 return { A, B, C, D };
4009 }).pipe(stack.deploy);
4010
4011 expect((yield* getState("A"))?.status).toEqual("created");
4012 expect((yield* getState("B"))?.status).toEqual("created");
4013 expect((yield* getState("C"))?.status).toEqual("created");
4014 expect((yield* getState("D"))?.status).toEqual("created");
4015 expect(output.B.replaceString).toEqual("b-original");
4016 expect(output.C.replaceString).toEqual("c-changed");
4017 expect(output.D.string).toEqual("a-value-b-a-value-c-replaced");
4018 }),
4019 );
4020
4021 test.provider("add D while both B and C replace", (stack) =>
4022 Effect.gen(function* () {
4023 yield* Effect.gen(function* () {
4024 const A = yield* TestResource("A", { string: "a-value" });
4025 yield* TestResource("B", {
4026 string: Output.interpolate`${A.string}-b`,
4027 replaceString: "b-original",
4028 });
4029 yield* TestResource("C", {
4030 string: Output.interpolate`${A.string}-c`,
4031 replaceString: "c-original",
4032 });
4033 }).pipe(stack.deploy);
4034
4035 const output = yield* Effect.gen(function* () {
4036 const A = yield* TestResource("A", { string: "a-value" });
4037 const B = yield* TestResource("B", {
4038 string: Output.interpolate`${A.string}-b-replaced`,
4039 replaceString: "b-changed",
4040 });
4041 const C = yield* TestResource("C", {
4042 string: Output.interpolate`${A.string}-c-replaced`,
4043 replaceString: "c-changed",
4044 });
4045 const D = yield* TestResource("D", {
4046 string: Output.interpolate`${B.string}-${C.string}`,
4047 });
4048 return { A, B, C, D };
4049 }).pipe(stack.deploy);
4050
4051 expect((yield* getState("A"))?.status).toEqual("created");
4052 expect((yield* getState("B"))?.status).toEqual("created");
4053 expect((yield* getState("C"))?.status).toEqual("created");
4054 expect((yield* getState("D"))?.status).toEqual("created");
4055 expect(output.B.replaceString).toEqual("b-changed");
4056 expect(output.C.replaceString).toEqual("c-changed");
4057 expect(output.D.string).toEqual("a-value-b-replaced-a-value-c-replaced");
4058 }),
4059 );
4060
4061 test.provider("delete all (D first, then B and C, then A)", (stack) =>
4062 Effect.gen(function* () {
4063 yield* Effect.gen(function* () {
4064 const A = yield* TestResource("A", { string: "a-value" });
4065 const B = yield* TestResource("B", { string: A.string });
4066 const C = yield* TestResource("C", { string: A.string });
4067 yield* TestResource("D", { string: Output.interpolate`${B.string}-${C.string}` });
4068 }).pipe(stack.deploy);
4069
4070 yield* stack.destroy();
4071
4072 expect(yield* getState("A")).toBeUndefined();
4073 expectNotStarted(yield* getState("B"));
4074 expectNotStarted(yield* getState("C"));
4075 expectNotStarted(yield* getState("D"));
4076 }),
4077 );
4078 });
4079
4080 describe("creation failures", () => {
4081 test.provider("A create fails - B, C, D never start", (stack) =>
4082 Effect.gen(function* () {
4083 const program = Effect.gen(function* () {
4084 const A = yield* TestResource("A", { string: "a-value" });
4085 const B = yield* TestResource("B", { string: A.string });
4086 const C = yield* TestResource("C", { string: A.string });
4087 const D = yield* TestResource("D", {
4088 string: Output.interpolate`${B.string}-${C.string}`,
4089 });
4090 return { A, B, C, D };
4091 });
4092
4093 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
4094
4095 expect((yield* getState("A"))?.status).toEqual("creating");
4096 expectNotStarted(yield* getState("B"));
4097 expectNotStarted(yield* getState("C"));
4098 expectNotStarted(yield* getState("D"));
4099
4100 // Recovery
4101 const output = yield* program.pipe(stack.deploy);
4102 expect((yield* getState("A"))?.status).toEqual("created");
4103 expect((yield* getState("B"))?.status).toEqual("created");
4104 expect((yield* getState("C"))?.status).toEqual("created");
4105 expect((yield* getState("D"))?.status).toEqual("created");
4106 expect(output.D.string).toEqual("a-value-a-value");
4107 }),
4108 );
4109
4110 test.provider("A creates, B create fails - C may create, D stuck", (stack) =>
4111 Effect.gen(function* () {
4112 const program = Effect.gen(function* () {
4113 const A = yield* TestResource("A", { string: "a-value" });
4114 const B = yield* TestResource("B", { string: A.string });
4115 const C = yield* TestResource("C", { string: A.string });
4116 const D = yield* TestResource("D", {
4117 string: Output.interpolate`${B.string}-${C.string}`,
4118 });
4119 return { A, B, C, D };
4120 });
4121
4122 yield* program.pipe(stack.deploy, hook(failOn("B", "create")));
4123
4124 expect((yield* getState("A"))?.status).toEqual("created");
4125 expect((yield* getState("B"))?.status).toEqual("creating");
4126 // C might have been created since it doesn't depend on B
4127 const cState = yield* getState("C");
4128 expect(cState === undefined || cState?.status === "created").toBe(true);
4129 expectNotStarted(yield* getState("D"));
4130
4131 // Recovery
4132 const output = yield* program.pipe(stack.deploy);
4133 expect((yield* getState("A"))?.status).toEqual("created");
4134 expect((yield* getState("B"))?.status).toEqual("created");
4135 expect((yield* getState("C"))?.status).toEqual("created");
4136 expect((yield* getState("D"))?.status).toEqual("created");
4137 expect(output.D.string).toEqual("a-value-a-value");
4138 }),
4139 );
4140
4141 test.provider("A creates, C create fails - B may create, D stuck", (stack) =>
4142 Effect.gen(function* () {
4143 const program = Effect.gen(function* () {
4144 const A = yield* TestResource("A", { string: "a-value" });
4145 const B = yield* TestResource("B", { string: A.string });
4146 const C = yield* TestResource("C", { string: A.string });
4147 const D = yield* TestResource("D", {
4148 string: Output.interpolate`${B.string}-${C.string}`,
4149 });
4150 return { A, B, C, D };
4151 });
4152
4153 yield* program.pipe(stack.deploy, hook(failOn("C", "create")));
4154
4155 expect((yield* getState("A"))?.status).toEqual("created");
4156 expect((yield* getState("C"))?.status).toEqual("creating");
4157 // B might have been created since it doesn't depend on C
4158 const bState = yield* getState("B");
4159 expect(bState === undefined || bState?.status === "created").toBe(true);
4160 expectNotStarted(yield* getState("D"));
4161
4162 // Recovery
4163 const output = yield* program.pipe(stack.deploy);
4164 expect((yield* getState("A"))?.status).toEqual("created");
4165 expect((yield* getState("B"))?.status).toEqual("created");
4166 expect((yield* getState("C"))?.status).toEqual("created");
4167 expect((yield* getState("D"))?.status).toEqual("created");
4168 expect(output.D.string).toEqual("a-value-a-value");
4169 }),
4170 );
4171
4172 test.provider("A, B, C create - D create fails", (stack) =>
4173 Effect.gen(function* () {
4174 const program = Effect.gen(function* () {
4175 const A = yield* TestResource("A", { string: "a-value" });
4176 const B = yield* TestResource("B", { string: A.string });
4177 const C = yield* TestResource("C", { string: A.string });
4178 const D = yield* TestResource("D", {
4179 string: Output.interpolate`${B.string}-${C.string}`,
4180 });
4181 return { A, B, C, D };
4182 });
4183
4184 yield* program.pipe(stack.deploy, hook(failOn("D", "create")));
4185
4186 expect((yield* getState("A"))?.status).toEqual("created");
4187 expect((yield* getState("B"))?.status).toEqual("created");
4188 expect((yield* getState("C"))?.status).toEqual("created");
4189 expect((yield* getState("D"))?.status).toEqual("creating");
4190
4191 // Recovery
4192 const output = yield* program.pipe(stack.deploy);
4193 expect((yield* getState("D"))?.status).toEqual("created");
4194 expect(output.D.string).toEqual("a-value-a-value");
4195 }),
4196 );
4197
4198 test.provider("both B and C fail to create - D stuck", (stack) =>
4199 Effect.gen(function* () {
4200 const program = Effect.gen(function* () {
4201 const A = yield* TestResource("A", { string: "a-value" });
4202 const B = yield* TestResource("B", { string: A.string });
4203 const C = yield* TestResource("C", { string: A.string });
4204 const D = yield* TestResource("D", {
4205 string: Output.interpolate`${B.string}-${C.string}`,
4206 });
4207 return { A, B, C, D };
4208 });
4209
4210 yield* program.pipe(
4211 stack.deploy,
4212 hook(
4213 failOnMultiple([
4214 { id: "B", hook: "create" },
4215 { id: "C", hook: "create" },
4216 ]),
4217 ),
4218 );
4219
4220 expect((yield* getState("A"))?.status).toEqual("created");
4221 // effect terminates eagerly, so it's possible that B or C to run first and block C from running
4222 const BState = yield* getState("B");
4223 const CState = yield* getState("C");
4224 expect(BState?.status).toBeOneOf(["creating", undefined]);
4225 expect(CState?.status).toBeOneOf(["creating", undefined]);
4226 // at leasst one of B or C should have been created
4227 expect(BState?.status ?? CState?.status).toEqual("creating");
4228
4229 expectNotStarted(yield* getState("D"));
4230
4231 // Recovery
4232 const output = yield* program.pipe(stack.deploy);
4233 expect((yield* getState("B"))?.status).toEqual("created");
4234 expect((yield* getState("C"))?.status).toEqual("created");
4235 expect((yield* getState("D"))?.status).toEqual("created");
4236 expect(output.D.string).toEqual("a-value-a-value");
4237 }),
4238 );
4239 });
4240
4241 describe("update failures", () => {
4242 test.provider("A update fails - B, C, D remain stable", (stack) =>
4243 Effect.gen(function* () {
4244 yield* Effect.gen(function* () {
4245 const A = yield* TestResource("A", { string: "a-value" });
4246 const B = yield* TestResource("B", { string: A.string });
4247 const C = yield* TestResource("C", { string: A.string });
4248 yield* TestResource("D", { string: Output.interpolate`${B.string}-${C.string}` });
4249 }).pipe(stack.deploy);
4250
4251 const program = Effect.gen(function* () {
4252 const A = yield* TestResource("A", { string: "updated" });
4253 const B = yield* TestResource("B", { string: A.string });
4254 const C = yield* TestResource("C", { string: A.string });
4255 const D = yield* TestResource("D", {
4256 string: Output.interpolate`${B.string}-${C.string}`,
4257 });
4258 return { A, B, C, D };
4259 });
4260
4261 yield* program.pipe(stack.deploy, hook(failOn("A", "update")));
4262
4263 expect((yield* getState("A"))?.status).toEqual("updating");
4264 expect((yield* getState("B"))?.status).toEqual("created");
4265 expect((yield* getState("C"))?.status).toEqual("created");
4266 expect((yield* getState("D"))?.status).toEqual("created");
4267
4268 // Recovery
4269 const output = yield* program.pipe(stack.deploy);
4270 expect((yield* getState("A"))?.status).toEqual("updated");
4271 expect((yield* getState("B"))?.status).toEqual("updated");
4272 expect((yield* getState("C"))?.status).toEqual("updated");
4273 expect((yield* getState("D"))?.status).toEqual("updated");
4274 expect(output.D.string).toEqual("updated-updated");
4275 }),
4276 );
4277
4278 test.provider("A updates, B update fails - C may update, D stuck", (stack) =>
4279 Effect.gen(function* () {
4280 yield* Effect.gen(function* () {
4281 const A = yield* TestResource("A", { string: "a-value" });
4282 const B = yield* TestResource("B", { string: A.string });
4283 const C = yield* TestResource("C", { string: A.string });
4284 yield* TestResource("D", { string: Output.interpolate`${B.string}-${C.string}` });
4285 }).pipe(stack.deploy);
4286
4287 const program = Effect.gen(function* () {
4288 const A = yield* TestResource("A", { string: "updated" });
4289 const B = yield* TestResource("B", { string: A.string });
4290 const C = yield* TestResource("C", { string: A.string });
4291 const D = yield* TestResource("D", {
4292 string: Output.interpolate`${B.string}-${C.string}`,
4293 });
4294 return { A, B, C, D };
4295 });
4296
4297 yield* program.pipe(stack.deploy, hook(failOn("B", "update")));
4298
4299 expect((yield* getState("A"))?.status).toEqual("updated");
4300 expect((yield* getState("B"))?.status).toEqual("updating");
4301 // C might have been updated since it doesn't depend on B
4302 const cState = yield* getState("C");
4303 expect(cState?.status === "created" || cState?.status === "updated").toBe(true);
4304 expect((yield* getState("D"))?.status).toEqual("created");
4305
4306 // Recovery
4307 const output = yield* program.pipe(stack.deploy);
4308 expect((yield* getState("B"))?.status).toEqual("updated");
4309 expect((yield* getState("C"))?.status).toEqual("updated");
4310 expect((yield* getState("D"))?.status).toEqual("updated");
4311 expect(output.D.string).toEqual("updated-updated");
4312 }),
4313 );
4314
4315 test.provider("A, B, C update - D update fails", (stack) =>
4316 Effect.gen(function* () {
4317 yield* Effect.gen(function* () {
4318 const A = yield* TestResource("A", { string: "a-value" });
4319 const B = yield* TestResource("B", { string: A.string });
4320 const C = yield* TestResource("C", { string: A.string });
4321 yield* TestResource("D", { string: Output.interpolate`${B.string}-${C.string}` });
4322 }).pipe(stack.deploy);
4323
4324 const program = Effect.gen(function* () {
4325 const A = yield* TestResource("A", { string: "updated" });
4326 const B = yield* TestResource("B", { string: A.string });
4327 const C = yield* TestResource("C", { string: A.string });
4328 const D = yield* TestResource("D", {
4329 string: Output.interpolate`${B.string}-${C.string}`,
4330 });
4331 return { A, B, C, D };
4332 });
4333
4334 yield* program.pipe(stack.deploy, hook(failOn("D", "update")));
4335
4336 expect((yield* getState("A"))?.status).toEqual("updated");
4337 expect((yield* getState("B"))?.status).toEqual("updated");
4338 expect((yield* getState("C"))?.status).toEqual("updated");
4339 expect((yield* getState("D"))?.status).toEqual("updating");
4340
4341 // Recovery
4342 const output = yield* program.pipe(stack.deploy);
4343 expect((yield* getState("D"))?.status).toEqual("updated");
4344 expect(output.D.string).toEqual("updated-updated");
4345 }),
4346 );
4347 });
4348
4349 describe("delete failures", () => {
4350 test.provider("D delete fails - B, C, A waiting", (stack) =>
4351 Effect.gen(function* () {
4352 yield* Effect.gen(function* () {
4353 const A = yield* TestResource("A", { string: "a-value" });
4354 const B = yield* TestResource("B", { string: A.string });
4355 const C = yield* TestResource("C", { string: A.string });
4356 yield* TestResource("D", { string: Output.interpolate`${B.string}-${C.string}` });
4357 }).pipe(stack.deploy);
4358
4359 yield* stack.destroy().pipe(hook(failOn("D", "delete")));
4360
4361 expect((yield* getState("D"))?.status).toEqual("deleting");
4362 expect((yield* getState("B"))?.status).toEqual("created");
4363 expect((yield* getState("C"))?.status).toEqual("created");
4364 expect((yield* getState("A"))?.status).toEqual("created");
4365
4366 // Recovery
4367 yield* stack.destroy();
4368 expect(yield* getState("A")).toBeUndefined();
4369 expectNotStarted(yield* getState("B"));
4370 expectNotStarted(yield* getState("C"));
4371 expectNotStarted(yield* getState("D"));
4372 }),
4373 );
4374
4375 test.provider("D deleted, B delete fails - C may delete, A waiting", (stack) =>
4376 Effect.gen(function* () {
4377 yield* Effect.gen(function* () {
4378 const A = yield* TestResource("A", { string: "a-value" });
4379 const B = yield* TestResource("B", { string: A.string });
4380 const C = yield* TestResource("C", { string: A.string });
4381 yield* TestResource("D", { string: Output.interpolate`${B.string}-${C.string}` });
4382 }).pipe(stack.deploy);
4383
4384 yield* stack.destroy().pipe(hook(failOn("B", "delete")));
4385
4386 expectNotStarted(yield* getState("D"));
4387 expect((yield* getState("B"))?.status).toEqual("deleting");
4388 // C may or may not be deleted depending on execution order
4389 const cState = yield* getState("C");
4390 expect(cState === undefined || cState?.status === "created").toBe(true);
4391 expect((yield* getState("A"))?.status).toEqual("created");
4392
4393 // Recovery
4394 yield* stack.destroy();
4395 expect(yield* getState("A")).toBeUndefined();
4396 expectNotStarted(yield* getState("B"));
4397 expectNotStarted(yield* getState("C"));
4398 }),
4399 );
4400 });
4401 });
4402
4403 // =============================================================================
4404 // INDEPENDENT RESOURCES (no dependencies between them)
4405 // =============================================================================
4406
4407 describe("independent resources (A, B with no dependencies)", { tags: ["unit", "local"] }, () => {
4408 describe("parallel failures", () => {
4409 test.provider("both A and B fail to create", (stack) =>
4410 Effect.gen(function* () {
4411 const program = Effect.gen(function* () {
4412 const A = yield* TestResource("A", { string: "a-value" });
4413 const B = yield* TestResource("B", { string: "b-value" });
4414 return { A, B };
4415 });
4416
4417 yield* program.pipe(
4418 stack.deploy,
4419 hook(
4420 failOnMultiple([
4421 { id: "A", hook: "create" },
4422 { id: "B", hook: "create" },
4423 ]),
4424 ),
4425 );
4426
4427 // effect terminates eagerly, so it's possible that A or B runs first and blocks the other from running
4428 const AState = yield* getState("A");
4429 const BState = yield* getState("B");
4430 expect(AState?.status).toBeOneOf(["creating", undefined]);
4431 expect(BState?.status).toBeOneOf(["creating", undefined]);
4432 // at least one of A or B should have been creating
4433 expect(AState?.status ?? BState?.status).toEqual("creating");
4434
4435 // Recovery
4436 const output = yield* program.pipe(stack.deploy);
4437 expect((yield* getState("A"))?.status).toEqual("created");
4438 expect((yield* getState("B"))?.status).toEqual("created");
4439 expect(output.A.string).toEqual("a-value");
4440 expect(output.B.string).toEqual("b-value");
4441 }),
4442 );
4443
4444 test.provider("A creates, B fails - recovery creates B", (stack) =>
4445 Effect.gen(function* () {
4446 const program = Effect.gen(function* () {
4447 const A = yield* TestResource("A", { string: "a-value" });
4448 const B = yield* TestResource("B", { string: "b-value" });
4449 return { A, B };
4450 });
4451
4452 yield* program.pipe(stack.deploy, hook(failOn("B", "create")));
4453
4454 expect((yield* getState("A"))?.status).toEqual("created");
4455 expect((yield* getState("B"))?.status).toEqual("creating");
4456
4457 // Recovery
4458 const output = yield* program.pipe(stack.deploy);
4459 expect((yield* getState("A"))?.status).toEqual("created");
4460 expect((yield* getState("B"))?.status).toEqual("created");
4461 expect(output.B.string).toEqual("b-value");
4462 }),
4463 );
4464
4465 test.provider("A update fails, B update succeeds", (stack) =>
4466 Effect.gen(function* () {
4467 yield* Effect.gen(function* () {
4468 yield* TestResource("A", { string: "a-value" });
4469 yield* TestResource("B", { string: "b-value" });
4470 }).pipe(stack.deploy);
4471
4472 const program = Effect.gen(function* () {
4473 const A = yield* TestResource("A", { string: "a-updated" });
4474 const B = yield* TestResource("B", { string: "b-updated" });
4475 return { A, B };
4476 });
4477
4478 yield* program.pipe(stack.deploy, hook(failOn("A", "update")));
4479
4480 expect((yield* getState("A"))?.status).toEqual("updating");
4481 // B might have been updated
4482 const bState = yield* getState("B");
4483 expect(bState?.status === "created" || bState?.status === "updated").toBe(true);
4484
4485 // Recovery
4486 const output = yield* program.pipe(stack.deploy);
4487 expect((yield* getState("A"))?.status).toEqual("updated");
4488 expect((yield* getState("B"))?.status).toEqual("updated");
4489 expect(output.A.string).toEqual("a-updated");
4490 expect(output.B.string).toEqual("b-updated");
4491 }),
4492 );
4493 });
4494
4495 describe("mixed state recovery", () => {
4496 test.provider("A in creating, B in updating state - recovery completes both", (stack) =>
4497 Effect.gen(function* () {
4498 // First create B successfully
4499 yield* Effect.gen(function* () {
4500 yield* TestResource("B", { string: "b-value" });
4501 }).pipe(stack.deploy);
4502 expect((yield* getState("B"))?.status).toEqual("created");
4503
4504 // Now try to create A and update B - A fails
4505 const program = Effect.gen(function* () {
4506 const A = yield* TestResource("A", { string: "a-value" });
4507 const B = yield* TestResource("B", { string: "b-updated" });
4508 return { A, B };
4509 });
4510
4511 yield* program.pipe(
4512 stack.deploy,
4513 hook(
4514 failOnMultiple([
4515 { id: "A", hook: "create" },
4516 { id: "B", hook: "update" },
4517 ]),
4518 ),
4519 );
4520
4521 // effect terminates eagerly, so it's possible that A or B runs first and blocks the other from running
4522 const AState = yield* getState("A");
4523 const BState = yield* getState("B");
4524 expect(AState?.status).toBeOneOf(["creating", undefined]);
4525 expect(BState?.status).toBeOneOf(["created", "updating"]);
4526 // at least one of A or B should have started their failing operation
4527 expect(AState?.status === "creating" || BState?.status === "updating").toBe(true);
4528
4529 // Recovery
4530 const output = yield* program.pipe(stack.deploy);
4531 expect((yield* getState("A"))?.status).toEqual("created");
4532 expect((yield* getState("B"))?.status).toEqual("updated");
4533 expect(output.A.string).toEqual("a-value");
4534 expect(output.B.string).toEqual("b-updated");
4535 }),
4536 );
4537
4538 test.provider("A in replacing, B in deleting state - complex recovery", (stack) =>
4539 Effect.gen(function* () {
4540 // Create both
4541 yield* Effect.gen(function* () {
4542 yield* TestResource("A", { replaceString: "original" });
4543 yield* TestResource("B", { string: "b-value" });
4544 }).pipe(stack.deploy);
4545 expect((yield* getState("A"))?.status).toEqual("created");
4546 expect((yield* getState("B"))?.status).toEqual("created");
4547
4548 // Try to replace A and delete B (by not including B) - both fail
4549 const program = Effect.gen(function* () {
4550 yield* TestResource("A", { replaceString: "changed" });
4551 });
4552
4553 yield* program.pipe(
4554 stack.deploy,
4555 hook(
4556 failOnMultiple([
4557 { id: "A", hook: "create" },
4558 { id: "B", hook: "delete" },
4559 ]),
4560 ),
4561 );
4562
4563 // effect terminates eagerly, so it's possible that A or B runs first and blocks the other from running
4564 const AState = yield* getState<ReplacingResourceState>("A");
4565 const BState = yield* getState("B");
4566 expect(AState?.status).toBeOneOf(["created", "replacing"]);
4567 expect(BState?.status).toBeOneOf(["created", "deleting"]);
4568 // at least one of A or B should have started their failing operation
4569 expect(AState?.status === "replacing" || BState?.status === "deleting").toBe(true);
4570
4571 // Recovery - complete the replace and delete
4572 yield* program.pipe(stack.deploy);
4573 expect((yield* getState("A"))?.status).toEqual("created");
4574 expectNotStarted(yield* getState("B"));
4575 }),
4576 );
4577 });
4578 });
4579
4580 // =============================================================================
4581 // MULTIPLE RESOURCES REPLACING SIMULTANEOUSLY
4582 // =============================================================================
4583
4584 describe("multiple resources replacing", { tags: ["unit", "local"] }, () => {
4585 test.provider("two independent resources replace successfully", (stack) =>
4586 Effect.gen(function* () {
4587 yield* Effect.gen(function* () {
4588 yield* TestResource("A", { replaceString: "a-original" });
4589 yield* TestResource("B", { replaceString: "b-original" });
4590 }).pipe(stack.deploy);
4591
4592 const output = yield* Effect.gen(function* () {
4593 const A = yield* TestResource("A", { replaceString: "a-new" });
4594 const B = yield* TestResource("B", { replaceString: "b-new" });
4595 return { A, B };
4596 }).pipe(stack.deploy);
4597
4598 expect((yield* getState("A"))?.status).toEqual("created");
4599 expect((yield* getState("B"))?.status).toEqual("created");
4600 expect(output.A.replaceString).toEqual("a-new");
4601 expect(output.B.replaceString).toEqual("b-new");
4602 }),
4603 );
4604
4605 test.provider("A replace fails, B replace succeeds - recovery completes A", (stack) =>
4606 Effect.gen(function* () {
4607 yield* Effect.gen(function* () {
4608 yield* TestResource("A", { replaceString: "a-original" });
4609 yield* TestResource("B", { replaceString: "b-original" });
4610 }).pipe(stack.deploy);
4611
4612 const program = Effect.gen(function* () {
4613 const A = yield* TestResource("A", { replaceString: "a-new" });
4614 const B = yield* TestResource("B", { replaceString: "b-new" });
4615 return { A, B };
4616 });
4617
4618 yield* program.pipe(stack.deploy, hook(failOn("A", "create")));
4619
4620 expect((yield* getState<ReplacingResourceState>("A"))?.status).toEqual("replacing");
4621 // B might have been replaced
4622 const bState = yield* getState("B");
4623 expect(
4624 bState?.status === "created" ||
4625 bState?.status === "replacing" ||
4626 bState?.status === "replaced",
4627 ).toBe(true);
4628
4629 // Recovery
4630 const output = yield* program.pipe(stack.deploy);
4631 expect((yield* getState("A"))?.status).toEqual("created");
4632 expect((yield* getState("B"))?.status).toEqual("created");
4633 expect(output.A.replaceString).toEqual("a-new");
4634 expect(output.B.replaceString).toEqual("b-new");
4635 }),
4636 );
4637
4638 test.provider("both A and B replace fail - recovery completes both", (stack) =>
4639 Effect.gen(function* () {
4640 yield* Effect.gen(function* () {
4641 yield* TestResource("A", { replaceString: "a-original" });
4642 yield* TestResource("B", { replaceString: "b-original" });
4643 }).pipe(stack.deploy);
4644
4645 const program = Effect.gen(function* () {
4646 const A = yield* TestResource("A", { replaceString: "a-new" });
4647 const B = yield* TestResource("B", { replaceString: "b-new" });
4648 return { A, B };
4649 });
4650
4651 yield* program.pipe(
4652 stack.deploy,
4653 hook(
4654 failOnMultiple([
4655 { id: "A", hook: "create" },
4656 { id: "B", hook: "create" },
4657 ]),
4658 ),
4659 );
4660
4661 // effect terminates eagerly, so it's possible that A or B runs first and blocks the other from running
4662 const AState = yield* getState<ReplacingResourceState>("A");
4663 const BState = yield* getState<ReplacingResourceState>("B");
4664 expect(AState?.status).toBeOneOf(["created", "replacing"]);
4665 expect(BState?.status).toBeOneOf(["created", "replacing"]);
4666 // at least one of A or B should have started replacing
4667 expect(AState?.status === "replacing" || BState?.status === "replacing").toBe(true);
4668
4669 // Recovery
4670 const output = yield* program.pipe(stack.deploy);
4671 expect((yield* getState("A"))?.status).toEqual("created");
4672 expect((yield* getState("B"))?.status).toEqual("created");
4673 expect(output.A.replaceString).toEqual("a-new");
4674 expect(output.B.replaceString).toEqual("b-new");
4675 }),
4676 );
4677
4678 test.provider(
4679 "A replaced, B replacing - old A delete fails, B create fails - recovery completes both",
4680 (stack) =>
4681 Effect.gen(function* () {
4682 yield* Effect.gen(function* () {
4683 yield* TestResource("A", { replaceString: "a-original" });
4684 yield* TestResource("B", { replaceString: "b-original" });
4685 }).pipe(stack.deploy);
4686
4687 const program = Effect.gen(function* () {
4688 const A = yield* TestResource("A", { replaceString: "a-new" });
4689 const B = yield* TestResource("B", { replaceString: "b-new" });
4690 return { A, B };
4691 });
4692
4693 yield* program.pipe(
4694 stack.deploy,
4695 hook(
4696 failOnMultiple([
4697 { id: "A", hook: "delete" },
4698 { id: "B", hook: "create" },
4699 ]),
4700 ),
4701 );
4702
4703 // effect terminates eagerly, so it's possible that A or B runs first and blocks the other from running
4704 // A should be replaced (new created, old pending delete) or still replacing/created if B failed first
4705 // B should be replacing (new not yet created) or already created if A failed first
4706 const AState = yield* getState<ReplacedResourceState>("A");
4707 const BState = yield* getState<ReplacingResourceState>("B");
4708 expect(AState?.status).toBeOneOf(["created", "replacing", "replaced"]);
4709 expect(BState?.status).toBeOneOf(["created", "replacing"]);
4710 // at least one of A or B should have started their failing operation
4711 expect(AState?.status === "replaced" || BState?.status === "replacing").toBe(true);
4712
4713 // Recovery
4714 const output = yield* program.pipe(stack.deploy);
4715 expect((yield* getState("A"))?.status).toEqual("created");
4716 expect((yield* getState("B"))?.status).toEqual("created");
4717 expect(output.A.replaceString).toEqual("a-new");
4718 expect(output.B.replaceString).toEqual("b-new");
4719 }),
4720 );
4721 });
4722
4723 describe("repeated replacements", { tags: ["unit", "local"] }, () => {
4724 test.provider("resource can be replaced again while still in replacing state", (stack) =>
4725 Effect.gen(function* () {
4726 yield* Effect.gen(function* () {
4727 yield* TestResource("A", { replaceString: "a-original" });
4728 }).pipe(stack.deploy);
4729
4730 const firstReplacement = Effect.gen(function* () {
4731 yield* TestResource("A", { replaceString: "a-first" });
4732 });
4733
4734 yield* firstReplacement.pipe(stack.deploy, hook(failOn("A", "create")));
4735
4736 const replacingState = yield* getState<ReplacingResourceState>("A");
4737 expect(replacingState?.status).toEqual("replacing");
4738
4739 const secondReplacement = Effect.gen(function* () {
4740 yield* TestResource("A", { replaceString: "a-second" });
4741 });
4742
4743 yield* secondReplacement.pipe(stack.deploy);
4744
4745 const finalState = yield* getState("A");
4746 expectConvergedStatus(finalState?.status);
4747 expect(finalState?.props?.replaceString).toEqual("a-second");
4748 }),
4749 );
4750
4751 test.provider("resource can be replaced again while still in replaced state", (stack) =>
4752 Effect.gen(function* () {
4753 yield* Effect.gen(function* () {
4754 yield* TestResource("A", { replaceString: "a-original" });
4755 }).pipe(stack.deploy);
4756
4757 const firstReplacement = Effect.gen(function* () {
4758 yield* TestResource("A", { replaceString: "a-first" });
4759 });
4760
4761 yield* firstReplacement.pipe(stack.deploy, hook(failOn("A", "delete")));
4762
4763 const replacedState = yield* getState<ReplacedResourceState>("A");
4764 expect(replacedState?.status).toEqual("replaced");
4765
4766 const secondReplacement = Effect.gen(function* () {
4767 yield* TestResource("A", { replaceString: "a-second" });
4768 });
4769
4770 yield* secondReplacement.pipe(stack.deploy);
4771
4772 const finalState = yield* getState("A");
4773 expectConvergedStatus(finalState?.status);
4774 expect(finalState?.props?.replaceString).toEqual("a-second");
4775 }),
4776 );
4777 });
4778
4779 // =============================================================================
4780 // ORPHAN CHAIN DELETION
4781 // =============================================================================
4782
4783 describe("orphan chain deletion", { tags: ["unit", "local"] }, () => {
4784 test.provider("three-level orphan chain deleted in correct order", (stack) =>
4785 Effect.gen(function* () {
4786 yield* Effect.gen(function* () {
4787 const A = yield* TestResource("A", { string: "a-value" });
4788 const B = yield* TestResource("B", { string: A.string });
4789 yield* TestResource("C", { string: B.string });
4790 }).pipe(stack.deploy);
4791 expect((yield* getState("A"))?.status).toEqual("created");
4792 expect((yield* getState("B"))?.status).toEqual("created");
4793 expect((yield* getState("C"))?.status).toEqual("created");
4794
4795 // Remove C from graph - should delete C only
4796 yield* Effect.gen(function* () {
4797 const A = yield* TestResource("A", { string: "a-value" });
4798 yield* TestResource("B", { string: A.string });
4799 }).pipe(stack.deploy);
4800 expect((yield* getState("A"))?.status).toEqual("created");
4801 expect((yield* getState("B"))?.status).toEqual("created");
4802 expectNotStarted(yield* getState("C"));
4803 }),
4804 );
4805
4806 test.provider("orphan with intermediate failure recovers correctly", (stack) =>
4807 Effect.gen(function* () {
4808 yield* Effect.gen(function* () {
4809 const A = yield* TestResource("A", { string: "a-value" });
4810 const B = yield* TestResource("B", { string: A.string });
4811 yield* TestResource("C", { string: B.string });
4812 }).pipe(stack.deploy);
4813
4814 // Remove all three - C fails to delete
4815 yield* stack.destroy().pipe(hook(failOn("C", "delete")));
4816
4817 expect((yield* getState("C"))?.status).toEqual("deleting");
4818 expect((yield* getState("B"))?.status).toEqual("created");
4819 expect((yield* getState("A"))?.status).toEqual("created");
4820
4821 // Recovery
4822 yield* stack.destroy();
4823 expect(yield* getState("A")).toBeUndefined();
4824 expectNotStarted(yield* getState("B"));
4825 expectNotStarted(yield* getState("C"));
4826 }),
4827 );
4828
4829 test.provider("partial orphan - remove leaf, add new dependent", (stack) =>
4830 Effect.gen(function* () {
4831 yield* Effect.gen(function* () {
4832 const A = yield* TestResource("A", { string: "a-value" });
4833 yield* TestResource("B", { string: A.string });
4834 }).pipe(stack.deploy);
4835 expect((yield* getState("A"))?.status).toEqual("created");
4836 expect((yield* getState("B"))?.status).toEqual("created");
4837
4838 // Remove B, add C dependent on A
4839 const output = yield* Effect.gen(function* () {
4840 const A = yield* TestResource("A", { string: "a-value" });
4841 const C = yield* TestResource("C", { string: A.string });
4842 return { A, C };
4843 }).pipe(stack.deploy);
4844 expect((yield* getState("A"))?.status).toEqual("created");
4845 expectNotStarted(yield* getState("B"));
4846 expect((yield* getState("C"))?.status).toEqual("created");
4847 expect(output.C.string).toEqual("a-value");
4848 }),
4849 );
4850 });
4851
4852 // =============================================================================
4853 // COMPLEX MIXED STATE SCENARIOS
4854 // =============================================================================
4855
4856 describe("complex mixed state scenarios", { tags: ["unit", "local"] }, () => {
4857 test.provider("replace upstream while creating downstream", (stack) =>
4858 Effect.gen(function* () {
4859 // Create A
4860 yield* Effect.gen(function* () {
4861 yield* TestResource("A", { string: "a-value", replaceString: "original" });
4862 }).pipe(stack.deploy);
4863 expect((yield* getState("A"))?.status).toEqual("created");
4864
4865 // Now add B dependent on A, and also replace A
4866 const output = yield* Effect.gen(function* () {
4867 const A = yield* TestResource("A", { string: "a-value-new", replaceString: "changed" });
4868 const B = yield* TestResource("B", { string: A.string });
4869 return { A, B };
4870 }).pipe(stack.deploy);
4871
4872 expect((yield* getState("A"))?.status).toEqual("created");
4873 expect((yield* getState("B"))?.status).toEqual("created");
4874 expect(output.A.string).toEqual("a-value-new");
4875 expect(output.B.string).toEqual("a-value-new");
4876 }),
4877 );
4878
4879 test.provider("update upstream, create and delete in same apply", (stack) =>
4880 Effect.gen(function* () {
4881 // Create A and B
4882 yield* Effect.gen(function* () {
4883 yield* TestResource("A", { string: "a-value" });
4884 yield* TestResource("B", { string: "b-value" });
4885 }).pipe(stack.deploy);
4886
4887 // Update A, delete B (by not including), create C
4888 const output = yield* Effect.gen(function* () {
4889 const A = yield* TestResource("A", { string: "a-updated" });
4890 const C = yield* TestResource("C", { string: A.string });
4891 return { A, C };
4892 }).pipe(stack.deploy);
4893
4894 expect((yield* getState("A"))?.status).toEqual("updated");
4895 expectNotStarted(yield* getState("B"));
4896 expect((yield* getState("C"))?.status).toEqual("created");
4897 expect(output.C.string).toEqual("a-updated");
4898 }),
4899 );
4900
4901 test.provider("chain reaction: A replace triggers B update triggers C update", (stack) =>
4902 Effect.gen(function* () {
4903 yield* Effect.gen(function* () {
4904 const A = yield* TestResource("A", { string: "a-value", replaceString: "original" });
4905 const B = yield* TestResource("B", { string: A.string });
4906 yield* TestResource("C", { string: B.string });
4907 }).pipe(stack.deploy);
4908
4909 // Replace A - should cascade updates to B and C
4910 const output = yield* Effect.gen(function* () {
4911 const A = yield* TestResource("A", { string: "a-replaced", replaceString: "changed" });
4912 const B = yield* TestResource("B", { string: A.string });
4913 const C = yield* TestResource("C", { string: B.string });
4914 return { A, B, C };
4915 }).pipe(stack.deploy);
4916
4917 expect((yield* getState("A"))?.status).toEqual("created");
4918 expect((yield* getState("B"))?.status).toEqual("updated");
4919 expect((yield* getState("C"))?.status).toEqual("updated");
4920 expect(output.C.string).toEqual("a-replaced");
4921 }),
4922 );
4923
4924 test.provider("multiple failures across all operation types", (stack) =>
4925 Effect.gen(function* () {
4926 // Setup: A, B created; C, D will be added
4927 yield* Effect.gen(function* () {
4928 yield* TestResource("A", { string: "a-value", replaceString: "original" });
4929 yield* TestResource("B", { string: "b-value" });
4930 }).pipe(stack.deploy);
4931
4932 // Complex operation: A replace, B update, C create, D not included (nothing to delete)
4933 const program = Effect.gen(function* () {
4934 const A = yield* TestResource("A", { string: "a-replaced", replaceString: "changed" });
4935 const B = yield* TestResource("B", { string: "b-updated" });
4936 const C = yield* TestResource("C", { string: "c-value" });
4937 return { A, B, C };
4938 });
4939
4940 // Fail on A replace (create phase) and C create
4941 yield* program.pipe(
4942 stack.deploy,
4943 hook(
4944 failOnMultiple([
4945 { id: "A", hook: "create" },
4946 { id: "C", hook: "create" },
4947 ]),
4948 ),
4949 );
4950
4951 // effect terminates eagerly, so it's possible that A or C runs first and blocks the other from running
4952 const AState = yield* getState<ReplacingResourceState>("A");
4953 // B might have been updated
4954 const bState = yield* getState("B");
4955 expect(bState?.status === "created" || bState?.status === "updated").toBe(true);
4956 const CState = yield* getState("C");
4957 expect(AState?.status).toBeOneOf(["created", "replacing"]);
4958 expect(CState?.status).toBeOneOf(["creating", undefined]);
4959 // at least one of A or C should have started their failing operation
4960 expect(AState?.status === "replacing" || CState?.status === "creating").toBe(true);
4961
4962 // Recovery
4963 const output = yield* program.pipe(stack.deploy);
4964 expect((yield* getState("A"))?.status).toEqual("created");
4965 expect((yield* getState("B"))?.status).toEqual("updated");
4966 expect((yield* getState("C"))?.status).toEqual("created");
4967 expect(output.A.replaceString).toEqual("changed");
4968 expect(output.B.string).toEqual("b-updated");
4969 expect(output.C.string).toEqual("c-value");
4970 }),
4971 );
4972 });
4973
4974 describe("artifacts", { tags: ["unit", "local"] }, () => {
4975 test.provider("shares artifacts from plan diff into apply update", (stack) =>
4976 Effect.gen(function* () {
4977 yield* Effect.gen(function* () {
4978 yield* ArtifactProbe("A", { value: "v1" });
4979 }).pipe(stack.deploy);
4980
4981 const updated = yield* Effect.gen(function* () {
4982 const A = yield* ArtifactProbe("A", { value: "v2" });
4983 return { A };
4984 }).pipe(stack.deploy);
4985
4986 expect(updated.A.value).toEqual("v2");
4987 expect(updated.A.artifactValue).toEqual("v2");
4988 expect((yield* getState("A"))?.status).toEqual("updated");
4989 }),
4990 );
4991
4992 test.provider(
4993 "isolates artifact bags by FQN for namespaced resources with the same leaf logical ID",
4994 (stack) =>
4995 Effect.gen(function* () {
4996 const Site = (id: string, props: { value: string }) =>
4997 Effect.gen(function* () {
4998 return yield* ArtifactProbe("Shared", { value: props.value });
4999 }).pipe(Namespace.push(id));
5000
Showing first 5,000 of 11,345 lines. View raw