brendmanager/test/approval.test.js
2026-11-15 21:54:18 +05:00

86 lines
5.1 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { createApp } from "../src/server.js";
async function withServer(fn) {
const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "brm28-")), "plan.json");
const srv = createApp({ file }).listen(0);
await new Promise((r) => srv.once("listening", r));
const base = `http://127.0.0.1:${srv.address().port}`;
const call = async (m, u, b) => {
const r = await fetch(base + u, { method: m, headers: { "content-type": "application/json" }, body: b && JSON.stringify(b) });
return [r.status, await r.json()];
};
try { await fn(call); } finally { srv.close(); }
}
const mkIdea = async (call) => (await call("POST", "/api/ideas", { title: "Пост", rubric: 1 }))[1].ideas.at(-1).id;
const get = async (call, id) => (await call("GET", "/api/state"))[1].ideas.find((i) => i.id === id);
test("Сценарий: новая запись — черновик", () => withServer(async (call) => {
assert.equal((await get(call, await mkIdea(call))).status, "draft");
}));
test("Сценарий: отправка на согласование, список «Ждёт решения», согласование", () => withServer(async (call) => {
const id = await mkIdea(call);
assert.equal((await call("POST", `/api/ideas/${id}/submit`, { approver: "Никто" }))[0], 400);
let [, s] = await call("POST", `/api/ideas/${id}/submit`, { approver: "Татьяна", by: "Команда" });
assert.equal(s.pending.length, 1);
assert.equal(s.pending[0].approver, "Татьяна");
assert.ok(s.pending[0].submittedAt);
assert.equal((await call("POST", `/api/ideas/${id}/decision`, { action: "approve", by: "Команда" }))[0], 400);
[, s] = await call("POST", `/api/ideas/${id}/decision`, { action: "approve", by: "Татьяна" });
assert.equal(s.pending.length, 0);
const i = s.ideas[0];
assert.equal(i.status, "approved");
assert.deepEqual(i.history.map((h) => [h.from, h.to, h.by]), [["draft", "review", "Команда"], ["review", "approved", "Татьяна"]]);
}));
test("Сценарий: возврат требует комментарий и сохраняет все комментарии", () => withServer(async (call) => {
const id = await mkIdea(call);
for (const c of ["Короче", "Добавьте цифры"]) {
await call("POST", `/api/ideas/${id}/submit`, { approver: "Татьяна" });
assert.equal((await call("POST", `/api/ideas/${id}/decision`, { action: "return", by: "Татьяна", comment: " " }))[0], 400);
await call("POST", `/api/ideas/${id}/decision`, { action: "return", by: "Татьяна", comment: c });
}
const i = await get(call, id);
assert.equal(i.status, "draft");
assert.deepEqual(i.comments.map((c) => c.text), ["Короче", "Добавьте цифры"]);
}));
test("Сценарий: опубликовать без согласования нельзя", () => withServer(async (call) => {
const id = await mkIdea(call);
const [code, d] = await call("POST", `/api/ideas/${id}/publish`, { channel: "telegram", date: "2026-12-05" });
assert.equal(code, 409);
assert.match(d.error, /согласованную/);
}));
test("Сценарий: публикация создаёт запись, второй канал не меняет статус", () => withServer(async (call) => {
const id = await mkIdea(call);
await call("POST", `/api/ideas/${id}/submit`, { approver: "Татьяна" });
await call("POST", `/api/ideas/${id}/decision`, { action: "approve", by: "Татьяна" });
assert.equal((await call("POST", `/api/ideas/${id}/publish`, { channel: "vk", date: "2026-12-05" }))[0], 400);
assert.equal((await call("POST", `/api/ideas/${id}/publish`, { channel: "site", date: "2026-13-05" }))[0], 400);
await call("POST", `/api/ideas/${id}/publish`, { channel: "telegram", date: "2026-12-05" });
await call("POST", `/api/ideas/${id}/publish`, { channel: "linkedin", date: "2026-12-06" });
const [, p] = await call("GET", "/api/publications");
assert.deepEqual(p.items.map((x) => [x.ideaId, x.channel]), [[id, "telegram"], [id, "linkedin"]]);
const i = await get(call, id);
assert.equal(i.status, "published");
assert.equal(i.history.filter((h) => h.to === "published").length, 1);
assert.equal((await call("POST", `/api/ideas/${id}/submit`, { approver: "Татьяна" }))[0], 400);
}));
test("Сценарий: правка согласованной записи возвращает её на согласование", () => withServer(async (call) => {
const id = await mkIdea(call);
await call("POST", `/api/ideas/${id}/submit`, { approver: "Татьяна" });
await call("POST", `/api/ideas/${id}/decision`, { action: "approve", by: "Татьяна" });
await call("PUT", `/api/ideas/${id}`, { title: "Пост (новая версия)" });
const [, s] = await call("GET", "/api/state");
assert.equal(s.ideas[0].status, "review");
assert.equal(s.ideas[0].history.at(-1).by, "system");
assert.equal(s.pending.length, 1);
}));