返回 slidev
export.test.ts
根目录 / packages / slidev / node / commands / export.test.ts
1 import fs from 'node:fs/promises'
2 import { tmpdir } from 'node:os'
3 import { dirname, join } from 'node:path'
4 import { afterEach, describe, expect, it } from 'vitest'
5 import { exportSlides, resolveExportDarkMode } from './export'
6
7 /**
8 * `exportSlides` writes to `--output` through several writers, and they used
9 * to disagree about whose job it was to create the parent directory:
10 * `page.pdf({ path })` creates it, `genPagePng` creates it, and everything
11 * calling `fs.writeFile` directly did not. `dist/slides.pptx` then failed
12 * with ENOENT while one-piece PDF wrote the same path happily.
13 *
14 * A real export needs a browser and a dev server, neither of which belongs in
15 * a unit test, and it never gets that far here: `importPlaywright` asks
16 * `getRoots` for the user root, which throws without an entry, so every call
17 * below rejects before anything renders. That is enough, because the
18 * directory is created first. The matcher pins each rejection to that known
19 * failure, so an error from the `mkdir` itself fails the test instead of
20 * satisfying it.
21 */
22
23 const NO_ENTRY = /Cannot find roots/
24
25 const tempRoots: string[] = []
26
27 afterEach(async () => {
28 await Promise.all(tempRoots.splice(0).map(root => fs.rm(root, { recursive: true, force: true })))
29 })
30
31 async function tempRoot() {
32 const root = await fs.mkdtemp(join(tmpdir(), 'slidev-export-'))
33 tempRoots.push(root)
34 return root
35 }
36
37 function exportInto(output: string, format: 'pdf' | 'pptx' | 'pptx-editable') {
38 return exportSlides({
39 output,
40 format,
41 slides: [],
42 total: 0,
43 waitUntil: undefined,
44 })
45 }
46
47 describe('exportSlides creates the output directory', () => {
48 it.each([
49 ['pptx', 'slides.pptx'],
50 ['pptx-editable', 'slides.pptx'],
51 ['pdf', 'slides.pdf'],
52 ] as const)('creates a missing directory for %s', async (format, filename) => {
53 const output = join(await tempRoot(), 'dist', filename)
54
55 await expect(exportInto(output, format)).rejects.toThrow(NO_ENTRY)
56 await expect(fs.stat(dirname(output))).resolves.toBeTruthy()
57 })
58
59 it('creates nested directories', async () => {
60 const output = join(await tempRoot(), 'a', 'b', 'c', 'slides.pptx')
61
62 await expect(exportInto(output, 'pptx')).rejects.toThrow(NO_ENTRY)
63 await expect(fs.stat(dirname(output))).resolves.toBeTruthy()
64 })
65
66 it('accepts an output with no directory part', async () => {
67 // Resolves to the working directory, which exists, so the rejection has
68 // to come from the missing entry rather than from the `mkdir`.
69 await expect(exportInto('slides.pptx', 'pptx')).rejects.toThrow(NO_ENTRY)
70 })
71 })
72
73 describe('resolveExportDarkMode', () => {
74 it.each([
75 ['light dark', false],
76 ['dark light', true],
77 ] as const)('uses the first scheme for "%s" when there is no override', (colorSchema, expected) => {
78 expect(resolveExportDarkMode(undefined, colorSchema)).toBe(expected)
79 })
80
81 it.each([
82 ['light dark', true, true],
83 ['dark light', false, false],
84 ] as const)('honors an explicit override for "%s"', (colorSchema, override, expected) => {
85 expect(resolveExportDarkMode(override, colorSchema)).toBe(expected)
86 })
87
88 it.each([
89 ['auto', false],
90 ['light', false],
91 ['dark', true],
92 ] as const)('preserves the default for "%s"', (colorSchema, expected) => {
93 expect(resolveExportDarkMode(undefined, colorSchema)).toBe(expected)
94 })
95
96 it.each([
97 ['auto', true, true],
98 ['light', true, true],
99 ['dark', false, true],
100 ] as const)('preserves existing override behavior for "%s"', (colorSchema, override, expected) => {
101 expect(resolveExportDarkMode(override, colorSchema)).toBe(expected)
102 })
103 })
104
104 lines TYPESCRIPT