// The scheduler does not queue an Instagram channel its fetch would refuse // (review 1, S10): no cookie source of its own, or Instagram held by its // alert or its cooldown. Such a channel takes no sync slot, and the schedule // view says why. import { test } from "node:test"; import assert from "node:assert/strict"; import type { ChannelConfig } from "../lib/channelConfig"; import { defaultSyncScheduler } from "../lib/settings"; import { defaultChannelPriority } from "../lib/channelPriority"; import { emptySchedulerState } from "./syncSchedulerState"; import { buildScheduleView, INSTAGRAM_SYNC_NO_COOKIES, instagramSyncHold, selectDueChannels } from "./syncScheduler"; const NOW = Date.parse("2026-03-10T12:00:00Z"); const scheduler = { ...defaultSyncScheduler(), enabled: true, defaultIntervalMinutes: 60, quietHoursStart: null, quietHoursEnd: null }; const ig = (cookies?: string): ChannelConfig => ({ handling: "transcribe", sourceKind: "social", platform: "instagram", url: "https://www.instagram.com/example.account/", ...(cookies ? { cookiesFromBrowser: cookies } : {}) }) as ChannelConfig; const x: ChannelConfig = { handling: "transcribe", sourceKind: "social", platform: "twitter", url: "https://x.com/example" } as ChannelConfig; const channels = [ { slug: "ig-bare", config: ig() }, { slug: "ig-ready", config: ig("firefox:ig-profile") }, { slug: "x-chan", config: x }, ]; const base = { channels, scheduler, state: emptySchedulerState(), activeSlugs: new Set(), now: NOW, priority: defaultChannelPriority() }; const HELD = "Instagram answered a rate limit a short while ago: no Instagram job starts before 13:00 UTC (60 min from now). Ask again then; nothing retries by itself."; test("an Instagram channel with no cookie source is never due; one with a source is, like any never-synced channel", () => { const r = selectDueChannels(base); assert.deepEqual([...r.due].sort(), ["ig-ready", "x-chan"]); // Configuration, not a hold: no line in the run log each tick. assert.deepEqual(r.skipped, []); }); test("while Instagram is held every Instagram channel is skipped with the sentence, and takes no slot; X is not Instagram's business", () => { const r = selectDueChannels({ ...base, instagramHeld: HELD }); assert.deepEqual(r.due, ["x-chan"]); assert.deepEqual(r.skipped, [{ slug: "ig-ready", reason: HELD }]); }); test("the schedule view agrees: not eligible, with the reason", () => { const view = buildScheduleView({ ...base, instagramHeld: HELD }); const of = (slug: string) => view.find((v) => v.slug === slug)!; assert.deepEqual([of("ig-bare").autoSyncEligible, of("ig-bare").heldReason], [false, INSTAGRAM_SYNC_NO_COOKIES]); assert.deepEqual([of("ig-ready").autoSyncEligible, of("ig-ready").heldReason], [false, HELD]); assert.deepEqual([of("x-chan").autoSyncEligible, of("x-chan").heldReason], [true, undefined]); const free = buildScheduleView(base); assert.equal(free.find((v) => v.slug === "ig-ready")!.autoSyncEligible, true); assert.equal(free.find((v) => v.slug === "ig-ready")!.heldReason, undefined); }); test("the one predicate", () => { assert.equal(instagramSyncHold(x, HELD), null); assert.equal(instagramSyncHold(ig(), null), INSTAGRAM_SYNC_NO_COOKIES); assert.equal(instagramSyncHold(ig(" "), null), INSTAGRAM_SYNC_NO_COOKIES); assert.equal(instagramSyncHold(ig("file:/x"), null), null); assert.equal(instagramSyncHold(ig("file:/x"), HELD), HELD); // A video channel that names the platform is not an Instagram posts channel. assert.equal(instagramSyncHold({ platform: "instagram" } as ChannelConfig, HELD), null); }); // Review 2, N7: the scheduler and the fetch agree about which channel is // Instagram's — by the platform, the fetcher or the URL's host. test("an Instagram channel told only by its URL (no platform in its config) is held back like any other", () => { const byUrl = { handling: "transcribe", sourceKind: "social", url: "https://www.instagram.com/example.account/" } as ChannelConfig; const byFetcher = { handling: "transcribe", sourceKind: "social", postFetcher: "instagram-gallery-dl", url: "https://example.invalid/x" } as ChannelConfig; assert.equal(instagramSyncHold(byUrl, null), INSTAGRAM_SYNC_NO_COOKIES); assert.equal(instagramSyncHold(byFetcher, null), INSTAGRAM_SYNC_NO_COOKIES); assert.equal(instagramSyncHold({ ...byUrl, cookiesFromBrowser: "firefox:ig" } as ChannelConfig, HELD), HELD); const r = selectDueChannels({ ...base, channels: [{ slug: "ig-by-url", config: byUrl }, { slug: "x-chan", config: x }] }); assert.deepEqual(r.due, ["x-chan"]); const view = buildScheduleView({ ...base, channels: [{ slug: "ig-by-url", config: byUrl }] }); assert.deepEqual([view[0]!.autoSyncEligible, view[0]!.heldReason], [false, INSTAGRAM_SYNC_NO_COOKIES]); // Its cadence is Instagram's too. assert.equal(view[0]!.intervalMinutes, 720); }); // Added scope R2 (lane x-followups): X's job cap is reached — the scheduler skips X's channels, like a cooldown. test("R2b: while X's job cap is reached every X posts channel is skipped with the sentence and takes no slot; Instagram is not X's business", async () => { const { xSyncHold } = await import("./syncScheduler"); const CAP = "X's job cap is reached on this machine: 12 X jobs started in the last 60 minutes (the cap is 12). The next X job may start at 13:00 UTC (in 4 min); …"; const r = selectDueChannels({ ...base, xCapHeld: CAP }); assert.ok(!r.due.includes("x-chan")); assert.ok(r.due.includes("ig-ready")); assert.deepEqual(r.skipped.filter((s) => s.slug === "x-chan"), [{ slug: "x-chan", reason: CAP }]); const view = buildScheduleView({ ...base, xCapHeld: CAP }); const x = view.find((v) => v.slug === "x-chan")!; assert.deepEqual([x.autoSyncEligible, x.heldReason], [false, CAP]); assert.equal(view.find((v) => v.slug === "ig-ready")!.autoSyncEligible, true); // With room under the cap nothing is held. assert.ok(selectDueChannels(base).due.includes("x-chan")); // The predicate: an X posts channel by its platform or its URL; never a video channel. assert.equal(xSyncHold({ sourceKind: "social", platform: "twitter" }, CAP), CAP); assert.equal(xSyncHold({ sourceKind: "social", url: "https://x.com/example" }, CAP), CAP); assert.equal(xSyncHold({ sourceKind: "social", platform: "bluesky" }, CAP), null); assert.equal(xSyncHold({ sourceKind: "video", url: "https://x.com/example" } as never, CAP), null); assert.equal(xSyncHold({ sourceKind: "social", platform: "twitter" }, null), null); });