import { describe, it, expect } from 'vitest';
import { PresenceService } from './presence.service';

describe('PresenceService', () => {
  it('reports viewing only for the focused thread, and clears on disconnect', async () => {
    const p = new PresenceService();
    expect(await p.isViewing('alice', 'T1')).toBe(false);
    await p.setFocus('sock1', 'alice', 'T1');
    expect(await p.isViewing('alice', 'T1')).toBe(true);
    expect(await p.isViewing('alice', 'T2')).toBe(false); // joined-elsewhere ≠ viewing
    await p.setFocus('sock1', 'alice', 'T2'); // moved focus
    expect(await p.isViewing('alice', 'T1')).toBe(false);
    expect(await p.isViewing('alice', 'T2')).toBe(true);
    await p.clearSocket('sock1');
    expect(await p.isViewing('alice', 'T2')).toBe(false);
  });

  it('any of the actor’s sockets counts as viewing', async () => {
    const p = new PresenceService();
    await p.setFocus('sockA', 'bob', 'T9');
    await p.setFocus('sockB', 'bob', null); // a second tab, no focus
    expect(await p.isViewing('bob', 'T9')).toBe(true);
  });
});
