63 lines
3.8 KiB
JavaScript
63 lines
3.8 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";
|
|
import { view, STAGES } from "../src/opportunities.js";
|
|
|
|
async function withServer(fn, dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm15-"))) {
|
|
const srv = createApp({ file: path.join(dir, "plan.json") }).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(); }
|
|
}
|
|
|
|
test("Сценарий: создание, перезапуск, правка, смена стадии, удаление", async () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "brm15-"));
|
|
let id;
|
|
await withServer(async (call) => {
|
|
let [c, o] = await call("POST", "/api/opportunities", { title: "Podcast Hub", organizer: "Анна", deadline: "2026-12-01" });
|
|
assert.equal(c, 201); assert.equal(o.stage, "Найдена"); id = o.id;
|
|
}, dir);
|
|
await withServer(async (call) => {
|
|
let [, l] = await call("GET", "/api/opportunities");
|
|
assert.equal(l.items.length, 1);
|
|
let [c, o] = await call("PUT", `/api/opportunities/${id}`, { stage: "Заявка отправлена" });
|
|
assert.equal(c, 200); assert.equal(o.stage, "Заявка отправлена"); assert.equal(o.title, "Podcast Hub");
|
|
assert.equal((await call("DELETE", `/api/opportunities/${id}`))[0], 200);
|
|
assert.equal((await call("GET", `/api/opportunities/${id}`))[0], 404);
|
|
}, dir);
|
|
});
|
|
|
|
test("Сценарий: валидация — пустое название, неизвестная стадия, дата, ссылка", () => withServer(async (call) => {
|
|
assert.equal((await call("POST", "/api/opportunities", { title: " " }))[0], 400);
|
|
assert.equal((await call("POST", "/api/opportunities", { title: "X", stage: "Выдумана" }))[0], 400);
|
|
assert.equal((await call("POST", "/api/opportunities", { title: "X", deadline: "2026-13-40" }))[0], 400);
|
|
assert.equal((await call("POST", "/api/opportunities", { title: "X", url: "javascript:alert(1)" }))[0], 400);
|
|
const [, o] = await call("POST", "/api/opportunities", { title: "X" });
|
|
assert.equal((await call("PUT", `/api/opportunities/${o.id}`, { stage: "Выдумана" }))[0], 400);
|
|
assert.equal((await call("GET", `/api/opportunities/${o.id}`))[1].stage, "Найдена");
|
|
assert.equal((await call("GET", "/api/opportunities"))[1].items.length, 1);
|
|
}));
|
|
|
|
test("Сценарий: порядок, просрочка и завершённые стадии", () => {
|
|
const it = (id, deadline, stage = "Найдена") => ({ id, deadline, stage });
|
|
const v = view([it(1, null), it(2, "2026-12-01"), it(3, "2026-11-01"), it(4, "2026-11-01", "Отклонена/неактуальна"), it(5, "2026-11-01", "Выступление состоялось")], "2026-11-15");
|
|
assert.deepEqual(v.map((x) => x.id), [3, 4, 5, 2, 1]);
|
|
assert.deepEqual(v.map((x) => x.overdue), [true, false, false, false, false]);
|
|
assert.equal(STAGES.length, 6);
|
|
});
|
|
|
|
test("Сценарий: фильтр по стадии и страница", () => withServer(async (call) => {
|
|
await call("POST", "/api/opportunities", { title: "A" });
|
|
await call("POST", "/api/opportunities", { title: "B", stage: "Ответ получен" });
|
|
const [, l] = await call("GET", "/api/opportunities?stage=" + encodeURIComponent("Ответ получен"));
|
|
assert.deepEqual(l.items.map((x) => x.title), ["B"]);
|
|
assert.equal((await call("GET", "/api/opportunities/meta"))[1].stages.length, 6);
|
|
}));
|