65 lines
4.1 KiB
JavaScript
65 lines
4.1 KiB
JavaScript
// BRM-140: на «Сводке» причина выключенной отправки написана один раз, у «Остановить/Возобновить» — одна фраза,
|
||
// а «Бот и отчёты» говорят то же самое.
|
||
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 { mockTelegram, createTelegram } from "../src/telegram.js";
|
||
import { TEXTS, LANGS } from "../src/i18n.js";
|
||
|
||
async function withServer(telegram, fn) {
|
||
const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "brm140-")), "plan.json");
|
||
const srv = createApp({ file, telegram }).listen(0);
|
||
await new Promise((r) => srv.once("listening", r));
|
||
const base = `http://127.0.0.1:${srv.address().port}`;
|
||
const page = async (u) => (await (await fetch(base + u)).text()).split("<script>")[0];
|
||
const post = (u) => fetch(base + u, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
|
||
try { await fn(page, post); } finally { srv.close(); }
|
||
}
|
||
const count = (s, sub) => s.split(sub).length - 1;
|
||
const between = (h, a, b) => h.slice(h.indexOf(a), h.indexOf(b, h.indexOf(a)));
|
||
|
||
test("Нет ключа бота: причина на «Сводке» написана один раз, под «Отправить в Telegram»", () =>
|
||
withServer(createTelegram({ token: "", chatId: "" }), async (page) => {
|
||
const h = await page("/summary.html?lang=ru");
|
||
const why = TEXTS.ru["why.tg.key"];
|
||
assert.equal(count(h, why), 1);
|
||
assert.equal(count(h, "Отправка в Telegram выключена"), 1);
|
||
assert.equal(count(h, "ещё не подключён отдельный бот"), 1);
|
||
assert.ok(h.indexOf(why) > h.indexOf('id="send"'), "причина под кнопкой отправки");
|
||
assert.match(h, new RegExp(`<p id="tg" role="status">${why} ${TEXTS.ru["ui.sum.tgCopy"]}</p>`));
|
||
assert.doesNotMatch(h, /id="(stopwhy|resumewhy|sendwhy)"/);
|
||
}));
|
||
|
||
test("У «Остановить отправку» и «Возобновить отправку» одна фраза о том, что они сейчас делают", () =>
|
||
withServer(createTelegram({ token: "", chatId: "" }), async (page, post) => {
|
||
let h = await page("/summary.html?lang=ru");
|
||
const row = between(h, '<button id="stop"', '<h2');
|
||
assert.equal(count(row, "<p "), 1, "одна строка под кнопками");
|
||
assert.ok(row.includes(`<p id="sched" class="hint" role="status">${TEXTS.ru["ui.sum.schedOnNoTg"]}</p>`));
|
||
assert.doesNotMatch(TEXTS.ru["ui.sum.schedOnNoTg"], /ключ бота|отдельный бот|Секреты/, "причину не повторяет");
|
||
assert.match(row, /id="resume"[^>]*title="Расписание уже включено\."/);
|
||
await post("/api/summary/stop");
|
||
h = await page("/summary.html?lang=ru");
|
||
assert.ok(between(h, '<button id="stop"', "<h2").includes(TEXTS.ru["ui.sum.schedOff"]));
|
||
assert.equal(count(h, TEXTS.ru["why.tg.key"]), 1);
|
||
}));
|
||
|
||
test("Telegram подключён: под кнопками фраза про остановку, плашка — «включена»", () =>
|
||
withServer(mockTelegram(), async (page) => {
|
||
const h = await page("/summary.html?lang=ru");
|
||
assert.ok(between(h, '<button id="stop"', "<h2").includes(TEXTS.ru["ui.sum.schedOn"]));
|
||
assert.match(h, new RegExp(`<p id="tg" class="on" role="status">${TEXTS.ru["ui.sum.tgOn"]}</p>`));
|
||
}));
|
||
|
||
test("«Бот и отчёты» называют ту же причину, что и «Сводка»", () =>
|
||
withServer(createTelegram({ token: "", chatId: "" }), async (page) => {
|
||
const why = TEXTS.ru["why.tg.key"];
|
||
assert.ok((await page("/bot.html?lang=ru")).includes(why));
|
||
assert.ok((await page("/summary.html?lang=ru")).includes(why));
|
||
for (const l of LANGS) assert.ok(TEXTS[l]["ui.sum.tgCopy"] && TEXTS[l]["ui.sum.schedOnNoTg"], l);
|
||
for (const l of LANGS) assert.equal(TEXTS[l]["ui.sum.tgOff"], undefined, "старой противоречащей фразы нет");
|
||
}));
|