|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { maskInlineLaTeX } from './latex-protection'; |
| 3 | + |
| 4 | +describe('maskInlineLaTeX', () => { |
| 5 | + it('should protect LaTeX $x + y$ but not money $3.99', () => { |
| 6 | + const latexExpressions: string[] = []; |
| 7 | + const input = 'I have $10, $3.99 and $x + y$ and $100x$. The amount is $2,000.'; |
| 8 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 9 | + |
| 10 | + expect(output).toBe('I have $10, $3.99 and <<LATEX_0>> and <<LATEX_1>>. The amount is $2,000.'); |
| 11 | + expect(latexExpressions).toEqual(['$x + y$', '$100x$']); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should ignore money like $5 and $12.99', () => { |
| 15 | + const latexExpressions: string[] = []; |
| 16 | + const input = 'Prices are $12.99 and $5. Tax?'; |
| 17 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 18 | + |
| 19 | + expect(output).toBe('Prices are $12.99 and $5. Tax?'); |
| 20 | + expect(latexExpressions).toEqual([]); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should protect inline math $a^2 + b^2$ even after text', () => { |
| 24 | + const latexExpressions: string[] = []; |
| 25 | + const input = 'Pythagorean: $a^2 + b^2 = c^2$.'; |
| 26 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 27 | + |
| 28 | + expect(output).toBe('Pythagorean: <<LATEX_0>>.'); |
| 29 | + expect(latexExpressions).toEqual(['$a^2 + b^2 = c^2$']); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should not protect math that has letter after closing $ (e.g. units)', () => { |
| 33 | + const latexExpressions: string[] = []; |
| 34 | + const input = 'The cost is $99 and change.'; |
| 35 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 36 | + |
| 37 | + expect(output).toBe('The cost is $99 and change.'); |
| 38 | + expect(latexExpressions).toEqual([]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should allow $x$ followed by punctuation', () => { |
| 42 | + const latexExpressions: string[] = []; |
| 43 | + const input = 'We know $x$, right?'; |
| 44 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 45 | + |
| 46 | + expect(output).toBe('We know <<LATEX_0>>, right?'); |
| 47 | + expect(latexExpressions).toEqual(['$x$']); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should work across multiple lines', () => { |
| 51 | + const latexExpressions: string[] = []; |
| 52 | + const input = `Emma buys cupcakes for $3 each.\nHow much is $x + y$?`; |
| 53 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 54 | + |
| 55 | + expect(output).toBe(`Emma buys cupcakes for $3 each.\nHow much is <<LATEX_0>>?`); |
| 56 | + expect(latexExpressions).toEqual(['$x + y$']); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should not protect $100 but protect $matrix$', () => { |
| 60 | + const latexExpressions: string[] = []; |
| 61 | + const input = '$100 and $\\mathrm{GL}_2(\\mathbb{F}_7)$ are different.'; |
| 62 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 63 | + |
| 64 | + expect(output).toBe('$100 and <<LATEX_0>> are different.'); |
| 65 | + expect(latexExpressions).toEqual(['$\\mathrm{GL}_2(\\mathbb{F}_7)$']); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should skip if $ is followed by digit and alphanumeric after close (money)', () => { |
| 69 | + const latexExpressions: string[] = []; |
| 70 | + const input = 'I paid $5 quickly.'; |
| 71 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 72 | + |
| 73 | + expect(output).toBe('I paid $5 quickly.'); |
| 74 | + expect(latexExpressions).toEqual([]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should protect LaTeX even with special chars inside', () => { |
| 78 | + const latexExpressions: string[] = []; |
| 79 | + const input = 'Consider $\\alpha_1 + \\beta_2$ now.'; |
| 80 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 81 | + |
| 82 | + expect(output).toBe('Consider <<LATEX_0>> now.'); |
| 83 | + expect(latexExpressions).toEqual(['$\\alpha_1 + \\beta_2$']); |
| 84 | + }); |
| 85 | + |
| 86 | + it('short text', () => { |
| 87 | + const latexExpressions: string[] = ['$0$']; |
| 88 | + const input = '$a$\n$a$ and $b$'; |
| 89 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 90 | + |
| 91 | + expect(output).toBe('<<LATEX_1>>\n<<LATEX_2>> and <<LATEX_3>>'); |
| 92 | + expect(latexExpressions).toEqual(['$0$', '$a$', '$a$', '$b$']); |
| 93 | + }); |
| 94 | + |
| 95 | + it('empty text', () => { |
| 96 | + const latexExpressions: string[] = []; |
| 97 | + const input = '$\n$$\n'; |
| 98 | + const output = maskInlineLaTeX(input, latexExpressions); |
| 99 | + |
| 100 | + expect(output).toBe('$\n$$\n'); |
| 101 | + expect(latexExpressions).toEqual([]); |
| 102 | + }); |
| 103 | +}); |
0 commit comments