|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | +import { |
| 5 | + type SwLike, |
| 6 | + SKIP_WAITING_MESSAGE, |
| 7 | + promoteWaitingWorker, |
| 8 | + shouldReloadOnControllerChange, |
| 9 | + wireServiceWorkerUpdates, |
| 10 | +} from '../../lib/sw-update'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Deploy-while-editing safety (matrix section C, "SW 缓存旧构建 -> 升级"). |
| 14 | + * A new build's worker must not take over while a document is open: it would |
| 15 | + * delete the old build's caches and the still-running old page would lazy-load |
| 16 | + * new-build pieces into its session (mixed-version editor). |
| 17 | + */ |
| 18 | +type FakeWorker = SwLike & { |
| 19 | + postMessage: ReturnType<typeof vi.fn<(msg: unknown) => unknown>>; |
| 20 | + listeners: Array<() => void>; |
| 21 | +}; |
| 22 | +const worker = (state = 'installed'): FakeWorker => { |
| 23 | + const w: FakeWorker = { |
| 24 | + state, |
| 25 | + postMessage: vi.fn<(msg: unknown) => unknown>(), |
| 26 | + listeners: [], |
| 27 | + addEventListener: (_t, cb) => { |
| 28 | + w.listeners.push(cb); |
| 29 | + }, |
| 30 | + }; |
| 31 | + return w; |
| 32 | +}; |
| 33 | +const registration = (waiting: ReturnType<typeof worker> | null = null) => { |
| 34 | + const r = { |
| 35 | + waiting: waiting as SwLike | null, |
| 36 | + installing: null as SwLike | null, |
| 37 | + updateListeners: [] as Array<() => void>, |
| 38 | + addEventListener: (_t: 'updatefound', cb: () => void) => { |
| 39 | + r.updateListeners.push(cb); |
| 40 | + }, |
| 41 | + }; |
| 42 | + return r; |
| 43 | +}; |
| 44 | + |
| 45 | +describe('promoteWaitingWorker', () => { |
| 46 | + it('sends SKIP_WAITING to a waiting worker when no document is open', () => { |
| 47 | + const w = worker(); |
| 48 | + expect(promoteWaitingWorker(registration(w), () => false)).toBe(true); |
| 49 | + expect(w.postMessage).toHaveBeenCalledWith(SKIP_WAITING_MESSAGE); |
| 50 | + }); |
| 51 | + |
| 52 | + it('never promotes while a document is open', () => { |
| 53 | + const w = worker(); |
| 54 | + expect(promoteWaitingWorker(registration(w), () => true)).toBe(false); |
| 55 | + expect(w.postMessage).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('is a no-op without a waiting worker', () => { |
| 59 | + expect(promoteWaitingWorker(registration(null), () => false)).toBe(false); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('wireServiceWorkerUpdates', () => { |
| 64 | + it('promotes a worker that finishes installing later (landing page, nothing open)', () => { |
| 65 | + const r = registration(); |
| 66 | + wireServiceWorkerUpdates(r, () => false); |
| 67 | + const w = worker('installing'); |
| 68 | + r.installing = w; |
| 69 | + r.updateListeners.forEach((cb) => cb()); |
| 70 | + w.state = 'installed'; |
| 71 | + r.waiting = w; |
| 72 | + w.listeners.forEach((cb) => cb()); |
| 73 | + expect(w.postMessage).toHaveBeenCalledWith(SKIP_WAITING_MESSAGE); |
| 74 | + }); |
| 75 | + |
| 76 | + it('leaves the new worker waiting while a document is open', () => { |
| 77 | + const r = registration(); |
| 78 | + wireServiceWorkerUpdates(r, () => true); |
| 79 | + const w = worker('installing'); |
| 80 | + r.installing = w; |
| 81 | + r.updateListeners.forEach((cb) => cb()); |
| 82 | + w.state = 'installed'; |
| 83 | + r.waiting = w; |
| 84 | + w.listeners.forEach((cb) => cb()); |
| 85 | + expect(w.postMessage).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('shouldReloadOnControllerChange', () => { |
| 90 | + it('reloads once on an update with nothing open', () => { |
| 91 | + expect( |
| 92 | + shouldReloadOnControllerChange({ hadController: true, alreadyReloading: false, hasOpenDocument: false }), |
| 93 | + ).toBe(true); |
| 94 | + }); |
| 95 | + it('does not reload on first install, twice, or with a document open', () => { |
| 96 | + expect( |
| 97 | + shouldReloadOnControllerChange({ hadController: false, alreadyReloading: false, hasOpenDocument: false }), |
| 98 | + ).toBe(false); |
| 99 | + expect( |
| 100 | + shouldReloadOnControllerChange({ hadController: true, alreadyReloading: true, hasOpenDocument: false }), |
| 101 | + ).toBe(false); |
| 102 | + expect( |
| 103 | + shouldReloadOnControllerChange({ hadController: true, alreadyReloading: false, hasOpenDocument: true }), |
| 104 | + ).toBe(false); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('public/sw.js keeps the waiting contract', () => { |
| 109 | + const src = readFileSync(resolve(__dirname, '../../public/sw.js'), 'utf8'); |
| 110 | + it('does not skipWaiting on install and honors the SKIP_WAITING message', () => { |
| 111 | + const install = src |
| 112 | + .slice(src.indexOf("addEventListener('install'"), src.indexOf("addEventListener('message'")) |
| 113 | + .replace(/\/\/.*$/gm, ''); |
| 114 | + expect(install).not.toMatch(/skipWaiting\(\)/); |
| 115 | + expect(src).toMatch(/type === 'SKIP_WAITING'[\s\S]*skipWaiting\(\)/); |
| 116 | + }); |
| 117 | +}); |
0 commit comments