brendmanager/test/brm188-bot-buttons-always.test.js
peppa-bot 3c2a250b02
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 23с
ci / checks (pull_request) Successful in 37s
deploy / deploy (pull_request) Successful in 18s
BRM-188: кнопки «Открыть бота в Telegram» и «Присылать сводки в мой личный чат с ботом» всегда видны на главной и «Бот и отчёты»
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 19:05:42 +00:00

86 lines
5.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// BRM-188: кнопки «Открыть бота в Telegram» и «Присылать сводки в мой личный чат с ботом» всегда видны
// на «Бот и отчёты» и на главной; рядом — какой чат выбран; «работает» — после подтверждённой доставки.
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 { mockTelegramApi } from "../src/telegram.js";
import { WEBHOOK_PATH } from "../src/bot-identity.js";
const TATIANA = 555;
const WORD = "секрет-слово";
const SITE = "https://brendmanager-staging.telecom.quest";
const plain = (html) => html.replace(/<[^>]+>/g, "");
async function startApp({ found = true } = {}) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm188-"));
const telegram = mockTelegramApi({ chatAware: true, webhook: { url: SITE + WEBHOOK_PATH, allowed_updates: [] }, me: found ? { id: 900, is_bot: true, username: "TanyaBrandManagerBot" } : null });
const srv = createApp({ file: path.join(dir, "plan.json"), telegram, ownerCode: WORD, webhookSecret: "mock-secret", webhook: SITE + WEBHOOK_PATH, siteUrl: SITE }).listen(0);
await new Promise((r) => srv.once("listening", r));
const base = `http://127.0.0.1:${srv.address().port}`;
const say = (text) => fetch(base + WEBHOOK_PATH, { method: "POST", headers: { "content-type": "application/json", "x-telegram-bot-api-secret-token": "mock-secret" },
body: JSON.stringify({ message: { from: { id: TATIANA, first_name: "Татьяна" }, chat: { id: TATIANA }, text } }) });
const get = async (u) => (await fetch(base + u)).text();
await srv.identity.refresh({ force: true }).catch(() => null);
return { srv, telegram, say, page: () => get("/bot.html"), home: () => get("/"), post: (u) => fetch(base + u, { method: "POST", redirect: "manual" }), close: () => srv.close() };
}
const OPEN = /<a class="btn" href="https:\/\/t\.me\/TanyaBrandManagerBot[^"]*"[^>]*>Открыть бота в Telegram<\/a>/;
const PICK = /<form method="post" action="[^"]*" id="nowpick"><button type="submit"[^>]*>Присылать сводки в мой личный чат с ботом<\/button>/;
test("Сценарий: Бот привязан, доставка подтверждена — кнопки видны", async () => {
const s = await startApp();
try {
await s.say("/start");
await s.say(WORD);
const r = await s.post("/bot/summary-here");
assert.match(r.headers.get("location"), /dc=ok/);
for (const html of [await s.page(), await s.home()]) {
assert.match(html, OPEN);
assert.match(html, PICK);
assert.match(html, /<p id="nowchat" class="st ok">Чат для сводок: выбран ваш личный чат с ботом\.<\/p>/);
assert.match(plain(html), /Отправка сводок: работает, доставка подтверждена/);
}
} finally { s.close(); }
});
test("Сценарий: Выбор личного чата с главной", async () => {
const s = await startApp();
try {
await s.say("/start");
await s.say(WORD);
let home = await s.home();
assert.match(home, /<p id="nowchat" class="st off">Чат для сводок: не выбран\.<\/p>/);
assert.match(home, /action="\/bot\/summary-here\?from=home" id="nowpick"><button type="submit">/);
assert.match(plain(home), /Отправка сводок: выключена/);
const r = await s.post("/bot/summary-here?from=home");
assert.equal(r.status, 303);
assert.equal(r.headers.get("location"), "/#botnow");
home = await s.home();
assert.match(home, /Чат для сводок: выбран ваш личный чат с ботом\./);
assert.match(plain(home), /Отправка сводок: работает, доставка подтверждена/);
} finally { s.close(); }
});
test("Сценарий: Привязки нет — выбрать чат нельзя, причина рядом", async () => {
const s = await startApp();
try {
for (const html of [await s.page(), await s.home()]) {
assert.match(html, /href="https:\/\/t\.me\/TanyaBrandManagerBot\?start=bind"/);
assert.match(html, /<button type="submit" class="off" aria-disabled="true" aria-describedby="nowpickwhy">Присылать сводки в мой личный чат с ботом<\/button>/);
assert.match(html, /<small id="nowpickwhy">Выбрать личный чат пока нельзя: бот ещё не привязан\./);
}
} finally { s.close(); }
});
test("Сценарий: Бот не найден — кнопка открытия серая с причиной", async () => {
const s = await startApp({ found: false });
try {
const html = await s.page();
assert.match(html, /<a class="btn off" role="link" aria-disabled="true" aria-describedby="nowopenwhy">Открыть бота в Telegram<\/a>/);
assert.match(html, /<small id="nowopenwhy">Открыть бота пока нельзя: бот не найден/);
assert.doesNotMatch(html, /href="https:\/\/t\.me\//);
} finally { s.close(); }
});