78 lines
4.4 KiB
JavaScript
78 lines
4.4 KiB
JavaScript
// BRM-167: форма «Задать новое слово» на «Бот и отчёты» видна и после привязки; менять слово может только владелица.
|
|
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 { webhookSecret, WEBHOOK_PATH } from "../src/bot-identity.js";
|
|
import { mockTelegramApi } from "../src/telegram.js";
|
|
import { createApp } from "../src/server.js";
|
|
import { t } from "../src/i18n.js";
|
|
|
|
const TOKEN = "123456:MOCK_SECRET_TOKEN_VALUE";
|
|
const STAGING = "brendmanager-staging.telecom.quest";
|
|
const OLD = "старое-слово-1", NEW = "Новое-Слово 2026";
|
|
|
|
async function startApp() {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm167-"));
|
|
const telegram = mockTelegramApi({ token: TOKEN, me: { id: 1, is_bot: true, username: "TanyaBrandManagerBot" }, webhook: { url: "" } });
|
|
const logs = [];
|
|
const orig = console.log;
|
|
console.log = (...a) => { logs.push(a.join(" ")); };
|
|
const app = createApp({ file: path.join(dir, "plan.json"), telegram, ownerCode: undefined, webhookSecret: webhookSecret(TOKEN, {}),
|
|
webhook: `https://${STAGING}${WEBHOOK_PATH}`, siteUrl: `https://${STAGING}` });
|
|
await new Promise((r) => app.listen(0, r));
|
|
const base = `http://127.0.0.1:${app.address().port}`;
|
|
const form = (url, body) => fetch(base + url, { method: "POST", redirect: "manual", headers: { "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(body).toString() }).then((r) => r.headers.get("location"));
|
|
const say = async (text, id = 7) => {
|
|
await fetch(base + WEBHOOK_PATH, { method: "POST", headers: { "content-type": "application/json", "x-telegram-bot-api-secret-token": webhookSecret(TOKEN, {}) },
|
|
body: JSON.stringify({ message: { from: { id, first_name: "Татьяна", username: "tanya" }, chat: { id }, text } }) });
|
|
return telegram.posts[String(id)].at(-1).text;
|
|
};
|
|
const page = (q = "") => fetch(base + "/bot.html" + q).then((r) => r.text());
|
|
return { form, say, page, logs, close: () => { console.log = orig; app.close(); } };
|
|
}
|
|
|
|
async function bound() {
|
|
const s = await startApp();
|
|
assert.match(await s.form("/bot/owner-word", { word: OLD, confirm: OLD }), /word=saved/);
|
|
assert.equal(await s.say(OLD), t("ru", "bot.bind.done"));
|
|
return s;
|
|
}
|
|
|
|
test("BRM-167 Сценарий: Смена слова после привязки", async () => {
|
|
const s = await bound();
|
|
try {
|
|
const html = await s.page();
|
|
assert.match(html, /id="tgstate"[^>]*>Сейчас: привязано/);
|
|
assert.match(html, /<h3 id="tgnew">Задать новое слово/);
|
|
assert.match(html, /name="current" type="password"/);
|
|
assert.match(html, /name="confirm" type="password"/);
|
|
assert.ok(html.includes(t("ru", "botp.tg.word.hintBound")));
|
|
assert.match(await s.form("/bot/owner-word", { current: OLD, word: NEW, confirm: NEW }), /word=saved#tgbot$/);
|
|
const after = await s.page("?word=saved");
|
|
assert.ok(after.includes(t("ru", "botp.tg.word.savedBound")));
|
|
assert.match(after, /id="tgstate"[^>]*>Сейчас: привязано/);
|
|
assert.match(await s.form("/bot/word-check", { word: OLD }), /check=wrong/);
|
|
assert.match(await s.form("/bot/word-check", { word: "новое-слово 2026" }), /check=ok/);
|
|
for (const x of [html, after, s.logs.join("\n")]) for (const w of [OLD, NEW, NEW.toLowerCase()]) assert.ok(!x.includes(w));
|
|
} finally { s.close(); }
|
|
});
|
|
|
|
test("BRM-167 Сценарий: Чужой человек после привязки", async () => {
|
|
const s = await bound();
|
|
try {
|
|
assert.match(await s.form("/bot/owner-word", { current: "не-то-слово", word: NEW, confirm: NEW }), /word=notOwner#tgbot$/);
|
|
assert.ok((await s.page("?word=notOwner")).includes(t("ru", "botp.tg.word.notOwner")));
|
|
assert.match(await s.form("/bot/word-check", { word: OLD }), /check=ok/);
|
|
assert.match(await s.page(), /id="tgstate"[^>]*>Сейчас: привязано/);
|
|
} finally { s.close(); }
|
|
});
|
|
|
|
test("BRM-167: тексты формы после привязки есть на всех языках", () => {
|
|
for (const l of ["ru", "kk", "en"]) for (const k of ["botp.tg.word.hintBound", "botp.tg.word.savedBound", "deploy.note.brm167"]) {
|
|
const v = t(l, k);
|
|
assert.ok(v && v !== k, `${l}:${k}`);
|
|
}
|
|
});
|