import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { recipeExample, hasNoRecipe, hasRecipe, addDish } from "../src/logic.js"; import { catalog } from "../src/catalog.js"; import { findRecipe } from "../src/recipes.js"; import { messages } from "../src/i18n.js"; const read = (f) => readFileSync(new URL(`../src/${f}`, import.meta.url), "utf8"); // WTC-20: подсказка с примером, как получить рецепт test("Сценарий: Пустой список — подсказка и пример", () => { assert.equal(recipeExample([], catalog, findRecipe), "Бешбармак"); assert.equal(messages.ru.addExample.replace("{name}", "Бешбармак"), "Добавить «Бешбармак»"); assert.match(messages.ru.recipeExampleHint, /появится кнопка «Рецепт»/); const html = read("index.html"); assert.ok(html.indexOf('id="listTitle"') < html.indexOf('id="recipeExampleHint"')); assert.ok(html.indexOf('id="recipeExampleHint"') < html.indexOf('id="addExample"')); for (const l of ["ru", "kk", "en"]) { for (const k of ["recipeExampleHint", "noRecipeHint"]) assert.ok(messages[l][k].length > 20, `${l}.${k}`); assert.match(messages[l].addExample, /\{name\}/, l); } }); test("Сценарий: Пример добавляется одним нажатием", () => { const name = recipeExample([], catalog, findRecipe); const dishes = addDish([], name); assert.deepEqual(dishes, ["Бешбармак"]); assert.equal(hasRecipe(dishes, findRecipe), true); const app = read("app.js"); const click = app.slice(app.indexOf('$("addExample").addEventListener'), app.indexOf("function showNameError")); assert.match(click, /addDish\(state\.dishes, name\)/); assert.match(click, /save\(\);.*render\(\)/); }); test("Сценарий: В списке только своё блюдо", () => { assert.equal(hasRecipe(["Мамин пирог"], findRecipe), false); assert.equal(recipeExample(["Мамин пирог"], catalog, findRecipe), "Бешбармак"); }); test("Сценарий: Блюдо с рецептом уже есть", () => { assert.equal(hasRecipe(["Плов"], findRecipe), true); assert.match(read("app.js"), /const example = hasRecipe\(state\.dishes, findRecipe\) \? null : recipeExample\(/); assert.match(read("app.js"), /\$\("recipeExample"\)\.hidden = example === null/); }); test("Граница: пример не повторяет блюдо из списка, пустая база — примера нет", () => { assert.equal(recipeExample(["бешбармак"], catalog, findRecipe), "Плов"); assert.equal(recipeExample([], [], findRecipe), null); assert.equal(recipeExample([], undefined, findRecipe), null); assert.equal(recipeExample([], ["Мамин пирог"], findRecipe), null); }); // WTC-20: подпись объясняет пометку «Рецепта нет» test("Сценарий: Своё блюдо в списке", () => { assert.equal(hasNoRecipe(["Бешбармак", "Мамин пирог"], findRecipe), true); assert.equal(findRecipe("Мамин пирог"), null); assert.match(messages.ru.noRecipeHint, /«Рецепта нет»/); assert.ok(messages.ru.noRecipeHint.includes(messages.ru.noRecipe)); assert.match(read("app.js"), /\$\("noRecipeHint"\)\.hidden = !hasNoRecipe\(state\.dishes, findRecipe\)/); const html = read("index.html"); assert.ok(html.indexOf('id="list"') < html.indexOf('id="noRecipeHint"')); }); test("Сценарий: Все блюда из базы", () => { assert.equal(hasNoRecipe(["Бешбармак"], findRecipe), false); assert.equal(hasNoRecipe([], findRecipe), false); });