import { describe, it, expect } from 'vitest';
import { createMaintenanceController } from './maintenance.js';

describe('SSH maintenance controller', () => {
  it('starts inactive', () => {
    const m = createMaintenanceController();
    expect(m.isActive()).toBe(false);
    expect(m.snapshot()).toEqual({ active: false, reason: null, enteredAt: null, jobId: null });
  });

  it('enter() sets active with reason + timestamp + optional jobId', () => {
    const m = createMaintenanceController();
    m.enter('rotating master key', 'job-abc');
    const snap = m.snapshot();
    expect(snap.active).toBe(true);
    expect(snap.reason).toBe('rotating master key');
    expect(snap.jobId).toBe('job-abc');
    expect(snap.enteredAt).toMatch(/^\d{4}-\d{2}-\d{2}T/);
    expect(m.isActive()).toBe(true);
  });

  it('enter() without jobId defaults to null', () => {
    const m = createMaintenanceController();
    m.enter('manual maintenance');
    expect(m.snapshot().jobId).toBeNull();
  });

  it('exit() clears all state', () => {
    const m = createMaintenanceController();
    m.enter('rotating');
    m.exit();
    expect(m.snapshot()).toEqual({ active: false, reason: null, enteredAt: null, jobId: null });
  });

  it('controllers are isolated from each other', () => {
    const a = createMaintenanceController();
    const b = createMaintenanceController();
    a.enter('a-reason');
    expect(b.isActive()).toBe(false);
  });
});
