// 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, /