|
| 1 | +/** |
| 2 | + * #3147 — deep()/snapshot readers and per-key readers must agree while a |
| 3 | + * transaction holds store landings. |
| 4 | + * |
| 5 | + * deep()/snapshot compose from store backings; per-key reads answer through |
| 6 | + * node/backing visibility. #3145 removed the accidental capture (a stranger |
| 7 | + * transaction adopting a landing), but a LEGITIMATE hold — the #3164 fold of |
| 8 | + * a truth landing into the retaining transaction — reopened the seam: the |
| 9 | + * per-key channel masked the held landing (committed + optimism) while |
| 10 | + * deep() composed over the post-mutation pending backing (staged truth + |
| 11 | + * optimism), a frame no timeline contains. |
| 12 | + * |
| 13 | + * Ruling pinned here: deep()/snapshot stay the SPECULATIVE peek channel |
| 14 | + * (they see ordinary pending staging synchronously — the documented |
| 15 | + * divergence from context-free committed reads), but the hold decision is |
| 16 | + * SHARED with per-key reads: held truth is masked from both until the |
| 17 | + * transaction's reveal. |
| 18 | + */ |
| 19 | +import { describe, expect, it } from "vitest"; |
| 20 | +import { |
| 21 | + action, |
| 22 | + createOptimisticStore, |
| 23 | + createRenderEffect, |
| 24 | + createRoot, |
| 25 | + deep, |
| 26 | + flush, |
| 27 | + snapshot |
| 28 | +} from "../../src/index.js"; |
| 29 | + |
| 30 | +const settle = async (n = 3) => { |
| 31 | + for (let i = 0; i < n; i++) { |
| 32 | + await new Promise(r => setTimeout(r, 0)); |
| 33 | + flush(); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +describe("#3147: reader families agree under a held fold", () => { |
| 38 | + it("deep() composes the same view as per-key reads through the hold and the reveal", async () => { |
| 39 | + let resolveTruth!: () => void; |
| 40 | + const truth = new Promise<void>(r => (resolveTruth = r)); |
| 41 | + let resolveGate!: () => void; |
| 42 | + const gate = new Promise<void>(r => (resolveGate = r)); |
| 43 | + |
| 44 | + const keyLog: string[] = []; |
| 45 | + const deepLog: string[] = []; |
| 46 | + const snapLog: string[] = []; |
| 47 | + let state!: { list: number[] }; |
| 48 | + let setState!: (fn: (s: { list: number[] }) => void) => void; |
| 49 | + let run!: () => Promise<unknown>; |
| 50 | + let dispose!: () => void; |
| 51 | + |
| 52 | + createRoot(d => { |
| 53 | + dispose = d; |
| 54 | + [state, setState] = createOptimisticStore<{ list: number[] }>( |
| 55 | + async function* () { |
| 56 | + yield { list: [1, 2, 3] }; |
| 57 | + await truth; |
| 58 | + yield { list: [3, 2, 1] }; |
| 59 | + }, |
| 60 | + { list: [] } |
| 61 | + ); |
| 62 | + run = action(function* () { |
| 63 | + // The optimistic edit retains the transaction on the family: the |
| 64 | + // truth landing below folds into it instead of revealing. |
| 65 | + setState(s => { |
| 66 | + s.list.push(1111); |
| 67 | + }); |
| 68 | + yield gate; |
| 69 | + }); |
| 70 | + createRenderEffect( |
| 71 | + () => JSON.stringify(state.list), |
| 72 | + v => { |
| 73 | + keyLog.push(v); |
| 74 | + } |
| 75 | + ); |
| 76 | + createRenderEffect( |
| 77 | + () => JSON.stringify(deep(state).list), |
| 78 | + v => { |
| 79 | + deepLog.push(v); |
| 80 | + } |
| 81 | + ); |
| 82 | + // snapshot() from the same tracked scope must agree with deep(). |
| 83 | + createRenderEffect( |
| 84 | + () => `${JSON.stringify(deep(state).list)}|${JSON.stringify(snapshot(state).list)}`, |
| 85 | + v => { |
| 86 | + snapLog.push(v); |
| 87 | + } |
| 88 | + ); |
| 89 | + }); |
| 90 | + flush(); |
| 91 | + await settle(); |
| 92 | + expect(keyLog.at(-1)).toBe("[1,2,3]"); |
| 93 | + expect(deepLog.at(-1)).toBe("[1,2,3]"); |
| 94 | + |
| 95 | + const done = run(); |
| 96 | + flush(); |
| 97 | + await settle(); |
| 98 | + // Optimistic frame in both reader families. |
| 99 | + expect(keyLog.at(-1)).toBe("[1,2,3,1111]"); |
| 100 | + expect(deepLog.at(-1)).toBe("[1,2,3,1111]"); |
| 101 | + |
| 102 | + // Truth lands mid-action: it folds into the retaining transaction and is |
| 103 | + // HELD. Per-key readers keep the pre-hold view; deep()/snapshot must |
| 104 | + // agree — not report the post-mutation [3,2,1,1111]. |
| 105 | + resolveTruth(); |
| 106 | + await settle(); |
| 107 | + expect(keyLog.at(-1)).toBe("[1,2,3,1111]"); |
| 108 | + expect(deepLog.at(-1)).toBe("[1,2,3,1111]"); |
| 109 | + |
| 110 | + // Settle: atomic reveal, override reverts, landed truth shows — in both. |
| 111 | + resolveGate(); |
| 112 | + await done; |
| 113 | + await settle(); |
| 114 | + expect(keyLog.at(-1)).toBe("[3,2,1]"); |
| 115 | + expect(deepLog.at(-1)).toBe("[3,2,1]"); |
| 116 | + |
| 117 | + // At no point may the families disagree (dedupe: the deep observer may |
| 118 | + // legitimately fire more often). |
| 119 | + const dedupe = (log: string[]) => log.filter((v, i) => v !== log[i - 1]); |
| 120 | + expect(dedupe(deepLog)).toEqual(dedupe(keyLog)); |
| 121 | + // deep() and snapshot() from the same scope always agree with each other. |
| 122 | + for (const frame of snapLog) { |
| 123 | + const [d, s] = frame.split("|"); |
| 124 | + expect(d).toBe(s); |
| 125 | + } |
| 126 | + dispose(); |
| 127 | + flush(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments