brendmanager/test/deploy-status.test.js
Peppa 5b4a215ca0
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 1с
ci / checks (pull_request) Successful in 5s
deploy / deploy (pull_request) Successful in 22s
BRM-32: статус автовыкладки на главной версии для проверки
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 12:25:54 +05:00

59 lines
3.5 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 { almatyTime } from "../src/deploy-status.js";
async function withServer(fn, secret = "s3") {
const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "brm32-")), "plan.json");
const start = () => createApp({ file, deploySecret: secret }).listen(0);
let srv = start();
await new Promise((r) => srv.once("listening", r));
const call = async (m, u, b, h = {}) => {
const r = await fetch(`http://127.0.0.1:${srv.address().port}${u}`, { method: m, headers: { "content-type": "application/json", ...h }, body: b && JSON.stringify(b) });
return [r.status, await r.json()];
};
const restart = async () => { srv.close(); srv = start(); await new Promise((r) => srv.once("listening", r)); };
try { await fn(call, restart); } finally { srv.close(); }
}
const tok = { "x-deploy-token": "s3" };
test("BRM-32: время по Алматы (UTC+5)", () => assert.equal(almatyTime("2026-11-16T20:30:00Z"), "17.11.2026 01:30"));
test("BRM-32 Сценарий: успешная выкладка видна на главной", () => withServer(async (call) => {
assert.equal((await call("GET", "/api/deploy-status"))[1].last, null);
const [code, v] = await call("POST", "/api/deploy-status", { status: "ok", task: "BRM-32", title: "Автовыкладка" }, tok);
assert.equal(code, 200);
assert.equal(v.last.task, "BRM-32");
assert.equal(v.shown.title, "Автовыкладка");
assert.match(v.last.atText, /^\d\d\.\d\d\.\d{4} \d\d:\d\d$/);
}));
test("BRM-32 Сценарий: сбой сохраняет прежнюю рабочую версию, следующая задача возвращает «успешно»", () => withServer(async (call, restart) => {
await call("POST", "/api/deploy-status", { status: "ok", task: "BRM-31", title: "Первая ссылка" }, tok);
await call("POST", "/api/deploy-status", { status: "failed", task: "BRM-32", title: "Автовыкладка", reason: "не удалось собрать новую версию" }, tok);
await restart();
let [, v] = await call("GET", "/api/deploy-status");
assert.equal(v.last.status, "failed");
assert.equal(v.last.reason, "не удалось собрать новую версию");
assert.equal(v.shown.task, "BRM-31");
await call("POST", "/api/deploy-status", { status: "ok", task: "BRM-33", title: "Следующая" }, tok);
[, v] = await call("GET", "/api/deploy-status");
assert.equal(v.last.status, "ok");
assert.equal(v.shown.task, "BRM-33");
}));
test("BRM-32 Сценарий: чужой запрос и неверные данные отклоняются", () => withServer(async (call) => {
assert.equal((await call("POST", "/api/deploy-status", { status: "ok" }))[0], 403);
assert.equal((await call("POST", "/api/deploy-status", { status: "ok" }, { "x-deploy-token": "bad" }))[0], 403);
assert.equal((await call("POST", "/api/deploy-status", { status: "maybe" }, tok))[0], 400);
assert.equal((await call("POST", "/api/deploy-status", { status: "ok", task: "<x>" }, tok))[0], 400);
assert.equal((await call("GET", "/api/deploy-status"))[1].last, null);
}));
test("BRM-32: без ключа на сервере запись закрыта", () => withServer(async (call) => {
assert.equal((await call("POST", "/api/deploy-status", { status: "ok" }, { "x-deploy-token": "" }))[0], 403);
}, ""));