treker-po-trenirovkam-i-pit.../tests/recipes.test.js

35 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const data = require('../recipes.data.json');
const translations = require('../recipes.i18n.json');
const { portions, nutrition } = require('../recipes-core');
test('Просмотр рецепта: три полных рецепта на каждом языке с локальными фото', () => {
assert.equal(data.recipes.length, 3);
for (const recipe of data.recipes) {
const image = fs.readFileSync(`assets/${recipe.id}.jpg`);
assert.equal(image.readUInt16BE(), 0xffd8);
assert.ok(image.length > 10000);
for (const lang of ['ru', 'kk', 'en']) {
const t = translations[lang];
assert.ok(t.recipes[recipe.id].title);
assert.ok(t.recipes[recipe.id].steps.length >= 3);
for (const [id, grams] of recipe.ingredients) {
assert.ok(t.foods[id]); assert.ok(grams > 0); assert.equal(data.foods[id].length, 4);
}
assert.deepEqual(Object.keys(t).sort(), Object.keys(translations.ru).sort());
}
}
});
test('Две порции: 100 г хлопьев и удвоенное общее КБЖУ', () => {
const oats = data.recipes[0];
assert.equal(oats.ingredients[0][1] * portions(2), 100);
const one = nutrition(oats, data.foods);
assert.ok(Math.abs(one[0] - 352) < .01);
assert.deepEqual(nutrition(oats, data.foods, 2), one.map(v => v * 2));
});
test('Границы количества порций: от 1 до 8, только целые числа', () => {
for (const v of [-1, 0, '', 'oops', null]) assert.equal(portions(v), 1);
assert.equal(portions(20), 8); assert.equal(portions(2.9), 2);
});