import { describe, expect, it } from 'vitest';
import {
  getBackupHealthSnapshot,
  initBackupHealth,
  markBackupFailure,
  markBackupSuccess,
} from './status.js';

describe('backup status tracker', () => {
  it('tracks failures and resets on success', () => {
    initBackupHealth(true);
    expect(getBackupHealthSnapshot()).toMatchObject({
      enabled: true,
      hasRun: false,
      consecutiveFailures: 0,
    });

    markBackupFailure('upload failed', 123);
    expect(getBackupHealthSnapshot()).toMatchObject({
      enabled: true,
      hasRun: true,
      consecutiveFailures: 1,
      lastFailureAt: 123,
      lastError: 'upload failed',
    });

    markBackupFailure('upload failed again', 456);
    expect(getBackupHealthSnapshot()).toMatchObject({
      consecutiveFailures: 2,
      lastFailureAt: 456,
      lastError: 'upload failed again',
    });

    markBackupSuccess(789);
    expect(getBackupHealthSnapshot()).toMatchObject({
      hasRun: true,
      consecutiveFailures: 0,
      lastSuccessAt: 789,
      lastError: undefined,
    });
  });
});
