Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-11 09:44:50 +03:00
commit ebb1cb7950
101 changed files with 4971 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
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);
});
});