|
8 | 8 |
|
9 | 9 | import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; |
10 | 10 | import { expect } from "@executablemd/test-support/expect"; |
11 | | -import { ensure, scoped, until } from "effection"; |
| 11 | +import { ensure, scoped, spawn, until, withResolvers } from "effection"; |
12 | 12 | import type { Operation } from "effection"; |
13 | 13 | import { forEach } from "@effectionx/stream-helpers"; |
14 | 14 | import { InMemoryStream } from "@executablemd/durable-streams"; |
@@ -486,6 +486,295 @@ describe("Tier RV — component return values", () => { |
486 | 486 | expect(said(result)).toContain("not the caller"); |
487 | 487 | }); |
488 | 488 |
|
| 489 | + it("RV11b: a caller's <Return> selects through a function component's content()", function* () { |
| 490 | + const result = yield* runFixture({ |
| 491 | + "doc.md": [ |
| 492 | + "---", |
| 493 | + "returns:", |
| 494 | + " type: string", |
| 495 | + "---", |
| 496 | + "", |
| 497 | + "<Wrap>", |
| 498 | + '<Return value="through content()" />', |
| 499 | + "</Wrap>", |
| 500 | + "", |
| 501 | + ].join("\n"), |
| 502 | + "Wrap.ts": [ |
| 503 | + 'import { content } from "@executablemd/core";', |
| 504 | + "export default function*() {", |
| 505 | + " const rendered = yield* content();", |
| 506 | + " return `wrapped ${rendered}`;", |
| 507 | + "}", |
| 508 | + "", |
| 509 | + ].join("\n"), |
| 510 | + }); |
| 511 | + expect(result.value).toBe("through content()"); |
| 512 | + }); |
| 513 | + |
| 514 | + it("RV11c: a <Return> render() generates stays reserved beneath a value body", function* () { |
| 515 | + // `render()` runs inside <Dynamic />, whose own value frame is active — |
| 516 | + // so this proves dynamic markdown is refused on its own terms rather than |
| 517 | + // for want of an owner to claim. |
| 518 | + const result = yield* run({ |
| 519 | + "doc.md": '<Dynamic as="d" />\n', |
| 520 | + "Dynamic.md": [ |
| 521 | + "---", |
| 522 | + "returns:", |
| 523 | + " type: string", |
| 524 | + "---", |
| 525 | + "", |
| 526 | + "```js eval", |
| 527 | + "const generated = yield* render(\'<Return value=\"dynamic\" />\');", |
| 528 | + "```", |
| 529 | + "", |
| 530 | + "{generated}", |
| 531 | + "", |
| 532 | + '<Return value="the component\'s own" />', |
| 533 | + "", |
| 534 | + ].join("\n"), |
| 535 | + }); |
| 536 | + // Diagnosed where it was generated, and no component named Return was |
| 537 | + // resolved to answer it. |
| 538 | + expect(said(result)).toContain("<Return> is reserved"); |
| 539 | + expect(said(result)).toContain("neither markdown produced at runtime"); |
| 540 | + // The generated element selected nothing: the run has no value at all. |
| 541 | + expect(result.value).toBeUndefined(); |
| 542 | + }); |
| 543 | + |
| 544 | + it("RV10c: two concurrent projections claim once and evaluate once", function* () { |
| 545 | + // Both projections of one authored <Return> are started before either is |
| 546 | + // awaited, so the second reaches the body while the first is in flight. |
| 547 | + // The claim is taken synchronously, before the claiming return evaluates |
| 548 | + // anything, so exactly one expression is ever evaluated. |
| 549 | + const result = yield* runFixture({ |
| 550 | + "doc.md": [ |
| 551 | + "---", |
| 552 | + "returns:", |
| 553 | + " type: string", |
| 554 | + "---", |
| 555 | + "", |
| 556 | + "```js eval", |
| 557 | + "globalThis.__rv10c = 0;", |
| 558 | + "const mark = () => {", |
| 559 | + " globalThis.__rv10c += 1;", |
| 560 | + ' return "evaluated";', |
| 561 | + "};", |
| 562 | + "```", |
| 563 | + "", |
| 564 | + "<Both>", |
| 565 | + "<Return value={mark()} />", |
| 566 | + "</Both>", |
| 567 | + "", |
| 568 | + ].join("\n"), |
| 569 | + "Both.ts": [ |
| 570 | + 'import { content } from "@executablemd/core";', |
| 571 | + 'import { all, spawn } from "effection";', |
| 572 | + "export default function*() {", |
| 573 | + " const first = yield* spawn(() => content());", |
| 574 | + " const second = yield* spawn(() => content());", |
| 575 | + " yield* all([first, second]);", |
| 576 | + ' return "both";', |
| 577 | + "}", |
| 578 | + "", |
| 579 | + ].join("\n"), |
| 580 | + }); |
| 581 | + |
| 582 | + expect(said(result)).toContain( |
| 583 | + "The root document declares `returns` but executed more than one <Return>.", |
| 584 | + ); |
| 585 | + // The second execution evaluated nothing of its own, and the first |
| 586 | + // selected value was discarded rather than published. |
| 587 | + expect((globalThis as Record<string, unknown>).__rv10c).toBe(1); |
| 588 | + expect(result.value).toBeUndefined(); |
| 589 | + }); |
| 590 | + |
| 591 | + it("RV12: a teardown failure after selection wins over the selected value", function* () { |
| 592 | + const result = yield* runFixture({ |
| 593 | + "doc.md": [ |
| 594 | + "---", |
| 595 | + "returns:", |
| 596 | + " type: string", |
| 597 | + "---", |
| 598 | + "", |
| 599 | + '<Return value="selected" />', |
| 600 | + "", |
| 601 | + '<Failing as="ignored" />', |
| 602 | + "", |
| 603 | + ].join("\n"), |
| 604 | + // The body completes; teardown is what fails, after the value was |
| 605 | + // already selected. |
| 606 | + "Failing.ts": [ |
| 607 | + 'import { ensure } from "effection";', |
| 608 | + "export const returns = { type: 'string' };", |
| 609 | + "export default function*() {", |
| 610 | + ' yield* ensure(function* () { throw new Error("TEARDOWN_FAILED"); });', |
| 611 | + ' return "body finished";', |
| 612 | + "}", |
| 613 | + "", |
| 614 | + ].join("\n"), |
| 615 | + }); |
| 616 | + |
| 617 | + expect(said(result)).toContain("TEARDOWN_FAILED"); |
| 618 | + expect(result.value).toBeUndefined(); |
| 619 | + }); |
| 620 | + |
| 621 | + it("RV12: halting after selection publishes no value and completes teardown", function* () { |
| 622 | + const reached = withResolvers<void>(); |
| 623 | + const torn: string[] = []; |
| 624 | + const globals = globalThis as Record<string, unknown>; |
| 625 | + globals.__rv12reached = () => reached.resolve(); |
| 626 | + globals.__rv12torn = () => torn.push("torn down"); |
| 627 | + yield* ensure(() => { |
| 628 | + delete globals.__rv12reached; |
| 629 | + delete globals.__rv12torn; |
| 630 | + }); |
| 631 | + |
| 632 | + yield* scoped(function* () { |
| 633 | + const dir = path.join(os.tmpdir(), `returns-halt-${randomUUID()}`); |
| 634 | + yield* ensureDir(dir); |
| 635 | + yield* ensure(() => rm(dir, { recursive: true, force: true })); |
| 636 | + yield* writeTextFile(path.join(dir, "package.json"), JSON.stringify({ type: "module" })); |
| 637 | + yield* until( |
| 638 | + symlink(path.join(ROOT, "node_modules"), path.join(dir, "node_modules"), "dir"), |
| 639 | + ); |
| 640 | + yield* writeTextFile( |
| 641 | + path.join(dir, "doc.md"), |
| 642 | + [ |
| 643 | + "---", |
| 644 | + "returns:", |
| 645 | + " type: string", |
| 646 | + "---", |
| 647 | + "", |
| 648 | + '<Return value="selected" />', |
| 649 | + "", |
| 650 | + // Suspends after the value was selected, so the halt below lands |
| 651 | + // between selection and completion. |
| 652 | + '<Suspends as="never" />', |
| 653 | + "", |
| 654 | + ].join("\n"), |
| 655 | + ); |
| 656 | + yield* writeTextFile( |
| 657 | + path.join(dir, "Suspends.ts"), |
| 658 | + [ |
| 659 | + 'import { ensure, suspend } from "effection";', |
| 660 | + "export const returns = { type: 'string' };", |
| 661 | + "export default function*() {", |
| 662 | + " yield* ensure(() => { globalThis.__rv12torn(); });", |
| 663 | + " globalThis.__rv12reached();", |
| 664 | + " yield* suspend();", |
| 665 | + ' return "unreachable";', |
| 666 | + "}", |
| 667 | + "", |
| 668 | + ].join("\n"), |
| 669 | + ); |
| 670 | + |
| 671 | + const outcome: string[] = []; |
| 672 | + const task = yield* spawn(function* () { |
| 673 | + const execution = yield* execute({ |
| 674 | + path: path.join(dir, "doc.md"), |
| 675 | + stream: new InMemoryStream(), |
| 676 | + includes: [dir], |
| 677 | + }); |
| 678 | + const result = yield* execution; |
| 679 | + outcome.push(result.ok ? "ok" : "err"); |
| 680 | + }); |
| 681 | + |
| 682 | + yield* reached.operation; |
| 683 | + yield* task.halt(); |
| 684 | + |
| 685 | + // Halted rather than completed: the selected value was never published |
| 686 | + // as a result, and the suspended component's teardown still ran. |
| 687 | + expect(outcome).toEqual([]); |
| 688 | + expect(torn).toEqual(["torn down"]); |
| 689 | + }); |
| 690 | + }); |
| 691 | + |
| 692 | + it("RV17a: a partial replay reconstructs selection without repeating an effect", function* () { |
| 693 | + const stream = new InMemoryStream(); |
| 694 | + const reached = withResolvers<void>(); |
| 695 | + const globals = globalThis as Record<string, unknown>; |
| 696 | + globals.__rv17reached = () => reached.resolve(); |
| 697 | + globals.__rv17suspend = true; |
| 698 | + yield* ensure(() => { |
| 699 | + delete globals.__rv17reached; |
| 700 | + delete globals.__rv17suspend; |
| 701 | + }); |
| 702 | + |
| 703 | + const dir = path.join(os.tmpdir(), `returns-replay-${randomUUID()}`); |
| 704 | + yield* ensureDir(dir); |
| 705 | + yield* ensure(() => rm(dir, { recursive: true, force: true })); |
| 706 | + yield* writeTextFile(path.join(dir, "package.json"), JSON.stringify({ type: "module" })); |
| 707 | + yield* until(symlink(path.join(ROOT, "node_modules"), path.join(dir, "node_modules"), "dir")); |
| 708 | + yield* writeTextFile( |
| 709 | + path.join(dir, "doc.md"), |
| 710 | + [ |
| 711 | + "---", |
| 712 | + "returns:", |
| 713 | + " type: string", |
| 714 | + "---", |
| 715 | + "", |
| 716 | + "```bash exec", |
| 717 | + "echo work", |
| 718 | + "```", |
| 719 | + "", |
| 720 | + '<Return value="selected" />', |
| 721 | + "", |
| 722 | + '<Gate as="gate" />', |
| 723 | + "", |
| 724 | + ].join("\n"), |
| 725 | + ); |
| 726 | + // Suspends on the first pass only, so the resume can finish. |
| 727 | + yield* writeTextFile( |
| 728 | + path.join(dir, "Gate.ts"), |
| 729 | + [ |
| 730 | + 'import { suspend } from "effection";', |
| 731 | + "export const returns = { type: 'string' };", |
| 732 | + "export default function*() {", |
| 733 | + " if (globalThis.__rv17suspend) {", |
| 734 | + " globalThis.__rv17reached();", |
| 735 | + " yield* suspend();", |
| 736 | + " }", |
| 737 | + ' return "gate";', |
| 738 | + "}", |
| 739 | + "", |
| 740 | + ].join("\n"), |
| 741 | + ); |
| 742 | + |
| 743 | + const first: string[] = []; |
| 744 | + yield* scoped(function* () { |
| 745 | + const task = yield* spawn(() => |
| 746 | + runExecution({ |
| 747 | + path: path.join(dir, "doc.md"), |
| 748 | + stream, |
| 749 | + includes: [dir], |
| 750 | + commands: first, |
| 751 | + }), |
| 752 | + ); |
| 753 | + yield* reached.operation; |
| 754 | + yield* task.halt(); |
| 755 | + }); |
| 756 | + expect(first).toEqual(["echo work"]); |
| 757 | + |
| 758 | + globals.__rv17suspend = false; |
| 759 | + const second: string[] = []; |
| 760 | + const resumed = yield* scoped(() => |
| 761 | + runExecution({ path: path.join(dir, "doc.md"), stream, includes: [dir], commands: second }), |
| 762 | + ); |
| 763 | + |
| 764 | + // The completed effect replayed from the journal rather than running. |
| 765 | + expect(second).toEqual([]); |
| 766 | + // Selection was reconstructed by ephemeral expansion, not recovered from |
| 767 | + // a record: the resumed run produces the same value. |
| 768 | + expect(resumed.value).toBe("selected"); |
| 769 | + // Nothing about the selection was journaled. |
| 770 | + const descriptions = stream |
| 771 | + .snapshot() |
| 772 | + .flatMap((event) => (event.type === "yield" ? [event.description.type] : [])); |
| 773 | + // The journal is not empty, so the absence above is a real absence. |
| 774 | + expect(descriptions).toContain("exec"); |
| 775 | + expect(descriptions).not.toContain("return"); |
| 776 | + }); |
| 777 | + |
489 | 778 | it("RV14: a nested value body owns a separate declaration", function* () { |
490 | 779 | const result = yield* run({ |
491 | 780 | "doc.md": '<Outer as="o" />\n\nGot {o}\n', |
@@ -614,11 +903,15 @@ describe("Tier RV — component return values", () => { |
614 | 903 | "", |
615 | 904 | '<Return value="one" />', |
616 | 905 | "", |
617 | | - '<Return value="two" />', |
| 906 | + // Evaluating this expression at all would throw a different, louder |
| 907 | + // error, so the duplicate diagnostic below is proof it was never |
| 908 | + // evaluated. |
| 909 | + "<Return value={missingBinding.evaluated} />", |
618 | 910 | "", |
619 | 911 | ].join("\n"), |
620 | 912 | }); |
621 | 913 | expect(said(result)).toContain("<Verdict /> declares `returns` but executed more than one"); |
| 914 | + expect(said(result)).not.toContain("missingBinding"); |
622 | 915 | // The first selected value is discarded rather than bound: the caller's |
623 | 916 | // `as` never resolves, so nothing renders it. |
624 | 917 | expect(said(result)).not.toContain("Got one"); |
|
0 commit comments