import {
  describe,
  expect,
  it
} from 'vitest';
import { isSelfPresenceUserId, resolveSelfPresenceUserIds } from './self-presence-identity.rules';

describe('resolveSelfPresenceUserIds', () => {
  it('includes home user id and oderId', () => {
    const ids = resolveSelfPresenceUserIds({
      homeUserId: 'home-id',
      homeOderId: 'peer-a'
    });

    expect([...ids]).toEqual(['home-id', 'peer-a']);
  });

  it('includes the per-server actor user id when provisioned on a foreign server', () => {
    const ids = resolveSelfPresenceUserIds({
      homeUserId: 'home-id',
      homeOderId: 'peer-a',
      actorUserId: 'foreign-id'
    });

    expect([...ids]).toEqual([
      'home-id',
      'peer-a',
      'foreign-id'
    ]);
  });

  it('deduplicates when actor id matches home id', () => {
    const ids = resolveSelfPresenceUserIds({
      homeUserId: 'same-id',
      homeOderId: 'same-id',
      actorUserId: 'same-id'
    });

    expect([...ids]).toEqual(['same-id']);
  });
});

describe('isSelfPresenceUserId', () => {
  it('returns true when the user id is part of the self set', () => {
    const selfIds = resolveSelfPresenceUserIds({
      homeUserId: 'home-id',
      actorUserId: 'foreign-id'
    });

    expect(isSelfPresenceUserId('foreign-id', selfIds)).toBe(true);
    expect(isSelfPresenceUserId('other-id', selfIds)).toBe(false);
  });
});
