import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('electron/main', () => ({ app: { getPath: () => join(tmpdir(), 'geo-rankly-monitor-scheduler-test'), }, })) import { enqueueMonitorLeaseTask, initMonitorScheduler, noteMonitorTaskActivated, noteMonitorTaskCompleted, selectNextMonitorTask, } from './monitor-scheduler' describe('monitor scheduler', () => { beforeEach(() => { vi.useFakeTimers() vi.setSystemTime(new Date('2026-06-22T00:00:00.000Z')) initMonitorScheduler() }) afterEach(() => { vi.useRealTimers() initMonitorScheduler() }) it('does not let high-priority monitor tasks bypass same-platform cooldown', () => { enqueueMonitorLeaseTask({ taskId: 'first', clientId: 'client-1', platform: 'doubao', routing: 'rabbitmq-primary', questionKey: 'q:first', }) expect( selectNextMonitorTask({ maxConcurrency: 2, activePlatforms: new Set(), activeQuestionKeys: new Set(), activeCount: 0, })?.taskId, ).toBe('first') noteMonitorTaskActivated('first') noteMonitorTaskCompleted('first') enqueueMonitorLeaseTask({ taskId: 'urgent', clientId: 'client-1', platform: 'doubao', routing: 'rabbitmq-primary', priority: 5000, lane: 'high', questionKey: 'q:urgent', }) expect( selectNextMonitorTask({ maxConcurrency: 2, activePlatforms: new Set(), activeQuestionKeys: new Set(), activeCount: 0, }), ).toBeNull() vi.advanceTimersByTime(5_001) expect( selectNextMonitorTask({ maxConcurrency: 2, activePlatforms: new Set(), activeQuestionKeys: new Set(), activeCount: 0, })?.taskId, ).toBe('urgent') }) it('does not let high-priority monitor tasks bypass an active same-platform task', () => { enqueueMonitorLeaseTask({ taskId: 'urgent-doubao', clientId: 'client-1', platform: 'doubao', routing: 'rabbitmq-primary', priority: 5000, lane: 'high', questionKey: 'q:urgent', }) expect( selectNextMonitorTask({ maxConcurrency: 2, activePlatforms: new Set(['doubao']), activeQuestionKeys: new Set(), activeCount: 1, }), ).toBeNull() }) it('prioritizes collect-now high lane before older normal tasks when platform is available', () => { enqueueMonitorLeaseTask({ taskId: 'old-normal', clientId: 'client-1', platform: 'qwen', routing: 'rabbitmq-primary', priority: 100, lane: 'normal', questionKey: 'q:normal', }) vi.advanceTimersByTime(1_000) enqueueMonitorLeaseTask({ taskId: 'collect-now', clientId: 'client-1', platform: 'doubao', routing: 'rabbitmq-primary', priority: 5000, lane: 'high', questionKey: 'q:collect-now', }) expect( selectNextMonitorTask({ maxConcurrency: 2, activePlatforms: new Set(), activeQuestionKeys: new Set(), activeCount: 0, })?.taskId, ).toBe('collect-now') }) })