dika/test/base-cards.test.js
peppa-bot 91a439695b
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 0с
peppa/build build: success за 0с
ci / checks (pull_request) Successful in 8s
deploy / deploy (pull_request) Successful in 17s
DIK-51: карточки на странице «Все заведения», таблица под «Подробная таблица»
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 10:12:21 +00:00

64 lines
3.4 KiB
JavaScript
Raw Permalink 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.

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import http from "node:http";
import { cardTexts } from "../public/base-cards.js";
import { createHandler } from "../src/index.js";
const tr = (l) => JSON.parse(fs.readFileSync(`public/i18n/${l}.json`, "utf8"));
const ru = tr("ru");
const today = "2026-11-24";
const marmelade = { name: "Marmelade", address: "ул. Сыганак, 60/5", lunch: { price: 3800, from: "12:00", to: "16:00" },
promo: { text: "сет за 4000 ₸", until: "2026-11-30" }, checked: "2026-11-20" };
test("Сценарий: Карточка с действующим ланчем и акцией (DIK-51)", () => {
const x = cardTexts(marmelade, ru, today);
assert.deepEqual(x.lunch, { text: "Бизнес-ланч 3800 ₸, 12:00–16:00", active: true });
assert.equal(x.promo.active, true);
assert.match(x.promo.text, /сет за 4000 ₸ \(до 30\.11\.2026\)/);
assert.equal(x.checked, "Проверено 20.11.2026");
});
test("Сценарий: Цены нет или она устарела (DIK-51)", () => {
for (const v of [{ ...marmelade, lunch: null, promo: null }, { ...marmelade, checked: "2026-11-01" }]) {
const x = cardTexts(v, ru, today);
assert.deepEqual(x.lunch, { text: "Цена не подтверждена", active: false });
assert.deepEqual(x.promo, { text: "Акции сейчас нет", active: false });
}
assert.equal(cardTexts({ ...marmelade, promo: { text: "x", until: "2026-11-22" } }, ru, today).promo.text, "Акция закончилась");
});
test("Сценарий: Дата проверки не указана (DIK-51)", () => {
const x = cardTexts({ ...marmelade, checked: null }, ru, today);
assert.equal(x.checked, "Дата проверки не указана");
assert.equal(x.lunch.active, true);
});
test("Сценарий: Карточки на выбранном языке (DIK-51)", () => {
const v = { ...marmelade, lunch: null, checked: null };
for (const [l, text] of [["kk", "Баға расталмаған"], ["en", "Price not confirmed"]]) {
const x = cardTexts(v, tr(l), today);
assert.equal(x.lunch.text, text);
assert.equal(x.checked, tr(l).cardNotChecked);
for (const s of [x.lunch.text, x.promo.text, x.checked]) assert.doesNotMatch(s, /undefined|null|\{/);
}
});
test("Сценарий: каждое заведение базы становится карточкой без пустых мест (DIK-51)", async () => {
const s = http.createServer(createHandler()).listen(0);
const base = `http://127.0.0.1:${s.address().port}`;
const html = await (await fetch(base + "/base")).text();
const list = await (await fetch(base + "/api/base")).json();
s.close();
assert.match(html, /<div id="cards" class="list"><\/div>/);
assert.match(html, /<summary data-i="baseTable">Подробная таблица с источниками<\/summary>/);
assert.ok(html.indexOf('id="cards"') < html.indexOf('id="sources"'), "карточки выше блока источников");
assert.ok(list.length > 0);
for (const l of ["ru", "kk", "en"]) for (const v of list) {
const x = cardTexts(v, tr(l), today);
for (const s of [x.lunch.text, x.promo.text, x.checked]) { assert.ok(s, v.name); assert.doesNotMatch(s, /undefined|null|\{\w+\}/); }
}
const js = fs.readFileSync("public/base.js", "utf8");
assert.match(js, /\$\("cards"\)\.replaceChildren\(\.\.\.list\.map\(card\)\)/);
});