brendmanager/test/market-competitors.test.js
Peppa cda27100b4
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 3с
ci / checks (pull_request) Successful in 15s
deploy / deploy (pull_request) Successful in 26s
BRM-135: раздел «Конкуренты» на «Рынке» собирает сервер — страна, дата и ссылка видны сразу
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 17:13:22 +05:00

60 lines
3.5 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.

// BRM-135: страна, дата проверки и ссылка на страницу конкурента видны в ответе сервера, без скриптов.
import { test } from "node:test";
import assert from "node:assert/strict";
import { competitorsBox } from "../src/site.js";
import { REPORT_RECORDS } from "../src/market-seed.js";
import { coverage, view, AGREED } from "../src/market.js";
const items = REPORT_RECORDS.map((r) => view({ ...r, sample: true }));
const comps = items.filter((i) => i.kind === "competitor");
const row = (html, key) => html.match(new RegExp(`<tr data-key="${key}">(.*?)</tr>`))?.[1] ?? "";
test("Сценарий: у каждого конкурента страна, дата проверки и ссылка на страницу", () => {
const html = competitorsBox("ru", items, coverage(items), AGREED);
assert.equal(comps.length, 19);
for (const c of comps) {
const r = row(html, c.key);
assert.ok(r, c.key);
assert.match(r, new RegExp(`<td class="country">${c.country.replace(/[()]/g, "\\$&")}</td>`), c.key);
assert.match(r, /<td class="checked">\d{1,2} [а-я]+ 2026 г\.<\/td>/, c.key);
assert.ok(r.includes(`href="${c.profileUrl}"`) && r.includes('target="_blank"'), c.key);
}
assert.match(row(html, "competitor-ca-1"), /<td class="checked">15 ноября 2026 г\.<\/td>/);
assert.match(row(html, "competitor-ca-2"), /<td class="checked">16 ноября 2026 г\.<\/td>/);
});
test("Сценарий: конкурент без ссылки — ячейка страницы пустая, страница не ломается", () => {
const one = [view({ key: "x", kind: "competitor", group: "kz", title: "X", country: "Казахстан", collectedAt: "2026-11-15", confidence: "низкая", profileUrl: "javascript:alert(1)" })];
const html = competitorsBox("ru", one, coverage(one), null);
assert.doesNotMatch(html, /javascript:/);
assert.match(html, /ждём согласия владельца/);
});
test("Сценарий: разделы на казахском и английском", () => {
for (const lang of ["kk", "en"]) {
const html = competitorsBox(lang, items, coverage(items), AGREED);
assert.ok(comps.every((c) => row(html, c.key).includes(c.profileUrl)), lang);
assert.doesNotMatch(html, /ui\.mk\./, lang);
}
});
test("Сценарий: страница «Рынок» без скриптов уже содержит страну, дату и ссылку", async () => {
const { createApp } = await import("../src/server.js");
const fs = await import("node:fs"); const os = await import("node:os"); const path = await import("node:path");
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm135-"));
const srv = createApp({ file: path.join(dir, "data.json") }).listen(0);
await new Promise((r) => srv.once("listening", r));
try {
const html = await (await fetch(`http://127.0.0.1:${srv.address().port}/market.html`)).text();
const r = row(html, "competitor-ca-1");
assert.match(r, /Узбекистан \(Ташкент\)/);
assert.match(r, /15 ноября 2026 г\./);
assert.match(r, /href="https:\/\/cx-lab\.uz\/"[^>]*>cx-lab\.uz<\/a>/);
assert.equal((html.match(/<tr data-key="competitor-/g) ?? []).length, 19);
} finally { srv.close(); }
});
test("Сценарий: в строке согласования нет двойной точки после даты", () => {
assert.doesNotMatch(competitorsBox("ru", items, coverage(items), AGREED), /г\.\./);
});