import { afterEach, describe, expect, it } from "vitest"; import type { FastifyInstance } from "fastify"; import { buildApp } from "../src/app.ts"; import { PRODUCTS } from "../src/modules/catalog/products.ts"; let app: FastifyInstance; afterEach(async () => app?.close()); describe("Requirement: Каталог продуктов в JSON", () => { it("Scenario: Получение каталога", async () => { app = await buildApp(); const res = await app.inject({ method: "GET", url: "/api/products" }); expect(res.statusCode).toBe(200); expect(res.headers["content-type"]).toContain("application/json"); const items = res.json() as { id: string; status: string }[]; expect(items).toHaveLength(6); expect(items.map((p) => p.id)).toEqual([ "digital-toolkits", "content-hub", "express-audit", "skill-coach", "hr-screening", "billing", ]); expect(items.find((p) => p.id === "express-audit")?.status).toBe("coming_soon"); for (const p of items) { expect(Object.keys(p).sort()).toEqual(["id", "pricing", "status", "summary", "title"]); } }); it("Scenario: Неизвестный продукт", async () => { app = await buildApp(); const res = await app.inject({ method: "GET", url: "/api/products/unknown" }); expect(res.statusCode).toBe(404); expect(res.json()).toEqual({ error: "not_found" }); }); it.each(["__proto__", "constructor", "toString"])("служебное имя %s — 404", async (id) => { app = await buildApp(); const res = await app.inject({ method: "GET", url: `/api/products/${id}` }); expect(res.statusCode).toBe(404); }); it.each([ ["digital-toolkits", "Цифровые комплекты"], ["express-audit", "Экспресс-аудит диалогов"], ["skill-coach", "AI-тренажёр диалогов"], ["hr-screening", "Тестирование кандидатов"], ])("Scenario Outline: Карточка продукта (%s)", async (id, title) => { app = await buildApp(); const res = await app.inject({ method: "GET", url: `/api/products/${id}` }); expect(res.statusCode).toBe(200); expect(res.json().title).toBe(title); }); }); describe("Requirement: Лендинг со списком продуктов", () => { it("Scenario: Посетитель открывает главную", async () => { app = await buildApp(); const res = await app.inject({ method: "GET", url: "/" }); expect(res.statusCode).toBe(200); expect(res.headers["content-type"]).toContain("text/html"); expect(res.body).toContain("Клиентология"); expect(res.body).toContain("Экспресс-аудит диалогов"); expect(res.body).toContain("Скоро"); expect(res.body).not.toMatch(/
{ app = await buildApp({ catalog: { products: [ { id: "x\">", title: "", summary: "a & b", pricing: "'q'", status: "available", }, ], }, }); const res = await app.inject({ method: "GET", url: "/" }); expect(res.body).toContain("<script>"); expect(res.body).not.toContain("