import { describe, it, expect } from 'vitest'; import { parseProblemPage } from './problem-page'; const MOCK_PROBLEM_HTML = ` A - ABC Problem A - ABC Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

Sample Input 1

1 2
3

Sample Output 1

6

Sample Input 2

10 20
30

Sample Output 2

60

入力例 1

1 2
3

出力例 1

6
`; describe('problem-page parser', () => { it('should parse details correctly', () => { const details = parseProblemPage(MOCK_PROBLEM_HTML); expect(details.title).toBe('A - ABC Problem'); expect(details.timeLimitMs).toBe(2000); expect(details.memoryLimitBytes).toBe(1024 * 1024 * 1024); expect(details.samples).toHaveLength(2); expect(details.samples[0]).toEqual({ index: 1, input: '1 2\n3\n', output: '6\n' }); expect(details.samples[1]).toEqual({ index: 2, input: '10 20\n30\n', output: '60\n' }); expect(details.problemStatementMd).toContain('# A - ABC Problem'); expect(details.problemStatementMd).toContain('### Sample Input 1'); }); it('should parse Japanese problem statement with preferredLang ja', () => { const details = parseProblemPage(MOCK_PROBLEM_HTML, 'ja'); expect(details.problemStatementMd).toContain('### 入力例 1'); expect(details.problemStatementMd).not.toContain('### Sample Input 1'); }); it('should preserve LaTeX formatting without escaping math expressions', () => { const htmlWithLatex = `

Given $N$ integers $A_1, A_2, \\dots, A_N$.

Find $$ \\sum_{i=1}^N A_i $$.

Constraints: $1 \\le N \\le 100$ and $A_i \\le 1000$ with some _underscores_ outside and grid cell # symbols.

`; const details = parseProblemPage(htmlWithLatex); expect(details.problemStatementMd).toContain('Given $N$ integers $A_1, A_2, \\dots, A_N$.'); expect(details.problemStatementMd).toContain('Find $$\n\\sum_{i=1}^N A_i\n$$.'); expect(details.problemStatementMd).toContain('Constraints: $1 \\le N \\le 100$ and $A_i \\le 1000$'); expect(details.problemStatementMd).toContain('\\_underscores\\_'); expect(details.problemStatementMd).toContain('grid cell # symbols'); }); it('should translate \\( and \\[] delimiters to $ and $$, and keep content unescaped', () => { const htmlWithLatex = `

Given \\(T_i\\) for all \\(i\\).

Find \\[ \\sum_{i=1}^N T_i \\].

Constraints: \\(1 \\le N,Q \\le 1000\\).

`; const details = parseProblemPage(htmlWithLatex); expect(details.problemStatementMd).toContain('Given $T_i$ for all $i$.'); expect(details.problemStatementMd).toContain('$$\n\\sum_{i=1}^N T_i\n$$'); expect(details.problemStatementMd).toContain('Constraints: $1 \\le N,Q \\le 1000$.'); }); it('should convert and span.math tags to $ math blocks and keep content unescaped', () => { const htmlWithVar = `

Let N be the number of elements.

Constraints:

  • 1 \\le N \\le 300
  • A_i \\le 1000

`; const details = parseProblemPage(htmlWithVar); expect(details.problemStatementMd).toContain('Let $N$ be the number of elements.'); expect(details.problemStatementMd).toContain('- $1 \\le N \\le 300$'); expect(details.problemStatementMd).toContain('- $A_i \\le 1000$'); }); it('should format
 blocks containing math or var tags as LaTeX array block', () => {
    const htmlWithPre = `
      

Input

N M
A_1 A_2 ... A_N
:
query_Q
          
`; // Add tag to trigger math conversion const htmlWithPreVar = htmlWithPre.replace('N M', 'N M'); const details = parseProblemPage(htmlWithPreVar); expect(details.problemStatementMd).toContain('\\begin{array}{l}'); expect(details.problemStatementMd).toContain('N\\ M'); expect(details.problemStatementMd).toContain('A_1'); expect(details.problemStatementMd).toContain('\\ldots'); expect(details.problemStatementMd).toContain('\\vdots'); expect(details.problemStatementMd).toContain('query_Q'); expect(details.problemStatementMd).toContain('\\end{array}'); }); it('should keep standard pre blocks (sample cases) as markdown code blocks', () => { const htmlWithStandardPre = `

Sample Input 1

3 125 175
200 300 400
`; const details = parseProblemPage(htmlWithStandardPre); expect(details.problemStatementMd).toContain('\`\`\`\n3 125 175\n200 300 400\n\`\`\`'); }); it('should format pre blocks that already contain LaTeX \\text{} commands correctly', () => { const htmlWithPreLaTeX = `

Input

Q
\\text{query}_1
\\text{query}_2
:
\\text{query}_Q
          
`; // Add tag to trigger math conversion const htmlWithPreVar = htmlWithPreLaTeX.replace('Q', 'Q'); const details = parseProblemPage(htmlWithPreVar); expect(details.problemStatementMd).toContain('\\begin{array}{l}'); expect(details.problemStatementMd).toContain('Q'); expect(details.problemStatementMd).toContain('\\text{query}_1'); expect(details.problemStatementMd).toContain('\\text{query}_2'); expect(details.problemStatementMd).toContain('\\vdots'); expect(details.problemStatementMd).toContain('\\text{query}_Q'); expect(details.problemStatementMd).toContain('\\end{array}'); expect(details.problemStatementMd).not.toContain('\\text\\text'); }); it('should format pre blocks with adjacent subscripted variables like s_1s_2s_3 correctly', () => { const htmlWithPreAdjacent = `

Input

s_1s_2s_3
          
`; // Add tag to trigger math conversion const htmlWithPreVar = htmlWithPreAdjacent.replace('s_1s_2s_3', 's_1s_2s_3'); const details = parseProblemPage(htmlWithPreVar); expect(details.problemStatementMd).toContain('s_1s_2s_3'); }); });