|
| 1 | +/** |
| 2 | + * IFR × native `<list>` interaction. |
| 3 | + * |
| 4 | + * A native list does not own its rows: `__CreateList` hands native three |
| 5 | + * main-thread closures, and the rows they hand back live in `list-apply`'s |
| 6 | + * registries, not in the element tree. IFR moves a first screen from the main |
| 7 | + * thread to the background thread, so these tests pin the two places where |
| 8 | + * that ownership could go wrong: |
| 9 | + * |
| 10 | + * 1. Steady state — a hydrated list must keep exactly one native list, and |
| 11 | + * the callbacks native holds must see the item order the *background* |
| 12 | + * thread maintains after hydration (not the frozen first-screen copy). |
| 13 | + * 2. Fallback — when hydration hits a structural mismatch, the abandoned |
| 14 | + * main-thread stream must not leave list registries behind: a later |
| 15 | + * background op addressing a reused element id would otherwise be routed |
| 16 | + * into a dead list instead of the element tree. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 20 | +import { |
| 21 | + h, |
| 22 | + defineComponent, |
| 23 | + ref, |
| 24 | + createApp, |
| 25 | + onMounted, |
| 26 | + resetForTesting, |
| 27 | +} from 'vue-lynx'; |
| 28 | +import type { Component } from 'vue-lynx'; |
| 29 | +import { IFR_MOUNT_APPS_GLOBAL, OP, PAGE_ROOT_ID } from 'vue-lynx/internal/ops'; |
| 30 | +import { |
| 31 | + enableIFR, |
| 32 | + getIfrPhase, |
| 33 | + resetIfrForTesting, |
| 34 | +} from '../../../vue-lynx/main-thread/src/ifr.js'; |
| 35 | +import { getListItemBgIdsForTest } from '../../../vue-lynx/main-thread/src/list-apply.js'; |
| 36 | +import { waitForUpdate } from '../render.js'; |
| 37 | + |
| 38 | +const env = () => (globalThis as any).lynxTestingEnv; |
| 39 | + |
| 40 | +/** Phase 1: main-thread first-screen render via renderPage. */ |
| 41 | +function mtFirstScreenRender(comp: Component): Document { |
| 42 | + const e = env(); |
| 43 | + e.switchToMainThread(); |
| 44 | + const doc = e.jsdom.window.document as Document; |
| 45 | + doc.body.innerHTML = ''; |
| 46 | + |
| 47 | + resetForTesting(); |
| 48 | + resetIfrForTesting(); |
| 49 | + enableIFR(); |
| 50 | + |
| 51 | + createApp(comp).mount(); |
| 52 | + (globalThis as any).renderPage({}); |
| 53 | + return doc; |
| 54 | +} |
| 55 | + |
| 56 | +/** Phase 2: background thread boots, renders the same app, hydrates. */ |
| 57 | +function bgHydrate(comp: Component): void { |
| 58 | + const e = env(); |
| 59 | + delete (globalThis as any).__VUE_LYNX_IFR_MT__; |
| 60 | + e.switchToBackgroundThread(); |
| 61 | + resetForTesting(); |
| 62 | + createApp(comp).mount(); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Ops-level harness: record a synthetic main-thread stream through the IFR |
| 67 | + * recorder, then feed synthetic background batches to `vuePatchUpdate`. |
| 68 | + * Component-level tests cannot produce a *multi-batch* first screen with a |
| 69 | + * mismatch in a later batch, which is exactly where fallback ordering matters. |
| 70 | + */ |
| 71 | +function mtRecordBatches(batches: unknown[][]): Document { |
| 72 | + const e = env(); |
| 73 | + e.switchToMainThread(); |
| 74 | + const doc = e.jsdom.window.document as Document; |
| 75 | + doc.body.innerHTML = ''; |
| 76 | + |
| 77 | + resetForTesting(); |
| 78 | + resetIfrForTesting(); |
| 79 | + enableIFR(); |
| 80 | + |
| 81 | + (globalThis as any)[IFR_MOUNT_APPS_GLOBAL] = () => { |
| 82 | + const apply = (globalThis as any)['__vueLynxIfrApplyOps'] as ( |
| 83 | + ops: unknown[], |
| 84 | + ) => void; |
| 85 | + for (const batch of batches) apply(batch); |
| 86 | + }; |
| 87 | + (globalThis as any).renderPage({}); |
| 88 | + delete (globalThis as any)[IFR_MOUNT_APPS_GLOBAL]; |
| 89 | + return doc; |
| 90 | +} |
| 91 | + |
| 92 | +function bgBatch(ops: unknown[]): void { |
| 93 | + (globalThis as any).vuePatchUpdate({ data: JSON.stringify(ops) }); |
| 94 | +} |
| 95 | + |
| 96 | +/** The BG-side element id the main thread stamped on an element. */ |
| 97 | +function bgIdOf(el: Element): number { |
| 98 | + const attr = Array.from(el.attributes).find((a) => |
| 99 | + a.name.startsWith('vue-ref-') |
| 100 | + ); |
| 101 | + return Number(attr!.name.slice('vue-ref-'.length)); |
| 102 | +} |
| 103 | + |
| 104 | +beforeEach(() => { |
| 105 | + delete (globalThis as any).__VUE_LYNX_IFR_MT__; |
| 106 | +}); |
| 107 | + |
| 108 | +describe('IFR + native <list>', () => { |
| 109 | + it('keeps one native list and lets the background thread own the rows', async () => { |
| 110 | + const Comp = defineComponent({ |
| 111 | + setup() { |
| 112 | + const rows = ref(['a', 'b', 'c']); |
| 113 | + // onMounted only runs on the background thread — this is the |
| 114 | + // post-hydration mutation native must be able to observe. |
| 115 | + onMounted(() => { |
| 116 | + rows.value = [...rows.value, 'd']; |
| 117 | + }); |
| 118 | + return () => |
| 119 | + h( |
| 120 | + 'list', |
| 121 | + null, |
| 122 | + rows.value.map((row) => |
| 123 | + h('list-item', { key: row, 'item-key': row }, [ |
| 124 | + h('text', null, row), |
| 125 | + ]) |
| 126 | + ), |
| 127 | + ); |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + const doc = mtFirstScreenRender(Comp); |
| 132 | + env().switchToMainThread(); |
| 133 | + // The first screen paints one native list. Rows are not in the tree yet: |
| 134 | + // native materializes them by calling back into componentAtIndex. |
| 135 | + expect(doc.querySelectorAll('list').length).toBe(1); |
| 136 | + |
| 137 | + bgHydrate(Comp); |
| 138 | + await waitForUpdate(); |
| 139 | + |
| 140 | + env().switchToMainThread(); |
| 141 | + // Hydration skipped the identical structural frame — still one list, and |
| 142 | + // the background's post-hydration append landed in the *live* registry the |
| 143 | + // callbacks read. |
| 144 | + expect(getIfrPhase()).toBe('hydrated'); |
| 145 | + const lists = doc.querySelectorAll('list'); |
| 146 | + expect(lists.length).toBe(1); |
| 147 | + const listEl = lists[0]! as Element & { |
| 148 | + componentAtIndex( |
| 149 | + list: Element, |
| 150 | + listID: number, |
| 151 | + cellIndex: number, |
| 152 | + operationID: number, |
| 153 | + ): number | undefined; |
| 154 | + }; |
| 155 | + expect(getListItemBgIdsForTest(bgIdOf(listEl)).length).toBe(4); |
| 156 | + |
| 157 | + // Native materializes a row that never existed during the IFR render. |
| 158 | + const sign = listEl.componentAtIndex(listEl, 0, 3, 1); |
| 159 | + expect(sign).toBeTypeOf('number'); |
| 160 | + const materialized = listEl.lastElementChild!; |
| 161 | + expect(materialized.tagName.toLowerCase()).toBe('list-item'); |
| 162 | + expect(materialized.textContent).toBe('d'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('does not route background inserts into an abandoned list after fallback', () => { |
| 166 | + // Main thread renders id 2 as a <list>; the background render diverges and |
| 167 | + // uses id 2 for a plain <view> with a child. If the abandoned list's |
| 168 | + // registries survived teardown, the child insert would be swallowed by |
| 169 | + // `insertListItem` and never reach the element tree. |
| 170 | + const doc = mtRecordBatches([ |
| 171 | + [ |
| 172 | + OP.CREATE, 2, 'list', |
| 173 | + OP.INSERT, PAGE_ROOT_ID, 2, -1, |
| 174 | + OP.CREATE, 3, 'list-item', |
| 175 | + OP.SET_PROP, 3, 'item-key', 'a', |
| 176 | + OP.INSERT, 2, 3, -1, |
| 177 | + ], |
| 178 | + ]); |
| 179 | + expect(doc.querySelectorAll('list').length).toBe(1); |
| 180 | + |
| 181 | + bgBatch([ |
| 182 | + OP.CREATE, 2, 'view', |
| 183 | + OP.INSERT, PAGE_ROOT_ID, 2, -1, |
| 184 | + OP.CREATE, 3, 'text', |
| 185 | + OP.INSERT, 2, 3, -1, |
| 186 | + OP.SET_TEXT, 3, 'bg', |
| 187 | + ]); |
| 188 | + |
| 189 | + expect(getIfrPhase()).toBe('hydrated'); |
| 190 | + expect(doc.querySelectorAll('list').length).toBe(0); |
| 191 | + const view = doc.querySelector('view')!; |
| 192 | + expect(view.children.length).toBe(1); |
| 193 | + expect(view.textContent).toBe('bg'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('replays earlier background batches when a later batch mismatches', () => { |
| 197 | + // Batch 1 matches and is skipped; batch 2 diverges. The list described by |
| 198 | + // batch 1 was only ever painted by the main-thread render, so teardown |
| 199 | + // removes it — the fallback has to replay batch 1 to put it back. |
| 200 | + const doc = mtRecordBatches([ |
| 201 | + [ |
| 202 | + OP.CREATE, 2, 'list', |
| 203 | + OP.INSERT, PAGE_ROOT_ID, 2, -1, |
| 204 | + OP.CREATE, 3, 'list-item', |
| 205 | + OP.SET_PROP, 3, 'item-key', 'a', |
| 206 | + OP.INSERT, 2, 3, -1, |
| 207 | + ], |
| 208 | + [ |
| 209 | + OP.CREATE, 4, 'text', |
| 210 | + OP.INSERT, PAGE_ROOT_ID, 4, -1, |
| 211 | + OP.SET_TEXT, 4, 'main-thread', |
| 212 | + ], |
| 213 | + ]); |
| 214 | + |
| 215 | + bgBatch([ |
| 216 | + OP.CREATE, 2, 'list', |
| 217 | + OP.INSERT, PAGE_ROOT_ID, 2, -1, |
| 218 | + OP.CREATE, 3, 'list-item', |
| 219 | + OP.SET_PROP, 3, 'item-key', 'a', |
| 220 | + OP.INSERT, 2, 3, -1, |
| 221 | + ]); |
| 222 | + expect(getIfrPhase()).toBe('rendered'); // skipped, one batch left |
| 223 | + |
| 224 | + bgBatch([ |
| 225 | + OP.CREATE, 4, 'image', |
| 226 | + OP.INSERT, PAGE_ROOT_ID, 4, -1, |
| 227 | + OP.SET_PROP, 4, 'src', 'x.png', |
| 228 | + ]); |
| 229 | + |
| 230 | + expect(getIfrPhase()).toBe('hydrated'); |
| 231 | + // The list from the replayed batch survives, exactly once, with its row. |
| 232 | + const lists = doc.querySelectorAll('list'); |
| 233 | + expect(lists.length).toBe(1); |
| 234 | + expect(getListItemBgIdsForTest(bgIdOf(lists[0]!))).toEqual([3]); |
| 235 | + expect(doc.querySelectorAll('image').length).toBe(1); |
| 236 | + expect(doc.querySelectorAll('text').length).toBe(0); |
| 237 | + }); |
| 238 | +}); |
0 commit comments