import { getReceiptItemReturnedQuantity } from './get-receipt-item-returned-quantity.helper';

describe('getReceiptItemReturnedQuantity', () => {
  it('should return 0 when referencedInOtherReceipts is missing', () => {
    // Arrange
    const item = { id: 123 };

    // Act
    const result = getReceiptItemReturnedQuantity(item);

    // Assert
    expect(result).toBe(0);
  });

  it('should return 0 when referencedInOtherReceipts is an empty array', () => {
    // Arrange
    const item = { id: 123, referencedInOtherReceipts: [] };

    // Act
    const result = getReceiptItemReturnedQuantity(item);

    // Assert
    expect(result).toBe(0);
  });

  it('should sum all quantities in referencedInOtherReceipts', () => {
    // Arrange
    const item = {
      id: 123,
      referencedInOtherReceipts: [
        { quantity: 2 },
        { quantity: 3 },
        { quantity: 5 },
      ],
    };

    // Act
    const result = getReceiptItemReturnedQuantity(item);

    // Assert
    expect(result).toBe(10);
  });

  it('should treat null or undefined quantities as 0', () => {
    // Arrange
    const item = {
      id: 123,
      referencedInOtherReceipts: [
        { quantity: 2 },
        { quantity: null as unknown as number },
        { quantity: undefined as unknown as number },
        { quantity: 3 },
      ],
    };

    // Act
    const result = getReceiptItemReturnedQuantity(item);

    // Assert
    expect(result).toBe(5);
  });
});
