|
| 1 | +import { fs } from '@vuepress/utils' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | +import { createAppDirFunction } from '../../src/app/resolveAppDir.js' |
| 4 | +import { resolveAppWriteTemp } from '../../src/app/resolveAppWriteTemp.js' |
| 5 | +import type { AppDir } from '../../src/index.js' |
| 6 | + |
| 7 | +vi.mock('@vuepress/utils', async (importOriginal) => { |
| 8 | + // eslint-disable-next-line @typescript-eslint/consistent-type-imports |
| 9 | + const actual = await importOriginal<typeof import('@vuepress/utils')>() |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + fs: { |
| 13 | + ...actual.fs, |
| 14 | + outputFile: vi.fn(), |
| 15 | + }, |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +describe('resolveAppWriteTemp', () => { |
| 20 | + const dir = { |
| 21 | + temp: createAppDirFunction('/temp'), |
| 22 | + } as AppDir |
| 23 | + |
| 24 | + it('should write temp file correctly', async () => { |
| 25 | + const writeTemp = resolveAppWriteTemp(dir) |
| 26 | + const file = 'foo.txt' |
| 27 | + const content = 'bar' |
| 28 | + await writeTemp(file, content) |
| 29 | + expect(fs.outputFile).toHaveBeenCalledWith(dir.temp(file), 'bar') |
| 30 | + }) |
| 31 | + |
| 32 | + it('should avoid overwriting newer content with older content (race condition)', async () => { |
| 33 | + const writeTemp = resolveAppWriteTemp(dir) |
| 34 | + const file = 'race.txt' |
| 35 | + const log: string[] = [] |
| 36 | + |
| 37 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 38 | + vi.mocked(fs.outputFile).mockImplementation(async (_f, c) => { |
| 39 | + const content = c as string |
| 40 | + // Simulate delay: '1' is slow, '2' is fast |
| 41 | + const delay = content === '1' ? 50 : 10 |
| 42 | + await new Promise((resolve) => { |
| 43 | + setTimeout(resolve, delay) |
| 44 | + }) |
| 45 | + log.push(content) |
| 46 | + }) |
| 47 | + |
| 48 | + // Call '1' then '2' immediately |
| 49 | + const p1 = writeTemp(file, '1') |
| 50 | + const p2 = writeTemp(file, '2') |
| 51 | + |
| 52 | + await Promise.all([p1, p2]) |
| 53 | + |
| 54 | + // With parallel execution and delays, '2' finishes first, then '1' overwrites it. |
| 55 | + // We expect the fix to ensure '1' finishes then '2', or '2' is the final write. |
| 56 | + // Ideally sequential execution: '1' writes, then '2' writes. |
| 57 | + expect(log).toEqual(['1', '2']) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should skip write if content is unchanged', async () => { |
| 61 | + vi.mocked(fs.outputFile).mockClear() |
| 62 | + const writeTemp = resolveAppWriteTemp(dir) |
| 63 | + const file = 'cache.txt' |
| 64 | + const content = 'foo' |
| 65 | + |
| 66 | + await writeTemp(file, content) |
| 67 | + expect(fs.outputFile).toHaveBeenCalledTimes(1) |
| 68 | + |
| 69 | + await writeTemp(file, content) |
| 70 | + expect(fs.outputFile).toHaveBeenCalledTimes(1) |
| 71 | + |
| 72 | + await writeTemp(file, 'bar') |
| 73 | + expect(fs.outputFile).toHaveBeenCalledTimes(2) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should skip intermediate writes', async () => { |
| 77 | + vi.mocked(fs.outputFile).mockClear() |
| 78 | + const writeTemp = resolveAppWriteTemp(dir) |
| 79 | + const file = 'skip.txt' |
| 80 | + const log: string[] = [] |
| 81 | + |
| 82 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 83 | + vi.mocked(fs.outputFile).mockImplementation(async (_f, c) => { |
| 84 | + const content = c as string |
| 85 | + // Simulate delay to ensure queuing |
| 86 | + await new Promise((resolve) => { |
| 87 | + setTimeout(resolve, 20) |
| 88 | + }) |
| 89 | + log.push(content) |
| 90 | + }) |
| 91 | + |
| 92 | + // 1. First write starts immediately |
| 93 | + const p1 = writeTemp(file, '1') |
| 94 | + |
| 95 | + // 2. Second and Third write come in while First is running |
| 96 | + // They should be collapsed into a single subsequent write |
| 97 | + const p2 = writeTemp(file, '2') |
| 98 | + const p3 = writeTemp(file, '3') |
| 99 | + |
| 100 | + await Promise.all([p1, p2, p3]) |
| 101 | + |
| 102 | + // '1' is written (started before others). |
| 103 | + // '2' is skipped because '3' arrived while '1' was running, updating the nextContent. |
| 104 | + // '3' is written after '1' finishes. |
| 105 | + expect(log).toEqual(['1', '3']) |
| 106 | + }) |
| 107 | +}) |
0 commit comments