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 { createBot, createBotStore, mockTranscriber, allowedActions } from "../src/bot.js"; import { mockTelegram } from "../src/telegram.js"; import { createPublisher } from "../src/publisher.js"; import { createOwner } from "../src/owner.js"; import { withApproval } from "../src/approval.js"; function setup() { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm52-")); const telegram = mockTelegram(); const pub = createPublisher({ file: path.join(dir, "p.json"), channels: { telegram } }); const plan = { ideas: [withApproval({ id: 1, title: "Пост", description: "Текст", status: "review", approver: "Татьяна" })], publications: [] }; const bot = createBot({ store: createBotStore(path.join(dir, "b.json")), telegram, pub, owner: createOwner(path.join(dir, "o.json"), "секрет-слово"), getPlan: () => plan, savePlan() {}, transcriber: mockTranscriber("Покажи статус") }); const msg = (id, text, extra = {}) => bot.handleUpdate({ message: { from: { id }, chat: { id }, text, ...extra } }); const cb = (id, data) => bot.handleUpdate({ callback_query: { from: { id }, message: { chat: { id } }, data } }); return { bot, telegram, pub, plan, msg, cb }; } const last = (s) => s.telegram.calls.at(-1); test("Сценарий: привязка по секретному слову, чужой аккаунт отклоняется", async () => { const s = setup(); await s.msg(7, "/start"); assert.match(last(s).text, /секретное слово/); await s.msg(7, "не то"); assert.match(last(s).text, /не подошло/); await s.msg(7, "секрет-слово"); assert.match(last(s).text, /подключён/); const r = await s.msg(99, "Согласования"); assert.equal(r.denied, true); assert.match(last(s).text, /только владельцу/); }); test("Сценарий: карточка, кнопки по правилам, утверждение", async () => { const s = setup(); await s.msg(7, "секрет-слово"); assert.deepEqual(allowedActions(s.plan.ideas[0]), ["approve", "rework", "reject", "more"]); await s.msg(7, "Согласования"); assert.equal(last(s).reply_markup.inline_keyboard.length, 4); await s.cb(7, "now:1"); assert.match(last(s).text, /недоступно/); await s.cb(7, "approve:1"); assert.equal(s.plan.ideas[0].status, "approved"); }); test("Сценарий: «Доработать» просит комментарий и возвращает в черновик", async () => { const s = setup(); await s.msg(7, "секрет-слово"); await s.cb(7, "rework:1"); await s.msg(7, "Короче"); assert.equal(s.plan.ideas[0].status, "draft"); assert.equal(s.plan.ideas[0].comments[0].text, "Короче"); }); test("Сценарий: пауза мгновенно останавливает отправки", async () => { const s = setup(); await s.msg(7, "секрет-слово"); await s.msg(7, "Пауза"); const r = await s.pub.publish({ channel: "telegram", text: "x", approvedBy: "tatiana" }); assert.equal(r.reason, "stopped"); await s.cb(7, "resume:0"); assert.equal(s.pub.status().stopped, false); }); test("Сценарий: голос показывается на подтверждение", async () => { const s = setup(); await s.msg(7, "секрет-слово"); await s.msg(7, undefined, { text: undefined, voice: { file_id: "f" } }); assert.match(last(s).text, /Покажи статус/); await s.cb(7, "vyes:0"); }); test("Сценарий: без ключа бота раздел пишет «отправка выключена»", async () => { const s = setup(); s.telegram.connected = () => false; await s.msg(7, "секрет-слово"); await s.msg(7, "Статус"); assert.match(last(s).text, /Отправка выключена/); });