import * as assert from 'node:assert/strict' import { test } from 'node:test' import { extractStructuredContent, isNonContentHeading, tableToText, } from '../../app/utils/zim_html.js' import * as cheerio from 'cheerio' test('isNonContentHeading flags boilerplate headings, case-insensitive', () => { assert.equal(isNonContentHeading('References'), true) assert.equal(isNonContentHeading('see also'), true) assert.equal(isNonContentHeading('EXTERNAL LINKS'), true) assert.equal(isNonContentHeading('Further reading'), true) }) test('isNonContentHeading keeps real content headings', () => { assert.equal(isNonContentHeading('Uses'), false) assert.equal(isNonContentHeading('Side effects'), false) assert.equal(isNonContentHeading('History'), false) }) test('extractStructuredContent drops non-content sections at emit time (#902)', () => { const html = `

Aspirin

Aspirin is a medication used to reduce pain and fever.

Uses

It is used to treat fever, pain, and inflammation.

References

See also

Ibuprofen, Paracetamol

External links

https://example.com/aspirin

` const { sections } = extractStructuredContent(html) const headings = sections.map((s) => s.heading) assert.deepEqual(headings, ['Introduction', 'Uses']) assert.equal( sections.some((s) => /Smith 2020|Ibuprofen|example\.com/.test(s.text)), false, 'boilerplate-section content must not reach any emitted chunk' ) }) test('extractStructuredContent keeps real sections including their content', () => { const html = `

Aspirin

Intro text here.

Side effects

May cause stomach upset.

` const { sections } = extractStructuredContent(html) const sideEffects = sections.find((s) => s.heading === 'Side effects') assert.ok(sideEffects, 'real content section should be emitted') assert.match(sideEffects.text, /stomach upset/) }) test('tableToText renders rows as delimited text, not concatenated cell soup (#902)', () => { const html = `
AgeDose
Adult500 mg
Child250 mg
` const $ = cheerio.load(html) const out = tableToText($, $('table').get(0)) assert.match(out, /Age \| Dose/) assert.match(out, /Adult \| 500 mg/) assert.match(out, /Child \| 250 mg/) assert.ok(out.includes('\n'), 'rows should be newline-separated') assert.equal( /AgeDose|Adult500/.test(out), false, 'cells must not be concatenated without separators' ) }) test('extractStructuredContent keeps table structure inside a content section', () => { const html = `

Dosage

AgeDose
Adult500 mg
` const { sections } = extractStructuredContent(html) const dosage = sections.find((s) => s.heading === 'Dosage') assert.ok(dosage, 'section with a table should be emitted') assert.match(dosage.text, /Age \| Dose/) assert.match(dosage.text, /Adult \| 500 mg/) })