Tofu/scripts/import-recipes/tests/extractOdtText.test.js
2026-09-18 01:41:02 +02:00

47 lines
1.9 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { extractOdtText } from '#import-recipes/odt/extractOdtText.js'
import { readOdtEntry } from '#import-recipes/odt/readOdtEntry.js'
const SAMPLE_ODT = new URL('./sample-recipe.odt', import.meta.url)
// Read from the real .odt, not from a hand-written string: the point is that the
// function survives what a word processor actually writes.
test('extracts the lines of a real .odt document', () => {
const lines = extractOdtText(readOdtEntry(readFileSync(SAMPLE_ODT), 'content.xml'))
assert.deepEqual(lines, [
'Gratin de courgettes',
'Pour 4 personnes',
'Ingrédients',
'600 g de courgettes',
'une poignée de pois chiches',
"2 c. à soupe d'huile d'olive",
'sel & poivre au goût',
'Préparation',
'1.\tCouper les courgettes en rondelles fines.',
'2.\tEnfourner 30 minutes à 180 °C.',
'Servir chaud.'
])
})
test('starts a new line on every paragraph, heading and manual break', () => {
const contentXml = '<text:h>Titre</text:h><text:p>a<text:line-break/>b</text:p>'
assert.deepEqual(extractOdtText(contentXml), ['Titre', 'a', 'b'])
})
test('expands text:s, text:c and text:tab', () => {
assert.deepEqual(extractOdtText('<text:p>a<text:s/>b</text:p>'), ['a b'])
assert.deepEqual(extractOdtText('<text:p>a<text:s text:c="3"/>b</text:p>'), ['a b'])
assert.deepEqual(extractOdtText('<text:p>a<text:tab/>b</text:p>'), ['a\tb'])
})
test('ignores markup that carries no text and drops empty lines', () => {
const contentXml = '<text:p><text:bookmark text:name="x"/><text:span>sel</text:span></text:p><text:p/>'
assert.deepEqual(extractOdtText(contentXml), ['sel'])
})
test('returns an empty list for anything that is not a string', () => {
assert.deepEqual(extractOdtText(null), [])
assert.deepEqual(extractOdtText(''), [])
})