dika/test/card-sources.test.js
2026-11-27 12:16:12 +00:00

53 lines
2.7 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 { cardSources } from "../public/card-sources.js";
import { loadBase } from "../src/venues.js";
const L = Object.fromEntries(["ru", "kk", "en"].map((l) => [l, JSON.parse(fs.readFileSync(`public/i18n/${l}.json`, "utf8"))]));
const ru = L.ru;
const show = (n) => n.text + (n.name || "");
const all = () => { const b = loadBase("data"); return Array.isArray(b) ? b : b.venues || Object.values(b); };
test("Сценарий: Описание собрал ИИ-агент по 2ГИС", () => {
const v = all().find((x) => x.name === "Аризона");
const [d] = cardSources(v, ru);
assert.equal(show(d), "Описание: ИИ-агент, по данным 2ГИС");
assert.equal(d.href, "https://2gis.kz/astana/firm/70000001018081639");
});
test("Сценарий: Фото пока нет", () => {
const v = all().find((x) => x.name === "Аризона");
assert.equal(show(cardSources(v, ru)[1]), "Фото: пока нет");
const withPhoto = { photo: "https://x/y.jpg", photoSource: "Wikimedia Commons — https://commons.wikimedia.org/a" };
assert.equal(show(cardSources(withPhoto, ru)[1]), "Фото: Wikimedia Commons");
assert.equal(show(cardSources(withPhoto, ru, { photoFailed: true })[1]), "Фото: пока нет");
});
test("Сценарий: Описание и фото от владельца", () => {
const v = { description: "Кафе.", descriptionSource: "владелец", photo: "https://x/y.jpg", photoSource: "владелец" };
assert.deepEqual(cardSources(v, ru).map(show), ["Описание: добавлено владельцем", "Фото: добавлено владельцем"]);
});
test("Сценарий: Описания нет", () => {
assert.equal(show(cardSources({}, ru)[0]), "Описание: пока нет");
});
test("Подписи одного вида у всех заведений базы, на трёх языках, без технических слов", () => {
for (const [l, t] of Object.entries(L)) for (const v of all()) {
const n = cardSources(v, t);
assert.deepEqual(n.map((x) => x.cls), ["src-desc", "src-photo"]);
for (const x of n) {
assert.ok(x.text && !/undefined|null/.test(show(x)), `${l} ${v.name}`);
assert.doesNotMatch(show(x), /https?:|CSV|URL|null/i);
}
}
});
test("Главная выводит обе подписи у каждой карточки", () => {
const app = fs.readFileSync("public/app.js", "utf8");
assert.match(app, /import \{ cardSources \} from "\.\/card-sources\.js"/);
assert.match(app, /cardSources\(v, t\)\.map\(srcNote\)/);
assert.match(app, /photoFailed: true/);
});