import { describe, expect, it, vi } from 'vitest' // oxlint-disable typescript/no-implied-eval, typescript/no-unsafe-call, no-new-func -- the tests evaluate the generated module exactly as the client does import type { App } from 'vuepress/core' import { prepareThemeData } from '../../src/node/prepareThemeData.js' import type { ThemeData } from '../../src/shared/index.js' interface TestApp { app: App getContent: () => string writeTemp: ReturnType } interface GeneratedThemeData { editLink: { pattern: (page: { path: string }) => string } navbar: { link: (page: { path: string }) => string; text: string }[] } /** * Create a stub app that captures the generated module instead of writing it * * 创建捕获生成模块内容而不写文件的桩 app * * @param isDev - Whether the app runs in dev mode / app 是否运行在开发模式 * @returns The stub app / 桩 app */ const createApp = (isDev = false): TestApp => { let content = '' const writeTemp = vi.fn< (filePath: string, fileContent: string) => Promise >((_filePath: string, fileContent: string): Promise => { content = fileContent return Promise.resolve(fileContent) }) return { app: { env: { isDev }, writeTemp, } as unknown as App, getContent: () => content, writeTemp, } } /** * Evaluate the generated module the same way the client imports it * * 以客户端导入的方式求值生成的模块 * * @param content - The generated module / 生成的模块内容 * @returns The exported theme data / 导出的主题数据 */ const evaluateThemeData = (content: string): any => { const moduleContent = content.replace( 'export const themeData =', 'const themeData =', ) return new Function(`${moduleContent}\nreturn themeData`)() } describe(prepareThemeData, () => { it('should write the theme data to the temp file', async () => { const { app, getContent, writeTemp } = createApp() await prepareThemeData(app, { title: 'My Theme' } as ThemeData) expect(writeTemp).toHaveBeenCalledWith( 'internal/themeData.js', expect.any(String), ) const content = getContent() expect(content).toContain('export const themeData = JSON.parse(') expect(evaluateThemeData(content) as { title: string }).toStrictEqual({ title: 'My Theme', }) }) it('should keep the plain JSON output when the theme data has no function', async () => { const { app, getContent } = createApp() await prepareThemeData(app, { navbar: [{ link: '/guide/', text: 'Guide' }], } as ThemeData) const content = getContent() // the extra deserializer must not be introduced for the common case expect(content).not.toContain('deserializeFunctions') expect(content).not.toContain('_vp-fn_') expect( evaluateThemeData(content) as { navbar: { link: string; text: string }[] }, ).toStrictEqual({ navbar: [{ link: '/guide/', text: 'Guide' }], }) }) it('should serialize the function values into the generated module', async () => { const { app, getContent } = createApp() const pattern = (page: { path: string }): string => `https://github.com/vuepress/ecosystem/edit/main/docs/${page.path}` const link = (page: { path: string }): string => page.path await prepareThemeData(app, { editLink: { pattern }, navbar: [{ link, text: 'Guide' }], } as ThemeData) const content = getContent() expect(content).toContain('function deserializeFunctions') expect(content).toContain('deserializeFunctions(JSON.parse(') expect(content).toContain('_vp-fn_') expect(content).toContain( 'https://github.com/vuepress/ecosystem/edit/main/docs/', ) const themeData = evaluateThemeData(content) as GeneratedThemeData expect(themeData.editLink.pattern).toBeTypeOf('function') expect(themeData.editLink.pattern({ path: 'guide/README.md' })).toBe( 'https://github.com/vuepress/ecosystem/edit/main/docs/guide/README.md', ) expect(themeData.navbar[0].link).toBeTypeOf('function') expect(themeData.navbar[0].link({ path: '/guide/' })).toBe('/guide/') // oxlint-disable-next-line vitest/max-expects -- the emitted module is checked textually and at runtime expect(themeData.navbar[0].text).toBe('Guide') }) it('should serialize the shorthand methods into the generated module', async () => { const { app, getContent } = createApp() await prepareThemeData(app, { editLink: { pattern(page: { path: string }): string { return `/${page.path}` }, }, } as ThemeData) const themeData = evaluateThemeData(getContent()) as GeneratedThemeData expect(themeData.editLink.pattern).toBeTypeOf('function') expect(themeData.editLink.pattern({ path: 'guide/' })).toBe('/guide/') }) it('should keep the HMR code in dev mode', async () => { const { app, getContent } = createApp(true) await prepareThemeData(app, { editLink: { pattern: (page: { path: string }): string => page.path }, } as ThemeData) const content = getContent() expect(content).toContain('import.meta.webpackHot') expect(content).toContain('__VUE_HMR_RUNTIME__.updateThemeData(themeData)') // `import.meta` is not allowed in `new Function`, the module is only // asserted textually in dev mode expect(content).toContain('deserializeFunctions(JSON.parse(') }) })