import { test } from "node:test"; import assert from "node:assert/strict"; import { once } from "node:events"; import { createApp } from "../src/server.js"; import { testConfig, tmp } from "./helpers.js"; import { writeFile, mkdir } from "node:fs/promises"; import { join } from "node:path"; async function withServer(config, fn) { const server = createApp(config).listen(0); await once(server, "listening"); try { await fn(`http://127.0.0.1:${server.address().port}`); } finally { server.close(); } } test("Сценарий: Посетитель открывает лендинг", async () => { await withServer(testConfig(), async (base) => { const html = await (await fetch(`${base}/`)).text(); assert.match(html, /9 900 ₸/); assert.ok((html.match(/= 3); assert.match(html, /href="https:\/\/t\.me\/demo_bot\?start=site"[^>]*>Купить/); assert.doesNotMatch(html, /USD|\$/); assert.doesNotMatch(html, /href="\/offer"/); }); }); test("Сценарий: Проверка живости", async () => { await withServer(testConfig(), async (base) => { assert.equal((await fetch(`${base}/healthz`)).status, 200); }); }); test("Сценарий: Документы не согласованы", async () => { await withServer(testConfig({ legalApproved: false }), async (base) => { assert.equal((await fetch(`${base}/offer`)).status, 404); assert.equal((await fetch(`${base}/refund`)).status, 404); }); }); test("Граница: черновик с пометками «ЗАПОЛНИТЬ» не публикуется даже при LEGAL_APPROVED=1", async () => { await withServer(testConfig({ legalApproved: true }), async (base) => { assert.equal((await fetch(`${base}/offer`)).status, 404); }); }); test("Сценарий: Документы согласованы", async () => { const dir = await tmp(); await mkdir(dir, { recursive: true }); await writeFile(join(dir, "offer.md"), "# Оферта\n\nТекст оферты\n"); await writeFile(join(dir, "refund.md"), "# Возврат\n"); await withServer(testConfig({ legalApproved: true, legalDir: dir }), async (base) => { assert.match(await (await fetch(`${base}/offer`)).text(), /Текст оферты/); const html = await (await fetch(`${base}/`)).text(); assert.match(html, /href="\/offer"/); assert.match(html, /href="\/refund"/); }); }); test("Граница: путь к картинке не выходит из каталога", async () => { await withServer(testConfig(), async (base) => { assert.equal((await fetch(`${base}/img/..%2Fpackage.json`)).status, 404); }); });