brendmanager/test/market.test.js
Peppa f91df83cea
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 0с
ci / checks (pull_request) Successful in 4s
deploy / deploy (pull_request) Successful in 3s
BRM-27: хранилище рынка (тренды, конкуренты, гипотезы), перенос данных отчёта
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 21:52:08 +05:00

62 lines
3.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 fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { createMarketStore } from "../src/market.js";
import { REPORT_RECORDS } from "../src/market-seed.js";
import { createApp } from "../src/server.js";
const tmp = () => path.join(fs.mkdtempSync(path.join(os.tmpdir(), "brm27-")), "market.json");
test("Сценарий: данные отчёта целиком, числа совпадают с отчётом", () => {
const s = createMarketStore(tmp());
const r = s.importReport(REPORT_RECORDS);
assert.deepEqual(r.rejected, []);
assert.equal(s.list("competitor").length, 8);
assert.equal(s.list("hypothesis").length, 9); // A–E и 4 ниши
assert.ok(s.list("trend").length >= 1);
});
test("Сценарий: у каждой записи источник, дата, уверенность; у конкурента профиль; у гипотезы статус", () => {
const s = createMarketStore(tmp()); s.importReport(REPORT_RECORDS);
for (const x of s.list()) { assert.ok(x.source && x.collectedAt && ["высокая", "средняя", "низкая"].includes(x.confidence)); }
for (const x of s.list("competitor")) assert.match(x.profileUrl, /^https?:\/\//);
assert.equal(s.get("niche-1").score, 75);
assert.ok(s.list("hypothesis").every((h) => ["подтверждена", "отклонена", "не проверена"].includes(h.status)));
assert.equal(s.get("niche-1").expertiseCategory, "практик-консультант");
});
test("Сценарий: повторный перенос не создаёт дублей", () => {
const f = tmp(); const s = createMarketStore(f);
s.importReport(REPORT_RECORDS);
const r = s.importReport(REPORT_RECORDS);
assert.equal(r.created, 0);
assert.equal(createMarketStore(f).list().length, REPORT_RECORDS.length);
});
test("Сценарий: конкурент без профиля и запись без источника отклоняются", () => {
const s = createMarketStore(tmp());
const ok = { key: "x", kind: "competitor", title: "X", source: "s", collectedAt: "2026-11-15", confidence: "низкая" };
assert.match(s.upsert(ok).err, /профиль/);
assert.match(s.upsert({ ...ok, profileUrl: "https://x.kz", source: "" }).err, /Источник/);
assert.match(s.upsert({ ...ok, profileUrl: "https://x.kz", confidence: "99" }).err, /Уверенность/);
assert.match(s.upsert({ ...ok, kind: "hypothesis", status: "не проверена", score: 120 }).err, /Оценка/);
assert.equal(s.list().length, 0);
});
test("Сценарий: чтение по API с фильтром по типу", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm27-"));
const srv = createApp({ file: path.join(dir, "plan.json") }).listen(0);
await new Promise((r) => srv.once("listening", r));
const base = `http://127.0.0.1:${srv.address().port}`;
try {
const j = async (u) => { const r = await fetch(base + u); return [r.status, await r.json()]; };
let [c, b] = await j("/api/market?type=competitor");
assert.equal(c, 200); assert.equal(b.items.length, 8);
[c, b] = await j("/api/market/niche-1"); assert.equal(b.score, 75);
[c] = await j("/api/market?type=bad"); assert.equal(c, 400);
[c] = await j("/api/market/nope"); assert.equal(c, 404);
} finally { srv.close(); }
});