dika/test/lib.test.js

84 lines
5.4 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.

import test from "node:test";
import assert from "node:assert/strict";
import { buildList, findAddress } from "../src/lib.js";
const venues = [
{ id: "a", name: "A", address: "ул. Ближняя, 1", lat: 51.1283, lng: 71.4305, lunch: { price: 2500, days: [1,2,3,4,5], from: "12:00", to: "15:00" }, promo: null },
{ id: "b", name: "B", address: "ул. Дальняя, 2", lat: 51.1383, lng: 71.4305, lunch: null, promo: { text: "x", until: "2026-11-10" } },
{ id: "c", name: "C", address: "ул. Средняя, 3", lat: 51.1303, lng: 71.4305, lunch: null, promo: null },
];
const wed13 = new Date("2026-11-18T08:00:00Z"); // среда 13:00 Астана
const sat13 = new Date("2026-11-21T08:00:00Z");
test("Сценарий: список от ближайшего к дальнему", () => {
const l = buildList(venues, 51.1283, 71.4305, { now: wed13 });
assert.deepEqual(l.map((v) => v.id), ["a", "c", "b"]);
});
test("Сценарий: ланч в будний день в обед", () => {
const l = buildList(venues, 51.1283, 71.4305, { now: wed13 });
assert.deepEqual(l[0].lunch, { price: 2500, from: "12:00", to: "15:00" });
});
test("Сценарий: ланч в выходной — заведение есть, ланча нет", () => {
const l = buildList(venues, 51.1283, 71.4305, { now: sat13 });
assert.equal(l[0].id, "a"); assert.equal(l[0].lunch, null);
});
test("Сценарий: акция закончилась", () => {
const l = buildList(venues, 51.1283, 71.4305, { now: wed13 });
assert.equal(l.find((v) => v.id === "b").promo, null);
});
test("Сценарий: фильтр только с предложениями", () => {
const l = buildList(venues, 51.1283, 71.4305, { now: wed13, only: true });
assert.deepEqual(l.map((v) => v.id), ["a"]);
});
test("Сценарий: рядом нет заведений", () => {
assert.deepEqual(buildList(venues, 51.3, 71.9, { now: wed13 }), []);
});
test("Сценарий: адрес найден / не найден", () => {
assert.equal(findAddress(venues, "Ближняя").id, "a");
assert.equal(findAddress(venues, "несуществующая"), null);
});
test("DIK-10: в списке есть дата проверки записи", () => {
const l = buildList([{ id: "a", name: "A", address: "x", lat: 51.1283, lng: 71.4305, checked: "2026-11-10", lunch: null, promo: null }], 51.1283, 71.4305, { now: new Date("2026-11-11T08:00:00Z") });
assert.equal(l[0].checked, "2026-11-10");
});
test("Сценарий: список с ценой и акцией, давние данные без цены, фильтр убирает пустые (DIK-21)", () => {
const vs = [
{ id: "ok", name: "OK", address: "x", lat: 51.1283, lng: 71.4305, checked: "2026-11-17",
lunch: { price: 2500, days: [1,2,3,4,5], from: "12:00", to: "15:00" }, promo: { text: "-10%", until: "2026-11-30" } },
{ id: "old", name: "Old", address: "y", lat: 51.1284, lng: 71.4305, checked: "2026-10-01",
lunch: { price: 2500, days: [1,2,3,4,5], from: "12:00", to: "15:00" }, promo: { text: "-10%", until: null } },
{ id: "gone", name: "Gone", address: "z", lat: 51.1285, lng: 71.4305, checked: "2026-11-17", lunch: null, promo: { text: "x", until: "2026-11-01" } },
];
const all = buildList(vs, 51.1283, 71.4305, { now: wed13 });
assert.deepEqual(all.map((v) => v.id), ["ok", "old", "gone"]);
assert.equal(all[0].lunch.price, 2500);
assert.equal(all[0].promo.text, "-10%");
assert.equal(all[1].lunch, null); assert.equal(all[1].promo, null); assert.equal(all[1].unconfirmed, true);
assert.equal(all[2].promo, null);
assert.deepEqual(buildList(vs, 51.1283, 71.4305, { now: wed13, only: true }).map((v) => v.id), ["ok"]);
});
test("карточка списка отдаёт фото и описание или null (DIK-41)", () => {
const base = { id: "a", name: "A", address: "x", lat: 51.12, lng: 71.43, lunch: null, promo: null, checked: null };
const [a, b] = buildList([{ ...base, photo: "https://e.kz/a.jpg", photoSource: "владелец", description: "Уютно", descriptionSource: "2ГИС" }, { ...base, id: "b", photo: "https://e.kz/z.jpg" }], 51.12, 71.43);
assert.equal(a.photo, "https://e.kz/a.jpg"); assert.equal(a.description, "Уютно");
assert.equal(a.photoSource, "владелец"); assert.equal(a.descriptionSource, "2ГИС");
assert.equal(b.photo, null); assert.equal(b.description, null); // фото без источника не показываем
});
test("Сценарий: фильтр расстояния до 1 км", () => {
const l = buildList(venues, 51.1283, 71.4305, { now: wed13, maxKm: 1 });
assert.deepEqual(l.map((v) => v.id), ["a", "c"]);
});
test("Сценарий: фильтр цены ланча — только места с действующим ланчем не дороже порога", () => {
assert.deepEqual(buildList(venues, 51.1283, 71.4305, { now: wed13, maxPrice: 3000 }).map((v) => v.id), ["a"]);
assert.deepEqual(buildList(venues, 51.1283, 71.4305, { now: wed13, maxPrice: 2000 }), []);
assert.deepEqual(buildList(venues, 51.1283, 71.4305, { now: sat13, maxPrice: 3000 }), []);
});
test("Сценарий: фильтры работают вместе с «только с ланчем или акцией»", () => {
const l = buildList(venues, 51.1283, 71.4305, { now: wed13, only: true, maxKm: 1, maxPrice: 4000 });
assert.deepEqual(l.map((v) => v.id), ["a"]);
});