import { expect, tap } from '@git.zone/tstest/tapbundle';
import { ForkedDaapdIntegration } from '../../ts/integrations/forked_daapd/index.js';

tap.test('creates OwnTone config from discovered endpoint', async () => {
  const integration = new ForkedDaapdIntegration();
  const step = await integration.configFlow.start({
    source: 'mdns',
    integrationDomain: 'forked_daapd',
    id: 'Living Room',
    host: 'living-room.local',
    port: 3689,
    name: 'Living Room',
  }, {});

  expect(step.kind).toEqual('form');
  const done = await step.submit!({ password: 'secret', name: 'Music Server' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('living-room.local');
  expect(done.config?.port).toEqual(3689);
  expect(done.config?.password).toEqual('secret');
  expect(done.config?.uniqueId).toEqual('Living Room');
  expect(done.config?.name).toEqual('Music Server');
});

tap.test('requires host and validates OwnTone API port', async () => {
  const integration = new ForkedDaapdIntegration();
  const missingHost = await integration.configFlow.start({ source: 'manual', integrationDomain: 'forked_daapd' }, {});
  const missingResult = await missingHost.submit!({});
  expect(missingResult.kind).toEqual('error');

  const invalidPort = await missingHost.submit!({ host: '192.168.1.50', port: 70000 });
  expect(invalidPort.kind).toEqual('error');
});

export default tap.start();
