peppa/bench/tasks/03-bugfix/checks/check.mjs
2026-11-17 02:09:17 +05:00

61 lines
2.6 KiB
JavaScript
Raw 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.

// BENCH-HIDDEN — скрытые проверки задачи 3 (заложенная ошибка: порог бесплатной доставки).
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
const BUGGY = readFileSync(new URL("./buggy-cart.mjs.txt", import.meta.url), "utf8");
async function q(t, items, promo) {
const r = await t.app.post("/api/quote", { items, promo });
t.assert.equal(r.status, 200, `POST /api/quote ответил ${r.status}: ${r.text.slice(0, 200)}`);
const b = r.json();
return [b.subtotal, b.discount, b.delivery, b.total];
}
export default [
{
name: "заказ ровно на 5 000 ₸ — доставка бесплатно",
async run(t) {
t.assert.deepEqual(await q(t, [{ sku: "rose", qty: 2 }, { sku: "wrap", qty: 1 }]), [5000, 0, 0, 5000]);
},
},
{
name: "заказ на 4 900 ₸ — доставка 1 000 ₸",
async run(t) {
t.assert.deepEqual(await q(t, [{ sku: "vase", qty: 1 }]), [4900, 0, 1000, 5900]);
},
},
{
name: "порог доставки считается после скидки",
async run(t) {
// SPRING10: 5 700 → 5 130 (бесплатно); 5 500 → 4 950 (платно).
t.assert.deepEqual(await q(t, [{ sku: "peony", qty: 2 }, { sku: "tulip", qty: 1 }], "SPRING10"), [5700, 570, 0, 5130]);
t.assert.deepEqual(await q(t, [{ sku: "wrap", qty: 2 }, { sku: "rose", qty: 1 }], "SPRING10"), [5500, 550, 1000, 5950]);
},
},
{
name: "остальной расчёт не изменился",
async run(t) {
t.assert.deepEqual(await q(t, [{ sku: "tulip", qty: 3 }]), [2100, 0, 1000, 3100]);
t.assert.deepEqual(await q(t, [{ sku: "peony", qty: 2 }], "spring10"), [5000, 500, 1000, 5500]);
t.assert.deepEqual(await q(t, [{ sku: "peony", qty: 4 }], "FRIEND15"), [10000, 1500, 0, 8500]);
const bad = await t.app.post("/api/quote", { items: [{ sku: "rose", qty: 1 }], promo: "XXX" });
t.assert.equal(bad.status, 400);
},
},
{
name: "тесты проекта зелёные",
async run(t) {
const r = await t.run("node", ["--test", "product/test/"]);
t.assert.equal(r.code, 0, `тесты упали:\n${r.out.slice(-1500)}`);
},
},
{
name: "тест ловит эту ошибку, если её вернуть",
async run(t) {
writeFileSync(join(t.workdir, "product", "cart.mjs"), BUGGY);
const r = await t.run("node", ["--test", "product/test/"]);
t.assert.notEqual(r.code, 0, "со старым расчётом все тесты зелёные — ошибка вернётся незаметно");
},
},
];