|
| 1 | +/** |
| 2 | + * Posture matrix pins (visibility-oracle-posture.test.ts is the discovery |
| 3 | + * runner; these are the cells brought to a ruling, 2026-09-15). |
| 4 | + * |
| 5 | + * A — same-tick adoption is BY DESIGN: transitions are ambient; an action is |
| 6 | + * the special case that links one across async, and every write is a |
| 7 | + * conceptual transition. An action started later in the same tick owns |
| 8 | + * the ambient writes made before it. |
| 9 | + * C — latest() ENTANGLES (A15): optimistic lanes are transition-bound, so a |
| 10 | + * latest() read inside another live action merges that action with the |
| 11 | + * source's — "that doesn't mean optimism for both can't poke through in |
| 12 | + * the meanwhile": the value is served immediately, the reveal is joint. |
| 13 | + * B — creation under a transaction ESCAPES the hold (spec O2, recorded as |
| 14 | + * current behavior, not ruled): a memo + render effect created inside a |
| 15 | + * live action over a value another action holds direct-commits and |
| 16 | + * publishes the held value while untracked reads keep the committed |
| 17 | + * frame. "Generally creation escapes because it isn't visible" — the |
| 18 | + * matrix shows the creation's own effect does publish; kept as is |
| 19 | + * ("capture things as they are", 2026-09-15). A change here flips the |
| 20 | + * pin loudly and is a design decision. |
| 21 | + */ |
| 22 | +import { describe, expect, it } from "vitest"; |
| 23 | +import { |
| 24 | + action, |
| 25 | + createMemo, |
| 26 | + createRenderEffect, |
| 27 | + createRoot, |
| 28 | + createSignal, |
| 29 | + flush, |
| 30 | + latest |
| 31 | +} from "../src/index.js"; |
| 32 | + |
| 33 | +const tick = async () => { |
| 34 | + await Promise.resolve(); |
| 35 | + await Promise.resolve(); |
| 36 | + flush(); |
| 37 | +}; |
| 38 | + |
| 39 | +/** `x` held at 1 by action T (yields until `release`). */ |
| 40 | +function heldByAction() { |
| 41 | + const [x, setX] = createSignal(0); |
| 42 | + let release!: () => void; |
| 43 | + const run = action(function* () { |
| 44 | + setX(1); |
| 45 | + yield new Promise<void>(r => (release = r)); |
| 46 | + }); |
| 47 | + run(); |
| 48 | + flush(); |
| 49 | + expect(x()).toBe(0); |
| 50 | + return { x, release }; |
| 51 | +} |
| 52 | + |
| 53 | +describe("A — same-tick adoption is by design: transitions are ambient (posture matrix, ruled 2026-09-15)", () => { |
| 54 | + it("an action started after an ambient write in the same tick owns that write; it reveals with the action", async () => { |
| 55 | + const [x, setX] = createSignal(0); |
| 56 | + setX(1); // ambient, unflushed |
| 57 | + let release!: () => void; |
| 58 | + const run = action(function* () { |
| 59 | + yield new Promise<void>(r => (release = r)); |
| 60 | + }); |
| 61 | + run(); |
| 62 | + flush(); |
| 63 | + expect(x()).toBe(0); // adopted: held with the action, not committed by the flush |
| 64 | + release(); |
| 65 | + await tick(); |
| 66 | + expect(x()).toBe(1); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("C — latest() entangles: lanes are transition-bound (A15, ruled 2026-09-15)", () => { |
| 71 | + it("latest(x) inside another live action serves the held value now and merges that action with x's transaction", async () => { |
| 72 | + const { x, release } = heldByAction(); |
| 73 | + const [y, setY] = createSignal(0); |
| 74 | + let seen: number | undefined; |
| 75 | + let releaseU!: () => void; |
| 76 | + const runU = action(function* () { |
| 77 | + setY(1); |
| 78 | + seen = latest(x); // optimism pokes through immediately … |
| 79 | + yield new Promise<void>(r => (releaseU = r)); |
| 80 | + }); |
| 81 | + runU(); |
| 82 | + flush(); |
| 83 | + expect(seen).toBe(1); |
| 84 | + release(); // … but T alone no longer reveals: U and T settle as one |
| 85 | + await tick(); |
| 86 | + expect(x()).toBe(0); |
| 87 | + expect(y()).toBe(0); |
| 88 | + releaseU(); |
| 89 | + await tick(); |
| 90 | + expect(x()).toBe(1); |
| 91 | + expect(y()).toBe(1); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("B — creation under a transaction escapes the hold (OBSERVED, spec O2; not a ruling)", () => { |
| 96 | + it("a memo + render effect created inside another live action over a held value publishes the held value; the frame keeps the committed one until both actions settle", async () => { |
| 97 | + const { x, release } = heldByAction(); |
| 98 | + const [y, setY] = createSignal(0); |
| 99 | + const published: number[] = []; |
| 100 | + let releaseU!: () => void; |
| 101 | + const runU = action(function* () { |
| 102 | + setY(1); |
| 103 | + createRoot(() => { |
| 104 | + const m = createMemo(() => x()); |
| 105 | + createRenderEffect(m, v => { |
| 106 | + published.push(v); |
| 107 | + }); |
| 108 | + }); |
| 109 | + yield new Promise<void>(r => (releaseU = r)); |
| 110 | + }); |
| 111 | + runU(); |
| 112 | + flush(); |
| 113 | + expect(published).toEqual([1]); // observed: the creation direct-committed the held value |
| 114 | + expect(x()).toBe(0); // while the frame reads committed |
| 115 | + release(); // the creation derived from T's world, so U merged into T (A15): T alone does not reveal |
| 116 | + await tick(); |
| 117 | + expect(x()).toBe(0); |
| 118 | + releaseU(); |
| 119 | + await tick(); |
| 120 | + expect(x()).toBe(1); |
| 121 | + expect(published).toEqual([1]); |
| 122 | + }); |
| 123 | +}); |
0 commit comments