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, createTelegram } from "../src/telegram.js"; import { nextRun, sentWhat } from "../src/bot-page.js"; import { DEFAULT_SETTINGS } from "../src/metrics.js"; async function withServer(telegram, fn, prep) { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm74-")); if (prep) prep(dir); const srv = createApp({ file: path.join(dir, "plan.json"), telegram }).listen(0); await new Promise((r) => srv.once("listening", r)); const base = `http://127.0.0.1:${srv.address().port}`; const page = async (u = "/bot.html") => (await fetch(base + u)).text(); try { await fn({ page, base, srv }); } finally { srv.close(); } } const noAccess = () => createTelegram({ token: "", chatId: "" }); test("BRM-74 Сценарий: Ссылка с главной", () => withServer(mockTelegram(), async ({ page }) => { const home = await page("/"); assert.match(home, /Бот и отчёты<\/a>/); const html = await page(); assert.match(html, /

Бот и отчёты<\/h1>/); })); test("BRM-74 Сценарий: Отправка включена", () => withServer(mockTelegram(), async ({ page }) => { const html = await page(); assert.match(html, /Отправка включена/); assert.doesNotMatch(html, /Отправка выключена/); })); test("BRM-74 Сценарий: Отправка выключена без доступа", () => withServer(noAccess(), async ({ page }) => { const html = await page(); assert.match(html, /Отправка выключена/); assert.match(html, /ключ бота у владельца есть, но в эту версию для проверки он не передан/); assert.doesNotMatch(html, /если это версия/i); })); test("BRM-74 Сценарий: Пауза — подпись, остановка, «Возобновить»", () => withServer(mockTelegram(), async ({ page, base }) => { const before = await page(); assert.match(before, /