import { ValidationPipe } from '@nestjs/common';
import {
  DingTalkThirdConfigDto,
  IntegrationType,
  SaveIntegrationConfigDto,
  WeComThirdConfigDto,
} from './config.dto';

const pipe = new ValidationPipe({ transform: true, whitelist: true });

const transform = (value: unknown) =>
  pipe.transform(value, { type: 'body', metatype: SaveIntegrationConfigDto });

describe('integration config request DTO', () => {
  it('validates and transforms DingTalk configuration', async () => {
    const result = await transform({
      type: 'DINGTALK',
      config: {
        agentId: 'app-key',
        corpId: 'corp-id',
        appSecret: '',
        appId: 'app-id',
        ignored: 'value',
      },
    });

    expect(result.type).toBe(IntegrationType.DINGTALK);
    expect(result.config).toBeInstanceOf(DingTalkThirdConfigDto);
    expect(result.config).toMatchObject({ agentId: 'app-key', corpId: 'corp-id', appSecret: '' });
    expect(result.config).not.toHaveProperty('ignored');
  });

  it('uses the WeCom nested DTO and removes DingTalk-only fields', async () => {
    const result = await transform({
      type: 'WECOM',
      config: { agentId: 'agent', corpId: 'corp', appId: 'not-supported' },
    });

    expect(result.config).toBeInstanceOf(WeComThirdConfigDto);
    expect(result.config).not.toHaveProperty('appId');
  });

  it('rejects invalid platform types and incomplete nested config', async () => {
    await expect(transform({ type: 'UNKNOWN', config: {} })).rejects.toThrow();
    await expect(transform({ type: 'DINGTALK', config: { corpId: 'corp' } })).rejects.toThrow();
    await expect(transform({ type: 'DINGTALK' })).rejects.toThrow();
  });
});
