import { describe, expect, it } from 'vitest';
import { idleCopy, parseRoomStatus } from './room-status';

// Thu 2026-10-29 12:00 New York; the show is Fri 2026-10-30 21:00 New York.
const NOW = Date.UTC(2026, 9, 29, 16, 0);
const FRIDAY_9PM = Date.UTC(2026, 9, 31, 1, 0);
const TZ = 'America/New_York';

const base = { roomId: 'r', active: false, activeEventKind: null, activeEventName: null };

describe('join page room status', () => {
  it('parses the public room endpoint defensively', () => {
    const s = parseRoomStatus(
      { ...base, alwaysOnEnabled: true, openNow: true, nextChampionship: { eventId: 'e', name: 'Friday Night Championship', scheduledAt: FRIDAY_9PM } },
      NOW,
    );
    expect(s).toMatchObject({ active: false, roundsRunAutomatically: true, nextChampionship: { name: 'Friday Night Championship', scheduledAt: FRIDAY_9PM } });
    expect(parseRoomStatus({ active: 'yes', nextChampionship: { name: 3 } }, NOW)).toMatchObject({ active: false, roundsRunAutomatically: false, nextChampionship: null });
  });

  it('promises automatic rounds only when always-on is on and the bar is open', () => {
    expect(idleCopy(parseRoomStatus({ ...base, alwaysOnEnabled: true, openNow: true, nextChampionship: null }, NOW)).detail).toMatch(/start automatically/);
    for (const flags of [
      { alwaysOnEnabled: false, openNow: false },
      { alwaysOnEnabled: false, openNow: true },
      { alwaysOnEnabled: true, openNow: false },
    ]) {
      const copy = idleCopy(parseRoomStatus({ ...base, ...flags, nextChampionship: null }, NOW));
      expect(copy.title).toBe('No games running right now');
      expect(copy.detail).not.toMatch(/automatically/);
    }
  });

  it('names the next championship when one is scheduled', () => {
    const status = parseRoomStatus(
      { ...base, alwaysOnEnabled: false, openNow: false, nextChampionship: { eventId: 'e', name: 'Friday Night Championship', scheduledAt: FRIDAY_9PM } },
      NOW,
    );
    expect(idleCopy(status, TZ).next).toMatch(/^Next: Friday Night Championship, Tomorrow 9:00\sPM$/u);
  });

  it('promises nothing when the status is unknown', () => {
    expect(idleCopy(null)).toEqual({ title: 'No game running right now', next: null, detail: expect.not.stringMatching(/automatically/) });
  });
});
