dika/test/server.test.js
2026-11-16 13:53:19 +05:00

31 lines
1.4 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { createHandler } from "../src/index.js";
test("healthz и API", async () => {
const s = http.createServer(createHandler()).listen(0);
const base = `http://127.0.0.1:${s.address().port}`;
assert.equal((await fetch(base + "/healthz")).status, 200);
assert.equal((await fetch(base + "/api/venues?lat=x&lng=1")).status, 400);
const list = await (await fetch(base + "/api/venues?lat=51.1283&lng=71.4305")).json();
assert.ok(list.length > 0 && list[0].distance <= list[1].distance);
assert.equal((await fetch(base + "/api/locate?q=zzzzzz")).status, 404);
assert.equal((await fetch(base + "/")).status, 200);
s.close();
});
test("Сценарий: страница «Вся база» показывает все заведения", async () => {
const s = http.createServer(createHandler()).listen(0);
const base = `http://127.0.0.1:${s.address().port}`;
assert.equal((await fetch(base + "/base")).status, 200);
const list = await (await fetch(base + "/api/base")).json();
assert.ok(list.length >= 20);
assert.equal(new Set(list.map((v) => v.name + v.address)).size, list.length);
for (const v of list) {
assert.ok(v.name && v.address && Number.isFinite(v.lat) && Number.isFinite(v.lng) && /^\d{4}-\d{2}-\d{2}$/.test(v.checked));
}
assert.ok(list.some((v) => v.lunch === null) || list.every((v) => v.lunch));
s.close();
});