import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { mkdtemp, rm, writeFile } from 'node:fs/promises'; import { describe, expect, test } from 'vite-plus/test'; import { encodeDocsPlaygroundHash } from './docs-playground-hash.js'; import { highlightSourcePlugin } from './highlight-source-plugin.js'; import type { HighlightedSource } from './highlighted-source.js'; describe('highlightSourcePlugin', () => { test('loads a highlighted source module for the highlight query', async () => { const directory = await mkdtemp(join(tmpdir(), 'luke-ui-highlight-source-')); const path = join(directory, 'example.tsx'); const source = '\n\tconst example =
Hello
;\n'; try { await writeFile(path, source); const load = highlightSourcePlugin().load; if (typeof load !== 'function') throw new Error('Expected a plugin load hook'); const module = await load.call({} as never, `${path}?highlight`); if (typeof module !== 'string') throw new Error('Expected a JavaScript module'); const highlighted = JSON.parse( module.slice('export default '.length, -';\n'.length), ) as HighlightedSource; expect(highlighted.html).toContain(' { const directory = await mkdtemp(join(tmpdir(), 'luke-ui-highlight-source-')); const path = join(directory, 'example.tsx'); const source = "import { DecorativeBox } from './decorative-box.js';\n"; try { await writeFile(path, source); const load = highlightSourcePlugin().load; if (typeof load !== 'function') throw new Error('Expected a plugin load hook'); const module = await load.call({} as never, `${path}?highlight`); if (typeof module !== 'string') throw new Error('Expected a JavaScript module'); const highlighted = JSON.parse( module.slice('export default '.length, -';\n'.length), ) as HighlightedSource; expect(highlighted.playgroundHash).toBeNull(); } finally { await rm(directory, { force: true, recursive: true }); } }); });