wrolpi/app/src/components/calculators/FoodStorageCalculator.test.js
Roland 3d6aead072
Some checks failed
CodeQL / Analyze (push) Has been cancelled
Controller Tests / controller-tests (push) Has been cancelled
Jest Tests / jest-tests (push) Has been cancelled
Rebuild Inventory as a config-only system with food catalog and calculators
Replace the DB-backed inventory module with a config-only design and build out
food-storage tooling around it.

Inventory (config-only, no database)
- Each inventory is a YAML file at config/inventory/<slug>.yaml, loaded once
  into memory and the single source of truth. New reusable MultiFileConfig
  (wrolpi/common.py) manages a directory of per-entity files.
- Inventory types (food/fuel/tool) seed a customizable field schema
  (text/number/quantity/date/select/location/calories). Unit math/aggregation
  moved to the frontend (mathjs); Pint dropped from the backend.
- Minimal API: GET / (all inventories full), POST /, PUT /<slug> (whole-
  inventory save with version-based optimistic concurrency; 404 for missing),
  DELETE. Legacy inventories.yaml / old tables migrate to per-inventory files
  (pound->lb), and an alembic migration drops the tables.

Frontend
- New app/src/components/inventory/ package: spreadsheet-style table with
  Tab/Enter keyboard entry, sortable columns, expired-row highlighting,
  customizable field editor, and pooled cross-inventory location autocomplete.
- A single useInventories store loads everything once and persists whole
  inventories optimistically; list/items/summary/locations derive client-side.
- Summary groups items (default by Category), totals size x count, shows a
  calories column, and embeds a ration estimate (how long the stock lasts).

Food catalog
- Shared, editable catalog (config/inventory/catalog.yaml) seeded from
  catalog_defaults.yaml (~60 generic items, total calories per package) with
  merge-keep-edits + tombstones. GET/PUT /api/inventory/catalog, a management
  modal, and entry-row pre-fill of category/size/calories from a name match.

Calculators
- Generic Food Storage planner (like the Water calculator): household +
  editable per-person/year amounts -> total to store over a chosen duration,
  with women/children weighted by DGA calorie shares and an imperial/metric
  toggle.

Also fixes ConfigFile.save() to accept nested file_names. Backend pytest and
frontend jest suites updated/added throughout; alembic check clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 17:14:54 -06:00

60 lines
2.4 KiB
JavaScript

import {adultEquivalents, amountNeeded, convertAmounts, DEFAULT_SHARES, formatMonths, totalPeople} from "./FoodStorageCalculator";
describe('convertAmounts', () => {
test('converts lb to kg and back, preserving blanks', () => {
const lb = {grains: '300', aux: ''};
const kg = convertAmounts(lb, true);
expect(Number(kg.grains)).toBeCloseTo(136.08, 1); // 300 lb -> ~136 kg
expect(kg.aux).toBe('');
const back = convertAmounts(kg, false);
expect(Number(back.grains)).toBeCloseTo(300, 0);
});
});
describe('adultEquivalents', () => {
test('weights women and children below an adult man', () => {
// 1 man (100%) + 1 woman (77%) + 1 child (69%) = 2.46 adult-equivalents.
expect(adultEquivalents({men: '1', women: '1', children: '1'}, DEFAULT_SHARES)).toBeCloseTo(2.46, 5);
});
test('a household of adult men equals the head count', () => {
expect(adultEquivalents({men: '3', women: '', children: ''}, DEFAULT_SHARES)).toBe(3);
});
test('custom shares are respected', () => {
expect(adultEquivalents({men: '0', women: '2', children: '0'}, {men: 100, women: 50, children: 69}))
.toBeCloseTo(1, 5);
});
});
describe('totalPeople', () => {
test('sums men, women, and children', () => {
expect(totalPeople({men: '2', women: '1', children: '3'})).toBe(6);
});
test('ignores blank/negative counts', () => {
expect(totalPeople({men: '1', women: '', children: '-2'})).toBe(1);
});
});
describe('amountNeeded', () => {
test('scales per-person-per-year by people and duration (one man, one year)', () => {
expect(amountNeeded(300, 1, 12)).toBe(300); // grains, 1 man, 1 year
});
test('scales by household and partial year', () => {
expect(amountNeeded(300, 4, 6)).toBe(600); // 300 * 4 people * 0.5 year
});
test('zero for impossible inputs', () => {
expect(amountNeeded(0, 4, 12)).toBe(0);
expect(amountNeeded(300, 0, 12)).toBe(0);
expect(amountNeeded(300, 4, 0)).toBe(0);
});
});
describe('formatMonths', () => {
test('whole years read as years', () => {
expect(formatMonths(12)).toBe('1 year');
expect(formatMonths(24)).toBe('2 years');
});
test('otherwise months', () => {
expect(formatMonths(3)).toBe('3 months');
expect(formatMonths(1)).toBe('1 month');
});
});