Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

27.3 KB
Raw Blame
import { describe, it, expect, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest';
import {
  schedule,
  cancel,
  pause,
  resume,
  getScheduledEvents,
  getEvent,
  getHistory,
  getStats,
  cancelAll,
  triggerNow,
  parseCronToNextRun,
  parseCronToPrevRun,
  isValidCron,
  parseRecurrenceToNextRun,
  isValidRecurrence,
  BOOT_GRACE_MS,
} from './eventScheduler.js';
import { cosEvents } from './cosEvents.js';

// eventScheduler.js's "UTC" branch (the default, and what every consumer below
// uses unless it passes an explicit IANA timezone) reads local Date methods
// (getMonth/getDate/getDay/getHours/getMinutes), not getUTC*. Production always
// runs under PM2 with TZ=UTC (see ecosystem.config.cjs), which makes that local
// time UTC. Pin the same env here so cron matches are deterministic regardless
// of the machine running the tests.
let originalTZ;
beforeAll(() => {
  originalTZ = process.env.TZ;
  process.env.TZ = 'UTC';
});
afterAll(() => {
  if (originalTZ === undefined) delete process.env.TZ;
  else process.env.TZ = originalTZ;
});

// 2024-01-01T00:00:00.000Z is a Monday (getUTCDay()/getDay() under TZ=UTC === 1).
const FIXED_NOW = new Date('2024-01-01T00:00:00.000Z');

// The scheduler's Maps (scheduledEvents/activeTimers) are module-level singleton
// state shared across every test in this file — clear it between tests so cases
// don't leak into each other.
beforeEach(() => {
  cancelAll();
});

// =============================================================================
// isValidCron
// =============================================================================

describe('isValidCron', () => {
  it('accepts a plain 5-field expression', () => {
    expect(isValidCron('5 0 * * *')).toBe(true);
  });

  it('accepts step syntax', () => {
    expect(isValidCron('*/15 * * * *')).toBe(true);
  });

  it('accepts range syntax', () => {
    expect(isValidCron('0 9-17 * * *')).toBe(true);
  });

  it('accepts list syntax', () => {
    expect(isValidCron('0 9,13,17 * * *')).toBe(true);
  });

  it('rejects a minute value of 60 (out of range)', () => {
    expect(isValidCron('60 0 * * *')).toBe(false);
  });

  it('rejects an hour value of 24 (out of range)', () => {
    expect(isValidCron('0 24 * * *')).toBe(false);
  });

  it('rejects a month value of 13 (out of range)', () => {
    expect(isValidCron('0 0 * 13 *')).toBe(false);
  });

  it('rejects a day-of-month value of 0 (below range)', () => {
    expect(isValidCron('0 0 0 * *')).toBe(false);
  });

  it('rejects a malformed field count', () => {
    expect(isValidCron('* * *')).toBe(false);
    expect(isValidCron('* * * * * *')).toBe(false);
  });

  it('rejects non-string and empty input', () => {
    expect(isValidCron('')).toBe(false);
    expect(isValidCron('   ')).toBe(false);
    expect(isValidCron(null)).toBe(false);
    expect(isValidCron(undefined)).toBe(false);
    expect(isValidCron(123)).toBe(false);
  });
});

// =============================================================================
// parseCronToNextRun
// =============================================================================

describe('parseCronToNextRun', () => {
  it('finds the next matching minute later the same day', () => {
    const next = parseCronToNextRun('5 0 * * *', FIXED_NOW);
    expect(next.toISOString()).toBe('2024-01-01T00:05:00.000Z');
  });

  it('throws for a malformed field count', () => {
    expect(() => parseCronToNextRun('* * *', FIXED_NOW)).toThrow(/Invalid cron expression/);
  });

  it('returns null for an out-of-range field instead of throwing', () => {
    expect(parseCronToNextRun('60 * * * *', FIXED_NOW)).toBeNull();
  });

  it('resolves step syntax to the next step boundary', () => {
    const next = parseCronToNextRun('*/15 * * * *', FIXED_NOW);
    expect(next.toISOString()).toBe('2024-01-01T00:15:00.000Z');
  });

  it('resolves range syntax to the first hour in range', () => {
    const next = parseCronToNextRun('0 9-17 * * *', FIXED_NOW);
    expect(next.toISOString()).toBe('2024-01-01T09:00:00.000Z');
  });

  it('resolves list syntax to the next listed minute', () => {
    const next = parseCronToNextRun('0,30 * * * *', FIXED_NOW);
    expect(next.toISOString()).toBe('2024-01-01T00:30:00.000Z');
  });

  it('treats day-of-week 7 the same as 0 (Sunday)', () => {
    const asZero = parseCronToNextRun('0 0 * * 0', FIXED_NOW);
    const asSeven = parseCronToNextRun('0 0 * * 7', FIXED_NOW);
    expect(asZero.toISOString()).toBe('2024-01-07T00:00:00.000Z');
    expect(asSeven.toISOString()).toBe(asZero.toISOString());
  });

  it('respects an explicit IANA timezone rather than the process TZ', () => {
    // 09:00 America/New_York on 2024-01-01 (EST, UTC-5) is 14:00 UTC.
    const next = parseCronToNextRun('0 9 * * *', FIXED_NOW, 'America/New_York');
    expect(next.toISOString()).toBe('2024-01-01T14:00:00.000Z');
  });

  it('returns null quickly for an impossible date within a tight until bound', () => {
    // Feb 31st never occurs; bound the search to a few days so it returns null
    // fast instead of scanning the full 2-year window.
    const until = new Date('2024-01-05T00:00:00.000Z');
    expect(parseCronToNextRun('0 0 31 2 *', FIXED_NOW, 'UTC', until)).toBeNull();
  });
});

// =============================================================================
// calendar recurrence scheduling
// =============================================================================

describe('parseRecurrenceToNextRun', () => {
  it('keeps every-two-weeks cadence anchored across a month boundary', () => {
    const next = parseRecurrenceToNextRun({
      frequency: 'weekly', interval: 2, weekdays: [1], time: '02:00', anchorDate: '2024-01-01',
    }, new Date('2024-01-01T03:00:00.000Z'));
    expect(next.toISOString()).toBe('2024-01-15T02:00:00.000Z');
  });

  it('finds the first Thursday of the month', () => {
    const next = parseRecurrenceToNextRun({
      frequency: 'monthly-weekday', ordinal: 'first', weekday: 4, time: '19:00',
    }, FIXED_NOW);
    expect(next.toISOString()).toBe('2024-01-04T19:00:00.000Z');
  });

  it('finds the last Thursday of the month', () => {
    const next = parseRecurrenceToNextRun({
      frequency: 'monthly-weekday', ordinal: 'last', weekday: 4, time: '19:00',
    }, FIXED_NOW);
    expect(next.toISOString()).toBe('2024-01-25T19:00:00.000Z');
  });

  it('jumps directly to sparse annual occurrences', () => {
    const next = parseRecurrenceToNextRun({
      frequency: 'monthly-date', interval: 12, dayOfMonth: 1, time: '00:00', anchorDate: '2024-01-01',
    }, new Date('2024-01-02T00:00:00.000Z'));
    expect(next.toISOString()).toBe('2025-01-01T00:00:00.000Z');
  });

  it('accepts a valid future anchor beyond the runtime two-year horizon', () => {
    const next = parseRecurrenceToNextRun({
      frequency: 'monthly-date', interval: 12, dayOfMonth: 1, time: '00:00', anchorDate: '2027-01-01',
    }, new Date('2024-01-02T00:00:00.000Z'));
    expect(next.toISOString()).toBe('2027-01-01T00:00:00.000Z');
  });

  it('validates both rich recurrence rules and custom cron rules', () => {
    expect(isValidRecurrence({ frequency: 'weekly', interval: 2, weekdays: [1], time: '02:00', anchorDate: '2024-01-01' })).toBe(true);
    expect(isValidRecurrence({ frequency: 'custom', cron: '*/15 * * * *' })).toBe(true);
    expect(isValidRecurrence({ frequency: 'custom', cron: 'not a cron' })).toBe(false);
    expect(isValidRecurrence({ frequency: 'weekly', weekdays: [], time: '02:00' })).toBe(false);
  });
});

// =============================================================================
// parseCronToPrevRun
// =============================================================================

describe('parseCronToPrevRun', () => {
  it('finds the most recent matching minute at or before `from`', () => {
    const from = new Date('2024-01-01T00:10:00.000Z');
    const prev = parseCronToPrevRun('5 0 * * *', from);
    expect(prev.toISOString()).toBe('2024-01-01T00:05:00.000Z');
  });

  it('returns the exact minute when `from` lands on a match', () => {
    const from = new Date('2024-01-01T00:05:00.000Z');
    const prev = parseCronToPrevRun('5 0 * * *', from);
    expect(prev.toISOString()).toBe('2024-01-01T00:05:00.000Z');
  });

  it('throws for a malformed field count', () => {
    expect(() => parseCronToPrevRun('* * *', FIXED_NOW)).toThrow(/Invalid cron expression/);
  });

  it('returns null for an out-of-range field instead of throwing', () => {
    expect(parseCronToPrevRun('0 25 * * *', FIXED_NOW)).toBeNull();
  });

  it('treats day-of-week 7 the same as 0 (Sunday) walking backward', () => {
    // 2024-01-08 is a Monday; the prior Sunday midnight is 2024-01-07.
    const from = new Date('2024-01-08T12:00:00.000Z');
    const asZero = parseCronToPrevRun('0 0 * * 0', from);
    const asSeven = parseCronToPrevRun('0 0 * * 7', from);
    expect(asZero.toISOString()).toBe('2024-01-07T00:00:00.000Z');
    expect(asSeven.toISOString()).toBe(asZero.toISOString());
  });
});

// =============================================================================
// schedule / cancel / pause / resume lifecycle
// =============================================================================

describe('schedule() validation', () => {
  it('throws when id is missing', () => {
    expect(() => schedule({ type: 'once', delayMs: 1000, handler: () => {} }))
      .toThrow(/requires id, type, and handler/);
  });

  it('throws when handler is missing', () => {
    expect(() => schedule({ id: 'e1', type: 'once', delayMs: 1000 }))
      .toThrow(/requires id, type, and handler/);
  });

  it('throws for an unknown event type', () => {
    expect(() => schedule({ id: 'e1', type: 'bogus', handler: () => {} }))
      .toThrow(/Unknown event type/);
  });

  it('throws when cron type is missing its cron expression', () => {
    expect(() => schedule({ id: 'e1', type: 'cron', handler: () => {} }))
      .toThrow(/requires cron expression/);
  });

  it('throws when interval type is missing intervalMs', () => {
    expect(() => schedule({ id: 'e1', type: 'interval', handler: () => {} }))
      .toThrow(/requires intervalMs/);
  });

  it('throws when once type is missing delayMs', () => {
    expect(() => schedule({ id: 'e1', type: 'once', handler: () => {} }))
      .toThrow(/requires delayMs/);
  });
});

describe('schedule() lifecycle with fake timers', () => {
  beforeEach(() => {
    vi.useFakeTimers();
    vi.setSystemTime(FIXED_NOW);
  });

  afterEach(() => {
    cancelAll();
    vi.useRealTimers();
  });

  it('fires an interval handler on the configured cadence and updates runCount', async () => {
    const handler = vi.fn();
    schedule({ id: 'iv-1', type: 'interval', intervalMs: 5000, handler });

    await vi.advanceTimersByTimeAsync(5000);
    expect(handler).toHaveBeenCalledTimes(1);
    expect(getEvent('iv-1').runCount).toBe(1);

    await vi.advanceTimersByTimeAsync(5000);
    expect(handler).toHaveBeenCalledTimes(2);
    expect(getEvent('iv-1').runCount).toBe(2);
  });

  it('first fires BOOT_GRACE_MS after registration when interval is overdue (#12624)', async () => {
    const handler = vi.fn();
    const intervalMs = 60 * 60 * 1000; // 1 hour
    // Last run 2 hours ago (overdue)
    const lastRunAt = FIXED_NOW.getTime() - 2 * 60 * 60 * 1000;

    schedule({ id: 'iv-overdue', type: 'interval', intervalMs, lastRunAt, handler });
    const event = getEvent('iv-overdue');
    expect(event.nextRunAt).toBe(FIXED_NOW.getTime() + BOOT_GRACE_MS);

    // Advancing just before BOOT_GRACE_MS should not fire
    await vi.advanceTimersByTimeAsync(BOOT_GRACE_MS - 1);
    expect(handler).not.toHaveBeenCalled();

    // Reaching BOOT_GRACE_MS fires the first run
    await vi.advanceTimersByTimeAsync(1);
    expect(handler).toHaveBeenCalledTimes(1);

    // Subsequent runs fire at intervalMs cadence
    await vi.advanceTimersByTimeAsync(intervalMs);
    expect(handler).toHaveBeenCalledTimes(2);
  });

  it('first fires at lastRunAt + intervalMs when last run was recent (#12624)', async () => {
    const handler = vi.fn();
    const intervalMs = 60 * 60 * 1000; // 1 hour
    // Last run 10 minutes ago
    const lastRunAt = FIXED_NOW.getTime() - 10 * 60 * 1000;

    schedule({ id: 'iv-recent', type: 'interval', intervalMs, lastRunAt, handler });
    const event = getEvent('iv-recent');
    // Expected first fire: lastRunAt + 60min = FIXED_NOW + 50min
    expect(event.nextRunAt).toBe(lastRunAt + intervalMs);

    await vi.advanceTimersByTimeAsync(50 * 60 * 1000 - 1);
    expect(handler).not.toHaveBeenCalled();

    await vi.advanceTimersByTimeAsync(1);
    expect(handler).toHaveBeenCalledTimes(1);
  });

  it('accepts ISO string, ms number, Date instance for lastRunAt and falls back on invalid (#12624)', () => {
    const handler = vi.fn();
    const intervalMs = 60 * 60 * 1000;
    const pastMs = FIXED_NOW.getTime() - 10 * 60 * 1000;
    const isoString = new Date(pastMs).toISOString();

    const e1 = schedule({ id: 'iv-iso', type: 'interval', intervalMs, lastRunAt: isoString, handler });
    expect(e1.nextRunAt).toBe(pastMs + intervalMs);

    const e2 = schedule({ id: 'iv-date', type: 'interval', intervalMs, lastRunAt: new Date(pastMs), handler });
    expect(e2.nextRunAt).toBe(pastMs + intervalMs);

    const e3 = schedule({ id: 'iv-invalid', type: 'interval', intervalMs, lastRunAt: 'not-a-date', handler });
    expect(e3.nextRunAt).toBe(FIXED_NOW.getTime() + intervalMs);
    expect(e3.lastRunAt).toBeNull();
  });

  it('fires a once event exactly once and then deactivates it', async () => {
    const handler = vi.fn();
    schedule({ id: 'once-1', type: 'once', delayMs: 1000, handler });

    await vi.advanceTimersByTimeAsync(1000);
    expect(handler).toHaveBeenCalledTimes(1);
    expect(getEvent('once-1').active).toBe(false);

    await vi.advanceTimersByTimeAsync(10000);
    expect(handler).toHaveBeenCalledTimes(1); // no further runs
  });

  it('fires a cron event at the next matching minute', async () => {
    const handler = vi.fn();
    schedule({ id: 'cron-1', type: 'cron', cron: '* * * * *', handler });

    const event = getEvent('cron-1');
    expect(event.nextRunAt).toBe(new Date('2024-01-01T00:01:00.000Z').getTime());

    await vi.advanceTimersByTimeAsync(60 * 1000);
    expect(handler).toHaveBeenCalledTimes(1);
  });

  it('fires an anchored recurrence event and re-arms its next occurrence', async () => {
    const handler = vi.fn();
    schedule({
      id: 'recurrence-1',
      type: 'recurrence',
      recurrence: { frequency: 'weekly', interval: 2, weekdays: [1], time: '00:05', anchorDate: '2024-01-01' },
      handler,
    });

    expect(getEvent('recurrence-1').nextRunAt).toBe(new Date('2024-01-01T00:05:00.000Z').getTime());
    await vi.advanceTimersByTimeAsync(5 * 60 * 1000);
    expect(handler).toHaveBeenCalledTimes(1);
    expect(getEvent('recurrence-1').nextRunAt).toBe(new Date('2024-01-15T00:05:00.000Z').getTime());
  });

  it('cancel() removes the event and its pending timer stops firing', async () => {
    const handler = vi.fn();
    schedule({ id: 'cancel-1', type: 'once', delayMs: 1000, handler });

    expect(cancel('cancel-1')).toBe(true);
    expect(getEvent('cancel-1')).toBeNull();

    await vi.advanceTimersByTimeAsync(5000);
    expect(handler).not.toHaveBeenCalled();
  });

  it('cancel() returns false for an unknown id', () => {
    expect(cancel('does-not-exist')).toBe(false);
  });

  it('pause() stops the timer without removing the event, resume() restarts it', async () => {
    const handler = vi.fn();
    schedule({ id: 'pause-1', type: 'interval', intervalMs: 5000, handler });

    expect(pause('pause-1')).toBe(true);
    expect(getEvent('pause-1').active).toBe(false);

    await vi.advanceTimersByTimeAsync(20000);
    expect(handler).not.toHaveBeenCalled(); // paused — no runs

    expect(resume('pause-1')).toBe(true);
    expect(getEvent('pause-1').active).toBe(true);

    await vi.advanceTimersByTimeAsync(5000);
    expect(handler).toHaveBeenCalledTimes(1);
  });

  it('pause() and resume() return false for an unknown id', () => {
    expect(pause('does-not-exist')).toBe(false);
    expect(resume('does-not-exist')).toBe(false);
  });

  it('triggerNow() runs the handler immediately regardless of nextRunAt', async () => {
    const handler = vi.fn();
    schedule({ id: 'trigger-1', type: 'interval', intervalMs: 60000, handler });

    expect(await triggerNow('trigger-1')).toBe(true);
    expect(handler).toHaveBeenCalledTimes(1);
  });

  it('triggerNow() replaces the pending timer instead of leaving a duplicate run armed', async () => {
    const handler = vi.fn();
    schedule({ id: 'trigger-dup', type: 'interval', intervalMs: 60000, handler });

    expect(await triggerNow('trigger-dup')).toBe(true);
    expect(handler).toHaveBeenCalledTimes(1);
    expect(getStats().activeTimers).toBe(1);

    // The original 60s deadline must have been cleared by the re-arm, so only the
    // fresh 60s-from-now timer fires in the next minute.
    await vi.advanceTimersByTimeAsync(60000);
    expect(handler).toHaveBeenCalledTimes(2);
  });

  it('triggerNow() does not let the original deadline fire during a long manual run', async () => {
    let release;
    const handler = vi.fn(() => new Promise(resolve => { release = resolve; }));
    schedule({ id: 'trigger-slow', type: 'interval', intervalMs: 60000, handler });

    const running = triggerNow('trigger-slow');
    expect(handler).toHaveBeenCalledTimes(1);

    // Cross the original 60s deadline while the manual run is still in flight
    await vi.advanceTimersByTimeAsync(60000);
    expect(handler).toHaveBeenCalledTimes(1); // no concurrent duplicate run

    release();
    expect(await running).toBe(true);
  });

  it('triggerNow() returns false for an unknown id', async () => {
    expect(await triggerNow('does-not-exist')).toBe(false);
  });

  it('triggerNow() mid-run joins the in-flight timer run instead of starting a second handler (#12022)', async () => {
    let release;
    const handler = vi.fn(() => new Promise(resolve => { release = resolve; }));
    schedule({ id: 'trigger-join', type: 'interval', intervalMs: 60000, handler });

    // The timer starts the run; "Run now" lands while it is still executing.
    await vi.advanceTimersByTimeAsync(60000);
    expect(handler).toHaveBeenCalledTimes(1);
    const manual = triggerNow('trigger-join');
    const secondManual = triggerNow('trigger-join');
    await Promise.resolve();
    expect(handler).toHaveBeenCalledTimes(1);

    release();
    expect(await manual).toEqual({ joined: true });
    expect(await secondManual).toEqual({ joined: true });
    expect(handler).toHaveBeenCalledTimes(1);
    expect(getEvent('trigger-join').runCount).toBe(1);
    expect(getHistory({ eventId: 'trigger-join' })).toHaveLength(1);

    // The latch releases with the run: the next trigger executes again.
    handler.mockResolvedValue(undefined);
    expect(await triggerNow('trigger-join')).toBe(true);
    expect(handler).toHaveBeenCalledTimes(2);
  });

  it('records a handler that reports { skipped } as a skipped slot, not a successful run (#12023)', async () => {
    const ran = vi.fn();
    cosEvents.on('scheduler:ran', ran);
    const handler = vi.fn()
      .mockResolvedValueOnce({ skipped: true, reason: 'already running' })
      .mockRejectedValueOnce(new Error('boom'));
    schedule({ id: 'skip-1', type: 'interval', intervalMs: 5000, handler });
    const rateBefore = getStats().recentSuccessRate;

    await vi.advanceTimersByTimeAsync(5000);

    const [entry] = getHistory({ eventId: 'skip-1' });
    expect(entry).toMatchObject({ success: true, skipped: true, skipReason: 'already running', error: null });
    expect(getEvent('skip-1')).toMatchObject({ lastRunSucceeded: true, lastRunSkipped: true, lastSkipReason: 'already running', runCount: 1 });
    expect(ran).toHaveBeenCalledWith(expect.objectContaining({ id: 'skip-1', success: true, skipped: true }));

    // The skipped slot counts toward neither side of the success rate; the
    // failure that follows does.
    expect(getStats().recentSuccessRate).toBe(rateBefore);
    await vi.advanceTimersByTimeAsync(5000);
    expect(getStats().recentSuccessRate).not.toBe(rateBefore);
    expect(getEvent('skip-1')).toMatchObject({ lastRunSucceeded: false, lastRunSkipped: false, lastSkipReason: null });
    cosEvents.off('scheduler:ran', ran);
  });

  it('records a failed handler in history without deactivating a recurring event', async () => {
    const handler = vi.fn().mockRejectedValue(new Error('boom'));
    schedule({ id: 'fail-1', type: 'interval', intervalMs: 5000, handler });

    await vi.advanceTimersByTimeAsync(5000);

    const [entry] = getHistory({ eventId: 'fail-1' });
    expect(entry.success).toBe(false);
    expect(entry.error).toBe('boom');
    expect(getEvent('fail-1').active).toBe(true); // still scheduled for next run

    await vi.advanceTimersByTimeAsync(5000);
    expect(handler).toHaveBeenCalledTimes(2); // recurring run still happened
  });

  it('logs the stack and emits a structured error carrying the event id when a handler throws (#7549)', async () => {
    const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
    const logListener = vi.fn();
    cosEvents.on('log', logListener);

    try {
      const handler = vi.fn().mockRejectedValue(new Error('boom'));
      schedule({ id: 'fail-log-1', type: 'once', delayMs: 1000, handler });

      await vi.advanceTimersByTimeAsync(1000);

      expect(consoleError).toHaveBeenCalledWith(
        expect.stringContaining('Event fail-log-1 failed: boom'),
        expect.stringContaining('Error: boom'),
      );

      const [entry] = logListener.mock.calls.map(([e]) => e).filter(e => e.level === 'error');
      expect(entry).toMatchObject({ eventId: 'fail-log-1', message: expect.stringContaining('boom') });
    } finally {
      cosEvents.off('log', logListener);
      consoleError.mockRestore();
    }
  });

  it('tracks lastRunSucceeded/lastError/consecutiveFailures across failing and passing runs', async () => {
    const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});

    try {
      const handler = vi.fn()
        .mockRejectedValueOnce(new Error('first failure'))
        .mockRejectedValueOnce(new Error('second failure'))
        .mockResolvedValueOnce(undefined);
      schedule({ id: 'failure-state-1', type: 'interval', intervalMs: 1000, handler });

      await vi.advanceTimersByTimeAsync(1000);
      let event = getEvent('failure-state-1');
      expect(event.lastRunSucceeded).toBe(false);
      expect(event.lastError).toBe('first failure');
      expect(event.consecutiveFailures).toBe(1);

      await vi.advanceTimersByTimeAsync(1000);
      event = getEvent('failure-state-1');
      expect(event.lastError).toBe('second failure');
      expect(event.consecutiveFailures).toBe(2);

      await vi.advanceTimersByTimeAsync(1000);
      event = getEvent('failure-state-1');
      expect(event.lastRunSucceeded).toBe(true);
      expect(event.lastError).toBeNull();
      expect(event.consecutiveFailures).toBe(0);
    } finally {
      consoleError.mockRestore();
    }
  });

  it('records a successful run in history with a non-negative duration', async () => {
    const handler = vi.fn().mockResolvedValue(undefined);
    schedule({ id: 'success-1', type: 'once', delayMs: 1000, handler });

    await vi.advanceTimersByTimeAsync(1000);

    const [entry] = getHistory({ eventId: 'success-1' });
    expect(entry.success).toBe(true);
    expect(entry.error).toBeNull();
    expect(entry.duration).toBeGreaterThanOrEqual(0);
  });

  it('re-arms a recurring event even when a scheduler:ran listener throws', async () => {
    // The re-arm used to sit after the emit, outside any try — one throwing
    // listener permanently stopped the schedule while the UI still showed it active.
    const handler = vi.fn();
    const listener = vi.fn(() => { throw new Error('listener boom'); });
    cosEvents.on('scheduler:ran', listener);
    const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});

    try {
      schedule({ id: 'listener-throw', type: 'interval', intervalMs: 5000, handler });

      await vi.advanceTimersByTimeAsync(5000);
      expect(handler).toHaveBeenCalledTimes(1);
      expect(listener).toHaveBeenCalledTimes(1);
      expect(getEvent('listener-throw').active).toBe(true);
      expect(getStats().activeTimers).toBe(1); // re-armed with a fresh handle

      await vi.advanceTimersByTimeAsync(5000);
      expect(handler).toHaveBeenCalledTimes(2); // schedule survived the throw
    } finally {
      cosEvents.off('scheduler:ran', listener);
      consoleError.mockRestore();
    }
  });

  it('stops a cron event whose expression became unparseable, naming the reason', async () => {
    const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});

    try {
      // The handler receives the live event object — corrupt the cron mid-run the
      // same way a bad persisted value would surface on the next recompute.
      schedule({
        id: 'bad-cron',
        type: 'cron',
        cron: '* * * * *',
        handler: event => { event.cron = '* * *'; },
      });

      await vi.advanceTimersByTimeAsync(60 * 1000);

      expect(getEvent('bad-cron').active).toBe(false);
      expect(getStats().activeTimers).toBe(0);
      expect(consoleError).toHaveBeenCalledWith(
        expect.stringContaining('Event bad-cron could not compute its next run')
      );
    } finally {
      consoleError.mockRestore();
    }
  });

  it('stops a recurrence event that yields no next occurrence, naming the reason', async () => {
    const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});

    try {
      schedule({
        id: 'no-next',
        type: 'recurrence',
        recurrence: { frequency: 'weekly', interval: 1, weekdays: [1], time: '00:05', anchorDate: '2024-01-01' },
        handler: event => { event.recurrence = { frequency: 'weekly', interval: 0 }; },
      });

      await vi.advanceTimersByTimeAsync(5 * 60 * 1000);

      expect(getEvent('no-next').active).toBe(false);
      expect(getStats().activeTimers).toBe(0);
      expect(consoleError).toHaveBeenCalledWith(
        expect.stringContaining('Event no-next has no next run time')
      );
    } finally {
      consoleError.mockRestore();
    }
  });

  it('cancelAll() removes every scheduled event and reports the count removed', () => {
    schedule({ id: 'a', type: 'once', delayMs: 1000, handler: () => {} });
    schedule({ id: 'b', type: 'once', delayMs: 1000, handler: () => {} });

    expect(getScheduledEvents()).toHaveLength(2);
    expect(cancelAll()).toBe(2);
    expect(getScheduledEvents()).toHaveLength(0);
  });

  it('getStats() reports totals broken down by type', () => {
    schedule({ id: 'stat-a', type: 'once', delayMs: 1000, handler: () => {} });
    schedule({ id: 'stat-b', type: 'interval', intervalMs: 5000, handler: () => {} });
    pause('stat-b');

    const stats = getStats();
    expect(stats.totalEvents).toBe(2);
    expect(stats.activeEvents).toBe(1); // stat-b paused
    expect(stats.byType).toEqual({ once: 1, interval: 1 });
  });
});

// A fresh module instance is required here: eventHistory is module-level state
// that every other test in this file has already appended to by the time this
// describe runs, so getStats() from the shared import can never observe a truly
// empty ring (#7549).
describe('getStats().recentSuccessRate on an empty ring', () => {
  it('is null, not "100%", when no run has been recorded', async () => {
    vi.resetModules();
    const fresh = await import('./eventScheduler.js');
    expect(fresh.getStats().recentSuccessRate).toBeNull();
  });
});