import { existsSync } from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import { describe, expect, it, vi } from 'vitest' import { getModulePath } from '../../src/node/utils/getModulePath.js' // Guard the POSIX path guarantee of `getModulePath`: `fileURLToPath` returns a // Windows path, and switching `path` back to `node:path` would keep the // backslashes on POSIX and fail these assertions. vi.mock(import('node:url'), async (importOriginal) => { const actual = await importOriginal() return { ...actual, fileURLToPath: vi.fn(actual.fileURLToPath), } }) const WINDOWS_PATH = String.raw`C:\Users\someone\node_modules\pkg\dist\client\style.css` const fakeMeta = { resolve: (module: string): string => `file:///C:/Users/someone/node_modules/${module}`, } as ImportMeta describe(getModulePath, () => { it('should return a POSIX path on Windows', () => { vi.mocked(fileURLToPath).mockReturnValueOnce(WINDOWS_PATH) const result = getModulePath('pkg/style.css', fakeMeta) expect(result).toBe( 'C:/Users/someone/node_modules/pkg/dist/client/style.css', ) expect(result).not.toContain('\\') }) it('should return the absolute POSIX path of an existing module', () => { const result = getModulePath('vuepress/package.json', import.meta) expect(path.isAbsolute(result)).toBe(true) expect(result).not.toContain('\\') expect(existsSync(result)).toBe(true) }) })