|
9 | 9 | */ |
10 | 10 | import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; |
11 | 11 | import { expect } from "@executablemd/test-support/expect"; |
12 | | -import { Err, scoped } from "effection"; |
| 12 | +import { scoped, suspend, withResolvers } from "effection"; |
13 | 13 | import type { Operation, Result } from "effection"; |
14 | 14 | import { InMemoryStream } from "@executablemd/durable-streams"; |
15 | 15 | import type { DurableEvent, Json } from "@executablemd/durable-streams"; |
@@ -487,6 +487,41 @@ function readingComponent(counted: Produced): ExecutionInstallation { |
487 | 487 | }; |
488 | 488 | } |
489 | 489 |
|
| 490 | +/** |
| 491 | + * One host component that says it was reached and then never finishes. |
| 492 | + * |
| 493 | + * The barrier is the component itself rather than a delay: the run is held at |
| 494 | + * a real unfinished operation, and the row below reads the handle while it is |
| 495 | + * genuinely in that state instead of while a timer happens to be pending. |
| 496 | + */ |
| 497 | +function holdingComponent(reached: { open: () => void }): ExecutionInstallation { |
| 498 | + return { |
| 499 | + components: [ |
| 500 | + { |
| 501 | + name: "Holding", |
| 502 | + origin: COMPUTED_ORIGIN, |
| 503 | + props: { type: "object", properties: {}, additionalProperties: false }, |
| 504 | + factory: () => |
| 505 | + function* Holding(): Operation<Json> { |
| 506 | + reached.open(); |
| 507 | + yield* suspend(); |
| 508 | + return null; |
| 509 | + }, |
| 510 | + }, |
| 511 | + ], |
| 512 | + }; |
| 513 | +} |
| 514 | + |
| 515 | +/** One entry that captures at its root and is then held, unfinished. */ |
| 516 | +const HOLDS_AFTER_CAPTURE = [ |
| 517 | + '<Let as="held" value={{ answer: 42 }} />', |
| 518 | + "", |
| 519 | + "<Holding />", |
| 520 | + "", |
| 521 | + "Never reached.", |
| 522 | + "", |
| 523 | +].join("\n"); |
| 524 | + |
490 | 525 | /** One entry that captures at its root through both sources. */ |
491 | 526 | const COMPUTES_ROOT = [ |
492 | 527 | '<Let as="direct" value={{ answer: 42 }} />', |
@@ -580,10 +615,9 @@ describe("Tier LET — the computed root data an execution hands back", () => { |
580 | 615 | it("C1: the owner reports every actual root capture, and output and value stay separate", function* () { |
581 | 616 | const counted: Produced = { calls: 0 }; |
582 | 617 | const stream = new InMemoryStream(); |
583 | | - const execution = yield* executeInstalled( |
584 | | - { ...inlineSource(COMPUTES_ROOT), stream }, |
585 | | - [readingComponent(counted)], |
586 | | - ); |
| 618 | + const execution = yield* executeInstalled({ ...inlineSource(COMPUTES_ROOT), stream }, [ |
| 619 | + readingComponent(counted), |
| 620 | + ]); |
587 | 621 | const rendered = String(yield* collect(execution)); |
588 | 622 | const settled = yield* execution; |
589 | 623 | expect(settled.ok).toBe(true); |
@@ -622,19 +656,68 @@ describe("Tier LET — the computed root data an execution hands back", () => { |
622 | 656 | expect(held["verdict"]).toBe(undefined); |
623 | 657 | }); |
624 | 658 |
|
| 659 | + it("C5: a halted execution's own handle refuses the root it was reading", function* () { |
| 660 | + const stream = new InMemoryStream(); |
| 661 | + const arrived = withResolvers<void>(); |
| 662 | + const reached = { open: () => arrived.resolve() }; |
| 663 | + let retained: HostExecution | undefined; |
| 664 | + |
| 665 | + yield* scoped(function* () { |
| 666 | + const execution = yield* executeInstalled({ ...inlineSource(HOLDS_AFTER_CAPTURE), stream }, [ |
| 667 | + holdingComponent(reached), |
| 668 | + ]); |
| 669 | + // Kept past this scope on purpose: the question is what the handle a host |
| 670 | + // is still holding answers once the run behind it is gone. |
| 671 | + retained = execution; |
| 672 | + yield* arrived.operation; |
| 673 | + |
| 674 | + // While it is unfinished the handle reads, and reads the real thing — |
| 675 | + // which is what makes the refusal below a refusal rather than an owner |
| 676 | + // that never had anything. |
| 677 | + expect(yield* readBindings(execution)).toEqual({ held: { answer: 42 } }); |
| 678 | + }); |
| 679 | + |
| 680 | + // Leaving that scope halts the execution and joins its teardown. The |
| 681 | + // document settled nothing, so there is no result to read and no live |
| 682 | + // environment left to read instead: the same handle refuses, with no map. |
| 683 | + if (retained === undefined) { |
| 684 | + throw new Error("this run never produced a handle to retain"); |
| 685 | + } |
| 686 | + const after = yield* retained.bindings(); |
| 687 | + expect(after.ok).toBe(false); |
| 688 | + expect(after.ok ? undefined : after.error).toBeInstanceOf(Error); |
| 689 | + expect(Object.hasOwn(after, "value")).toBe(false); |
| 690 | + // And halting it acknowledged nothing of its own. |
| 691 | + expect(stream.snapshot().some((event) => event.type === "close")).toBe(false); |
| 692 | + }); |
| 693 | + |
625 | 694 | it("C2: an ordinary failure keeps its computed data, and a cold reader finds both", function* () { |
626 | 695 | const counted: Produced = { calls: 0 }; |
627 | 696 | const stream = new InMemoryStream(); |
628 | | - const execution = yield* executeInstalled( |
629 | | - { ...inlineSource(COMPUTES_THEN_FAILS), stream }, |
630 | | - [readingComponent(counted)], |
631 | | - ); |
| 697 | + const execution = yield* executeInstalled({ ...inlineSource(COMPUTES_THEN_FAILS), stream }, [ |
| 698 | + readingComponent(counted), |
| 699 | + ]); |
632 | 700 | const settled = yield* execution; |
633 | 701 |
|
634 | 702 | // The public outcome is the failure it was, and the live error survives. |
635 | 703 | expect(settled.ok).toBe(false); |
636 | 704 | expect(settled.ok ? "" : settled.error.message).toContain("stopped on purpose"); |
637 | 705 | // And the data computed before the failure is still the execution's own. |
| 706 | + const answered = yield* readBindings(execution); |
| 707 | + expect(answered).toEqual({ |
| 708 | + direct: { answer: 42 }, |
| 709 | + functionJson: { answer: 42 }, |
| 710 | + }); |
| 711 | + expect(counted.calls).toBe(1); |
| 712 | + |
| 713 | + // C4: the answer is the reader's own graph. Writing through it is allowed |
| 714 | + // and reaches nothing: the accepted result is what the execution decided, |
| 715 | + // and a consumer editing its copy cannot edit that. |
| 716 | + const nested = answered["direct"]; |
| 717 | + if (nested === null || typeof nested !== "object" || Array.isArray(nested)) { |
| 718 | + throw new Error("this control needs a nested object to write through"); |
| 719 | + } |
| 720 | + nested["answer"] = 999; |
638 | 721 | expect(yield* readBindings(execution)).toEqual({ |
639 | 722 | direct: { answer: 42 }, |
640 | 723 | functionJson: { answer: 42 }, |
@@ -667,5 +750,8 @@ describe("Tier LET — the computed root data an execution hands back", () => { |
667 | 750 | }); |
668 | 751 | expect(cold.calls).toBe(0); |
669 | 752 | expect(reread.snapshot().length).toBe(records.length); |
| 753 | + // Neither the bytes the mutation was made against nor a separate execution |
| 754 | + // over them moved: the same records, and the same accepted data. |
| 755 | + expect(stream.snapshot()).toEqual(records); |
670 | 756 | }); |
671 | 757 | }); |
0 commit comments