89 lines
4.0 KiB
JavaScript
89 lines
4.0 KiB
JavaScript
import { test, before } from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import { createBot } from "../src/bot.js";
|
||
import { Store } from "../src/store.js";
|
||
import { buildFinmodelFiles } from "../src/finmodel/build.js";
|
||
import { mockTelegram, testConfig, tmp } from "./helpers.js";
|
||
|
||
let productDir;
|
||
before(async () => {
|
||
productDir = await tmp();
|
||
await buildFinmodelFiles({ productDir, imgDir: await tmp() });
|
||
});
|
||
|
||
const BUYER = { id: 500, first_name: "Анна", username: "anna" };
|
||
const OWNER = { id: 1, first_name: "Владелец" };
|
||
const start = (bot) => bot.handleUpdate({ message: { text: "/start", chat: { id: 500, type: "private" }, from: BUYER } });
|
||
const paid = (bot) => bot.handleUpdate({ callback_query: { id: "c1", data: "paid", from: BUYER, message: { chat: { id: 500 } } } });
|
||
const confirm = (bot, from = OWNER, id = 1) => bot.handleUpdate({ callback_query: { id: "c2", data: `confirm:${id}`, from } });
|
||
|
||
async function setup(opts) {
|
||
const telegram = mockTelegram(opts);
|
||
const store = await new Store(await tmp()).load();
|
||
const config = testConfig({ productDir });
|
||
return { telegram, store, bot: createBot({ telegram, store, config }) };
|
||
}
|
||
|
||
test("Сценарий: бот показывает продукт, цену и реквизиты Kaspi", async () => {
|
||
const { bot, telegram } = await setup();
|
||
await start(bot);
|
||
const text = telegram.sent(500)[0].text;
|
||
assert.match(text, /9 900 ₸/);
|
||
assert.match(text, /Kaspi \+7 700 000 00 00/);
|
||
assert.equal(telegram.sent(500)[0].extra.reply_markup.inline_keyboard[0][0].text, "Я оплатил(а)");
|
||
});
|
||
|
||
test("Сценарий: Успешная покупка", async () => {
|
||
const { bot, telegram } = await setup();
|
||
await paid(bot);
|
||
const ownerMsg = telegram.sent(1)[0];
|
||
assert.match(ownerMsg.text, /заказ №1/);
|
||
assert.match(ownerMsg.text, /Анна/);
|
||
assert.equal(ownerMsg.extra.reply_markup.inline_keyboard[0][0].text, "Подтвердить");
|
||
assert.match(telegram.sent(500).at(-1).text, /Платёж проверяется.*@owner/);
|
||
assert.equal(telegram.calls.filter((c) => c.type === "document").length, 0);
|
||
});
|
||
|
||
test("Сценарий: Владелец подтверждает платёж", async () => {
|
||
const { bot, telegram, store } = await setup();
|
||
await paid(bot);
|
||
await confirm(bot);
|
||
const docs = telegram.calls.filter((c) => c.type === "document");
|
||
assert.deepEqual(docs.map((d) => d.path.split("/").pop()), ["finmodel-example.xlsx", "finmodel-empty.xlsx", "instruction.docx", "license.docx"]);
|
||
assert.ok(docs.every((d) => d.chat === 500));
|
||
assert.match(telegram.sent(500).at(-1).text, /@owner/);
|
||
assert.equal(store.get(1).status, "delivered");
|
||
});
|
||
|
||
test("Сценарий: Подтверждение не от владельца", async () => {
|
||
const { bot, telegram, store } = await setup();
|
||
await paid(bot);
|
||
await confirm(bot, { id: 999 });
|
||
assert.equal(telegram.calls.filter((c) => c.type === "document").length, 0);
|
||
assert.equal(store.get(1).status, "pending");
|
||
});
|
||
|
||
test("Сценарий: Повторное подтверждение", async () => {
|
||
const { bot, telegram } = await setup();
|
||
await paid(bot);
|
||
await confirm(bot);
|
||
await confirm(bot);
|
||
assert.equal(telegram.calls.filter((c) => c.type === "document").length, 4);
|
||
});
|
||
|
||
test("Граница: двойное нажатие «Я оплатил(а)» не создаёт второй заказ", async () => {
|
||
const { bot, telegram, store } = await setup();
|
||
await paid(bot);
|
||
await paid(bot);
|
||
assert.equal(store.state.orders.length, 1);
|
||
assert.equal(telegram.sent(1).length, 1);
|
||
});
|
||
|
||
test("Ошибка отправки файлов: заказ остаётся ожидающим, владелец предупреждён", async () => {
|
||
const { bot, telegram, store } = await setup({ failDocuments: true });
|
||
await paid(bot);
|
||
await confirm(bot);
|
||
assert.equal(store.get(1).status, "pending");
|
||
assert.match(telegram.sent(1).at(-1).text, /Не удалось отправить/);
|
||
});
|