import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { ConsumeInventoryDto } from './dto/consume-inventory.dto';
import { CreateInventoryDto } from './dto/create-inventory.dto';
import { UpdateInventoryDto } from './dto/update-inventory.dto';

describe('Inventory DTO validation security', () => {
  it('CreateInventoryDto nekar negativ quantity', async () => {
    const dto = plainToInstance(CreateInventoryDto, {
      productId: 10,
      quantity: -1,
      unit: 'st',
    });

    const errors = await validate(dto);
    expect(errors.length).toBeGreaterThan(0);
    expect(errors.some((e) => e.property === 'quantity')).toBe(true);
  });

  it('CreateInventoryDto nekar icke-numerisk quantity', async () => {
    const dto = plainToInstance(CreateInventoryDto, {
      productId: 10,
      quantity: 'abc',
      unit: 'st',
    });

    const errors = await validate(dto);
    expect(errors.some((e) => e.property === 'quantity')).toBe(true);
  });

  it('UpdateInventoryDto nekar ogiltig opened-typ', async () => {
    const dto = plainToInstance(UpdateInventoryDto, {
      opened: 'true',
    });

    const errors = await validate(dto);
    expect(errors.some((e) => e.property === 'opened')).toBe(true);
  });

  it('ConsumeInventoryDto nekar amountUsed under minimum', async () => {
    const dto = plainToInstance(ConsumeInventoryDto, {
      amountUsed: 0,
    });

    const errors = await validate(dto);
    expect(errors.some((e) => e.property === 'amountUsed')).toBe(true);
  });
});
