Repository navigation
Expand file tree
/
Copy pathcfStatementParser.test.js
More file actions
329 lines (293 loc) · 10.2 KB
/
Copy pathcfStatementParser.test.js
File metadata and controls
329 lines (293 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// @vitest-environment node
import { describe, expect, it } from 'vitest';
import { parseCFProblemStatement } from './cfStatementParser.js';
// ---------------------------------------------------------------------------
// Minimal but realistic Codeforces problem page HTML fixture.
// The structure mirrors the actual CF page HTML that the parser targets.
// ---------------------------------------------------------------------------
const FIXTURE_HTML = `
<!DOCTYPE html>
<html>
<body>
<div class="problem-statement">
<div class="header">
<div class="title">A. Two Sum</div>
<div class="time-limit">
<div class="property-title">time limit per test</div>2 seconds
</div>
<div class="memory-limit">
<div class="property-title">memory limit per test</div>256 megabytes
</div>
<div class="input-file">
<div class="property-title">input</div>standard input
</div>
<div class="output-file">
<div class="property-title">output</div>standard output
</div>
</div>
<p>You are given two integers <span class="tex-span"><i>a</i></span> and <span class="tex-span"><i>b</i><sup>2</sup></span>.</p>
<p>Print their sum.</p>
<div class="input-specification">
<div class="section-title">Input</div>
<p>The first line contains two integers $a$ and $b$ ($1 <= a, b <= 10^9$).</p>
</div>
<div class="output-specification">
<div class="section-title">Output</div>
<p>Print a single integer — the sum of $a$ and $b$.</p>
</div>
<div class="sample-tests">
<div class="section-title">Examples</div>
<div class="sample-test">
<div class="input">
<div class="title">Input</div>
<pre>3 4</pre>
</div>
<div class="output">
<div class="title">Output</div>
<pre>7</pre>
</div>
</div>
<div class="sample-test">
<div class="input">
<div class="title">Input</div>
<pre>1000000000 1000000000</pre>
</div>
<div class="output">
<div class="title">Output</div>
<pre>2000000000</pre>
</div>
</div>
</div>
<div class="note">
<div class="section-title">Note</div>
<p>In the first example the answer is 7.</p>
</div>
</div>
</body>
</html>
`;
// A page that contains no .problem-statement (e.g. 404 page, wrong URL).
const FIXTURE_NO_STATEMENT = `
<!DOCTYPE html>
<html>
<body>
<div class="content">Page not found</div>
</body>
</html>
`;
// A problem with multiple paragraphs in the body and a multi-line sample.
const FIXTURE_MULTI_PARA = `
<!DOCTYPE html>
<html>
<body>
<div class="problem-statement">
<div class="header">
<div class="title">B. Multi Para</div>
<div class="time-limit">
<div class="property-title">time limit per test</div>1 second
</div>
<div class="memory-limit">
<div class="property-title">memory limit per test</div>512 megabytes
</div>
</div>
<p>First paragraph.</p>
<p>Second paragraph.</p>
<p>Third paragraph.</p>
<div class="input-specification">
<div class="section-title">Input</div>
<p>First line is $n$.</p>
<p>Second line contains $n$ integers.</p>
</div>
<div class="output-specification">
<div class="section-title">Output</div>
<p>Print the answer.</p>
</div>
<div class="sample-tests">
<div class="section-title">Examples</div>
<div class="sample-test">
<div class="input">
<div class="title">Input</div>
<pre>3
1 2 3</pre>
</div>
<div class="output">
<div class="title">Output</div>
<pre>6</pre>
</div>
</div>
</div>
</div>
</body>
</html>
`;
// A problem where Codeforces encodes newlines inside <pre> as <br> elements
// (the actual format used on the live Codeforces site).
const FIXTURE_BR_SAMPLES = `
<!DOCTYPE html>
<html>
<body>
<div class="problem-statement">
<div class="header">
<div class="title">C. Br Newlines</div>
<div class="time-limit">
<div class="property-title">time limit per test</div>2 seconds
</div>
<div class="memory-limit">
<div class="property-title">memory limit per test</div>256 megabytes
</div>
</div>
<p>Given $n$ integers, sort them.</p>
<div class="input-specification">
<div class="section-title">Input</div>
<p>First line is $n$, second line is the integers.</p>
</div>
<div class="output-specification">
<div class="section-title">Output</div>
<p>Print sorted integers.</p>
</div>
<div class="sample-tests">
<div class="section-title">Examples</div>
<div class="sample-test">
<div class="input">
<div class="title">Input</div>
<pre>3<br>1 3 2</pre>
</div>
<div class="output">
<div class="title">Output</div>
<pre>1<br>2<br>3</pre>
</div>
</div>
</div>
</div>
</body>
</html>
`;
// Modern Codeforces format where each sample line is wrapped in a
// <div class="test-example-line"> inside <pre> instead of using <br>.
const FIXTURE_DIV_SAMPLES = `
<!DOCTYPE html>
<html>
<body>
<div class="problem-statement">
<div class="header">
<div class="title">D. Div Lines</div>
<div class="time-limit">
<div class="property-title">time limit per test</div>2 seconds
</div>
<div class="memory-limit">
<div class="property-title">memory limit per test</div>256 megabytes
</div>
</div>
<p>Find the count and sum.</p>
<div class="input-specification">
<div class="section-title">Input</div>
<p>First line is $n$, next $n$ lines are values.</p>
</div>
<div class="output-specification">
<div class="section-title">Output</div>
<p>Print count then sum.</p>
</div>
<div class="sample-tests">
<div class="section-title">Examples</div>
<div class="sample-test">
<div class="input">
<div class="title">Input</div>
<pre><div class="test-example-line test-example-line-even">3</div><div class="test-example-line test-example-line-odd">10</div><div class="test-example-line test-example-line-even">20</div><div class="test-example-line test-example-line-odd">30</div></pre>
</div>
<div class="output">
<div class="title">Output</div>
<pre><div class="test-example-line test-example-line-even">3</div><div class="test-example-line test-example-line-odd">60</div></pre>
</div>
</div>
</div>
</div>
</body>
</html>
`;
describe('parseCFProblemStatement', () => {
it('returns null when there is no .problem-statement element', () => {
expect(parseCFProblemStatement(FIXTURE_NO_STATEMENT)).toBeNull();
});
it('parses the problem title', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.title).toBe('A. Two Sum');
});
it('parses the time limit without the property-title label', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.timeLimit).toBe('2 seconds');
});
it('parses the memory limit without the property-title label', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.memoryLimit).toBe('256 megabytes');
});
it('parses statement body paragraphs', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.statement).toContain('You are given two integers');
expect(result.statement).toContain('Print their sum');
});
it('does not include special section text in the statement body', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.statement).not.toContain('Input');
expect(result.statement).not.toContain('Output');
expect(result.statement).not.toContain('Note');
expect(result.statement).not.toContain('Examples');
});
it('parses input specification without section-title', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.inputSpecification).toContain('two integers');
expect(result.inputSpecification).not.toContain('Input');
});
it('parses output specification without section-title', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.outputSpecification).toContain('sum of');
expect(result.outputSpecification).not.toContain('Output');
});
it('parses sample tests into input/output pairs', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.samples).toHaveLength(2);
expect(result.samples[0]).toEqual({ input: '3 4', output: '7' });
expect(result.samples[1]).toEqual({
input: '1000000000 1000000000',
output: '2000000000',
});
});
it('returns all expected fields', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result).toMatchObject({
title: expect.any(String),
timeLimit: expect.any(String),
memoryLimit: expect.any(String),
statement: expect.any(String),
statementHtml: expect.any(String),
inputSpecification: expect.any(String),
inputSpecificationHtml: expect.any(String),
outputSpecification: expect.any(String),
outputSpecificationHtml: expect.any(String),
samples: expect.any(Array),
});
});
it('preserves sanitized Codeforces math markup for rendering', () => {
const result = parseCFProblemStatement(FIXTURE_HTML);
expect(result.statementHtml).toContain('<span class="tex-span"><i>a</i></span>');
expect(result.statementHtml).toContain('<sup>2</sup>');
});
it('handles multi-paragraph statement and multi-line sample input', () => {
const result = parseCFProblemStatement(FIXTURE_MULTI_PARA);
expect(result.title).toBe('B. Multi Para');
expect(result.statement).toContain('First paragraph');
expect(result.statement).toContain('Second paragraph');
expect(result.statement).toContain('Third paragraph');
expect(result.samples[0].input).toBe('3\n1 2 3');
expect(result.samples[0].output).toBe('6');
});
it('converts <br> tags in sample <pre> blocks to newlines', () => {
const result = parseCFProblemStatement(FIXTURE_BR_SAMPLES);
expect(result.samples[0].input).toBe('3\n1 3 2');
expect(result.samples[0].output).toBe('1\n2\n3');
});
it('handles modern CF format where sample lines are <div class="test-example-line"> elements', () => {
const result = parseCFProblemStatement(FIXTURE_DIV_SAMPLES);
expect(result.samples[0].input).toBe('3\n10\n20\n30');
expect(result.samples[0].output).toBe('3\n60');
});
});