brendmanager/test/brm182-word-proof.test.js
peppa-bot 0d8c860e8a
All checks were successful
peppa/lint lint: success за 2с
peppa/test test: success за 25с
ci / checks (pull_request) Successful in 41s
deploy / deploy (pull_request) Successful in 17s
BRM-182: «Как проверено слово для одобрения» на «Бот и отчёты» — цепочка на отдельной копии проверочными словами
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 18:57:41 +00:00

61 lines
3.8 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-182: на «Бот и отчёты» видно, как проверено слово для одобрения — цепочка на отдельной копии.
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 { runWordProof, PROOF_NEW, PROOF_OLD } from "../src/word-proof.js";
import { mockTelegramApi } from "../src/telegram.js";
import { createApp } from "../src/server.js";
import { t } from "../src/i18n.js";
import { webhookSecret, WEBHOOK_PATH } from "../src/bot-identity.js";
const STEPS = ["set", "replace", "checkSpaces", "checkCase", "checkOld", "stateBefore", "botOld", "botNew", "restart", "stranger", "stored"];
test("BRM-182 Сценарий: цепочка смены слова видна на странице", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm182-"));
const realOwner = path.join(dir, "owner.json");
// Версия для проверки, куда Telegram шлёт сообщения бота (как на настоящей версии для проверки).
const TOKEN = "123456:MOCK_SECRET_TOKEN_VALUE", HOST = "brendmanager-staging.telecom.quest";
const telegram = mockTelegramApi({ token: TOKEN, webhook: { url: `https://${HOST}${WEBHOOK_PATH}` } });
const app = createApp({ file: path.join(dir, "plan.json"), telegram, ownerCode: undefined, webhookSecret: webhookSecret(TOKEN, {}), siteUrl: `https://${HOST}` });
await new Promise((r) => app.listen(0, r));
try {
for (const lang of ["ru", "kk", "en"]) {
const html = await fetch(`http://127.0.0.1:${app.address().port}/bot.html?lang=${lang}`).then((r) => r.text());
assert.ok(html.includes(t(lang, "botp.proof.h")), lang);
assert.match(html, /id="wordproofsum" class="st ok"/, lang);
for (const s of STEPS) assert.match(html, new RegExp(`<tr id="wp-${s}">.*?</tr>`), s);
assert.equal((html.match(new RegExp(`>${t(lang, "botp.proof.pass")}</td>`, "g")) ?? []).length, STEPS.length, lang);
assert.ok(!html.includes(`>${t(lang, "botp.proof.fail")}<`), lang);
assert.ok(html.includes(t(lang, "bot.bind.done")) && html.includes(t(lang, "bot.denied")) && html.includes(t(lang, "botp.tg.state.bound")), lang);
}
// Настоящие данные не тронуты: слова нет, бот не привязан.
assert.ok(!fs.existsSync(realOwner));
} finally { await new Promise((r) => { app.close(r); app.closeAllConnections?.(); }); }
});
test("BRM-182 Сценарий: проверка проходит шаги с пробелами, регистром, перезапуском и чужим человеком", async () => {
const p = await runWordProof();
assert.equal(p.ok, true);
assert.deepEqual(p.rows.map((r) => r.step), STEPS);
const by = Object.fromEntries(p.rows.map((r) => [r.step, r]));
assert.deepEqual(by.checkOld.got, ["bot.bind.wrong"]);
assert.deepEqual(by.botNew.got, ["bot.bind.done", "botp.tg.state.bound"]);
assert.deepEqual(by.restart.got, ["botp.tg.state.bound", "botp.tg.check.ok", "bot.bind.wrong"]);
assert.deepEqual(by.stranger.got, ["bot.denied", "botp.proof.stillOwner"]);
assert.equal(by.checkSpaces.word.trim(), PROOF_NEW.toLowerCase());
assert.equal(by.checkOld.word, PROOF_OLD);
});
test("BRM-182 Сценарий: шаг не прошёл", async () => {
const p = await runWordProof({ mockBreak: "bot.denied" });
assert.equal(p.ok, false);
assert.equal(p.rows.find((r) => r.step === "stranger").ok, false);
const { proofBox } = await import("../src/bot-page.js");
const html = proofBox("ru", p);
assert.match(html, /<tr id="wp-stranger">.*?не прошло/);
assert.match(html, /id="wordproofsum" class="st off"/);
assert.ok(html.includes("нашла сбой"));
});