import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { resolveExplicitFiles } from '../src/resolveFiles.js'; let baseDir: string; async function writeFixture(rel: string) { const filePath = path.join(baseDir, rel); await fs.mkdir(path.dirname(filePath), { recursive: true }); await fs.writeFile(filePath, '# fixture\n'); } beforeEach(async () => { baseDir = await fs.mkdtemp(path.join(os.tmpdir(), 'resolve-files-test-')); }); afterEach(async () => { await fs.rm(baseDir, { recursive: true, force: true }); }); describe('resolveExplicitFiles', () => { it('resolves literal paths', async () => { await writeFixture('docs/a.mdx'); await expect(resolveExplicitFiles(baseDir, ['docs/a.mdx'])).resolves.toEqual(['docs/a.mdx']); }); it('normalizes leading ./', async () => { await writeFixture('docs/a.md'); await expect(resolveExplicitFiles(baseDir, ['./docs/a.md'])).resolves.toEqual(['docs/a.md']); }); it('errors listing paths that matched nothing', async () => { await writeFixture('docs/a.mdx'); await expect( resolveExplicitFiles(baseDir, ['docs/a.mdx', 'docs/missing.mdx', 'nope/*.mdx']) ).rejects.toThrow('No matching files for: docs/missing.mdx, nope/*.mdx'); }); it('accepts existing literal paths regardless of extension', async () => { await writeFixture('README.txt'); await expect(resolveExplicitFiles(baseDir, ['README.txt'])).resolves.toEqual(['README.txt']); }); it('rejects a directory passed as a literal path', async () => { await fs.mkdir(path.join(baseDir, 'docs'), { recursive: true }); await expect(resolveExplicitFiles(baseDir, ['docs'])).rejects.toThrow('No matching files'); }); it('expands globs recursively and keeps only docs files', async () => { await writeFixture('docs/a.mdx'); await writeFixture('docs/nested/deep/b.mdx'); await writeFixture('docs/nested/notes.txt'); await writeFixture('top.md'); await expect(resolveExplicitFiles(baseDir, ['docs/**/*.mdx'])).resolves.toEqual([ 'docs/a.mdx', 'docs/nested/deep/b.mdx', ]); await expect(resolveExplicitFiles(baseDir, ['*.md'])).resolves.toEqual(['top.md']); }); it('dedupes overlapping literal and glob matches', async () => { await writeFixture('docs/a.mdx'); await expect(resolveExplicitFiles(baseDir, ['docs/a.mdx', 'docs/*.mdx'])).resolves.toEqual([ 'docs/a.mdx', ]); }); });