// BRM-147: путь проверочной публикации на странице «Бот и отчёты» (сценарии specs/test-path-screen). 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 { createTestRun, CHANNEL } from "../src/test-run.js"; import { mockTelegramApi } from "../src/telegram.js"; import { createPublisher } from "../src/publisher.js"; import { createStrategy, SECTIONS } from "../src/strategy.js"; import { withApproval } from "../src/approval.js"; import { createAssetStore } from "../src/assets.js"; import { createFactsStore } from "../src/facts.js"; import { createBot, createBotStore, mockTranscriber } from "../src/bot.js"; import { createOwner } from "../src/owner.js"; import { botPage, botView } from "../src/bot-page.js"; import { createJobs } from "../src/jobs.js"; import { createMetricsStore } from "../src/metrics.js"; import { createApp } from "../src/server.js"; const TATIANA = 555; const CH = -1001234567890; const at = (s) => Date.parse(s); const T0 = at("2026-11-22T05:00:00Z"); // 10:00 по Алматы const NOON = at("2026-11-22T07:00:00Z"); // 12:00 по Алматы function setup({ approve = true } = {}) { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm147-")); const telegram = mockTelegramApi({}); const chat = { id: CH, type: "channel", title: "Тест BRM" }; telegram.chats[String(CH)] = chat; telegram.members[String(CH)] = { status: "administrator", can_post_messages: true, user: { id: 900 } }; const store = createBotStore(path.join(dir, "bot.json")); store.bind(TATIANA); const strategy = createStrategy(path.join(dir, "strategy.json")); const plan = { ideas: [withApproval({ id: 1, title: "Первый пост", description: "Как мы считаем эффект CX", rubric: 1, date: "2026-11-22" })], publications: [] }; const clock = { now: T0 }; let pub; const tr = createTestRun({ file: path.join(dir, "tr.json"), telegram, getOwnerChat: () => store.chatId(), getPlan: () => plan, facts: createFactsStore(path.join(dir, "facts.json")), assets: createAssetStore(path.join(dir, "assets.json")), strategy, pub: () => pub }); pub = createPublisher({ file: path.join(dir, "p.json"), channels: { [CHANNEL]: tr.adapter }, gate: (r) => strategy.gate(r), now: () => clock.now }); const bot = createBot({ store, telegram, pub, owner: createOwner(path.join(dir, "o.json"), "слово"), getPlan: () => plan, savePlan() {}, transcriber: mockTranscriber(), testRun: tr, now: () => new Date(clock.now) }); const press = (data) => bot.handleUpdate({ callback_query: { from: { id: TATIANA }, message: { chat: { id: TATIANA } }, data } }); const card = () => (telegram.posts[String(TATIANA)] ?? []).find((m) => m.reply_markup?.inline_keyboard?.[0]?.[0]?.callback_data?.startsWith("tr:approve")); const btn = (name) => card().reply_markup.inline_keyboard[0].find((b) => b.text === name).callback_data; const tick = (ms) => { clock.now = ms; return tr.tick(ms); }; const jobs = createJobs({ file: path.join(dir, "jobs.json") }); const metrics = createMetricsStore(path.join(dir, "metrics.json")); const page = (extra = {}) => botPage("ru", { ...botView({ pub, jobs, metrics, telegram, testRun: tr, now: clock.now }), wordSet: true, ...extra }); const ready = async () => { await bot.handleUpdate({ my_chat_member: { chat, from: { id: TATIANA }, new_chat_member: { status: "administrator" } } }); await press(`tc:yes:${CH}`); if (!approve) return; strategy.edit(Object.fromEntries(SECTIONS.map((s) => [s, "текст"])), "агент", new Date(T0).toISOString()); strategy.decide({ action: "approve", by: "Татьяна" }, new Date(T0).toISOString()); tr.arm(T0); }; return { dir, telegram, plan, tr, press, card, btn, tick, page, ready, clock }; } const text = (html) => html.replace(/<[^>]+>/g, "").replace(/"/g, '"').replace(/&/g, "&"); test("Сценарий: Стратегия утверждена — время запуска по расписанию видно", async () => { const s = setup(); await s.ready(); const html = s.page(); assert.match(text(html), /Работа по расписанию запущена: 22\.11\.2026 10:00 по Алматы, после утверждения стратегии/); assert.match(text(html), /Следующая проверочная публикация по расписанию: 22\.11\.2026 12:00 по Алматы/); }); test("Сценарий: Стратегия не утверждена — блока карточки нет", async () => { const s = setup({ approve: false }); await s.ready(); const html = s.page(); assert.match(text(html), /Работа по расписанию начнётся после утверждения стратегии/); assert.ok(!html.includes('id="trcards"')); assert.ok(!html.includes('id="trnext"')); }); test("Сценарий: Карточка ждёт решения", async () => { const s = setup(); await s.ready(); await s.tick(T0 + 60e3); const html = s.page(); const box = text(html.slice(html.indexOf('id="trcard"'), html.indexOf("", html.indexOf('id="trform"')))); for (const w of ["[Тест] Первый пост", "Материалы: нет", "Канал: «Тест BRM»", "Время публикации: 22.11.2026 12:00 по Алматы", "Решение нужно до", "Ничего не отправлено", "ждёт решения"]) assert.ok(box.includes(w), w); for (const b of ["approve", "rework", "reject"]) assert.match(html, new RegExp(`