brendmanager/test/card.test.js
Peppa 26ea6eec97
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 0с
ci / checks (pull_request) Successful in 4s
deploy / deploy (pull_request) Successful in 16s
BRM-9: карточка личного бренда (аудитория, позиционирование, 3 темы, соцсети)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 14:39:19 +05:00

102 lines
4.4 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.

import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createApp } from "../src/server.js";
import { createStore } from "../src/store.js";
const valid = () => ({
audience: "Молодые эксперты",
positioning: "Объясняю просто",
topics: ["Личный бренд", "Выступления", "Карьера"],
socials: [],
});
async function start(dir) {
const server = createApp(createStore(dir ?? (await mkdtemp(join(tmpdir(), "brm-")))));
await new Promise((r) => server.listen(0, r));
const base = `http://127.0.0.1:${server.address().port}`;
const put = (body) => fetch(`${base}/api/card`, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
return { server, base, put, get: () => fetch(`${base}/api/card`).then((r) => r.json()) };
}
test("Сценарий «Пустая карточка»: пустые блоки, не ошибка", async () => {
const { server, base, get } = await start();
assert.deepEqual(await get(), { audience: "", positioning: "", topics: [], socials: [] });
const page = await fetch(base);
assert.equal(page.status, 200);
assert.match(await page.text(), /Аудитория/);
server.close();
});
test("Сценарий «Сохранённая карточка после обновления» (и после перезапуска)", async () => {
const dir = await mkdtemp(join(tmpdir(), "brm-"));
const a = await start(dir);
assert.equal((await a.put(valid())).status, 200);
a.server.close();
const b = await start(dir);
assert.equal((await b.get()).audience, "Молодые эксперты");
b.server.close();
});
test("Сценарий «Пустое позиционирование»", async () => {
const { server, put, get } = await start();
const res = await put({ ...valid(), positioning: " " });
assert.equal(res.status, 400);
assert.equal((await res.json()).errors.positioning, "Позиционирование: не заполнено");
assert.equal((await get()).audience, "");
server.close();
});
test("Сценарий «Слишком длинный текст»", async () => {
const { server, put } = await start();
const res = await put({ ...valid(), audience: "а".repeat(1001) });
assert.equal((await res.json()).errors.audience, "Аудитория: не больше 1000 знаков");
assert.equal((await put({ ...valid(), audience: "а".repeat(1000) })).status, 200);
server.close();
});
for (const n of [0, 2, 4]) {
test(`Сценарий «Неверное число тем»: ${n}`, async () => {
const { server, put } = await start();
const res = await put({ ...valid(), topics: ["тема"].concat(Array(5).fill("т")).slice(0, n).map((t, i) => `${t}${i}`) });
assert.equal(res.status, 400);
assert.equal((await res.json()).errors.topics, "Темы: нужно ровно три");
server.close();
});
}
test("Сценарий «Три темы» и «Карточка без соцсетей»", async () => {
const { server, put } = await start();
const res = await put(valid());
assert.equal(res.status, 200);
assert.deepEqual((await res.json()).socials, []);
server.close();
});
test("Сценарий «Добавление и удаление соцсети»", async () => {
const { server, put, get } = await start();
await put({ ...valid(), socials: [{ platform: "Telegram", url: "https://t.me/example" }] });
assert.equal((await get()).socials.length, 1);
await put(valid());
assert.deepEqual((await get()).socials, []);
server.close();
});
test("Сценарий «Ссылка не похожа на ссылку»", async () => {
const { server, put } = await start();
const res = await put({ ...valid(), socials: [{ platform: "Telegram", url: "просто текст" }] });
assert.equal(res.status, 400);
assert.equal((await res.json()).errors.socials, "Соцсети: ссылка должна начинаться с http:// или https://");
server.close();
});
test("healthz и некорректный JSON", async () => {
const { server, base } = await start();
assert.equal((await fetch(`${base}/healthz`)).status, 200);
const res = await fetch(`${base}/api/card`, { method: "PUT", body: "{" });
assert.equal(res.status, 400);
server.close();
});