shablony-prodavanam/test/autopay.test.js
Peppa 4172c24daf
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 3с
peppa/build build: success за 1с
ci / checks (pull_request) Successful in 10s
deploy / deploy (pull_request) Successful in 13s
SP-5: автоподтверждение оплаты Kaspi (мок-провайдер), заключение по ApiPay
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 16:39:08 +05:00

119 lines
5.1 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 { mockAutopayProvider, normalizePhone } from "../src/autopay.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 mkBuyer = (id) => ({ id, first_name: `Покупатель${id}`, username: `b${id}` });
const OWNER = { id: 1 };
async function setup(extra = {}) {
const telegram = mockTelegram();
const store = await new Store(await tmp()).load();
const autopay = mockAutopayProvider();
const config = testConfig({ productDir, autopayStuckMs: 15 * 60 * 1000, ...extra });
const bot = createBot({ telegram, store, config, autopay });
const buy = async (id = 500, phone = "+7 700 111 22 33") => {
const from = mkBuyer(id);
await bot.handleUpdate({ callback_query: { id: "a", data: "auto", from, message: { chat: { id } } } });
await bot.handleUpdate({ message: { text: phone, chat: { id, type: "private" }, from } });
};
return { telegram, store, autopay, bot, buy };
}
const docs = (t, chat) => t.calls.filter((c) => c.type === "document" && String(c.chat) === String(chat)).length;
test("Нормализация телефона", () => {
assert.equal(normalizePhone("+7 (700) 111-22-33"), "77001112233");
assert.equal(normalizePhone("87001112233"), "77001112233");
assert.equal(normalizePhone("abc"), null);
});
test("Сценарий: Успешная автооплата", async () => {
const { bot, telegram, store, autopay, buy } = await setup();
await buy();
assert.equal(store.get(1).status, "invoiced");
autopay.settle("mock-inv-1", { status: "paid", paymentId: "P1" });
await bot.checkInvoices();
assert.equal(store.get(1).status, "delivered");
assert.ok(docs(telegram, 500) >= 1);
});
test("Сценарий: Платёж не прошёл", async () => {
const { bot, telegram, store, autopay, buy } = await setup();
await buy();
autopay.settle("mock-inv-1", { status: "expired" });
await bot.checkInvoices();
assert.equal(store.get(1).status, "failed");
assert.equal(docs(telegram, 500), 0);
assert.match(telegram.sent(500).at(-1).text, /@owner/);
});
test("Сценарий: Платёж уже использован", async () => {
const { bot, telegram, store, autopay, buy } = await setup();
await buy(500);
await buy(501);
autopay.settle("mock-inv-1", { status: "paid", paymentId: "P1" });
autopay.settle("mock-inv-2", { status: "paid", paymentId: "P1" });
await bot.checkInvoices();
assert.equal(store.get(1).status, "delivered");
assert.equal(store.get(2).status, "pending");
assert.equal(docs(telegram, 501), 0);
assert.match(telegram.sent(1).at(-1).text, /другому заказу/);
});
test("Сценарий: Неверная сумма", async () => {
const { bot, telegram, store, autopay, buy } = await setup();
await buy();
autopay.settle("mock-inv-1", { status: "paid", paymentId: "P9", amount: 100 });
await bot.checkInvoices();
assert.equal(store.get(1).status, "pending");
assert.equal(docs(telegram, 500), 0);
});
test("Сценарий: Повторный опрос не выдаёт файл дважды", async () => {
const { bot, telegram, autopay, buy } = await setup();
await buy();
autopay.settle("mock-inv-1", { status: "paid", paymentId: "P1" });
await bot.checkInvoices();
const n = docs(telegram, 500);
await bot.checkInvoices();
assert.equal(docs(telegram, 500), n);
});
test("Сценарий: Платёж завис — владелец подтверждает вручную", async () => {
const { bot, telegram, store, buy } = await setup();
await buy();
await bot.checkInvoices(Date.now() + 16 * 60 * 1000);
assert.equal(store.get(1).status, "pending");
assert.ok(telegram.sent(1).at(-1).extra.reply_markup.inline_keyboard[0].some((b) => b.callback_data === "confirm:1"));
await bot.handleUpdate({ callback_query: { id: "c", data: "confirm:1", from: OWNER } });
assert.equal(store.get(1).status, "delivered");
assert.ok(docs(telegram, 500) >= 1);
});
test("Сценарий: Сервис не отвечает — один раз уведомляем владельца", async () => {
const { bot, telegram, autopay, buy } = await setup();
await buy();
autopay.down = true;
await bot.checkInvoices();
await bot.checkInvoices();
assert.equal(telegram.sent(1).filter((c) => /не отвечает/.test(c.text)).length, 1);
});
test("Сценарий: Автоплатёж выключен", async () => {
const telegram = mockTelegram();
const store = await new Store(await tmp()).load();
const bot = createBot({ telegram, store, config: testConfig({ productDir }) });
await bot.handleUpdate({ message: { text: "/start", chat: { id: 500, type: "private" }, from: mkBuyer(500) } });
const buttons = telegram.sent(500)[0].extra.reply_markup.inline_keyboard.flat();
assert.ok(!buttons.some((b) => b.callback_data === "auto"));
});