|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { normalizeChatMath } from "./markdown-math"; |
| 4 | + |
| 5 | +describe("normalizeChatMath", () => { |
| 6 | + it.each([ |
| 7 | + ["\\[x+y\\]\n$$z$$", "$$\nx+y\n$$\n$$\nz\n$$"], |
| 8 | + [ |
| 9 | + "math $x^2+1$ and $$\\sum_{i=1}^n i$$ reply ok", |
| 10 | + "math $x^2+1$ and \n\n$$\n\\sum_{i=1}^n i\n$$\n\n reply ok", |
| 11 | + ], |
| 12 | + ["before\n\\[\nx=\\pm i\n\\]\nafter", "before\n$$\nx=\\pm i\n$$\nafter"], |
| 13 | + ["Inline \\(x^2=-1\\)", "Inline $x^2=-1$"], |
| 14 | + ])("normalizes chat math in %s", (markdown, expected) => { |
| 15 | + expect(normalizeChatMath(markdown)).toBe(expected); |
| 16 | + }); |
| 17 | + |
| 18 | + it.each([ |
| 19 | + ["$5.5$", "$5.5$"], |
| 20 | + ["$5$", "$5$"], |
| 21 | + ["价格:$5 和 $5/month", "价格:\\$5 和 \\$5/month"], |
| 22 | + ["$20,000 and $30,000", "\\$20,000 and \\$30,000"], |
| 23 | + ])("keeps only valid Pandoc-style dollar pairs in %s", (markdown, expected) => { |
| 24 | + expect(normalizeChatMath(markdown)).toBe(expected); |
| 25 | + }); |
| 26 | + |
| 27 | + it("keeps unpaired dollar amounts literal", () => { |
| 28 | + expect(normalizeChatMath("Prices are $5 and $3 today.")).toBe( |
| 29 | + "Prices are \\$5 and \\$3 today.", |
| 30 | + ); |
| 31 | + expect(normalizeChatMath("The total is $12.50 today.")).toBe("The total is \\$12.50 today."); |
| 32 | + }); |
| 33 | + |
| 34 | + it("does not alter math-like text in fenced or inline code", () => { |
| 35 | + const markdown = ["```txt", "~~~", "\\(x\\)", "```", "``code ` \\(y\\)`` and \\(z\\)"].join( |
| 36 | + "\n", |
| 37 | + ); |
| 38 | + expect(normalizeChatMath(markdown)).toBe( |
| 39 | + ["```txt", "~~~", "\\(x\\)", "```", "``code ` \\(y\\)`` and $z$"].join("\n"), |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it("does not alter math-like text in link destinations or HTML tags", () => { |
| 44 | + const markdown = |
| 45 | + '[docs](https://example.com/\\(intro\\)) <a href="/\\(raw\\)">link</a> \\(x\\)'; |
| 46 | + expect(normalizeChatMath(markdown)).toBe( |
| 47 | + '[docs](https://example.com/\\(intro\\)) <a href="/\\(raw\\)">link</a> $x$', |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it("escapes dollar signs that cannot form valid pairs", () => { |
| 52 | + expect(normalizeChatMath("$5-$10, ($5 off), $5%, and $5: each")).toBe( |
| 53 | + "\\$5-\\$10, (\\$5 off), \\$5%, and \\$5: each", |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("normalizes multiple display spans on one line separately", () => { |
| 58 | + expect(normalizeChatMath("$$a$$ and $$b$$")).toBe("\n\n$$\na\n$$\n\n and \n\n$$\nb\n$$\n\n"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("normalizes math in prose comparisons", () => { |
| 62 | + expect(normalizeChatMath("for 0 < x, \\(x^2\\) grows when x > 1")).toBe( |
| 63 | + "for 0 < x, $x^2$ grows when x > 1", |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("does not alter indented code or blockquote fenced code", () => { |
| 68 | + const markdown = [ |
| 69 | + " code block \\(x\\) indented", |
| 70 | + "> ```txt", |
| 71 | + "> \\(y\\)", |
| 72 | + "> ```", |
| 73 | + "after \\(z\\)", |
| 74 | + ].join("\n"); |
| 75 | + expect(normalizeChatMath(markdown)).toBe( |
| 76 | + [" code block \\(x\\) indented", "> ```txt", "> \\(y\\)", "> ```", "after $z$"].join("\n"), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it("only treats bracket math as display math when it stands alone", () => { |
| 81 | + // Inline bracket escapes are commonly emitted as citations, so only a |
| 82 | + // whole-line \\[…] span is interpreted as display math. |
| 83 | + expect(normalizeChatMath("See \\[1\\] for details\n\\[x+y\\]")).toBe( |
| 84 | + "See \\[1\\] for details\n$$\nx+y\n$$", |
| 85 | + ); |
| 86 | + }); |
| 87 | +}); |
0 commit comments