项目文件夹

文件
wehub-resource-sync 2771cff92b
CI / lint (push) Failing after 1s
CI / typecheck (push) Failing after 2s
CI / test (push) Failing after 1s
CI / build (push) Has been skipped
chore: import upstream snapshot with attribution
2026-07-13 12:22:56 +08:00

55 行
1.6 KiB
TypeScript

import {
escapeMathDelimitersForStreaming,
hasStreamingMathDelimiters,
} from '@/utils/markdownMath';
describe('markdownMath', () => {
describe('escapeMathDelimitersForStreaming', () => {
it('escapes inline and display math delimiters outside code', () => {
expect(escapeMathDelimitersForStreaming('Use $x + y$ and $$z^2$$.')).toBe(
'Use \\$x + y\\$ and \\$\\$z^2\\$\\$.'
);
});
it('preserves inline code and fenced code dollars', () => {
const markdown = [
'Text $x$',
'`echo $PATH`',
'```bash',
'echo "$HOME"',
'```',
'Done $$y$$',
].join('\n');
expect(escapeMathDelimitersForStreaming(markdown)).toBe([
'Text \\$x\\$',
'`echo $PATH`',
'```bash',
'echo "$HOME"',
'```',
'Done \\$\\$y\\$\\$',
].join('\n'));
});
it('keeps already escaped dollars unchanged', () => {
expect(escapeMathDelimitersForStreaming('Cost is \\$5, math is $x$.')).toBe(
'Cost is \\$5, math is \\$x\\$.'
);
});
it('does not alter dollars inside raw html tag attributes', () => {
expect(escapeMathDelimitersForStreaming('<span title="$x$">value $y$</span>')).toBe(
'<span title="$x$">value \\$y\\$</span>'
);
});
});
describe('hasStreamingMathDelimiters', () => {
it('detects unescaped dollars outside code', () => {
expect(hasStreamingMathDelimiters('math $x$')).toBe(true);
expect(hasStreamingMathDelimiters('`echo $PATH`')).toBe(false);
expect(hasStreamingMathDelimiters('\\$5')).toBe(false);
});
});
});