|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + * |
| 5 | + * C18 — classification waits for the drain. |
| 6 | + * |
| 7 | + * "A recordless adopted occurrence is classified (direct-insert vs invoked) |
| 8 | + * only after every record the document already holds for the boundary has |
| 9 | + * been applied: no sync that runs between the parser's end and the deferred |
| 10 | + * drain — the drain's own first `host.apply`, a live op, the live pump's |
| 11 | + * catch-up read — may evaluate a render prop as a zero-arg accessor." |
| 12 | + * |
| 13 | + * Ruling (frames-rulings 3.5, proposed): "an occurrence is classified only |
| 14 | + * after every delivered record has drained" — "pending" is the DRAIN's |
| 15 | + * state, not the parser's. The #2968 defer's bound was `recordsPending()` = |
| 16 | + * parser running or a fragment pending; a record whose data script already |
| 17 | + * ran sat in `_$HY.r` until the deferred `drainRecords` moved it into the |
| 18 | + * store, and nothing read that gap (contract §Red R9). |
| 19 | + * |
| 20 | + * Mechanism meant to carry it: frames/src/client.ts |
| 21 | + * `adoptBoundary.recordsPending`'s third term — `_$HY.r` holds a key under |
| 22 | + * the boundary's `sc:slot:<id>:` / `sc:region:<id>.` prefix that is not yet |
| 23 | + * in `appliedRecords` — read by frames/src/frame-client.ts `#syncSlots`' |
| 24 | + * defer arm. `drainRecords` marks a key applied BEFORE its `host.apply`, so |
| 25 | + * the sync that apply runs sees the other delivered records as pending and |
| 26 | + * its own as drained. |
| 27 | + * |
| 28 | + * Observation: the fills here are REAL — `p => <li>{p.text}{tick()}</li>`, |
| 29 | + * the props read unguarded (plus one untracked identification read, as a |
| 30 | + * fill's top-level read is otherwise a STRICT_READ diagnostic). Classified |
| 31 | + * direct-insert, the render prop is evaluated as a zero-arg accessor inside |
| 32 | + * the insert effect: `p.text` is a `TypeError` and the reactive system |
| 33 | + * halts (`REACTIVITY_HALTED`). The pin |
| 34 | + * asserts the opposite: every occurrence invoked once with its args, the |
| 35 | + * server `<li>` claimed in place, no error, and the page still reactive |
| 36 | + * after the `tick` bump. The harness's replay pins (`harness/replay.spec.tsx` |
| 37 | + * C18 ×3) hold the same three orders through the oracle's tolerant fill; |
| 38 | + * this file pins the consequence for a real one. |
| 39 | + */ |
| 40 | +import { afterEach, describe, expect, test, vi } from "vitest"; |
| 41 | +import { createSignal, flush, untrack } from "solid-js"; |
| 42 | +import { hydrate } from "@solidjs/web"; |
| 43 | +import { |
| 44 | + bootPage, |
| 45 | + fillHtml2, |
| 46 | + frameHtml, |
| 47 | + freshFid, |
| 48 | + holeHtml, |
| 49 | + microtasks, |
| 50 | + quiesce, |
| 51 | + slotRange, |
| 52 | + type Page |
| 53 | +} from "./support.js"; |
| 54 | + |
| 55 | +let page: Page | undefined; |
| 56 | +afterEach(async () => { |
| 57 | + await page?.cleanup(); |
| 58 | + page = undefined; |
| 59 | +}); |
| 60 | + |
| 61 | +/** The server render of `p => <li>{p.text}{tick()}</li>` at tick 0. */ |
| 62 | +const liveFillHtml = (fid: string, occ: string, text: string) => fillHtml2(fid, occ, text, "0"); |
| 63 | + |
| 64 | +/** |
| 65 | + * The parser's clock: `document.readyState` reads "loading" until `done()` |
| 66 | + * — the records the document still owes execute while it is running, and |
| 67 | + * the response's tail (the last data script, then the end) parses in one |
| 68 | + * go before any timer fires, so the restore is synchronous with the last |
| 69 | + * record. |
| 70 | + */ |
| 71 | +function parserRunning() { |
| 72 | + const spy = vi.spyOn(document, "readyState", "get").mockReturnValue("loading"); |
| 73 | + return () => spy.mockRestore(); |
| 74 | +} |
| 75 | + |
| 76 | +describe("C18 — classification waits for the drain", () => { |
| 77 | + // Arm (a): the drain's own first apply. Two render-prop occurrences; both |
| 78 | + // records owed when the boundary adopts (the #2968 defer arms); both |
| 79 | + // execute, the parser finishes, THEN the deferred drain fires. Its first |
| 80 | + // `host.apply` syncs the frame while the second record is still one loop |
| 81 | + // iteration away in `_$HY.r`: that sync must defer item#1, not classify |
| 82 | + // it — the loop's next apply mounts it with its args. |
| 83 | + test("(a) two records drained after the parser finished: each occurrence claims with its args; nothing is evaluated argless", async () => { |
| 84 | + const fid = freshFid("c18a"); |
| 85 | + const parserDone = parserRunning(); |
| 86 | + page = bootPage( |
| 87 | + frameHtml( |
| 88 | + fid, |
| 89 | + `<ul>${slotRange("item#0", liveFillHtml(fid, "item#0", "p0"))}${slotRange( |
| 90 | + "item#1", |
| 91 | + liveFillHtml(fid, "item#1", "p1") |
| 92 | + )}</ul>` |
| 93 | + ) |
| 94 | + ); |
| 95 | + const serverLis = [...page.container.querySelectorAll("li")]; |
| 96 | + const Comp = (globalThis as any)._$SC.r(fid); |
| 97 | + const [tick, setTick] = createSignal(0); |
| 98 | + const invoked: string[] = []; |
| 99 | + const dispose = hydrate( |
| 100 | + () => ( |
| 101 | + <Comp |
| 102 | + item={(p: { text: string }) => { |
| 103 | + invoked.push(untrack(() => p.text)); |
| 104 | + return ( |
| 105 | + <li> |
| 106 | + {p.text} |
| 107 | + {tick()} |
| 108 | + </li> |
| 109 | + ); |
| 110 | + }} |
| 111 | + /> |
| 112 | + ), |
| 113 | + page.container |
| 114 | + ); |
| 115 | + // Adopted with the parser running: both occurrences deferred, nothing |
| 116 | + // invoked, the server markup untouched. |
| 117 | + expect(invoked).toEqual([]); |
| 118 | + expect(page.container.textContent).toBe("p0" + "0" + "p1" + "0"); |
| 119 | + |
| 120 | + // The document's tail: both data scripts, then the end of the response |
| 121 | + // — before the deferred drain's macrotask. |
| 122 | + page.slotRecord(fid, "item#0", { text: "p0" }); |
| 123 | + page.slotRecord(fid, "item#1", { text: "p1" }); |
| 124 | + parserDone(); |
| 125 | + expect(invoked).toEqual([]); |
| 126 | + |
| 127 | + await quiesce(); |
| 128 | + await quiesce(); |
| 129 | + expect(invoked.sort()).toEqual(["p0", "p1"]); |
| 130 | + expect([...page.container.querySelectorAll("li")]).toEqual(serverLis); |
| 131 | + expect(page.container.textContent).toBe("p0" + "0" + "p1" + "0"); |
| 132 | + setTick(1); |
| 133 | + flush(); |
| 134 | + expect(page.container.textContent).toBe("p0" + "1" + "p1" + "1"); |
| 135 | + expect(page.warnings).toEqual([]); |
| 136 | + expect(page.errors).toEqual([]); |
| 137 | + dispose(); |
| 138 | + }); |
| 139 | + |
| 140 | + // Arm (b): a sync the drain did not trigger, in the same window. One |
| 141 | + // occurrence and a live hole; the record executes and the parser finishes |
| 142 | + // while the defer is armed; a live hole op then lands — the pump's read is |
| 143 | + // a microtask, the drain a macrotask — and its `host.apply` syncs the frame |
| 144 | + // over the undrained record. That sync must defer the occurrence; the |
| 145 | + // drain's apply mounts it. (The pump's catch-up read over ops logged |
| 146 | + // before adoption is the same sync from the other side of adoption — |
| 147 | + // `harness/replay.spec.tsx`'s third C18 pin.) |
| 148 | + test("(b) a live op syncs the frame between the parser's end and the drain: the occurrence defers, then claims with its args", async () => { |
| 149 | + const fid = freshFid("c18b"); |
| 150 | + const parserDone = parserRunning(); |
| 151 | + page = bootPage( |
| 152 | + frameHtml( |
| 153 | + fid, |
| 154 | + `<ul>${slotRange("item#0", liveFillHtml(fid, "item#0", "p0"))}</ul><p>${holeHtml( |
| 155 | + 18, |
| 156 | + "hole-v0" |
| 157 | + )}</p>` |
| 158 | + ) |
| 159 | + ); |
| 160 | + const serverLi = page.container.querySelector("li")!; |
| 161 | + const Comp = (globalThis as any)._$SC.r(fid); |
| 162 | + const [tick, setTick] = createSignal(0); |
| 163 | + const invoked: string[] = []; |
| 164 | + const dispose = hydrate( |
| 165 | + () => ( |
| 166 | + <Comp |
| 167 | + item={(p: { text: string }) => { |
| 168 | + invoked.push(untrack(() => p.text)); |
| 169 | + return ( |
| 170 | + <li> |
| 171 | + {p.text} |
| 172 | + {tick()} |
| 173 | + </li> |
| 174 | + ); |
| 175 | + }} |
| 176 | + /> |
| 177 | + ), |
| 178 | + page.container |
| 179 | + ); |
| 180 | + expect(invoked).toEqual([]); |
| 181 | + |
| 182 | + page.slotRecord(fid, "item#0", { text: "p0" }); |
| 183 | + parserDone(); |
| 184 | + // The live op's sync lands on the pump's microtask read — before the |
| 185 | + // deferred drain's macrotask. |
| 186 | + page.live.push({ type: "hole", key: "lh:18", html: "hole-v1" }); |
| 187 | + await microtasks(4); |
| 188 | + expect(page.container.querySelector("p")!.textContent).toBe("hole-v1"); |
| 189 | + expect(invoked).toEqual([]); |
| 190 | + |
| 191 | + await quiesce(); |
| 192 | + await quiesce(); |
| 193 | + expect(invoked).toEqual(["p0"]); |
| 194 | + expect(page.container.querySelector("li")).toBe(serverLi); |
| 195 | + expect(page.container.textContent).toBe("p0" + "0" + "hole-v1"); |
| 196 | + setTick(1); |
| 197 | + flush(); |
| 198 | + expect(page.container.textContent).toBe("p0" + "1" + "hole-v1"); |
| 199 | + expect(page.warnings).toEqual([]); |
| 200 | + expect(page.errors).toEqual([]); |
| 201 | + dispose(); |
| 202 | + }); |
| 203 | + |
| 204 | + // Control: a tick between the two records — the drain runs while the |
| 205 | + // parser is still owed the second, every sync reads `recordsPending()` |
| 206 | + // true through the parser's term alone. Green before and after 3.5. |
| 207 | + test("control: a drain per record while the parser is still running classifies nothing early", async () => { |
| 208 | + const fid = freshFid("c18c"); |
| 209 | + const parserDone = parserRunning(); |
| 210 | + page = bootPage( |
| 211 | + frameHtml( |
| 212 | + fid, |
| 213 | + `<ul>${slotRange("item#0", liveFillHtml(fid, "item#0", "p0"))}${slotRange( |
| 214 | + "item#1", |
| 215 | + liveFillHtml(fid, "item#1", "p1") |
| 216 | + )}</ul>` |
| 217 | + ) |
| 218 | + ); |
| 219 | + const Comp = (globalThis as any)._$SC.r(fid); |
| 220 | + const [tick, setTick] = createSignal(0); |
| 221 | + const invoked: string[] = []; |
| 222 | + const dispose = hydrate( |
| 223 | + () => ( |
| 224 | + <Comp |
| 225 | + item={(p: { text: string }) => { |
| 226 | + invoked.push(untrack(() => p.text)); |
| 227 | + return ( |
| 228 | + <li> |
| 229 | + {p.text} |
| 230 | + {tick()} |
| 231 | + </li> |
| 232 | + ); |
| 233 | + }} |
| 234 | + /> |
| 235 | + ), |
| 236 | + page.container |
| 237 | + ); |
| 238 | + page.slotRecord(fid, "item#0", { text: "p0" }); |
| 239 | + await quiesce(); |
| 240 | + expect(invoked).toEqual(["p0"]); |
| 241 | + page.slotRecord(fid, "item#1", { text: "p1" }); |
| 242 | + parserDone(); |
| 243 | + await quiesce(); |
| 244 | + await quiesce(); |
| 245 | + expect(invoked).toEqual(["p0", "p1"]); |
| 246 | + setTick(1); |
| 247 | + flush(); |
| 248 | + expect(page.container.textContent).toBe("p0" + "1" + "p1" + "1"); |
| 249 | + expect(page.warnings).toEqual([]); |
| 250 | + expect(page.errors).toEqual([]); |
| 251 | + dispose(); |
| 252 | + }); |
| 253 | +}); |
0 commit comments