freetravel/test/server.test.ts
2026-11-14 22:50:18 +05:00

88 lines
3.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { emptyCatalog } from "../src/domain/internship.js";
import { mockSearchProvider } from "../src/search/mockSearchProvider.js";
import { SearchService } from "../src/search/searchService.js";
import { buildServer } from "../src/server.js";
import { JsonCatalogStore } from "../src/store/catalogStore.js";
import { MemoryStore } from "./helpers.js";
async function setup(searchToken?: string, delayMs = 0) {
const store = new MemoryStore();
const search = new SearchService({ store, provider: mockSearchProvider({ delayMs }) });
const app = await buildServer({ store, search, searchToken, providerName: "mock" });
return { app, store, search };
}
describe("HTTP API", () => {
it("GET /healthz → 200", async () => {
const { app } = await setup();
const response = await app.inject({ method: "GET", url: "/healthz" });
expect(response.statusCode).toBe(200);
});
it("GET / отдаёт страницу", async () => {
const { app } = await setup();
const response = await app.inject({ method: "GET", url: "/" });
expect(response.statusCode).toBe(200);
expect(response.body).toContain("Найти новые");
});
it("Scenario: Запуск без токена → 401, поиск не начинается", async () => {
const { app, search } = await setup("s3cret");
const response = await app.inject({ method: "POST", url: "/api/search" });
expect(response.statusCode).toBe(401);
expect(response.json()).toEqual({ error: "Неверный токен" });
expect(search.running).toBe(false);
});
it("Scenario: Запуск с верным токеном → 202", async () => {
const { app, search } = await setup("s3cret", 20);
const response = await app.inject({
method: "POST",
url: "/api/search",
headers: { authorization: "Bearer s3cret" },
});
expect(response.statusCode).toBe(202);
expect(search.running).toBe(true);
const status = await app.inject({ method: "GET", url: "/api/search/status" });
expect(status.json()).toMatchObject({ state: "running", provider: "mock", tokenRequired: true });
});
it("повторный запуск во время поиска → 409 «Поиск уже идёт»", async () => {
const { app } = await setup(undefined, 50);
await app.inject({ method: "POST", url: "/api/search" });
const second = await app.inject({ method: "POST", url: "/api/search" });
expect(second.statusCode).toBe(409);
expect(second.json()).toEqual({ error: "Поиск уже идёт" });
});
it("после поиска GET /api/internships отдаёт новые карточки и отчёт", async () => {
const { app, search } = await setup();
await app.inject({ method: "POST", url: "/api/search" });
while (search.running) await new Promise((resolve) => setTimeout(resolve, 5));
const body = (await app.inject({ method: "GET", url: "/api/internships" })).json();
expect(body.internships).toHaveLength(3);
expect(body.internships.every((card: { isNew: boolean }) => card.isNew)).toBe(true);
expect(body.lastRun).toMatchObject({ status: "ok", found: 3, added: 3 });
});
});
describe("JsonCatalogStore", () => {
let dir = "";
afterEach(async () => {
if (dir) await rm(dir, { recursive: true, force: true });
});
it("возвращает пустой каталог без файла и читает записанное", async () => {
dir = await mkdtemp(path.join(tmpdir(), "frtvl-"));
const store = new JsonCatalogStore(path.join(dir, "nested"));
expect(await store.load()).toEqual(emptyCatalog());
const catalog = { ...emptyCatalog(), lastSuccessfulRunId: "run-1" };
await store.save(catalog);
expect(await store.load()).toEqual(catalog);
});
});