import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EmbyClient, type IEmbySnapshot } from '../../ts/integrations/emby/index.js';

const snapshot: IEmbySnapshot = {
  server: { Id: 'server-1', ServerName: 'Home Emby' },
  sessions: [],
  online: true,
  source: 'snapshot',
};

tap.test('returns static Emby snapshots without requiring network access', async () => {
  const client = new EmbyClient({ snapshot });
  const result = await client.getSnapshot();
  expect(result.online).toBeTrue();
  expect(result.server.ServerName).toEqual('Home Emby');
  expect(result.source).toEqual('snapshot');
});

tap.test('uses injected Emby clients for snapshots', async () => {
  const client = new EmbyClient({
    client: {
      async getSystemInfo() {
        return { Id: 'server-2', ServerName: 'Injected Emby' };
      },
      async getSessions() {
        return [{ Id: 'session-2', DeviceName: 'Tablet', DeviceId: 'tablet-1', SupportsRemoteControl: true }];
      },
    },
  });
  const result = await client.getSnapshot();
  expect(result.source).toEqual('client');
  expect(result.server.ServerName).toEqual('Injected Emby');
  expect(result.sessions[0].Id).toEqual('session-2');
});

tap.test('does not report command success without local token, client, or executor', async () => {
  const client = new EmbyClient({ snapshot });
  let error: unknown;
  try {
    await client.pause('session-1');
  } catch (errorArg) {
    error = errorArg;
  }
  expect(error instanceof Error ? error.message : String(error)).toInclude('Static snapshots/manual data are read-only');
});

export default tap.start();
