import { describe, it, expect } from 'vitest';
import { createFleetState, ensureHostState, stampLastSeen } from '../fleet-state.js';

describe('createFleetState', () => {
  it('creates an empty fleet', () => {
    const fleet = createFleetState();
    expect(fleet.hosts.size).toBe(0);
  });
});

describe('ensureHostState', () => {
  it('creates a new host state if none exists', () => {
    const fleet = createFleetState();
    const state = ensureHostState(fleet, 'test-host');
    expect(state.providerId).toBe('test-host');
    expect(state.liveness).toBe('down');
    expect(state.lastSeenAt).toBeNull();
    expect(state.seq).toBe(0);
    expect(state.models.size).toBe(0);
  });

  it('returns existing host state', () => {
    const fleet = createFleetState();
    const state1 = ensureHostState(fleet, 'test-host');
    const state2 = ensureHostState(fleet, 'test-host');
    expect(state1).toBe(state2);
  });

  it('seq is 0 on first call', () => {
    const fleet = createFleetState();
    const state = ensureHostState(fleet, 'test-host');
    expect(state.seq).toBe(0);
  });

  it('stamps lastSeenAt on connection', () => {
    const fleet = createFleetState();
    const state = ensureHostState(fleet, 'test-host');
    expect(state.lastSeenAt).toBeNull();
    stampLastSeen(state);
    expect(state.lastSeenAt).not.toBeNull();
  });
});
