|
| 1 | +import { mkdir, mkdtemp, readdir, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 5 | +import { clearRouteCacheFromOtherBuild } from '@/lib/core/utils/route-cache' |
| 6 | + |
| 7 | +let nextDirectory: string |
| 8 | +let routeCache: string |
| 9 | + |
| 10 | +beforeEach(async () => { |
| 11 | + nextDirectory = await mkdtemp(join(tmpdir(), 'route-cache-')) |
| 12 | + routeCache = join(nextDirectory, 'server', 'route-cache') |
| 13 | + await mkdir(join(routeCache, 'app-page'), { recursive: true }) |
| 14 | + await writeFile(join(routeCache, 'app-page', 'contact.html'), '<html>old build</html>') |
| 15 | + await writeFile(join(nextDirectory, 'BUILD_ID'), 'build-2\n') |
| 16 | +}) |
| 17 | + |
| 18 | +afterEach(async () => { |
| 19 | + await rm(nextDirectory, { recursive: true, force: true }) |
| 20 | +}) |
| 21 | + |
| 22 | +describe('clearRouteCacheFromOtherBuild', () => { |
| 23 | + it('clears pages rendered by an earlier build', async () => { |
| 24 | + await writeFile(join(routeCache, '.build-id'), 'build-1') |
| 25 | + await clearRouteCacheFromOtherBuild(nextDirectory) |
| 26 | + expect(await readdir(routeCache)).toEqual(['.build-id']) |
| 27 | + }) |
| 28 | + |
| 29 | + it('clears a cache that records no build', async () => { |
| 30 | + await clearRouteCacheFromOtherBuild(nextDirectory) |
| 31 | + expect(await readdir(routeCache)).toEqual(['.build-id']) |
| 32 | + }) |
| 33 | + |
| 34 | + it('keeps pages rendered by the current build across restarts', async () => { |
| 35 | + await clearRouteCacheFromOtherBuild(nextDirectory) |
| 36 | + await writeFile(join(routeCache, 'app-page.html'), '<html>current build</html>') |
| 37 | + await clearRouteCacheFromOtherBuild(nextDirectory) |
| 38 | + expect((await readdir(routeCache)).sort()).toEqual(['.build-id', 'app-page.html']) |
| 39 | + }) |
| 40 | + |
| 41 | + it('leaves the cache alone when the build id is unreadable', async () => { |
| 42 | + await rm(join(nextDirectory, 'BUILD_ID')) |
| 43 | + await clearRouteCacheFromOtherBuild(nextDirectory) |
| 44 | + expect(await readdir(routeCache)).toEqual(['app-page']) |
| 45 | + }) |
| 46 | +}) |
0 commit comments