111 lines
6.2 KiB
JavaScript
111 lines
6.2 KiB
JavaScript
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 |"), /<h1>A<\/h1>[\s\S]*<li><b>y<\/b><\/li>[\s\S]*<td>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[^>]*>Каналы<\/h1>/);
|
|
} finally { srv.close(); }
|
|
});
|
|
|
|
test("BRM-96: «Сводка» и письмо до скриптов показывают серую кнопку и причину", async () => {
|
|
const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "brm96-")), "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}`;
|
|
try {
|
|
let h = await (await fetch(base + "/summary.html")).text();
|
|
assert.match(h, /<button id="resume" class="off" aria-disabled="true"/);
|
|
assert.ok(h.includes('<small id="resumewhy">Расписание сохранено, но сводки в Telegram не уходят, пока команда не передаст ключ бота. Поэтому возобновлять пока нечего.</small>'));
|
|
assert.ok(!h.includes("Рассылка уже идёт"));
|
|
assert.match(h, /id="resume"[^>]*title="Расписание сохранено/);
|
|
assert.match(h, /<button id="stop" data-t/, "«Остановить» доступна, причины нет");
|
|
assert.ok(h.includes('<small id="stopwhy"></small>'));
|
|
await fetch(base + "/api/summary/stop", { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
|
|
h = await (await fetch(base + "/summary.html")).text();
|
|
assert.match(h, /<button id="stop" class="off" aria-disabled="true"/);
|
|
assert.ok(h.includes('<small id="stopwhy">Рассылка уже остановлена.</small>'));
|
|
assert.match(h, /id="stop"[^>]*title="Рассылка уже остановлена\."/);
|
|
assert.ok(h.includes('<small id="resumewhy"></small>'));
|
|
const o = await (await fetch(base + "/opportunities.html")).text();
|
|
assert.match(o, /id="lmark" disabled aria-disabled="true"/);
|
|
assert.ok(o.includes('id="lmarkn" data-t="letter.btnWhy.approve"'));
|
|
assert.ok(o.includes('id="lapprn" data-t="letter.btnWhy.pick"')); // BRM-102
|
|
} finally { srv.close(); }
|
|
});
|