import { test } from "node:test"; import assert from "node:assert/strict"; import type { JobRecord } from "./registry"; import { windowInFlight } from "./windowJobs"; // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test jobs/windowJobs.test.ts // // Which queued or running job will already write a requested clip window // (release 19, A5): a single fetch-window whose window contains it, or a // fetch-windows batch with such an item — same channel, same video, same // height cap — read off the job's replay spec. function job( id: string, kind: string, status: JobRecord["status"], params: Record | null, slug = "chan", ): JobRecord { return { id, kind, queueKey: "clips:youtube", status, queuedAt: 0, logPath: "/dev/null", ...(params ? { spec: { kind, slug, params } } : {}), } as JobRecord; } const want = { slug: "chan", videoId: "v1", from: 100, to: 110 }; test("a single job whose window contains the asked one is in flight; its own window is answered", () => { const hit = windowInFlight([job("A", "fetch-window", "queued", { videoId: "v1", from: 95, to: 130 })], want); assert.deepEqual(hit, { jobId: "A", kind: "fetch-window", status: "queued", from: 95, to: 130 }); // Exactly the same window, running. assert.equal( windowInFlight([job("B", "fetch-window", "running", { videoId: "v1", from: 100, to: 110 })], want)?.jobId, "B", ); }); test("a window that only overlaps, another video, another channel or an ended job is not", () => { const records = [ job("overlap", "fetch-window", "queued", { videoId: "v1", from: 105, to: 130 }), job("video", "fetch-window", "queued", { videoId: "v2", from: 0, to: 900 }), job("chan", "fetch-window", "queued", { videoId: "v1", from: 0, to: 900 }, "other"), job("done", "fetch-window", "done", { videoId: "v1", from: 0, to: 900 }), job("nospec", "fetch-window", "queued", null), job("sync", "sync", "running", { videoId: "v1", from: 0, to: 900 }), ]; assert.equal(windowInFlight(records, want), null); }); test("a batch with a covering item is in flight; the height cap must match", () => { const batch = job("C", "fetch-windows", "running", { items: [ { slug: "chan", id: "v9", from: 0, to: 10 }, { slug: "chan", id: "v1", from: 90, to: 120 }, ], maxHeight: 480, }); assert.equal(windowInFlight([batch], want), null, "no cap asked, 480 held"); assert.deepEqual(windowInFlight([batch], { ...want, maxHeight: 480 }), { jobId: "C", kind: "fetch-windows", status: "running", from: 90, to: 120, }); });