import { describe, expect, test } from 'bun:test';

import { getEffectiveDeviceStatus } from '../utils/device-status';

describe('device status helper', () => {
  test('reports online when status is online and heartbeat is fresh', () => {
    const now = new Date('2026-02-25T12:00:00.000Z');
    const lastSeenAt = new Date('2026-02-25T11:59:45.000Z');

    const status = getEffectiveDeviceStatus({
      status: 'online',
      lastSeenAt,
      now,
      staleAfterSeconds: 30,
    });

    expect(status).toBe('online');
  });

  test('reports offline when heartbeat is stale', () => {
    const now = new Date('2026-02-25T12:00:00.000Z');
    const lastSeenAt = new Date('2026-02-25T11:59:00.000Z');

    const status = getEffectiveDeviceStatus({
      status: 'online',
      lastSeenAt,
      now,
      staleAfterSeconds: 30,
    });

    expect(status).toBe('offline');
  });

  test('reports offline when stored status is not online', () => {
    const now = new Date('2026-02-25T12:00:00.000Z');
    const lastSeenAt = new Date('2026-02-25T11:59:55.000Z');

    const status = getEffectiveDeviceStatus({
      status: 'offline',
      lastSeenAt,
      now,
      staleAfterSeconds: 30,
    });

    expect(status).toBe('offline');
  });

  test('reports offline when lastSeenAt is missing', () => {
    const now = new Date('2026-02-25T12:00:00.000Z');

    const status = getEffectiveDeviceStatus({
      status: 'online',
      lastSeenAt: null,
      now,
      staleAfterSeconds: 30,
    });

    expect(status).toBe('offline');
  });
});
