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

42 lines
1.5 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { buildRecipeIndex } from '#import-recipes/recipes/buildRecipeIndex.js'
const RECIPES = [
{
id: 'gratin-de-courgettes',
document: {
name: 'Gratin de courgettes',
'tofu:parsedIngredients': [{ name: 'courgettes' }, { name: 'crème' }]
}
},
{
id: 'gnocchis-a-la-sauge',
document: {
name: 'Gnocchis à la sauge',
'tofu:parsedIngredients': [{ name: 'gnocchis' }, { name: 'beurre' }, { name: 'sauge' }]
}
}
]
// This is the file the app reads as is, so its shape is the contract.
test('has the shape the app consumes', () => {
const index = buildRecipeIndex({ recipes: RECIPES, generatedAt: '2026-09-18T10:00:00.000Z' })
assert.deepEqual(Object.keys(index), ['generatedAt', 'recipes'])
assert.equal(index.generatedAt, '2026-09-18T10:00:00.000Z')
assert.deepEqual(index.recipes[0], {
id: 'gnocchis-a-la-sauge',
name: 'Gnocchis à la sauge',
ingredientNames: ['gnocchis', 'beurre', 'sauge']
})
for (const entry of index.recipes) assert.deepEqual(Object.keys(entry), ['id', 'name', 'ingredientNames'])
})
test('sorts by name, so the file does not reshuffle between runs', () => {
const index = buildRecipeIndex({ recipes: RECIPES, generatedAt: 'now' })
assert.deepEqual(index.recipes.map((entry) => entry.name), ['Gnocchis à la sauge', 'Gratin de courgettes'])
})
test('accepts an empty run', () => {
assert.deepEqual(buildRecipeIndex({ recipes: [], generatedAt: 'now' }), { generatedAt: 'now', recipes: [] })
})