|
| 1 | +/** |
| 2 | + * A18 body-end corollary (#3427) — visibility during the correction window. |
| 3 | + * |
| 4 | + * When the action bodies have ended and nothing authoritative is in flight, |
| 5 | + * each override is superseded by the truth at hand — here the COMMITTED value, |
| 6 | + * since no landing staged anything. The window that follows must look like a |
| 7 | + * landing supersession to every reader: the display keeps the override until |
| 8 | + * the commit (A18 c), a fresh derivation is held (A29), and the verdict says |
| 9 | + * the truth differs (A18 d). Found by the visibility oracle: with nothing |
| 10 | + * staged the node carried no `_transition` stamp (an override written inside |
| 11 | + * an action never passes the adoption loop), so `supersededRead` served the |
| 12 | + * committed truth to a stale reader beside a display still showing the |
| 13 | + * override, a fresh memo published it, and `isPending` read false while |
| 14 | + * `latest` read the truth. Ownership for an override node is `_overrideOwner` |
| 15 | + * (#2912); the read path and the verdict now resolve it. |
| 16 | + */ |
| 17 | +import { describe, expect, it } from "vitest"; |
| 18 | +import { |
| 19 | + action, |
| 20 | + createMemo, |
| 21 | + createOptimistic, |
| 22 | + createRenderEffect, |
| 23 | + createRoot, |
| 24 | + createSignal, |
| 25 | + flush, |
| 26 | + isPending, |
| 27 | + latest |
| 28 | +} from "../src/index.js"; |
| 29 | + |
| 30 | +const settle = async () => { |
| 31 | + await Promise.resolve(); |
| 32 | + await Promise.resolve(); |
| 33 | + flush(); |
| 34 | +}; |
| 35 | + |
| 36 | +describe("A18 body-end supersession: the correction window", () => { |
| 37 | + it("display keeps the override, a stale re-run keeps it, a fresh derivation is held, the verdict says it differs", async () => { |
| 38 | + const flights: Array<() => void> = []; |
| 39 | + const [u, setU] = createSignal(0); |
| 40 | + const staleLog: number[] = []; |
| 41 | + let x!: () => number; |
| 42 | + let setX!: (v: number) => void; |
| 43 | + createRoot(() => { |
| 44 | + [x, setX] = createOptimistic(0); |
| 45 | + const downstream = createMemo(() => { |
| 46 | + const n = x(); |
| 47 | + return new Promise<string>(r => flights.push(() => r(`${n}!`))); |
| 48 | + }); |
| 49 | + createRenderEffect(downstream, () => {}); |
| 50 | + }); |
| 51 | + flush(); |
| 52 | + flights.shift()!(); // prime downstream(0) |
| 53 | + await settle(); |
| 54 | + // A pre-existing reader in another root (a sibling component), also |
| 55 | + // tracking an unrelated signal so it can be re-run mainline. |
| 56 | + createRoot(() => { |
| 57 | + createRenderEffect( |
| 58 | + () => { |
| 59 | + u(); |
| 60 | + return x(); |
| 61 | + }, |
| 62 | + v => { |
| 63 | + staleLog.push(v); |
| 64 | + } |
| 65 | + ); |
| 66 | + }); |
| 67 | + flush(); |
| 68 | + staleLog.length = 0; |
| 69 | + |
| 70 | + action(function* () { |
| 71 | + setX(1); |
| 72 | + yield Promise.resolve(); // the body ends; downstream(1) is still up |
| 73 | + })(); |
| 74 | + flush(); |
| 75 | + await settle(); |
| 76 | + await settle(); |
| 77 | + // Body-end: the override (1) is superseded by the committed truth (0); the |
| 78 | + // graph re-derives (a downstream flight for 0 starts) and the transaction |
| 79 | + // waits for it. |
| 80 | + expect(flights.length).toBe(2); |
| 81 | + expect(x()).toBe(1); // display |
| 82 | + expect(latest(x)).toBe(0); // truth |
| 83 | + expect(isPending(x)).toBe(true); // differs |
| 84 | + // (Reading the verdicts here is deliberate: a latest() pull once entered |
| 85 | + // the owning transaction ambiently and the two checks below depended on |
| 86 | + // whether latest() had been called first.) |
| 87 | + |
| 88 | + // A stale reader re-run by an unrelated write keeps displaying the override. |
| 89 | + setU(1); |
| 90 | + flush(); |
| 91 | + expect(staleLog).toEqual([1]); |
| 92 | + |
| 93 | + // A fresh mainline derivation derives from the truth and is held. |
| 94 | + const fresh: number[] = []; |
| 95 | + createRoot(() => { |
| 96 | + const m = createMemo(() => x()); |
| 97 | + createRenderEffect(m, v => { |
| 98 | + fresh.push(v); |
| 99 | + }); |
| 100 | + }); |
| 101 | + flush(); |
| 102 | + expect(fresh).toEqual([]); |
| 103 | + |
| 104 | + // The 0-flight lands: the transaction commits, everything reveals at once. |
| 105 | + for (const f of flights.splice(0)) f(); |
| 106 | + await settle(); |
| 107 | + await settle(); |
| 108 | + expect(x()).toBe(0); |
| 109 | + expect(isPending(x)).toBe(false); |
| 110 | + expect(fresh).toEqual([0]); |
| 111 | + expect(staleLog.at(-1)).toBe(0); |
| 112 | + }); |
| 113 | +}); |
0 commit comments