Files
khb_microservices/packages/khb_common/src/common.test.ts
Yuri Yaryshev ebb1cb7950
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped
Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 09:44:55 +03:00

61 lines
3.2 KiB
TypeScript
Raw 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 { describe, expect, it } from "vitest";
import { addWorkDays, isWorkday, todayIn, weekBounds, workDaysBetween } from "./calendar/index.js";
import { evaluateFreshness, type FreshnessRow } from "./mirror-access/index.js";
describe("calendar", () => {
const holidays = new Set(["2026-11-26"]); // четверг (День благодарения)
it("выходные и праздники - нерабочие", () => {
expect(isWorkday("2026-10-10", holidays)).toBe(false); // суббота
expect(isWorkday("2026-11-26", holidays)).toBe(false);
expect(isWorkday("2026-10-12", holidays)).toBe(true); // понедельник
});
it("addWorkDays пропускает выходные и праздники", () => {
expect(addWorkDays("2026-10-09", 1, holidays)).toBe("2026-10-12"); // пт -> пн
expect(addWorkDays("2026-11-25", 1, holidays)).toBe("2026-11-27"); // ср -> пт через праздник
expect(addWorkDays("2026-10-10", 0, holidays)).toBe("2026-10-12");
});
it("workDaysBetween", () => {
expect(workDaysBetween("2026-10-09", "2026-10-16", holidays)).toBe(5);
expect(workDaysBetween("2026-10-16", "2026-10-09", holidays)).toBe(0);
});
it("todayIn учитывает часовой пояс Tampa", () => {
expect(todayIn(new Date("2026-10-11T02:00:00Z"))).toBe("2026-10-10"); // ещё вечер 10-го в Tampa
});
it("weekBounds: пн-вс", () => {
expect(weekBounds("2026-10-14")).toEqual({ start: "2026-10-12", end: "2026-10-18" });
});
});
describe("evaluateFreshness", () => {
const now = new Date("2026-10-11T12:00:00Z");
const row = (table_name: string, minutesAgo: number, status: "ok" | "error" = "ok", error_message: string | null = null): FreshnessRow => ({
table_name,
snapshot_ts: new Date(now.getTime() - minutesAgo * 60000),
status,
error_message,
});
it("свежая таблица без ошибки - ok", () => {
expect(evaluateFreshness([row("jt_task", 30)], ["jt_task"], now, 90)[0]!.ok).toBe(true);
});
it("таблица без снапшота - no_snapshot", () => {
expect(evaluateFreshness([], ["jt_task"], now, 90)[0]).toMatchObject({ ok: false, reason: "no_snapshot" });
});
it("устаревшая - stale", () => {
expect(evaluateFreshness([row("jt_task", 120)], ["jt_task"], now, 90)[0]).toMatchObject({ ok: false, reason: "stale" });
});
it("используется последний снапшот: старая ошибка, новая успешная загрузка - ok", () => {
const rows = [row("jt_task", 100, "error", "boom"), row("jt_task", 10)];
expect(evaluateFreshness(rows, ["jt_task"], now, 90)[0]!.ok).toBe(true);
});
it("последний снапшот с ошибкой - error", () => {
const rows = [row("jt_task", 100), row("jt_task", 10, "error", "boom")];
expect(evaluateFreshness(rows, ["jt_task"], now, 90)[0]).toMatchObject({ ok: false, reason: "error", message: "boom" });
});
it("ошибка в чужой таблице не влияет на нужные", () => {
const rows = [row("jt_task", 10), row("new_source_table", 5, "error", "no access")];
const res = evaluateFreshness(rows, ["jt_task"], now, 90);
expect(res.every((r) => r.ok)).toBe(true);
});
});