import { readFileSync } from "node:fs"; import { join } from "node:path"; import { json, mockFetch } from "./fetchMock.ts"; // хаб в памяти для тестов обновления из Jira: снимок, список пакетов, пакет по id; счётчики запросов export const fixture = (name: string) => readFileSync(join(import.meta.dirname, "..", "modules", "jira", "board", "__fixtures__", name), "utf8"); export const SNAP = JSON.parse(fixture("hub-basic.board.json")); export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); export const deferred = () => { let resolve!: (v: T) => void; const promise = new Promise((r) => { resolve = r; }); return { promise, resolve }; }; export function hub(over: Partial Response | Promise>> = {}) { const calls: string[] = []; const inFlight: Record = {}, maxInFlight: Record = {}; const fetch = mockFetch(async (method, url, body) => { const k = `${method} ${url.pathname}`; calls.push(k); inFlight[k] = (inFlight[k] ?? 0) + 1; maxInFlight[k] = Math.max(maxInFlight[k] ?? 0, inFlight[k]); try { const h = over[k] ?? over[`${method} ${url.pathname.replace(/\d+$/, ":id")}`]; if (h) return await h(url, body); if (k === "GET /board") return json(200, SNAP); if (k === "GET /issues") return json(200, { issues: [] }); if (k === "GET /projects") return json(200, { projects: [] }); if (k === "GET /batches") return json(200, { batches: [] }); if (k === "GET /agent") return json(200, { online: true, last_seen_at: null, warning: null }); if (k === "POST /refresh") return json(201, { id: 7, estimate_seconds: 5, count: 1 }); return json(404, { error: "not_found" }); } finally { inFlight[k] -= 1; } }); return { fetch, calls, count: (k: string) => calls.filter((c) => c === k).length, maxInFlight: (k: string) => maxInFlight[k] ?? 0 }; } export const batch = (status: string, cmd: Record = { status: "done" }) => json(200, { id: 7, title: "Обновление", author: "viewer", created_at: "", cancelled_at: null, status, counts: {}, remaining_seconds: 0, commands: [{ id: 1, seq: 1, type: "refresh", error: null, finished_at: null, ...cmd }] });