import { calculateAvailableStock } from './calc-available-stock.helper';

describe('calculateAvailableStock', () => {
  it('should return stock when removedFromStock is undefined', () => {
    // Arrange
    const input = { stock: 10 };

    // Act
    const result = calculateAvailableStock(input);

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

  it('should return 0 when stock is undefined and removedFromStock is undefined', () => {
    // Arrange
    const input = { stock: undefined };

    // Act
    const result = calculateAvailableStock(input);

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

  it('should subtract removedFromStock from stock', () => {
    // Arrange
    const input = { stock: 20, removedFromStock: 5 };

    // Act
    const result = calculateAvailableStock(input);

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

  it('should return 0 if result is negative', () => {
    // Arrange
    const input = { stock: 3, removedFromStock: 5 };

    // Act
    const result = calculateAvailableStock(input);

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

  it('should treat undefined stock as 0', () => {
    // Arrange
    const input = { stock: undefined, removedFromStock: 2 };

    // Act
    const result = calculateAvailableStock(input);

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

  it('should treat undefined removedFromStock as 0', () => {
    // Arrange
    const input = { stock: 7, removedFromStock: undefined };

    // Act
    const result = calculateAvailableStock(input);

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

  it('should return 0 if both stock and removedFromStock are undefined', () => {
    // Arrange
    const input = { stock: undefined, removedFromStock: undefined };

    // Act
    const result = calculateAvailableStock(input);

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