import assert from 'node:assert/strict';
import { test } from 'node:test';
import { paneBadges } from '../src/lib/pane-badges.ts';
import type { RoomSummary } from '../src/lib/types.ts';

function room(id: string, extra: Partial<RoomSummary> = {}): RoomSummary {
  return {
    room_id: id,
    name: id,
    membership: 'Joined',
    space: false,
    direct: false,
    unread_messages: 0,
    unread_mentions: 0,
    notification_mode: 'AllMessages',
    notification_mode_custom: false,
    parent_spaces: [],
    children: [],
    ...extra
  } as RoomSummary;
}

const isMuted = (r: RoomSummary) => r.notification_mode === 'Mute';

const rooms = [
  room('!a', { unread_messages: 5, unread_mentions: 1 }),
  room('!b', { unread_messages: 4, unread_mentions: 2 }),
  room('!quiet', { unread_messages: 9 }),
  room('!dm', { direct: true, unread_messages: 3 }),
  room('!muted', { unread_mentions: 4, notification_mode: 'Mute' }),
  room('!left', { unread_mentions: 4, membership: 'Left' })
];

test('mentions in the shown space go on channels, others and DMs on the rail', () => {
  const badges = paneBadges(rooms, new Set(['!a', '!quiet']), null, isMuted);
  assert.deepEqual(badges.channels, { count: 1, mention: true });
  assert.deepEqual(badges.rail, { count: 5, mention: true });
});

test('home lists every room, so a DM goes on channels', () => {
  const badges = paneBadges(rooms, new Set(rooms.map((r) => r.room_id)), null, isMuted);
  assert.deepEqual(badges.channels, { count: 6, mention: true });
  assert.deepEqual(badges.rail, { count: 0, mention: false });
});

test('the open room, muted rooms and plain unread channels count for nothing', () => {
  const badges = paneBadges(rooms, new Set(), '!b', (r) => isMuted(r) || r.room_id === '!a');
  assert.deepEqual(badges.rail, { count: 3, mention: false });
});
