Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

10.1 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import {
  convertFlowFenceToMermaid,
  convertLatexDelimiters,
  convertSequenceFenceToMermaid,
  hasMarkdownMath,
  processLatexContent,
  processMarkdownContent,
} from "../lib/latex";

// ---------------------------------------------------------------------------
// hasMarkdownMath — shared Simple/Rich renderer routing
// ---------------------------------------------------------------------------

test("hasMarkdownMath: detects plain inline formulas from chat prose", () => {
  const content = "假设有一个函数 $f(x)$,在 $x=2$ 这个点上,函数值趋近 $5$。";

  assert.equal(hasMarkdownMath(content), true);
  assert.equal(hasMarkdownMath("The limit is $2$."), true);
});

test("hasMarkdownMath: detects display and backslash delimiters while streaming", () => {
  assert.equal(hasMarkdownMath("Working... $$"), true);
  assert.equal(hasMarkdownMath("Solve \\(x=2"), true);
  assert.equal(hasMarkdownMath("Then \\[x^2"), true);
});

test("hasMarkdownMath: does not confuse ordinary currency or escaped dollars", () => {
  assert.equal(hasMarkdownMath("Tickets cost $5 and $10 respectively."), false);
  assert.equal(hasMarkdownMath("Write \\$5 and \\$10 literally."), false);
});

// ---------------------------------------------------------------------------
// convertLatexDelimiters
// ---------------------------------------------------------------------------

test("convertLatexDelimiters: \\\\[...\\\\] → block $$...$$", () => {
  const input = "Before \\[x^2 + 1\\] after";
  const result = convertLatexDelimiters(input);
  assert.ok(result.includes("$$\nx^2 + 1\n$$"));
});

test("convertLatexDelimiters: \\\\(...\\\\) → inline $...$", () => {
  const input = "Solve \\(x = 2\\) now";
  const result = convertLatexDelimiters(input);
  assert.ok(result.includes("$x = 2$"));
});

test("convertLatexDelimiters: strips \\\\(\\\\) inside $$...$$", () => {
  const input = "$$\\(x^2\\)$$";
  const result = convertLatexDelimiters(input);
  assert.ok(result.includes("$$\nx^2\n$$"));
  assert.ok(!result.includes("\\("));
});

test("convertLatexDelimiters: multiline \\\\[...\\\\]", () => {
  const input = "\\[\n\\frac{a}{b}\n\\]";
  const result = convertLatexDelimiters(input);
  assert.ok(result.includes("$$"));
  assert.ok(result.includes("\\frac{a}{b}"));
});

test("convertLatexDelimiters: preserves expr containing $& replacement char", () => {
  const input = "\\[x \\$\\& y\\]";
  const result = convertLatexDelimiters(input);
  assert.ok(
    result.includes("x \\$\\& y"),
    "special regex replacement char $& must be preserved",
  );
});

test("convertLatexDelimiters: returns empty-ish input unchanged", () => {
  assert.equal(convertLatexDelimiters(""), "");
  assert.equal(convertLatexDelimiters(null as unknown as string), null);
});

test("convertLatexDelimiters: collapses triple+ newlines", () => {
  const input = "a\n\n\n\nb";
  const result = convertLatexDelimiters(input);
  assert.ok(!result.includes("\n\n\n"));
});

// ---------------------------------------------------------------------------
// processLatexContent (thin wrapper)
// ---------------------------------------------------------------------------

test("processLatexContent: delegates to convertLatexDelimiters", () => {
  assert.equal(processLatexContent(""), "");
  const out = processLatexContent("\\(x\\)");
  assert.ok(out.includes("$x$"));
});

// ---------------------------------------------------------------------------
// processMarkdownContent — heading normalisation
// ---------------------------------------------------------------------------

test("headings: inserts space when missing", () => {
  const result = processMarkdownContent("##Title");
  assert.ok(result.includes("## Title"));
});

test("headings: leaves properly-spaced headings alone", () => {
  const result = processMarkdownContent("## Title");
  assert.ok(result.includes("## Title"));
});

test("headings: does not split 7+ consecutive hashes", () => {
  const result = processMarkdownContent("#######");
  assert.equal(result.trim(), "#######");
});

test("headings: leaves heading-like fenced code verbatim", () => {
  const input = [
    "```c",
    "##define FEATURE",
    "```",
    "",
    "~~~text",
    "###literal",
    "~~~",
  ].join("\n");

  assert.equal(processMarkdownContent(input), input);
});

test("headings: leaves raw HTML code blocks verbatim", () => {
  const input = [
    "<pre>",
    "###literal",
    "</pre>",
    "",
    "<code>",
    "##define FEATURE",
    "</code>",
  ].join("\n");

  assert.equal(processMarkdownContent(input), input);
});

// ---------------------------------------------------------------------------
// processMarkdownContent — inline math normalisation ($$...$$ → $...$)
// ---------------------------------------------------------------------------

test("inline math: $$x$$ on a line with other text → $x$", () => {
  const result = processMarkdownContent("Compute $$x^2$$ now");
  assert.ok(result.includes("$x^2$"));
  assert.ok(!result.includes("$$x^2$$"));
});

test("inline math: standalone one-line $$...$$ → split to block", () => {
  const result = processMarkdownContent("$$\\frac{a}{b}$$");
  const lines = result.trim().split("\n");
  assert.equal(lines[0], "$$");
  assert.ok(lines[1].includes("\\frac{a}{b}"));
  assert.equal(lines[2], "$$");
});

test("display math: $$ hugging a multi-line formula → fences on their own lines (#1623)", () => {
  const input = "$$Testing\\ Latex \\rightarrow\n\\leftarrow ENTER\\ HERE$$";
  const lines = processMarkdownContent(input).trim().split("\n");
  assert.deepEqual(lines, [
    "$$",
    "Testing\\ Latex \\rightarrow",
    "\\leftarrow ENTER\\ HERE",
    "$$",
  ]);
});

test("display math: aligned block with the opening fence hugging, closing alone", () => {
  const input = "$$\\begin{aligned}\na &= b \\\\\nc &= d\n\\end{aligned}\n$$";
  const lines = processMarkdownContent(input).trim().split("\n");
  assert.equal(lines[0], "$$");
  assert.equal(lines[1], "\\begin{aligned}");
  assert.equal(lines[lines.length - 2], "\\end{aligned}");
  assert.equal(lines[lines.length - 1], "$$");
});

test("display math: fences already on their own lines are left alone", () => {
  const input = "$$\na = b\nc = d\n$$";
  assert.equal(processMarkdownContent(input).trim(), input);
});

test("display math: an unclosed $$ across a blank line is not rewritten", () => {
  const input = "$$x = 1\n\nprice is 5$ tomorrow";
  const result = processMarkdownContent(input);
  assert.ok(result.includes("$$x = 1"));
});

// ---------------------------------------------------------------------------
// processMarkdownContent — loose block math ($...\n...\n$) promotion
// ---------------------------------------------------------------------------

test("loose block math: single-$ lines wrapping LaTeX → promoted to $$", () => {
  const input = "$\n\\frac{a}{b}\n$";
  const result = processMarkdownContent(input);
  const lines = result.trim().split("\n");
  assert.equal(lines[0], "$$");
  assert.ok(lines.some((l) => l.includes("\\frac{a}{b}")));
  assert.equal(lines[lines.length - 1], "$$");
});

test("loose block math: single-$ lines with non-LaTeX content → not promoted", () => {
  const input = "$\nhello world\n$";
  const result = processMarkdownContent(input);
  assert.ok(!result.startsWith("$$"));
});

test("loose block math: recognises \\\\ (line break) as LaTeX", () => {
  const input = "$\na \\\\\nb\n$";
  const result = processMarkdownContent(input);
  const lines = result.trim().split("\n");
  assert.equal(lines[0], "$$");
});

test("loose block math: recognises _ and ^ as LaTeX", () => {
  const input = "$\nx_1 + y^2\n$";
  const result = processMarkdownContent(input);
  const lines = result.trim().split("\n");
  assert.equal(lines[0], "$$");
});

// ---------------------------------------------------------------------------
// processMarkdownContent — end-to-end pipeline
// ---------------------------------------------------------------------------

test("pipeline: combined heading + inline math + delimiter conversion", () => {
  const input = "##Heading\n\nSolve \\(x=1\\) and $$y$$";
  const result = processMarkdownContent(input);
  assert.ok(result.includes("## Heading"), "heading should be normalised");
  assert.ok(result.includes("$x=1$"), "\\\\(...\\\\) should become $...$");
  assert.ok(result.includes("$y$"), "$$y$$ inline should become $y$");
});

test("pipeline: preserves fenced code blocks", () => {
  const input = "```python\nprint('hello')\n```";
  const result = processMarkdownContent(input);
  assert.ok(result.includes("print('hello')"));
});

// ---------------------------------------------------------------------------
// editor.md fence conversion (flow / seq)
// ---------------------------------------------------------------------------

test("flow fence: keeps yes/no branch labels from the source side", () => {
  const input = [
    "st=>start: Start",
    "cond=>condition: Ready?",
    "a=>operation: Go",
    "b=>operation: Wait",
    "e=>end: Done",
    "st->cond",
    "cond(yes)->a->e",
    "cond(no)->b->e",
  ].join("\n");
  const result = convertFlowFenceToMermaid(input);
  assert.ok(result, "conversion should succeed");
  assert.ok(result.includes("cond -->|yes| a"));
  assert.ok(result.includes("cond -->|no| b"));
});

test("flow fence: layout hints are not edge labels", () => {
  const input = [
    "st=>start: Start",
    "op=>operation: Work",
    "e=>end: Done",
    "st(right)->op->e",
  ].join("\n");
  const result = convertFlowFenceToMermaid(input);
  assert.ok(result, "conversion should succeed");
  assert.ok(result.includes("st --> op"));
  assert.ok(!result.includes("|right|"));
});

test("seq fence: converts messages and notes", () => {
  const input = [
    "Student->DeepTutor: Ask for help",
    "Note right of DeepTutor: Collect memory\\nand context",
    "DeepTutor-->Student: Respond",
  ].join("\n");
  const result = convertSequenceFenceToMermaid(input);
  assert.ok(result, "conversion should succeed");
  assert.ok(result.startsWith("sequenceDiagram"));
  assert.ok(result.includes("Student->>DeepTutor: Ask for help"));
  assert.ok(result.includes("Collect memory<br/>and context"));
});