import { describe, expect, it } from 'vitest';

import {
  createPeerPresenceChannelName,
  getEffectivePresenceState,
  PRESENCE_DEVICE_STALE_MS,
} from './presence';

describe('getEffectivePresenceState', () => {
  const now = Date.parse('2026-04-24T12:00:00.000Z');

  it('keeps a live profile state when a device checked in recently', () => {
    const seenAt = new Date(now - PRESENCE_DEVICE_STALE_MS + 1_000).toISOString();

    expect(getEffectivePresenceState('online', seenAt, now)).toBe('online');
    expect(getEffectivePresenceState('idle', seenAt, now)).toBe('idle');
    expect(getEffectivePresenceState('dnd', seenAt, now)).toBe('dnd');
  });

  it('falls back to offline when the latest device check-in is stale', () => {
    const seenAt = new Date(now - PRESENCE_DEVICE_STALE_MS - 1_000).toISOString();

    expect(getEffectivePresenceState('online', seenAt, now)).toBe('offline');
  });

  it('respects explicit offline and invisible states', () => {
    const seenAt = new Date(now).toISOString();

    expect(getEffectivePresenceState('offline', seenAt, now)).toBe('offline');
    expect(getEffectivePresenceState('invisible', seenAt, now)).toBe('invisible');
  });
});

describe('createPeerPresenceChannelName', () => {
  it('keeps simultaneous subscriptions for the same user isolated', () => {
    const userId = '48c2959f-1a21-4de4-a69b-7481a3e68fbd';

    expect(createPeerPresenceChannelName(userId, 'header')).not.toBe(
      createPeerPresenceChannelName(userId, 'profile-card'),
    );
  });
});
