import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' import type { PackageManager } from '../src/utils/packageManager.js' import { updateProject } from '../src/utils/updateProject.js' // oxlint-disable promise/prefer-await-to-callbacks -- the mocked child process only exposes the callback API // oxlint-disable typescript/consistent-type-imports -- the callback signatures of the mocked node modules are only available through their module types // oxlint-disable vitest/no-hooks -- the mocks and console spies are reset before each test interface SpawnOptions { cwd?: string shell?: boolean stdio?: string } interface ChildProcessLike { on: (event: 'close', callback: (code: number) => void) => void } type Spawn = typeof import('node:child_process').spawn const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn< (command: string, options?: SpawnOptions) => ChildProcessLike >((_command, _options) => ({ // oxlint-disable-next-line promise/prefer-await-to-callbacks -- the mocked child process only exposes the callback API on: (event: 'close', callback: (code: number) => void): void => { if (event === 'close') return callback(0) }, })), })) vi.mock(import('node:child_process'), () => ({ spawn: spawnMock as unknown as Spawn, })) const { checkTaobaoRegistryMock, getPackageManagerMock, getUpdateCommandMock } = vi.hoisted(() => ({ checkTaobaoRegistryMock: vi.fn<() => void>(), getPackageManagerMock: vi.fn<() => PackageManager>(() => 'pnpm'), getUpdateCommandMock: vi.fn<() => string>(() => 'pnpm update'), })) vi.mock(import('../src/utils/packageManager.js'), () => ({ getPackageManager: getPackageManagerMock, getUpdateCommand: getUpdateCommandMock, })) vi.mock(import('../src/utils/registry.js'), () => ({ checkTaobaoRegistry: checkTaobaoRegistryMock, })) const { updatePackagesMock } = vi.hoisted(() => ({ updatePackagesMock: vi.fn<() => Promise>(() => Promise.resolve()), })) vi.mock(import('../src/utils/updatePackage.js'), () => ({ updatePackages: updatePackagesMock, })) /** * Create a temporary VuePress project that is removed after the current test * * @param dependencies - The dependencies to write / 要写入的 dependencies * @param devDependencies - The dev dependencies to write / 要写入的 devDependencies * @returns The absolute path of the project directory / 项目目录的绝对路径 */ const createProject = ( dependencies?: Record, devDependencies?: Record, ): string => { const dir = mkdtempSync(path.join(tmpdir(), 'vp-update-project-')) onTestFinished(() => rmSync(dir, { force: true, recursive: true })) writeFileSync( path.join(dir, 'package.json'), `${JSON.stringify( { name: 'site', dependencies, devDependencies }, null, 2, )}\n`, ) return dir } describe(updateProject, () => { beforeEach(() => { vi.clearAllMocks() vi.spyOn(console, 'log').mockImplementation(() => {}) vi.spyOn(console, 'info').mockImplementation(() => {}) }) it('should run install and update in the target dir', async () => { const dir = createProject({ vuepress: '^2.0.0' }, { vue: '^3.5.0' }) await updateProject(dir) const [installCall, updateCall] = spawnMock.mock.calls expect(installCall?.[0]).toBe('pnpm install') expect(installCall?.[1]?.cwd).toBe(dir) expect(updateCall?.[0]).toBe('pnpm update') expect(updateCall?.[1]?.cwd).toBe(dir) expect(getPackageManagerMock).toHaveBeenCalledWith(dir) expect(checkTaobaoRegistryMock).toHaveBeenCalledWith('pnpm', dir) expect(updatePackagesMock).toHaveBeenCalledWith( 'pnpm', { vuepress: '^2.0.0' }, dir, ) expect(updatePackagesMock).toHaveBeenCalledWith( 'pnpm', { vue: '^3.5.0' }, dir, ) }) it('should report the target dir when it does not exist', async () => { const dir = path.join(createProject(), 'missing') await expect(updateProject(dir)).rejects.toThrow( `Target directory "${dir}" does not exist`, ) }) it('should report the target dir when it is not a directory', async () => { const dir = createProject() const file = path.join(dir, 'package.json') await expect(updateProject(file)).rejects.toThrow( `Target path "${file}" is not a directory`, ) }) it('should report the target dir when it has no package.json', async () => { const dir = createProject() rmSync(path.join(dir, 'package.json')) await expect(updateProject(dir)).rejects.toThrow( `No package.json found in "${dir}"`, ) }) })