import {
  bookingTrainLengthMeters,
  deriveTrainCapacityFromLocomotive,
} from './train-capacity.util';

describe('train-capacity.util', () => {
  const nw5 = { lengthMeters: 14, capacityTons: 70 };

  it('derives wagon slots from locomotive length and weight, not a fixed 53', () => {
    const shortLoco = deriveTrainCapacityFromLocomotive(
      { maxPullWeightTons: 2000, maxTrainLengthMeters: 280 },
      [nw5],
    );
    expect(shortLoco.maxWagonSlots).toBe(20); // 280 / 14
    expect(shortLoco.maxWagonSlots).not.toBe(53);

    const heavyLoco = deriveTrainCapacityFromLocomotive(
      { maxPullWeightTons: 2100, maxTrainLengthMeters: 760 },
      [nw5],
    );
    expect(heavyLoco.maxWagonSlots).toBe(30); // min(54, 30) from weight 2100/70
  });

  it('uses shortest wagon type when mixed types are present', () => {
    const longBulk = { lengthMeters: 18, capacityTons: 80 };
    const mixed = deriveTrainCapacityFromLocomotive(
      { maxPullWeightTons: 3500, maxTrainLengthMeters: 760 },
      [nw5, longBulk],
    );
    expect(mixed.maxWagonSlots).toBe(
      Math.min(Math.floor(760 / 14), Math.floor(3500 / 70)),
    );
  });

  it('computes booking length by freight type', () => {
    expect(
      bookingTrainLengthMeters('CONTAINER', 2, { container: 14, bulk: 14 }),
    ).toBe(28);
    expect(bookingTrainLengthMeters('BULK', 3, { container: 14, bulk: 18 })).toBe(54);
  });
});
