// DIK-67: цена и акция на каждой карточке главной и «Все заведения» import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import http from "node:http"; import os from "node:os"; import path from "node:path"; import { homeCard, baseCard, baseOffer } from "../public/cards.js"; import { buildList } from "../src/lib.js"; import { createHandler } from "../src/index.js"; const T = Object.fromEntries(["ru", "kk", "en"].map((l) => [l, JSON.parse(fs.readFileSync(`public/i18n/${l}.json`, "utf8"))])); const today = "2026-11-27"; const venue = (o = {}) => ({ id: "x", name: "Кафе", address: "ул. Кунаева, 14", lat: 51.13, lng: 71.43, lunch: { price: 2500, from: "12:00", to: "15:00", days: [1, 2, 3, 4, 5, 6, 7] }, promo: null, checked: "2026-11-26", ...o }); const text = (html) => html.replace(/<[^>]+>/g, " ").replace(/\s+/g, " "); // 17:00 по Астане = 12:00 UTC const at17 = new Date("2026-11-27T12:00:00Z"), at13 = new Date("2026-11-27T08:00:00Z"); test("Сценарий: свежая цена вне часов ланча видна на карточке главной", () => { const [v] = buildList([venue()], null, null, { now: at17 }); assert.match(text(homeCard(v, T.ru)), /Бизнес-ланч 2500 ₸, 12:00–15:00 — в другое время/); for (const l of ["kk", "en"]) assert.match(text(homeCard(v, T[l])), /2500 ₸, 12:00–15:00/, l); const [now] = buildList([venue()], null, null, { now: at13 }); assert.match(homeCard(now, T.ru), /class="badge lunch">Бизнес-ланч 2500 ₸, 12:00–15:00 { const old = venue({ checked: "2026-11-07", promo: { text: "Скидка 10%", until: null } }); const [v] = buildList([old], null, null, { now: at13 }); const home = text(homeCard(v, T.ru)), base = text(baseCard(old, T.ru, today)); for (const s of [home, base]) { assert.match(s, /Цена не подтверждена/); assert.match(s, /Акции сейчас нет/); assert.doesNotMatch(s, /2500/); } }); test("Сценарий: акция закончилась — не выглядит действующей", () => { const ended = venue({ promo: { text: "Скидка 10%", until: "2026-11-26" } }); const html = baseCard(ended, T.ru, today); assert.match(text(html), /Акция закончилась/); assert.doesNotMatch(html, /badge promo/); const [v] = buildList([ended], null, null, { now: at13 }); assert.match(text(homeCard(v, T.ru)), /Акция закончилась/); assert.doesNotMatch(homeCard(v, T.ru), /badge promo/); const active = baseOffer(venue({ promo: { text: "Скидка 10%", until: "2026-11-30" } }), T.ru, today); assert.equal(active.promo.active, true); assert.match(active.promo.text, /Скидка 10% \(до 30\.11\.2026\)/); }); for (const [l, price] of [["ru", "Цена не подтверждена"], ["kk", "Баға расталмаған"], ["en", "Price not confirmed"]]) { test(`Сценарий: без пустых мест на языке ${l}`, () => { const bare = venue({ lunch: null, promo: null, checked: null, photo: null, description: null }); const [v] = buildList([bare], null, null, { now: at13 }); for (const html of [homeCard(v, T[l]), baseCard(bare, T[l], today)]) { assert.ok(text(html).includes(price), html); assert.doesNotMatch(text(html), /\bnull\b|\bundefined\b|NaN/); assert.ok(text(html).includes(T[l].promoNone)); } assert.ok(text(baseCard(bare, T[l], today)).includes(T[l].cardNotChecked)); }); } test("Сценарий: «Все заведения» и главная показывают карточки сразу, без скрипта", async () => { const s = http.createServer(createHandler()).listen(0); const url = `http://127.0.0.1:${s.address().port}`; const all = await (await fetch(url + "/api/base")).json(); const base = await (await fetch(url + "/base")).text(); const cards = base.slice(base.indexOf('id="cards"'), base.indexOf('id="sources"')); assert.equal((cards.match(/class="card"/g) || []).length, all.length); assert.ok(base.indexOf('id="cards"') < base.indexOf('id="sources"'), "карточки над блоком об источниках"); assert.match(base, new RegExp(`Заведений: ${all.length}`)); for (const v of all) assert.ok(cards.includes(v.name.replace(/&/g, "&").replace(/"/g, """).replace(/'/g, "'")), v.name); assert.doesNotMatch(text(cards), /\bnull\b|\bundefined\b/); const home = await (await fetch(url + "/")).text(); const out = home.slice(home.indexOf('id="out"'), home.indexOf('id="about"')); assert.equal((out.match(/class="card"/g) || []).length, all.length); assert.match(out, /Найдено мест: \d+/); assert.doesNotMatch(text(out), /\bnull\b|\bundefined\b/); s.close(); }); test("Граница: пустая база — на «Все заведения» «Список готовится», карточек нет", async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "dik67-")); fs.writeFileSync(path.join(dir, "venues.csv"), fs.readFileSync("data/venues.csv", "utf8").split("\n")[0] + "\n"); const s = http.createServer(createHandler({ dataDir: dir, seedDir: dir })).listen(0); const html = await (await fetch(`http://127.0.0.1:${s.address().port}/base`)).text(); assert.doesNotMatch(html, /class="card"/); assert.ok(html.includes(T.ru.listPreparing)); s.close(); });