|
| 1 | +import { expect, it } from 'vitest' |
| 2 | +import { serializeBatches } from '../../utils/serial-batches' |
| 3 | +import { createWatchers, filterChangedFiles } from './index' |
| 4 | + |
| 5 | +type WatchEvent = { type: 'create' | 'update' | 'delete'; path: string } |
| 6 | +type WatchCallback = (error: Error | null, events: WatchEvent[]) => Promise<void> |
| 7 | + |
| 8 | +function fakeWatcher() { |
| 9 | + let callbacks: WatchCallback[] = [] |
| 10 | + return { |
| 11 | + callbacks, |
| 12 | + watcher: { |
| 13 | + async subscribe(_directory: string, callback: WatchCallback) { |
| 14 | + callbacks.push(callback) |
| 15 | + return { unsubscribe() {} } |
| 16 | + }, |
| 17 | + }, |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function nextTask() { |
| 22 | + return new Promise((resolve) => setTimeout(resolve, 0)) |
| 23 | +} |
| 24 | + |
| 25 | +it('removes duplicate output and map events from a coalesced batch', () => { |
| 26 | + expect( |
| 27 | + filterChangedFiles( |
| 28 | + ['output.css', 'source.html', 'output.css', 'output.css.map'], |
| 29 | + 'output.css', |
| 30 | + 'output.css.map', |
| 31 | + ), |
| 32 | + ).toEqual(['source.html']) |
| 33 | +}) |
| 34 | + |
| 35 | +it('uses one rebuild queue across watcher generations', async () => { |
| 36 | + let releaseFirst!: () => void |
| 37 | + let firstCanFinish = new Promise<void>((resolve) => (releaseFirst = resolve)) |
| 38 | + let calls: string[][] = [] |
| 39 | + let queue = serializeBatches<string>(async (files) => { |
| 40 | + calls.push(files) |
| 41 | + if (calls.length === 1) await firstCanFinish |
| 42 | + }) |
| 43 | + let first = fakeWatcher() |
| 44 | + let second = fakeWatcher() |
| 45 | + |
| 46 | + let oldGeneration = await createWatchers(['/old'], async () => {}, queue, first.watcher) |
| 47 | + await first.callbacks[0](null, [{ type: 'delete', path: 'old-change' }]) |
| 48 | + await nextTask() |
| 49 | + expect(calls).toEqual([['old-change']]) |
| 50 | + |
| 51 | + let newGeneration = await createWatchers(['/new'], async () => {}, queue, second.watcher) |
| 52 | + await second.callbacks[0](null, [{ type: 'delete', path: 'new-change' }]) |
| 53 | + await nextTask() |
| 54 | + expect(calls).toEqual([['old-change']]) |
| 55 | + |
| 56 | + releaseFirst() |
| 57 | + await queue.close() |
| 58 | + expect(calls).toEqual([['old-change'], ['new-change']]) |
| 59 | + |
| 60 | + await Promise.all([oldGeneration.cleanup(), newGeneration.cleanup()]) |
| 61 | +}) |
| 62 | + |
| 63 | +it('buffers watcher events until the initial build finishes', async () => { |
| 64 | + let finishInitialBuild!: () => void |
| 65 | + let initialBuild = new Promise<void>((resolve) => (finishInitialBuild = resolve)) |
| 66 | + let calls: string[][] = [] |
| 67 | + let queue = serializeBatches<string>(async (files) => { |
| 68 | + calls.push(files) |
| 69 | + }, initialBuild) |
| 70 | + let fake = fakeWatcher() |
| 71 | + let generation = await createWatchers(['/watch'], async () => {}, queue, fake.watcher) |
| 72 | + |
| 73 | + await fake.callbacks[0](null, [{ type: 'delete', path: 'early-change' }]) |
| 74 | + await nextTask() |
| 75 | + expect(calls).toEqual([]) |
| 76 | + |
| 77 | + finishInitialBuild() |
| 78 | + await queue.close() |
| 79 | + expect(calls).toEqual([['early-change']]) |
| 80 | + await generation.cleanup() |
| 81 | +}) |
| 82 | + |
| 83 | +it('flushes a collected event when shutdown cancels its debounce timer', async () => { |
| 84 | + let calls: string[][] = [] |
| 85 | + let queue = serializeBatches<string>(async (files) => { |
| 86 | + calls.push(files) |
| 87 | + }) |
| 88 | + let fake = fakeWatcher() |
| 89 | + let generation = await createWatchers(['/watch'], async () => {}, queue, fake.watcher) |
| 90 | + |
| 91 | + await fake.callbacks[0](null, [{ type: 'delete', path: 'last-change' }]) |
| 92 | + await generation.cleanup() |
| 93 | + await queue.close() |
| 94 | + |
| 95 | + expect(calls).toEqual([['last-change']]) |
| 96 | +}) |
| 97 | + |
| 98 | +it('waits for an entered watcher callback before shutdown flushes changes', async () => { |
| 99 | + let releaseLstat!: () => void |
| 100 | + let lstatCanFinish = new Promise<void>((resolve) => (releaseLstat = resolve)) |
| 101 | + let calls: string[][] = [] |
| 102 | + let queue = serializeBatches<string>(async (files) => { |
| 103 | + calls.push(files) |
| 104 | + }) |
| 105 | + let fake = fakeWatcher() |
| 106 | + let generation = await createWatchers( |
| 107 | + ['/watch'], |
| 108 | + async () => {}, |
| 109 | + queue, |
| 110 | + fake.watcher, |
| 111 | + async () => { |
| 112 | + await lstatCanFinish |
| 113 | + return { isFile: () => true, isSymbolicLink: () => false } |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | + let callback = fake.callbacks[0](null, [{ type: 'update', path: 'delayed-change' }]) |
| 118 | + let cleanup = generation.cleanup() |
| 119 | + await nextTask() |
| 120 | + expect(calls).toEqual([]) |
| 121 | + |
| 122 | + releaseLstat() |
| 123 | + await Promise.all([callback, cleanup]) |
| 124 | + await queue.close() |
| 125 | + expect(calls).toEqual([['delayed-change']]) |
| 126 | +}) |
0 commit comments