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

67 lines
2.5 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { buildRecipeDocument } from '#import-recipes/recipes/buildRecipeDocument.js'
const PARSED = [
{ raw: '200 g de tomates', name: 'tomates', quantity: 200, unit: 'g', imprecise: false },
{ raw: 'sel au goût', name: 'sel', quantity: null, unit: null, imprecise: true }
]
function buildSample(recipe) {
return buildRecipeDocument({
recipe,
parsedIngredients: PARSED,
fileName: 'Gratin.odt',
importedAt: '2026-09-18T10:00:00.000Z',
model: 'claude-opus-5'
})
}
test('writes a schema.org Recipe', () => {
const document = buildSample({
name: 'Gratin de courgettes',
recipeYield: '4 personnes',
recipeIngredient: ['200 g de tomates', 'sel au goût'],
recipeInstructions: ['Couper.', 'Enfourner.']
})
assert.equal(document['@context'], 'https://schema.org')
assert.equal(document['@type'], 'Recipe')
assert.equal(document.name, 'Gratin de courgettes')
assert.equal(document.recipeYield, '4 personnes')
assert.deepEqual(document.recipeInstructions, [
{ '@type': 'HowToStep', text: 'Couper.' },
{ '@type': 'HowToStep', text: 'Enfourner.' }
])
})
// The printed line and the split must stay the same text, entry by entry.
test('recipeIngredient is exactly the raw lines of the split', () => {
const document = buildSample({
name: 'Gratin',
recipeYield: null,
recipeIngredient: ['autre chose'],
recipeInstructions: []
})
assert.deepEqual(document.recipeIngredient, ['200 g de tomates', 'sel au goût'])
assert.equal(document.recipeIngredient.length, document['tofu:parsedIngredients'].length)
document.recipeIngredient.forEach((line, index) => {
assert.equal(line, document['tofu:parsedIngredients'][index].raw)
})
})
test('prefixes with tofu: everything schema.org does not define', () => {
const document = buildSample({ name: 'Gratin', recipeYield: null, recipeIngredient: [], recipeInstructions: [] })
const ownKeys = Object.keys(document).filter((key) => key.startsWith('tofu:'))
assert.deepEqual(ownKeys, ['tofu:parsedIngredients', 'tofu:source'])
assert.deepEqual(document['tofu:source'], {
file: 'Gratin.odt',
importedAt: '2026-09-18T10:00:00.000Z',
model: 'claude-opus-5'
})
})
test('turns a missing yield or missing steps into null and an empty list', () => {
const document = buildSample({ name: 'Gratin', recipeIngredient: [], recipeInstructions: undefined })
assert.equal(document.recipeYield, null)
assert.deepEqual(document.recipeInstructions, [])
})