Skip to content

The agent that grows with you

26.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import { preprocessMarkdown } from '@/lib/markdown-preprocess'

describe('preprocessMarkdown', () => {
  it('strips inline accidental triple-backtick starts', () => {
    const input = [
      'Working as intended.',
      "Here's your scene: ``` http://localhost:8812/",
      '',
      '- **Multicolored cube**',
      '- **Rotates**'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('```')
    expect(output).toContain("Here's your scene:")
    // Loopback URLs in prose are user-facing content (#121683): the address
    // autolinks instead of being deleted from the sentence.
    expect(output).toContain('<http://localhost:8812/>')
    expect(output).toContain('- **Multicolored cube**')
  })

  it('demotes invalid fenced prose blocks with closers', () => {
    const fence = '```'

    const input = [
      `${fence} http://localhost:8812/`,
      '- **Scroll wheel** - zoom',
      '- **Right-drag/pan** - disabled',
      fence
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('```')
    // Loopback URLs in prose are user-facing content (#121683): the address
    // autolinks instead of being deleted from the sentence.
    expect(output).toContain('<http://localhost:8812/>')
    expect(output).toContain('- **Scroll wheel** - zoom')
  })

  it('drops fences around a preview-only URL block', () => {
    const fence = '```'
    const input = ['Server is back.', '', fence, 'http://localhost:8812/', fence].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toContain('Server is back.')
    expect(output).not.toContain('```')
    // Bare localhost URLs (no path after port) are still stripped.
    expect(output).not.toContain('http://localhost:8812/')
  })

  it('preserves localhost URLs with paths in fenced blocks', () => {
    const fence = '```'
    const input = ['Open this:', '', fence, 'http://localhost:8080/piwo', fence].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toContain('Open this:')
    expect(output).not.toContain('```')
    expect(output).toContain('http://localhost:8080/piwo')
  })

  it('preserves localhost URLs with paths in prose', () => {
    const output = preprocessMarkdown('Use this URL:\nhttp://localhost:8080/piwo')

    expect(output).toContain('Use this URL:')
    expect(output).toContain('http://localhost:8080/piwo')
  })

  it('demotes prose sentence masquerading as fence info', () => {
    const input = ['```Heads up - a bunny got added', '- Pure white (`#ffffff`)', '- Ambient dropped to 0.18'].join(
      '\n'
    )

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('```heads')
    expect(output).toContain('Heads up - a bunny got added')
    expect(output).toContain('- Pure white (`#ffffff`)')
  })

  it('keeps valid code fences intact', () => {
    const fence = '```'
    const input = [`${fence}ts`, 'const value = 1;', fence].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toContain('```ts')
    expect(output).toContain('const value = 1;')
  })

  it('keeps dangling real code fences during streaming', () => {
    const input = ['```ts', 'const value = 1;'].join('\n')
    const output = preprocessMarkdown(input)

    expect(output.startsWith('```ts')).toBe(true)
    expect(output).toContain('const value = 1;')
  })

  it('demotes dangling prose fences', () => {
    const input = ['```', '- Pure white (`#ffffff`)', '- Ambient dropped to 0.18'].join('\n')
    const output = preprocessMarkdown(input)

    expect(output).not.toContain('```')
    expect(output).toContain('- Pure white (`#ffffff`)')
  })

  it('autolinks raw urls in prose', () => {
    const output = preprocessMarkdown(
      'Book here:\nhttps://www.getyourguide.com/culebra-island-l145468/from-fajardo-tour-t19894/'
    )

    expect(output).toContain('<https://www.getyourguide.com/culebra-island-l145468/from-fajardo-tour-t19894/>')
  })

  it('does not include wrapper closing parens in raw-url autolinks', () => {
    const output = preprocessMarkdown('Check (https://example.com/page)')

    expect(output).toContain('(<https://example.com/page>)')
    expect(output).not.toContain('<https://example.com/page)>')
  })

  it('strips wrapper closing parens from bare raw-url autolinks', () => {
    const output = preprocessMarkdown('(https://example.com/page)')

    expect(output).toBe('(<https://example.com/page>)')
    expect(output).not.toContain('<https://example.com/page)>')
  })

  it('strips multiple wrapper closing parens from raw-url autolinks', () => {
    const output = preprocessMarkdown('((https://example.com/page))')

    expect(output).toBe('((<https://example.com/page>))')
    expect(output).not.toContain('<https://example.com/page)>')
    expect(output).not.toContain('<https://example.com/page))>')
  })

  it('leaves raw URLs inside inline code spans unchanged', () => {
    const input = 'Keep `https://example.com/page)` literal.'
    const output = preprocessMarkdown(input)

    expect(output).toBe(input)
  })

  it('keeps trailing punctuation outside wrapper-paren raw-url autolinks', () => {
    expect(preprocessMarkdown('(https://example.com/page).')).toBe('(<https://example.com/page>).')
    expect(preprocessMarkdown('(https://example.com/page),')).toBe('(<https://example.com/page>),')
  })

  it('preserves balanced parens inside raw-url autolinks', () => {
    const output = preprocessMarkdown('See https://example.com/wiki/Foo_(bar)')

    expect(output).toContain('<https://example.com/wiki/Foo_(bar)>')
  })

  it('preserves a trailing balanced pair after an earlier stray closing paren', () => {
    const output = preprocessMarkdown('See https://example.com/a)(b)')

    expect(output).toContain('<https://example.com/a)(b)>')
  })

  it('strips three wrapper closing parens from raw-url autolinks', () => {
    const output = preprocessMarkdown('(((https://example.com/page)))')

    expect(output).toBe('(((<https://example.com/page>)))')
  })

  it('preserves an unmatched opening paren at the end of a raw URL', () => {
    const output = preprocessMarkdown('https://example.com/foo(')

    expect(output).toContain('<https://example.com/foo(>')
  })

  it('strips only the wrapper closing paren around balanced-paren URLs', () => {
    const output = preprocessMarkdown('(https://example.com/wiki/Foo_(bar))')

    expect(output).toBe('(<https://example.com/wiki/Foo_(bar)>)')
    expect(output).not.toContain('<https://example.com/wiki/Foo_(bar))>')
  })

  it('does not autolink canonical markdown links', () => {
    const input = '[link](https://github.com/NousResearch/hermes-agent/issues)'
    const output = preprocessMarkdown(input)

    expect(output).toBe(input)
  })

  it('strips orphan numeric citation markers outside code spans', () => {
    const output = preprocessMarkdown('This is the source[0], but keep `items[0]` untouched.')

    expect(output).toContain('source,')
    expect(output).not.toContain('source[0]')
    expect(output).toContain('`items[0]`')
  })

  it('keeps citation markers anchored by a numbered source list', () => {
    const input = [
      'Ice floats because it is less dense than liquid water.[1][2]',
      '',
      '## Sources',
      '',
      '[1] https://example.com/a',
      '[2] https://example.com/b'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toContain('water.[1][2]')
    expect(output).toMatch(/\[1\][^\n]*example\.com\/a/)
    expect(output).toMatch(/\[2\][^\n]*example\.com\/b/)
  })

  it('strips a citation marker whose number is absent from the source list', () => {
    const input = 'A claim[1] and another[7].\n\n## Sources\n\n[1] https://example.com/a'

    const output = preprocessMarkdown(input)

    expect(output).toContain('claim[1]')
    expect(output).not.toContain('another[7]')
  })

  it('does not anchor a citation marker on source-list-like prose without a Sources header', () => {
    const input = 'Claim.[7]\n\n[7] todo'

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('Claim.[7]')
  })

  it('does not anchor a citation marker on a source entry inside fenced code', () => {
    const input = 'Claim.[7]\n\n```\n[7] https://example.com/a\n```'

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('Claim.[7]')
  })

  it('anchors citation markers under a plain Sources: header too', () => {
    const input = 'Claim.[7]\n\nSources:\n\n[7] https://example.com/a'

    const output = preprocessMarkdown(input)

    expect(output).toContain('Claim.[7]')
  })

  it('collects entries only after the last Sources header', () => {
    const input = [
      'Claim one[1] and claim two[7].',
      '',
      '## Sources',
      '',
      '[1] https://example.com/a',
      '',
      '## Sources',
      '',
      '[7] https://example.com/b'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('one[1]')
    expect(output).toContain('two[7]')
  })

  it('demotes title/url blocks wrapped in malformed inline fences', () => {
    const input = [
      '**🚢 TOMORROW (Fajardo, crystal clear cays, pickup avail):**',
      '',
      'Icacos Full-Day Catamaran — 6hr, $140, small group, pickup```',
      'https://www.getyourguide.com/fajardo-l882/from-fajardo-icacos-island-full-day-catamaran-trip-t19891/',
      '```Sail Getaway Luxury Cat (Cordillera Cays, water slide, unlimited rum) — 6hr, $195```',
      'https://www.getyourguide.com/fajardo-l882/icacos-all-inclusive-sailing-catamaran-beach-and-snorkel-t466138/'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('```')
    expect(output).toContain('Sail Getaway Luxury Cat')
    expect(output).toContain(
      '<https://www.getyourguide.com/fajardo-l882/from-fajardo-icacos-island-full-day-catamaran-trip-t19891/>'
    )
    expect(output).toContain(
      '<https://www.getyourguide.com/fajardo-l882/icacos-all-inclusive-sailing-catamaran-beach-and-snorkel-t466138/>'
    )
  })

  it('autolinks urls glued to prices and removes orphan fence tails', () => {
    const input = [
      '**🐢 TODAY (from San Juan, no driving):**',
      '',
      'Sea Turtles & Manatees Snorkel + Free Rum — 1.5hr,',
      '~$56```https://www.getyourguide.com/san-juan-puerto-rico-l355/san-juan-snorkel-sea-turtles-manatees-free-video-rum-t879147/ Old San Juan Sunset Cruise w/ Drinks + Hotel Pickup — 1.5hr, ~$99 (drinks, no snorkel)```',
      'https://www.getyourguide.com/en-gb/san-juan-puerto-rico-l355/san-juan-old-san-juan-sunset-cruise-with-drinks-transfer-t405191/'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('```')
    // Currency dollar amounts get escaped to `\$` in the preprocessor
    // so they don't get parsed as math delimiters by remark-math (we
    // enable singleDollarTextMath, which would otherwise greedy-match
    // `$56...$99` as one big inline math span). The escape is invisible
    // to the user — `\$` renders as a literal `$` in the final output.
    expect(output).toContain(
      '~\\$56<https://www.getyourguide.com/san-juan-puerto-rico-l355/san-juan-snorkel-sea-turtles-manatees-free-video-rum-t879147/> Old San Juan Sunset Cruise'
    )
    expect(output).toContain(
      '<https://www.getyourguide.com/en-gb/san-juan-puerto-rico-l355/san-juan-old-san-juan-sunset-cruise-with-drinks-transfer-t405191/>'
    )
  })

  it('demotes url-only fenced blocks to clickable markdown links', () => {
    const input = [
      'Sea Turtles & Manatees Snorkel + Free Rum — 1.5hr, ~$56',
      '```',
      'https://www.getyourguide.com/san-juan-puerto-rico-l355/san-juan-snorkel-sea-turtles-manatees-free-video-rum-t879147/',
      '```',
      '',
      'Old San Juan Sunset Cruise w/ Drinks + Hotel Pickup — 1.5hr, ~$99',
      '```',
      'https://www.getyourguide.com/en-gb/san-juan-puerto-rico-l355/san-juan-old-san-juan-sunset-cruise-with-drinks-transfer-t405191/',
      '```'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).not.toContain('```')
    expect(output).toContain(
      '<https://www.getyourguide.com/san-juan-puerto-rico-l355/san-juan-snorkel-sea-turtles-manatees-free-video-rum-t879147/>'
    )
    expect(output).toContain(
      '<https://www.getyourguide.com/en-gb/san-juan-puerto-rico-l355/san-juan-old-san-juan-sunset-cruise-with-drinks-transfer-t405191/>'
    )
  })

  it('does not swallow trailing emphasis asterisks into an autolinked url', () => {
    const input = '**PR opened: https://github.com/NousResearch/hermes-agent/pull/12345**'

    const output = preprocessMarkdown(input)

    // The URL is autolinked WITHOUT the trailing `**` glued into the href,
    // and the bold emphasis run stays intact so it renders as bold + a link.
    expect(output).toContain('<https://github.com/NousResearch/hermes-agent/pull/12345>')
    expect(output).not.toContain('pull/12345**>')
    expect(output).not.toContain('12345*')
  })

  it('stops an autolinked url at mid-string bold markers', () => {
    const input = 'See https://github.com/foo/bar**bold** for details.'

    const output = preprocessMarkdown(input)

    expect(output).toContain('<https://github.com/foo/bar>')
    expect(output).toContain('**bold**')
  })

  it('keeps underscores and tildes inside autolinked url paths', () => {
    const input = 'Docs at https://example.com/a_b/c~d/page'

    const output = preprocessMarkdown(input)

    expect(output).toContain('<https://example.com/a_b/c~d/page>')
  })

  it('escapes lone tildes in CJK ranges without touching strikethrough syntax', () => {
    const output = preprocessMarkdown('Ranges: 1~10,11~20 and ~~deleted~~ text.')

    expect(output).toContain('1\\~10,11\\~20')
    expect(output).toContain('~~deleted~~')
  })

  it('escapes lone-tilde approximation prefixes so they cannot pair up mid-paragraph', () => {
    const output = preprocessMarkdown('收益为 3~5 倍,成本约 ~¥0.089。')

    expect(output).toContain('3\\~5 倍')
    expect(output).toContain('\\~¥0.089')
  })

  it('does not escape lone tildes inside inline or fenced code', () => {
    const input = ['Use `1~10` as a literal.', '', '```txt', '1~10,11~20', '```'].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toContain('`1~10`')
    expect(output).toContain(['```txt', '1~10,11~20', '```'].join('\n'))
  })

  it('escapes unknown html-like prose tokens before they reach the renderer', () => {
    const output = preprocessMarkdown(
      'The proxy uses <tool_call> and <observation> blocks. Keep the rest of the sentence visible.'
    )

    expect(output).toContain(
      'The proxy uses &lt;tool_call&gt; and &lt;observation&gt; blocks. Keep the rest of the sentence visible.'
    )
    expect(output).not.toContain('<tool_call>')
    expect(output).not.toContain('<observation>')
  })

  it('preserves known html tags and autolinks while escaping unknown tags', () => {
    const output = preprocessMarkdown(
      'Use <strong>bold</strong> and visit https://example.com/page, then <span>ok</span> and <unk> text.'
    )

    expect(output).toContain('<strong>bold</strong>')
    expect(output).toContain('<https://example.com/page>')
    expect(output).toContain('<span>ok</span>')
    expect(output).toContain('&lt;unk&gt;')
  })

  it('leaves math comparisons like a < b and 2<3 untouched', () => {
    const output = preprocessMarkdown('If a < b and 2<3 then keep it as text.')

    expect(output).toContain('a < b and 2<3')
  })

  it('handles a fenced block larger than V8 spread-argument limit', () => {
    // A single huge code block (e.g. a logged minified bundle) used to throw
    // `RangeError: Maximum call stack size exceeded` via `out.push(...lines)`.
    const body = Array.from({ length: 200_000 }, (_, i) => `line ${i}`).join('\n')
    const input = `\`\`\`js\n${body}\n\`\`\``

    expect(() => preprocessMarkdown(input)).not.toThrow()
  })

  it('keeps $$<digit>$$ display math intact instead of escaping it as currency', () => {
    const output = preprocessMarkdown('$$5x = 10$$')

    expect(output).toContain('$$5x = 10$$')
    expect(output).not.toContain('\\$')
  })

  it('keeps numeric inline math intact instead of escaping it as currency', () => {
    const input = ['- The observed outcome might be $4$', '- Because $4\\in A$, event $A$ occurred'].join('\n')

    expect(preprocessMarkdown(input)).toBe(input)
  })

  it.each(['$4$', '$2/3$', '$5x=10$', '$4xy$', '$10kg$'])('preserves balanced numeric inline math: %s', input => {
    expect(preprocessMarkdown(input)).toBe(input)
  })

  it('does not mistake a numeric formula closer for a later price opener', () => {
    expect(preprocessMarkdown('Probability is $2/3$ and fee is $7.')).toBe('Probability is $2/3$ and fee is \\$7.')
    expect(preprocessMarkdown('$4$ and $10')).toBe('$4$ and \\$10')
  })

  it('keeps escaping currency ranges instead of treating them as inline math', () => {
    expect(preprocessMarkdown('$5-$10')).toBe('\\$5-\\$10')
    expect(preprocessMarkdown('$5 and $x$')).toBe('\\$5 and $x$')
    expect(preprocessMarkdown('Costs $5 + tax; formula is $x$.')).toBe('Costs \\$5 + tax; formula is $x$.')
    expect(preprocessMarkdown('Costs $5 = base rate; formula is $x$.')).toBe('Costs \\$5 = base rate; formula is $x$.')
  })

  it.each([
    ['Costs $5; delta is $-x$.', 'Costs \\$5; delta is $-x$.'],
    ['Costs $5; result is $(x+1)$.', 'Costs \\$5; result is $(x+1)$.'],
    ['Costs $5; set is $[1,2]$.', 'Costs \\$5; set is $[1,2]$.']
  ])('escapes a price before a later complete math span: %s', (input, expected) => {
    expect(preprocessMarkdown(input)).toBe(expected)
  })

  it('keeps the existing currency escaping semantics', () => {
    expect(preprocessMarkdown('$1,299 total')).toBe('\\$1,299 total')
    expect(preprocessMarkdown('already \\$5')).toBe('already \\$5')
    expect(preprocessMarkdown('\\\\$5')).toBe('\\\\\\$5')
  })

  it('escapes a price while preserving numeric math later in the same sentence', () => {
    const input = 'Costs $5; outcome is $4\\in A$.'

    expect(preprocessMarkdown(input)).toBe('Costs \\$5; outcome is $4\\in A$.')
  })

  it('escapes prefixed currency written with a space before the amount', () => {
    // `R$ 12.345` (BRL), `US$ 1,200`, `AU$ 40`: outside the US the symbol
    // carries a letter prefix and a space. Two of them on one line used to
    // pair as an inline math span and render the prose between as an equation.
    expect(preprocessMarkdown('Saldo R$ 1.000 e diferença R$ 200.')).toBe('Saldo R\\$ 1.000 e diferença R\\$ 200.')
    expect(preprocessMarkdown('R$800 mil, dos quais R$ 9.876,54 pagos.')).toBe(
      'R\\$800 mil, dos quais R\\$ 9.876,54 pagos.'
    )
    expect(preprocessMarkdown('US$ 1,200 vs AU$ 40')).toBe('US\\$ 1,200 vs AU\\$ 40')
  })

  it('leaves spaced inline math alone — a space-then-digit is only currency after a letter', () => {
    expect(preprocessMarkdown('valor $ 2 + 2 $ fim')).toBe('valor $ 2 + 2 $ fim')
  })

  it('normalizes multiline bracket display math with delimiter-only lines', () => {
    const input = [
      'Correct.',
      '',
      'Both paths reach the same intersection:',
      '',
      '\\[',
      'P(B)\\cdot P(A\\mid B)',
      '=',
      'P(A)\\cdot P(B\\mid A)',
      '\\]',
      '',
      'Now isolate $P(A\\mid B)$.'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toContain('$$\nP(B)\\cdot P(A\\mid B)\n=\nP(A)\\cdot P(B\\mid A)\n$$')
    expect(output).not.toContain('$$P(B)')
  })

  it('keeps display math inside its markdown container', () => {
    const listInput = ['- \\[', '  P(A)', '  =', '  P(B)', '  \\]'].join('\n')
    const listOutput = ['- $$', '  P(A)', '  =', '  P(B)', '  $$'].join('\n')

    expect(preprocessMarkdown(listInput)).toBe(listOutput)
    expect(preprocessMarkdown(['> \\[', '> P(A)', '>  \\]'].join('\n'))).toBe(['> $$', '> P(A)', '>  $$'].join('\n'))
  })

  it('rewrites double-backslash bracket math to dollar delimiters', () => {
    const output = preprocessMarkdown('\\\\(x^2\\\\)')

    expect(output).toContain('$x^2$')
  })

  it('rewrites [/math] and [/inline] tag pairs to dollar delimiters', () => {
    expect(preprocessMarkdown('[/math]a+b[/math]')).toContain('$$a+b$$')
    expect(preprocessMarkdown('[/inline]x[/inline]')).toContain('$x$')
  })

  it('moves hugging $$ delimiters of multiline display math onto their own lines', () => {
    const input = [
      '$$\\begin{aligned}',
      '\\nabla \\cdot \\mathbf{E} &= \\frac{\\rho}{\\varepsilon_0} \\\\',
      '\\nabla \\cdot \\mathbf{B} &= 0',
      '\\end{aligned}$$'
    ].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toBe(
      [
        '$$',
        '\\begin{aligned}',
        '\\nabla \\cdot \\mathbf{E} &= \\frac{\\rho}{\\varepsilon_0} \\\\',
        '\\nabla \\cdot \\mathbf{B} &= 0',
        '\\end{aligned}',
        '$$'
      ].join('\n')
    )
  })

  it('keeps hugging display math inside its markdown container', () => {
    const input = ['> $$\\begin{aligned}', '> a &= b', '> \\end{aligned}$$'].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toBe(['> $$', '> \\begin{aligned}', '> a &= b', '> \\end{aligned}', '> $$'].join('\n'))
  })

  it('splits the hugging $$ form that the bracket rewrite itself produces', () => {
    const input = ['\\[\\begin{aligned}', 'a &= b', '\\end{aligned}\\]'].join('\n')

    const output = preprocessMarkdown(input)

    expect(output).toBe(['$$', '\\begin{aligned}', 'a &= b', '\\end{aligned}', '$$'].join('\n'))
  })

  it('keeps CRLF line endings consistent when splitting hugging delimiters', () => {
    const input = '$$\\begin{aligned}\r\na &= b\r\n\\end{aligned}$$'

    expect(preprocessMarkdown(input)).toBe('$$\r\n\\begin{aligned}\r\na &= b\r\n\\end{aligned}\r\n$$')
  })

  it('leaves single-line display math alone', () => {
    expect(preprocessMarkdown('$$x^2 + y^2 = r^2$$')).toBe('$$x^2 + y^2 = r^2$$')
  })

  it('leaves a multiline $$ block that sits wholly inside one inline code span alone', () => {
    const input = '`$$a\nb$$`'

    expect(preprocessMarkdown(input)).toBe(input)
  })

  it('keeps a radical index inside inline math', () => {
    expect(preprocessMarkdown('$\\sqrt[3]{8}$')).toBe('$\\sqrt[3]{8}$')
  })

  it('keeps a radical index inside display math', () => {
    expect(preprocessMarkdown('$$\\sqrt[3]{8}$$')).toBe('$$\\sqrt[3]{8}$$')
  })

  it('keeps a radical index inside a multiline display block', () => {
    const input = ['$$', '\\sqrt[3]{8} + \\sqrt[4]{16}', '$$'].join('\n')

    expect(preprocessMarkdown(input)).toBe(input)
  })

  it('keeps a radical index in math that arrived as bracket delimiters', () => {
    expect(preprocessMarkdown('\\(\\sqrt[3]{8}\\)')).toContain('$\\sqrt[3]{8}$')
  })

  it('still strips a citation marker in prose that also contains math', () => {
    const output = preprocessMarkdown('Per the paper[2], $\\sqrt[3]{8}$ is 2.')

    expect(output).toBe('Per the paper, $\\sqrt[3]{8}$ is 2.')
  })

  // #103546: a bare `$identifier` twice in CJK prose is not math. The escape
  // fires on the OPENING `$` of a span whose body carries East Asian script or
  // punctuation, so remark-math reads it as a literal dollar and the sentence
  // renders as prose with recoverable copy-out.
  it('does not pair two bare dollars around CJK prose as inline math (#103546)', () => {
    const input =
      '...的经典嫌疑是 **$connection 被别的写者整包覆盖**(丢了 `isFullscreen` 字段)...搜 `$connection` 的所有写者:'

    const output = preprocessMarkdown(input)

    expect(output).toContain('\\$connection 被别的写者整包覆盖')
    // The backticked `$connection` is untouched — inline code stays code.
    expect(output).toContain('`$connection`')
  })

  it('escapes the opening dollar when both identifiers are bare in CJK prose (#103546)', () => {
    const output = preprocessMarkdown('搜 $connection 的所有写者,再搜 $session 的读者')

    expect(output).toContain('\\$connection')
  })

  it('escapes a span whose body is fullwidth punctuation plus Latin (#103546)', () => {
    const output = preprocessMarkdown('值 $foo(bar)$ 已确认')

    expect(output).toContain('\\$foo(bar)$')
  })

  it('leaves real inline math in CJK prose untouched (#103546)', () => {
    const output = preprocessMarkdown('代入 $x^2 + y^2$ 得到结果')

    expect(output).toContain('$x^2 + y^2$')
    expect(output).not.toContain('\\$x^2')
  })

  it('keeps every real inline math span when CJK prose separates them (#123163)', () => {
    expect(preprocessMarkdown('$E = mc^2$ 代入 $x$ 求解')).toBe('$E = mc^2$ 代入 $x$ 求解')
    expect(preprocessMarkdown('$a$ 与 $b$ 之间的说明')).toBe('$a$ 与 $b$ 之间的说明')
    expect(preprocessMarkdown('$a$ 甲 $b$ 乙 $c$ 丙')).toBe('$a$ 甲 $b$ 乙 $c$ 丙')
    // Backslash commands are real math too; the closer must not be re-opened.
    expect(preprocessMarkdown('根据 $\\alpha$ 和 $\\beta$ 计算')).toBe('根据 $\\alpha$ 和 $\\beta$ 计算')
  })

  it('still escapes a CJK variable inside one equation and keeps the next span (#103546)', () => {
    expect(preprocessMarkdown('$x = 变量$ 与 $y$')).toBe('\\$x = 变量\\$ 与 $y$')
  })

  it('leaves real inline math adjacent to CJK untouched (#103546)', () => {
    const output = preprocessMarkdown('其中 $\\alpha = 1$,所以')

    expect(output).toContain('$\\alpha = 1$')
    expect(output).not.toContain('\\$\\alpha')
  })

  it('leaves display math in CJK prose untouched (#103546)', () => {
    const output = preprocessMarkdown('公式 $$E = mc^2$$ 成立')

    expect(output).toContain('$$E = mc^2$$')
  })

  it('shields inline math closed after an escaped backslash', () => {
    // #92371: in `$x[2]\\$` the `\\` is an escaped backslash (a literal
    // backslash, valid TeX), so the final `$` really closes the span. A
    // one-character lookbehind on the closer saw the backslash and refused
    // to shield, letting the prose citation-marker rewrite eat `[2]`.
    const output = preprocessMarkdown(String.raw`Per the paper, $x[2]\\$ is the value.`)

    expect(output).toBe(String.raw`Per the paper, $x[2]\\$ is the value.`)

    // Minimal shape: the span containing only a+escaped-backslash.
    expect(preprocessMarkdown(String.raw`$a\\$ plain`)).toBe(String.raw`$a\\$ plain`)
  })

  it('still escapes bare currency dollars next to an escaped-backslash span', () => {
    // The fix must not widen the math branch into currency: an escaped `\$`
    // stays a price opener, an escaped `\\` stays a literal backslash.
    expect(preprocessMarkdown(String.raw`costs \$5 and $a\\$ ok`)).toBe(String.raw`costs \$5 and $a\\$ ok`)
  })
})