shablony-prodavanam/test/bot.test.js
Peppa 1be1380e87
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 3с
peppa/build build: success за 1с
ci / checks (pull_request) Successful in 9s
deploy / deploy (pull_request) Successful in 13s
SP-4: отклонение оплаты и список заявок для владельца
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 16:37:09 +05:00

124 lines
5.6 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.

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, /Не удалось отправить/);
});
const reject = (bot, from = OWNER, id = 1) => bot.handleUpdate({ callback_query: { id: "c3", data: `reject:${id}`, from } });
const orders = (bot, from = OWNER) => bot.handleUpdate({ message: { text: "/orders", chat: { id: from.id, type: "private" }, from } });
test("Сценарий: Владелец отклоняет платёж — покупатель получает контакт, файлы не уходят", async () => {
const { bot, telegram, store } = await setup();
await paid(bot);
await reject(bot);
assert.equal(store.get(1).status, "rejected");
assert.match(telegram.sent(500).at(-1).text, /@owner/);
assert.equal(telegram.calls.filter((c) => c.type === "document").length, 0);
await confirm(bot);
assert.equal(telegram.calls.filter((c) => c.type === "document").length, 0);
});
test("Граница: отклонить может только владелец", async () => {
const { bot, store } = await setup();
await paid(bot);
await reject(bot, { id: 999 });
assert.equal(store.get(1).status, "pending");
});
test("Сценарий: Владелец видит список заявок со статусами", async () => {
const { bot, telegram } = await setup();
await orders(bot);
assert.match(telegram.sent(1).at(-1).text, /пока нет/);
await paid(bot);
await orders(bot);
assert.match(telegram.sent(1).at(-1).text, /№1 .*ожидает/);
await confirm(bot);
await orders(bot);
assert.match(telegram.sent(1).at(-1).text, /№1 .*подтверждено/);
await orders(bot, { id: 999 });
assert.equal(telegram.sent(999).length, 0);
});