53 lines
2.8 KiB
TypeScript
53 lines
2.8 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { createLogger, createTestDb, testDatabaseUrl, type TestDb } from "khb_common";
|
|
import { tick, type DispatchResult } from "./tick.js";
|
|
import type { JobSettings } from "./settings.js";
|
|
|
|
const url = testDatabaseUrl();
|
|
const d = url ? describe : describe.skip;
|
|
const log = createLogger("test", "silent");
|
|
|
|
const job: JobSettings = { name: "llmtest-job", at: "06:00", days: "daily", call: { service: "svc", method: "POST", path: "/runs" } };
|
|
|
|
d("тик планировщика (временная БД)", () => {
|
|
let db: TestDb;
|
|
let calls: string[];
|
|
const mk = (now: Date, dispatch?: () => Promise<DispatchResult>) => ({
|
|
pool: db.pool, jobs: [job], tz: "America/New_York", graceSeconds: 60, now: () => now,
|
|
nonWorkdays: async () => new Set<string>(), log,
|
|
dispatch: dispatch ?? (async () => { calls.push("call"); return { httpStatus: 202, responseRef: "ref-1" }; }),
|
|
});
|
|
beforeAll(async () => { db = await createTestDb(url!); });
|
|
afterAll(async () => { await db?.drop(); });
|
|
|
|
it("в нужное время стартует задание и пишет результат", async () => {
|
|
calls = [];
|
|
await Promise.all(await tick(mk(new Date("2026-10-11T10:00:10Z"))));
|
|
expect(calls).toHaveLength(1);
|
|
const { rows } = await db.pool.query("select status, http_status, response_ref from sched_run");
|
|
expect(rows).toEqual([{ status: "ok", http_status: 202, response_ref: "ref-1" }]);
|
|
});
|
|
|
|
it("повторный тик в ту же минуту не запускает задание снова", async () => {
|
|
calls = [];
|
|
await Promise.all(await tick(mk(new Date("2026-10-11T10:00:20Z"))));
|
|
expect(calls).toHaveLength(0);
|
|
expect((await db.pool.query("select count(*)::int n from sched_run")).rows[0].n).toBe(1);
|
|
});
|
|
|
|
it("пропущенное задание не запускается, момент обнаружения логируется", async () => {
|
|
calls = [];
|
|
await Promise.all(await tick(mk(new Date("2026-10-12T10:07:00Z")))); // сервис "проснулся" через 7 минут после 06:00
|
|
expect(calls).toHaveLength(0);
|
|
const { rows } = await db.pool.query("select status, message from sched_run where scheduled_minute = '2026-10-12T10:00:00Z'");
|
|
expect(rows[0].status).toBe("missed");
|
|
expect(rows[0].message).toContain("пропуск замечен");
|
|
});
|
|
|
|
it("ошибка вызова фиксируется как error", async () => {
|
|
await Promise.all(await tick(mk(new Date("2026-10-13T10:00:05Z"), async () => ({ httpStatus: 500, message: "boom" }))));
|
|
const { rows } = await db.pool.query("select status, message from sched_run where scheduled_minute = '2026-10-13T10:00:00Z'");
|
|
expect(rows[0]).toEqual({ status: "error", message: "boom" });
|
|
});
|
|
});
|