diff --git a/app/src/components/calculators/WaterCalculator.js b/app/src/components/calculators/WaterCalculator.js index 61b16e60..42ac3744 100644 --- a/app/src/components/calculators/WaterCalculator.js +++ b/app/src/components/calculators/WaterCalculator.js @@ -57,6 +57,29 @@ export function waterWeight(volume, metric) { return metric ? volume * 1 : volume * 8.345; } +/** + * Pure function that computes the main derived values the UI shows, + * given the same inputs the component works with. This is primarily + * exposed to enable high-quality lifecycle / integration-style tests. + */ +export function computeStorage({ counts, rates, extra = '', days, metric = false }) { + const baseDaily = dailyDemand(counts, rates); + const extraDaily = Number(extra) > 0 ? Number(extra) : 0; + const daily = baseDaily + extraDaily; + + const total = totalWater(daily, days); + const weight = waterWeight(total, metric); + + // Convenient container results for common sizes used in tests + const bucketCapacity = metric ? 18.9 : 5; // ~5 gal bucket or ~19 L + const bucket = total !== null ? containersNeeded(total, bucketCapacity) : null; + + const ibcCapacity = metric ? 1041 : 275; + const ibc = total !== null ? containersNeeded(total, ibcCapacity) : null; + + return { daily, total, weight, bucket, ibc }; +} + // Human-friendly label for a duration in days: days up to a week, then weeks, then months, // then whole years. Keeps the slider from showing awkward values like "68 days". export function formatDuration(days) { @@ -176,7 +199,7 @@ function fmt(value, unit) { // Keep a numeric input non-negative without disrupting in-progress decimal entry. // A can only become negative via a leading "-", so stripping it is // enough; returning the raw string otherwise lets the user finish typing "1.5". -function clampNonNegative(value) { +export function clampNonNegative(value) { if (value === '') { return ''; } diff --git a/app/src/components/calculators/WaterCalculator.test.js b/app/src/components/calculators/WaterCalculator.test.js index 9d84ad05..0a855101 100644 --- a/app/src/components/calculators/WaterCalculator.test.js +++ b/app/src/components/calculators/WaterCalculator.test.js @@ -1,4 +1,6 @@ import { + clampNonNegative, + computeStorage, CONTAINERS, containersNeeded, dailyDemand, @@ -35,15 +37,40 @@ describe('dailyDemand', () => { }); }); -describe('totalWater', () => { - test('multiplies daily demand by the number of days', () => { - expect(totalWater(4, 14)).toBe(56); +describe('daily demand including extra', () => { + test('adds extra demand on top of per-person demand', () => { + const base = dailyDemand({men: 2, women: 1}, {men: 1, women: 0.9}); + expect(base).toBeCloseTo(2.9, 5); + + const totalWithExtra = base + 5; // 5 extra gallons per day + expect(totalWithExtra).toBeCloseTo(7.9, 5); }); - test('returns null when demand or days is not positive', () => { + test('extra demand works when there are no people', () => { + const base = dailyDemand({men: 0, women: 0, children: 0, pregnant: 0}, {men: 1, women: 1}); + expect(base).toBe(0); + + const totalWithExtra = base + 12; + expect(totalWithExtra).toBe(12); + }); + + test('treats non-positive extra as zero', () => { + const base = dailyDemand({men: 1}, {men: 2}); + expect(base + Math.max(0, Number(-3))).toBe(2); + expect(base + Math.max(0, Number(''))).toBe(2); + }); +}); + +describe('totalWater', () => { + test('returns null for non-positive inputs', () => { expect(totalWater(0, 14)).toBeNull(); expect(totalWater(4, 0)).toBeNull(); - expect(totalWater(NaN, 14)).toBeNull(); + expect(totalWater(-5, 10)).toBeNull(); + expect(totalWater(10, NaN)).toBeNull(); + }); + + test('computes total for valid positive inputs', () => { + expect(totalWater(4.5, 14)).toBe(63); }); }); @@ -61,23 +88,29 @@ describe('containersNeeded', () => { expect(containersNeeded(0.1, 55)).toEqual({count: 1, leftover: 54.9}); }); + test('handles values very close to container boundaries (floating point)', () => { + // Just under 2 full 5-gal buckets + const result = containersNeeded(9.999999, 5); + expect(result.count).toBe(2); + expect(result.leftover).toBeCloseTo(0.000001, 5); + }); + test('returns null for impossible inputs', () => { expect(containersNeeded(0, 5)).toBeNull(); expect(containersNeeded(10, 0)).toBeNull(); + expect(containersNeeded(NaN, 5)).toBeNull(); }); }); describe('waterWeight', () => { - test('imperial uses ~8.345 lb per gallon', () => { - expect(waterWeight(10, false)).toBeCloseTo(83.45, 2); - }); - - test('metric uses 1 kg per liter', () => { - expect(waterWeight(10, true)).toBe(10); - }); - test('returns null for non-positive volume', () => { expect(waterWeight(0, false)).toBeNull(); + expect(waterWeight(-3, true)).toBeNull(); + }); + + test('converts using standard densities', () => { + expect(waterWeight(10, false)).toBeCloseTo(83.45, 2); + expect(waterWeight(10, true)).toBe(10); }); }); @@ -111,47 +144,129 @@ describe('formatDuration', () => { }); }); -describe('constants', () => { - const CATEGORIES = ['men', 'women', 'children', 'pregnant']; - - test('presets define imperial and metric rates for every category', () => { - for (const preset of Object.values(WATER_PRESETS)) { - for (const system of [preset.imperial, preset.metric]) { - for (const cat of CATEGORIES) { - expect(system[cat]).toBeGreaterThan(0); - } - } - } - }); - - test('comfortable preset is wetter than the minimum for every category', () => { - const {minimum, comfortable} = WATER_PRESETS; - for (const cat of CATEGORIES) { - expect(comfortable.imperial[cat]).toBeGreaterThan(minimum.imperial[cat]); - expect(comfortable.metric[cat]).toBeGreaterThan(minimum.metric[cat]); - } - }); - - test('pregnant/nursing rate exceeds the baseline woman rate', () => { +describe('data invariants (presets, containers, duration stops)', () => { + test('pregnant/nursing rate is meaningfully higher than baseline woman rate', () => { for (const preset of Object.values(WATER_PRESETS)) { expect(preset.imperial.pregnant).toBeGreaterThan(preset.imperial.women); expect(preset.metric.pregnant).toBeGreaterThan(preset.metric.women); } }); - test('every container has a unique key and positive capacities', () => { + test('comfortable preset uses higher rates than minimum', () => { + const {minimum, comfortable} = WATER_PRESETS; + ['men', 'women', 'children', 'pregnant'].forEach(cat => { + expect(comfortable.imperial[cat]).toBeGreaterThan(minimum.imperial[cat]); + expect(comfortable.metric[cat]).toBeGreaterThan(minimum.metric[cat]); + }); + }); + + test('all defined containers have positive capacity in both units and unique keys', () => { const keys = CONTAINERS.map(c => c.key); expect(new Set(keys).size).toBe(keys.length); + CONTAINERS.forEach(c => { expect(c.gallons).toBeGreaterThan(0); expect(c.liters).toBeGreaterThan(0); }); }); - test('duration stops are strictly increasing and start at one day', () => { + test('duration stops start at 1 day and are strictly increasing', () => { expect(DURATION_STOPS[0]).toBe(1); for (let i = 1; i < DURATION_STOPS.length; i++) { expect(DURATION_STOPS[i]).toBeGreaterThan(DURATION_STOPS[i - 1]); } }); }); + +describe('realistic scenarios', () => { + test('typical family of 4 for 14 days using minimum rates + some extra', () => { + const rates = WATER_PRESETS.minimum.imperial; + const counts = { men: 1, women: 1, children: 2, pregnant: 0 }; + const extra = 2; // e.g. two large dogs + + const daily = dailyDemand(counts, rates) + extra; + const total = totalWater(daily, 14); + + // 1.0 + 0.9 + 0.8*2 = 3.5 base + 2 extra = 5.5 gal/day + expect(daily).toBeCloseTo(5.5, 5); + expect(total).toBe(77); + + const buckets = containersNeeded(total, 5); + expect(buckets.count).toBe(16); // 16 × 5 gal = 80 gal capacity + expect(buckets.leftover).toBeCloseTo(3, 5); + }); + + test('large household for 90 days in metric using comfortable preset', () => { + const rates = WATER_PRESETS.comfortable.metric; + const counts = { men: 2, women: 2, children: 3, pregnant: 1 }; + + const daily = dailyDemand(counts, rates); + const totalLiters = totalWater(daily, 90); + + expect(daily).toBeGreaterThan(30); + expect(totalLiters).toBeGreaterThan(2700); + + const ibcTotes = containersNeeded(totalLiters, 1041); + expect(ibcTotes.count).toBeGreaterThanOrEqual(3); + }); +}); + +describe('realistic user lifecycle', () => { + test('user changes multiple inputs over time and outputs update correctly', () => { + // Start: Imperial, Minimum preset, 1 man + 1 woman, 14 days + let state = { + metric: false, + preset: 'minimum', + counts: { men: '1', women: '1', children: '', pregnant: '' }, + extra: '', + rates: { ...WATER_PRESETS.minimum.imperial }, + days: 14, + }; + + let outputs = computeStorage(state); + expect(outputs.daily).toBeCloseTo(1.9, 5); // 1.0 + 0.9 + expect(outputs.total).toBeCloseTo(26.6, 5); + expect(outputs.bucket.count).toBe(6); // 5-gal buckets + + // User switches to Metric + state = { ...state, metric: true, rates: { ...WATER_PRESETS.minimum.metric } }; + outputs = computeStorage(state); + expect(outputs.daily).toBeCloseTo(7.2, 5); // 3.8 + 3.4 in liters + expect(outputs.weight).toBeCloseTo(100.8, 5); // 14 days * 7.2 L ≈ 100.8 kg + + // User adds a child + state.counts = { ...state.counts, children: '1' }; + outputs = computeStorage(state); + expect(outputs.daily).toBeCloseTo(10.2, 5); // +3.0 for child + + // User manually edits the men's rate upward (simulating typing) + state.rates = { ...state.rates, men: clampNonNegative('5.5') }; + outputs = computeStorage(state); + // 5.5 (men) + 3.4 (women) + 3.0 (child) = 11.9 base + expect(outputs.daily).toBeCloseTo(11.9, 5); + + // User adds some extra demand for a dog + state.extra = clampNonNegative('3'); + outputs = computeStorage(state); + expect(outputs.daily).toBeCloseTo(14.9, 5); // 11.9 + 3 + + // User changes duration to 90 days via slider + state.days = 90; + outputs = computeStorage(state); + expect(outputs.total).toBeCloseTo(1341, 5); // 14.9 * 90 + + // User switches preset to Comfortable (this would normally reset rates in the component) + state = { + ...state, + preset: 'comfortable', + rates: { ...WATER_PRESETS.comfortable.metric }, + }; + outputs = computeStorage(state); + // Comfortable rates are higher, so daily should jump even with same counts + extra + expect(outputs.daily).toBeGreaterThan(20); + + // Final sanity: still using metric, total water is now quite large + expect(outputs.total).toBeGreaterThan(1800); + expect(outputs.ibc.count).toBeGreaterThanOrEqual(2); + }); +});