Files
orca/config/scripts/worktree-storage/reclaim-vite-caches.test.mjs

157 lines
5.1 KiB
JavaScript

import {
mkdir,
mkdtemp,
readFile,
readdir,
rename,
rm,
symlink,
utimes,
writeFile
} from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { pathToFileURL } from 'node:url'
import { runProcess } from '@orca/process-host'
import { createServer } from 'vite'
import { afterEach, expect, it, vi } from 'vitest'
import { createViteCacheRetentionPlugin } from '../../build-plugins/vite-cache-retention'
import { reclaimViteCaches } from './reclaim-vite-caches.mjs'
import { retainViteCache, VITE_CACHE_USAGE_DIRECTORY } from './vite-cache-activity.mjs'
const roots = []
const nowMs = Date.now()
const dayMs = 86_400_000
afterEach(async () => {
vi.restoreAllMocks()
for (const root of roots.splice(0)) {
await rm(root, { recursive: true, force: true })
}
})
async function age(cacheRoot, days = 31) {
const time = new Date(nowMs - days * dayMs)
await utimes(join(cacheRoot, VITE_CACHE_USAGE_DIRECTORY), time, time)
}
async function fixture(days = 31) {
const root = await mkdtemp(join(tmpdir(), 'orca-vite-cache-'))
roots.push(root)
const worktree = join(root, 'worktree')
const cacheRoot = join(worktree, '.cache', 'vite')
const release = await retainViteCache(cacheRoot)
await release()
const files = ['renderer', 'web'].map((target) => join(cacheRoot, target, 'deps', 'react.js'))
for (const file of files) {
await mkdir(resolve(file, '..'), { recursive: true })
await writeFile(file, 'export const cached = true')
}
await age(cacheRoot, days)
return {
root,
worktree,
cacheRoot,
files,
worktrees: [worktree, worktree],
nowMs,
report: vi.fn()
}
}
it('previews and removes old owned caches for both build targets', async () => {
const options = await fixture()
expect(await reclaimViteCaches(options)).toMatchObject({ entries: 1 })
for (const file of options.files) {
expect(await readFile(file, 'utf8')).toContain('cached')
}
expect(await reclaimViteCaches({ ...options, apply: true })).toMatchObject({ entries: 1 })
for (const file of options.files) {
await expect(readFile(file)).rejects.toMatchObject({ code: 'ENOENT' })
}
})
it.each([false, true])(
'reclaims exited owners only on this host (foreign: %s)',
async (foreign) => {
const options = await fixture()
const moduleUrl = pathToFileURL(resolve(import.meta.dirname, 'vite-cache-activity.mjs')).href
const child = await runProcess({
program: process.env.ORCA_TEST_NODE_EXECUTABLE ?? process.execPath,
args: [
'--input-type=module',
'-e',
`import { retainViteCache } from ${JSON.stringify(moduleUrl)}; await retainViteCache(${JSON.stringify(options.cacheRoot)});`
],
env: foreign
? { ...process.env, WSL_DISTRO_NAME: `${process.env.WSL_DISTRO_NAME ?? ''}-other` }
: process.env,
timeoutMs: 10_000,
terminationBarrier: true
})
expect(child.code, child.stderr).toBe(0)
await age(options.cacheRoot)
expect(await reclaimViteCaches({ ...options, apply: true })).toMatchObject({
entries: foreign ? 0 : 1
})
}
)
it.each(['.cache', 'vite'])('does not follow a %s directory link during cleanup', async (part) => {
const options = await fixture()
const source = part === '.cache' ? join(options.worktree, '.cache') : options.cacheRoot
const target = join(options.root, 'outside')
await rename(source, target)
await symlink(target, source, 'junction')
expect(await reclaimViteCaches({ ...options, apply: true })).toMatchObject({ entries: 0 })
expect(await readdir(target)).not.toEqual([])
})
it('tracks a real Vite server until shutdown has completed', async () => {
const options = await fixture()
let unblockClose
let enteredClose
const closeBlocked = new Promise((resolve) => {
unblockClose = resolve
})
const closeEntered = new Promise((resolve) => {
enteredClose = resolve
})
const server = await createServer({
configFile: false,
root: options.worktree,
cacheDir: join(options.cacheRoot, 'renderer'),
logLevel: 'silent',
server: { middlewareMode: true, watch: null, hmr: false },
optimizeDeps: { noDiscovery: true, include: [] },
plugins: [
createViteCacheRetentionPlugin(options.cacheRoot),
{
name: 'hold-vite-close',
async closeBundle() {
enteredClose()
await closeBlocked
}
}
]
})
try {
await age(options.cacheRoot)
expect(await reclaimViteCaches({ ...options, apply: true })).toMatchObject({ entries: 0 })
} finally {
const closing = server.close()
try {
await closeEntered
await age(options.cacheRoot)
expect(await reclaimViteCaches({ ...options, apply: true })).toMatchObject({ entries: 0 })
} finally {
unblockClose()
await closing
}
}
expect(await readdir(join(options.cacheRoot, VITE_CACHE_USAGE_DIRECTORY))).toEqual([])
expect(await reclaimViteCaches({ ...options, apply: true })).toMatchObject({ entries: 0 })
await age(options.cacheRoot)
expect(await reclaimViteCaches({ ...options, apply: true })).toMatchObject({ entries: 1 })
})