|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + * |
| 4 | + * Two defects in one lane: |
| 5 | + * - a duplicated tab inherited the original's sender id through sessionStorage, |
| 6 | + * so each treated the other's edits as its own echo and they stopped syncing; |
| 7 | + * - concurrent sends raced, and a last-write-wins server could store the OLDER |
| 8 | + * snapshot for every peer. |
| 9 | + */ |
| 10 | + |
| 11 | +jest.mock('../src/vendor/hyper-morph.vendor.js', () => ({ |
| 12 | + HyperMorph: { morph: jest.fn(() => Promise.resolve()) }, |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('../src/utilities/mutation.js', () => ({ |
| 16 | + __esModule: true, |
| 17 | + default: { pause: jest.fn(), resume: jest.fn() }, |
| 18 | +})); |
| 19 | + |
| 20 | +class FakeEventSource { |
| 21 | + constructor(url) { |
| 22 | + this.url = url; |
| 23 | + this.readyState = 0; |
| 24 | + } |
| 25 | + close() {} |
| 26 | +} |
| 27 | + |
| 28 | +import { LiveSync } from '../src/communication/live-sync.js'; |
| 29 | + |
| 30 | +describe('LiveSync sender identity + send ordering', () => { |
| 31 | + beforeEach(() => { |
| 32 | + global.EventSource = FakeEventSource; |
| 33 | + window.EventSource = FakeEventSource; |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + delete global.EventSource; |
| 38 | + delete window.EventSource; |
| 39 | + delete global.fetch; |
| 40 | + }); |
| 41 | + |
| 42 | + test("a duplicated tab does not inherit the original's sender id", () => { |
| 43 | + // What tab duplication actually does: the copy starts with the original's |
| 44 | + // sessionStorage already populated. |
| 45 | + sessionStorage.setItem('livesync-client-id', 'inherited-id'); |
| 46 | + |
| 47 | + const original = new LiveSync(); |
| 48 | + const duplicate = new LiveSync(); |
| 49 | + |
| 50 | + expect(original.clientId).not.toBe(duplicate.clientId); |
| 51 | + expect(original.clientId).not.toBe('inherited-id'); |
| 52 | + expect(duplicate.clientId).not.toBe('inherited-id'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('one POST in flight at a time, and the newest queued payload wins', async () => { |
| 56 | + const settle = []; |
| 57 | + global.fetch = jest.fn( |
| 58 | + () => new Promise((resolve) => settle.push(() => resolve({ ok: true }))) |
| 59 | + ); |
| 60 | + |
| 61 | + const sync = new LiveSync(); |
| 62 | + |
| 63 | + sync._enqueueSend('<html>A</html>', null); |
| 64 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 65 | + |
| 66 | + // Both land while A is still on the wire; C must replace B in the one slot. |
| 67 | + sync._enqueueSend('<html>B</html>', null); |
| 68 | + sync._enqueueSend('<html>C</html>', null); |
| 69 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 70 | + |
| 71 | + settle[0](); |
| 72 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 73 | + |
| 74 | + expect(global.fetch).toHaveBeenCalledTimes(2); |
| 75 | + expect(JSON.parse(global.fetch.mock.calls[1][1].body).html).toBe('<html>C</html>'); |
| 76 | + }); |
| 77 | +}); |
0 commit comments