import { describe, expect, it } from 'vitest';

import { synthesizeAppApplyServices } from './ipc.js';

describe('app IPC service synthesis', () => {
  it('preserves explicit services arrays', () => {
    const services = [{ name: 'hello', subdomain: 'hello', type: 'proxy' }];
    expect(synthesizeAppApplyServices({ services })).toBe(services);
  });

  it('synthesizes proxy services from app shorthand', () => {
    expect(
      synthesizeAppApplyServices({
        app: 'hello-app',
        subdomain: 'hello',
        port: 20801,
      }),
    ).toEqual([
      {
        name: 'hello-app',
        subdomain: 'hello',
        port: 20801,
        type: 'proxy',
      },
    ]);
  });

  it('defaults the subdomain to the app name and omits invalid ports', () => {
    expect(
      synthesizeAppApplyServices({
        app: 'hello-app',
        port: 'not-a-port',
      }),
    ).toEqual([
      {
        name: 'hello-app',
        subdomain: 'hello-app',
        type: 'proxy',
      },
    ]);
  });

  it('returns null when no valid app or services were supplied', () => {
    expect(synthesizeAppApplyServices({ app: '!!!' })).toBeNull();
    expect(synthesizeAppApplyServices({})).toBeNull();
  });
});
