import { collectClientDefines, createTestApp } from '@vuepress/test-utils' import { describe, expect, it } from 'vitest' import type { GiscusPluginOptions, WalinePluginOptions, } from '../../src/node/index.js' import { commentPlugin } from '../../src/node/index.js' const customLocales = { '/': { placeholder: 'Leave a comment here' }, } const walineOptions: WalinePluginOptions = { provider: 'Waline', serverURL: 'https://waline.example.com', locales: customLocales, } const giscusOptions: GiscusPluginOptions = { provider: 'Giscus', repo: 'vuepress/ecosystem', repoId: 'R_kgDOExample', category: 'Comments', categoryId: 'DIC_kwDOExample', } describe('comment plugin defines', () => { it('resolves the custom waline locales on every define call', async () => { const app = await createTestApp({ plugins: [commentPlugin(walineOptions)] }) try { // VuePress runs the hook once per bundle, the server bundle first const serverDefines = await collectClientDefines(app, true) const clientDefines = await collectClientDefines(app, false) expect(serverDefines.WALINE_LOCALES).toMatchObject({ '/': customLocales['/'], }) expect(clientDefines.WALINE_LOCALES).toMatchObject({ '/': customLocales['/'], }) } finally { app.cleanup() } }) it('does not mutate the options of the user', async () => { const before = structuredClone(walineOptions) const app = await createTestApp({ plugins: [commentPlugin(walineOptions)] }) try { await collectClientDefines(app, true) await collectClientDefines(app, false) expect('locales' in walineOptions).toBe(true) expect(walineOptions).toStrictEqual(before) } finally { app.cleanup() } }) it('keeps the client options without the waline locales', async () => { const app = await createTestApp({ plugins: [commentPlugin(walineOptions)] }) try { const clientOptions = (await collectClientDefines(app, false)) .__COMMENT_OPTIONS__ as Record // the options of the user are never exposed as the client payload expect(clientOptions).not.toBe(walineOptions) expect(clientOptions).toStrictEqual({ provider: 'Waline', serverURL: 'https://waline.example.com', }) expect(clientOptions).not.toHaveProperty('locales') } finally { app.cleanup() } }) it('leaves the options and defines of the other providers untouched', async () => { const before = structuredClone(giscusOptions) const app = await createTestApp({ plugins: [commentPlugin(giscusOptions)] }) try { const serverDefines = await collectClientDefines(app, true) const clientDefines = await collectClientDefines(app, false) for (const defines of [serverDefines, clientDefines]) { expect(defines.WALINE_LOCALES).toBeUndefined() expect(defines.WALINE_META).toBeUndefined() expect(defines.__COMMENT_OPTIONS__).toStrictEqual(giscusOptions) expect(defines.__COMMENT_OPTIONS__).not.toHaveProperty('locales') } expect(giscusOptions).toStrictEqual(before) } finally { app.cleanup() } }) })