import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { formatHandler } from '../src/format.js'; import { terminate } from '../src/helpers.js'; const fixture = vi.hoisted(() => ({ directory: '' })); vi.mock('../src/helpers.js', () => ({ get CMD_EXEC_PATH() { return fixture.directory; }, terminate: vi.fn(), })); vi.mock('@mintlify/prebuild', () => ({ getMintIgnore: vi.fn(async () => []) })); vi.mock('@mintlify/previewing', () => ({ addLog: vi.fn(), ErrorLog: () => null, SuccessLog: () => null, })); beforeEach(async () => { vi.clearAllMocks(); fixture.directory = await fs.mkdtemp(path.join(os.tmpdir(), 'mint-format-math-')); }); afterEach(async () => { vi.restoreAllMocks(); await fs.rm(fixture.directory, { recursive: true, force: true }); }); describe('formatHandler math and currency', () => { it.each([ ['single-dollar math', 'The formula is $x$ in text', 'The formula is $$x$$ in text'], [ 'paired currency dollars (escaped, not converted to math)', 'Savings are $1 today ($5 tomorrow).', 'Savings are \\$1 today (\\$5 tomorrow).', ], ['literal currency', 'Savings are $1 today.', 'Savings are \\$1 today.'], [ 'escaped currency', 'Savings are \\$1 today (\\$5 tomorrow).', 'Savings are \\$1 today (\\$5 tomorrow).', ], ['double-dollar math', 'The formula is $$x$$ in text', 'The formula is $$x$$ in text'], ['double-dollar math followed by a digit', 'The value is $$2^3$$8.', 'The value is $$2^3$$8.'], [ 'paired currency dollars on a page with an HTML comment', '\n\nSavings are $1 today ($5 tomorrow).', '{/* note */}\n\nSavings are \\$1 today (\\$5 tomorrow).', ], ])('preserves %s and produces stable output', async (_label, source, expected) => { const file = path.join(fixture.directory, 'page.mdx'); await fs.writeFile(file, source); const writeFile = vi.spyOn(fs, 'writeFile'); await formatHandler(); expect(terminate).toHaveBeenLastCalledWith(0); expect(await fs.readFile(file, 'utf8')).toBe(`${expected}\n`); writeFile.mockClear(); await formatHandler(); expect(terminate).toHaveBeenLastCalledWith(0); expect(writeFile).not.toHaveBeenCalled(); }); });