brendmanager/test/server.test.js
Peppa a137ccb960
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 0с
ci / checks (pull_request) Successful in 3s
deploy / deploy (pull_request) Successful in 2s
BRM-13: банк идей и 60-дневный календарь с долями рубрик
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 14:51:37 +05:00

37 lines
1.7 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(), "brm13-")), "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(); }
}
test("Сценарий: добавить, назначить дату, пересчитать, удалить", () => withServer(async (call) => {
assert.equal((await call("GET", "/healthz"))[0], 200);
let [, s] = await call("GET", "/api/state");
await call("PUT", "/api/settings", { start: "2026-12-01" });
assert.equal((await call("POST", "/api/ideas", { title: "", rubric: 1 }))[0], 400);
[, s] = await call("POST", "/api/ideas", { title: "Кейс", rubric: 2 });
assert.equal(s.unscheduled.length, 1);
const id = s.ideas[0].id;
assert.equal((await call("PUT", `/api/ideas/${id}`, { date: "2027-02-01" }))[0], 400);
[, s] = await call("PUT", `/api/ideas/${id}`, { date: "2026-12-10" });
assert.equal(s.unscheduled.length, 0);
assert.equal(s.summary.total, 1);
[, s] = await call("PUT", `/api/ideas/${id}`, { date: null });
assert.equal(s.summary.total, 0);
[, s] = await call("DELETE", `/api/ideas/${id}`);
assert.equal(s.ideas.length, 0);
}));