|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + */ |
| 5 | +/** |
| 6 | + * #4525 (ReactiveScript timing fuzzer, issue tracker apps): a keyed |
| 7 | + * `createOptimisticStore` list whose question reads `filter()`. The filter |
| 8 | + * changes by a plain write, and before the new question answers an action |
| 9 | + * guesses on the list (closes #1), yields and then refreshes it. |
| 10 | + * |
| 11 | + * The guess was written over the open list while the closed question was in |
| 12 | + * flight: the rows it only carried over (#3, #4) are the open answer's, not |
| 13 | + * the guess's. The closed answer landed as a current truth and folded |
| 14 | + * beneath the guess (D-34), so with `Loading on={filter()}` — whose fallback |
| 15 | + * had already taken the filter change (D-14) — the region left its fallback |
| 16 | + * showing `closed` beside the open list minus the guess (F-1) until the |
| 17 | + * action's refresh landed. |
| 18 | + * |
| 19 | + * Ruled: the answer to a question asked before the guess re-bases it like |
| 20 | + * an older truth (D-35, `_laneRebase`): the guess's own edit, #1 removed, |
| 21 | + * over the closed answer, so the screen is `closed: [#2]` (the issue's second |
| 22 | + * acceptable frame), with or without `on`; never a row of the open list |
| 23 | + * beside `closed`. |
| 24 | + */ |
| 25 | +import { expect, test } from "vitest"; |
| 26 | +import { |
| 27 | + For, |
| 28 | + Loading, |
| 29 | + action, |
| 30 | + createOptimisticStore, |
| 31 | + createSignal, |
| 32 | + flush, |
| 33 | + refresh |
| 34 | +} from "solid-js"; |
| 35 | +import { render } from "../src/index.js"; |
| 36 | + |
| 37 | +const tick = async () => { |
| 38 | + for (let i = 0; i < 4; i++) { |
| 39 | + await new Promise(r => setTimeout(r, 0)); |
| 40 | + flush(); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +for (const on of [true, false]) |
| 45 | + test(`#4525: a guess written while a new question is in flight re-bases over its answer (on=${on})`, async () => { |
| 46 | + const asks: { key: string; res: (v: any) => void }[] = []; |
| 47 | + const ask = (key: string) => new Promise<any>(res => asks.push({ key, res })); |
| 48 | + const settle = async (key: string, v: any) => { |
| 49 | + const i = asks.findIndex(a => a.key === key); |
| 50 | + asks.splice(i, 1)[0].res(v); |
| 51 | + await tick(); |
| 52 | + }; |
| 53 | + const container = document.body.appendChild(document.createElement("div")); |
| 54 | + // Every DOM state the screen passes through. |
| 55 | + const seen: string[] = []; |
| 56 | + const observer = new MutationObserver(() => seen.push(container.textContent!)); |
| 57 | + observer.observe(container, { childList: true, subtree: true, characterData: true }); |
| 58 | + let setFilter!: (f: string) => void; |
| 59 | + let closeOne!: (id: number) => any; |
| 60 | + const dispose = render(() => { |
| 61 | + const [filter, sf] = createSignal("open"); |
| 62 | + setFilter = sf; |
| 63 | + const [issues, set] = createOptimisticStore<any[]>(() => ask(`issues:${filter()}`), [], { |
| 64 | + key: "id" |
| 65 | + }); |
| 66 | + closeOne = action(function* (id: number) { |
| 67 | + set(list => { |
| 68 | + const i = list.findIndex(t => t.id === id); |
| 69 | + if (i >= 0) list.splice(i, 1); |
| 70 | + }); |
| 71 | + yield ask(`close:${id}`); |
| 72 | + refresh(issues); |
| 73 | + }); |
| 74 | + const body = () => ( |
| 75 | + <p> |
| 76 | + {filter()}: <For each={issues}>{(it: any) => <i>[#{it.id}]</i>}</For> |
| 77 | + </p> |
| 78 | + ); |
| 79 | + return on ? ( |
| 80 | + <Loading on={filter()} fallback={<p>loading</p>}> |
| 81 | + {body()} |
| 82 | + </Loading> |
| 83 | + ) : ( |
| 84 | + <Loading fallback={<p>loading</p>}>{body()}</Loading> |
| 85 | + ); |
| 86 | + }, container); |
| 87 | + try { |
| 88 | + await tick(); |
| 89 | + await settle("issues:open", [{ id: 1 }, { id: 3 }, { id: 4 }]); |
| 90 | + expect(container.textContent).toBe("open: [#1][#3][#4]"); |
| 91 | + setFilter("closed"); |
| 92 | + await tick(); |
| 93 | + closeOne(1); |
| 94 | + await tick(); |
| 95 | + expect(container.textContent).toBe(on ? "loading" : "open: [#3][#4]"); |
| 96 | + await settle("issues:closed", [{ id: 1 }, { id: 2 }]); |
| 97 | + expect(container.textContent).toBe("closed: [#2]"); |
| 98 | + await settle("close:1", {}); |
| 99 | + expect(container.textContent).toBe("closed: [#2]"); |
| 100 | + await settle("issues:closed", [{ id: 2 }]); |
| 101 | + expect(container.textContent).toBe("closed: [#2]"); |
| 102 | + expect(asks).toEqual([]); |
| 103 | + // Never a row of the open list beside `closed` (F-1). |
| 104 | + expect(seen.filter(s => s.startsWith("closed") && /#3|#4/.test(s))).toEqual([]); |
| 105 | + } finally { |
| 106 | + observer.disconnect(); |
| 107 | + dispose(); |
| 108 | + container.remove(); |
| 109 | + } |
| 110 | + }); |
0 commit comments