import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import {
  PublicQueryGoodDto,
  PublicTagFilterDto,
} from './public-query-good.dto';

describe('PublicQueryGoodDto', () => {
  it('parses tags from a JSON query parameter into nested DTOs', async () => {
    const dto = plainToInstance(PublicQueryGoodDto, {
      tags: JSON.stringify([
        { tagGroupId: '1', tagIds: ['11', '12'] },
        { tagGroupId: '2', tagIds: ['25'] },
      ]),
    });

    expect(dto.tags).toHaveLength(2);
    expect(dto.tags?.[0]).toBeInstanceOf(PublicTagFilterDto);
    expect(dto.tags?.[0]).toEqual({
      tagGroupId: '1',
      tagIds: ['11', '12'],
    });
    await expect(validate(dto)).resolves.toHaveLength(0);
  });

  it('rejects malformed group and tag ids', async () => {
    const dto = plainToInstance(PublicQueryGoodDto, {
      tags: JSON.stringify([
        { tagGroupId: 'craft', tagIds: ['11', 'bad'] },
      ]),
    });

    expect(await validate(dto)).not.toHaveLength(0);
  });
});
