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 { createApp } from "../src/server.js"; import { mockTelegram } from "../src/telegram.js"; async function withServer(fn) { const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "brm13-")), "plan.json"); const srv = createApp({ file }).listen(0); await new Promise((r) => srv.once("listening", r)); const base = `http://127.0.0.1:${srv.address().port}`; const call = async (m, u, b) => { const r = await fetch(base + u, { method: m, headers: { "content-type": "application/json" }, body: b && JSON.stringify(b) }); return [r.status, await r.json()]; }; try { await fn(call); } finally { srv.close(); } } test("Сценарий: добавить, назначить дату, пересчитать, удалить", () => withServer(async (call) => { assert.equal((await call("GET", "/healthz"))[0], 200); let [, s] = await call("GET", "/api/state"); await call("PUT", "/api/settings", { start: "2026-12-01" }); assert.equal((await call("POST", "/api/ideas", { title: "", rubric: 1 }))[0], 400); [, s] = await call("POST", "/api/ideas", { title: "Кейс", rubric: 2 }); assert.equal(s.unscheduled.length, 1); const id = s.ideas[0].id; assert.equal((await call("PUT", `/api/ideas/${id}`, { date: "2027-02-01" }))[0], 400); [, s] = await call("PUT", `/api/ideas/${id}`, { date: "2026-12-10" }); assert.equal(s.unscheduled.length, 0); assert.equal(s.summary.total, 1); [, s] = await call("PUT", `/api/ideas/${id}`, { date: null }); assert.equal(s.summary.total, 0); [, s] = await call("DELETE", `/api/ideas/${id}`); assert.equal(s.ideas.length, 0); })); import { renderMarkdown, docPage } from "../src/site.js"; test("BRM-31: главная и страницы продакта открываются", async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm31-")); const server = createApp({ file: path.join(dir, "plan.json"), telegram: mockTelegram() }); await new Promise((r) => server.listen(0, r)); const base = `http://127.0.0.1:${server.address().port}`; try { for (const p of ["/", "/plan.html", "/market.html", "/opportunities.html", "/summary.html", "/docs/brand-core", "/docs/audit", "/healthz"]) { const r = await fetch(base + p); assert.equal(r.status, 200, p); } assert.match(await (await fetch(base + "/")).text(), /Как проверить результат/); assert.equal((await fetch(base + "/docs/nope")).status, 404); assert.equal((await fetch(base + "/docs/..%2Fpackage")).status, 404); const market = await (await fetch(base + "/api/market")).json(); assert.ok(market.items.length > 0); } finally { server.close(); } }); test("BRM-31: перезапуск не дублирует рынок", async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm31-")); const count = async () => { const server = createApp({ file: path.join(dir, "plan.json"), telegram: mockTelegram() }); await new Promise((r) => server.listen(0, r)); const j = await (await fetch(`http://127.0.0.1:${server.address().port}/api/market`)).json(); server.close(); return j.items.length; }; assert.equal(await count(), await count()); }); test("BRM-31: markdown в читаемую страницу", () => { assert.match(renderMarkdown("# A\n\n- x\n- **y**\n\n| a | b |\n|---|---|\n| 1 | 2 |"), /

A<\/h1>[\s\S]*
  • y<\/b><\/li>[\s\S]*2<\/td>/); assert.equal(docPage("../x"), null); }); test("Сценарий: /api/channels отдаёт таблицу, /channels.html открывается", async () => { const { createApp } = await import("../src/server.js"); const os = await import("node:os"), fs = await import("node:fs"), path = await import("node:path"); const srv = createApp({ file: path.join(fs.mkdtempSync(path.join(os.tmpdir(), "brm56-")), "plan.json") }).listen(0); await new Promise((r) => srv.once("listening", r)); try { const b = `http://127.0.0.1:${srv.address().port}`; assert.equal((await (await fetch(b + "/api/channels")).json()).items.length, 12); const h = await (await fetch(b + "/channels.html")).text(); assert.match(h, /]*>Каналы<\/h1>/); } finally { srv.close(); } });