# Bug Condition Exploration Results

## Test Execution Date
2025-01-XX (executed on UNFIXED code)

## Summary
✅ **Bug Confirmed**: All 3 bug condition tests FAILED as expected, confirming the bug exists.

## Counterexamples Found

### Counterexample 1: Abu Dhabi Warehouse
- **Input**: `findBatches(itemId, warehouseAbuDhabiId)`
- **Batch Distribution**:
  - Abu Dhabi Musaffah: 1,000 bags × 20kg = 20 MT
  - Dubai Warehouse: 10,000 bags × 20kg = 200 MT
  - Sharjah Warehouse: 11,000 bags × 20kg = 220 MT
  - Total: 22,000 bags × 20kg = 440 MT
- **Expected**: 20 MT (warehouse-specific)
- **Actual**: 440 MT (global total)
- **Bug Confirmed**: ✅ YES

### Counterexample 2: Dubai Warehouse
- **Input**: `findBatches(itemId, warehouseDubaiId)`
- **Expected**: 200 MT (warehouse-specific)
- **Actual**: 440 MT (global total)
- **Bug Confirmed**: ✅ YES

### Counterexample 3: Zero Bags in Warehouse
- **Input**: `findBatches(itemId, warehouseFujairahId)` (Fujairah has 0 bags)
- **Expected**: Empty array (batch filtered out)
- **Actual**: 1 batch returned with 440 MT
- **Bug Confirmed**: ✅ YES

### Counterexample 4: Single-Warehouse Batch (Control)
- **Input**: `findBatches(itemId, singleWarehouseId)` (batch only exists in one warehouse)
- **Expected**: 10 MT
- **Actual**: 10 MT
- **Bug Confirmed**: ❌ NO (works correctly by coincidence)

## Root Cause Analysis

The test results confirm the hypothesized root cause:

1. **Incorrect MT Calculation**: The `findBatches` method uses `li.totalMt` (440 MT) instead of `li.warehouseBags[warehouseId]` (warehouse-specific bags)

2. **Missing Warehouse Filter**: The method does not filter the MT calculation by the `warehouseId` parameter

3. **Evidence**: 
   - All multi-warehouse scenarios return 440 MT regardless of which warehouse is selected
   - Even warehouses with 0 bags return 440 MT instead of being filtered out
   - Single-warehouse batches work correctly (because totalMt equals warehouse-specific MT by coincidence)

## Conclusion

The bug is confirmed. The `findBatches` method in `items.service.ts` needs to be fixed to:
1. Use `li.warehouseBags[warehouseId]` instead of `li.totalMt`
2. Calculate warehouse-specific MT: `(warehouseBags * bagWeightKg) / 1000`
3. Filter out batches with `availableMt <= 0`

## Next Steps

1. ✅ Task 1 Complete: Bug condition exploration test written and executed
2. ⏭️ Task 2: Write preservation property tests
3. ⏭️ Task 3: Implement the fix
4. ⏭️ Task 4: Verify all tests pass
