mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 21:59:58 -05:00
Add a recommended Food Storage example, a Ration tab, and a supply planner
Build on the rebuilt config-only Inventory with three related additions.
Recommended example inventory:
- A fresh install now seeds the "Food Storage" example with the recommended
one-adult / one-year (~3,000 kcal/day) emergency food supply (60 items,
~1.12M kcal), instead of an empty inventory.
- The counts are layered on top of the shipped food catalog (single source of
per-package nutrition) via RECOMMENDED_ONE_YEAR_FOOD_COUNTS, joined at seed
time by recommended_food_storage_items().
- Genericized the two remaining brand names in the catalog defaults
(Velveeta -> Processed Cheese, Spam -> Canned Lunch Meat) so both the catalog
and the seeded inventory stay brand-free.
Ration tab (split out of Summary):
- Moved the Ration Estimate (People / Daily Calories / estimate table) out of
the increasingly long Summary tab into its own "Ration" tab, shown only for
inventories that have a calories field.
Supply planner ("Plan More Supply"):
- A slider picks a target duration longer than the current estimate; the whole
inventory is scaled up proportionally (every item by the same factor, mix
kept balanced) into a sortable shopping list of what to buy.
- Any split package rounds UP to a whole one (ceil), matching the "1.6 buckets
-> buy 1 more" rule.
- The shopping list is exportable as CSV (reusing toCSV) and PDF (reusing the
print-to-PDF path, scoped via body.printing-shopping so only the list prints
-- no inventory or summary).
Search filter:
- Moved the items search out of the top toolbar (which implied it filtered
every tab) into the Items tab, so it clearly narrows Items only;
Summary/Ration/Export/print always use the full inventory.
Aggregation/role helpers (findNameKey, planSupplyPurchase) live in summarize.js,
the single source of truth shared by Summary, export, and the ration views.
All new pure functions and UI flows are covered by tests.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
5427beb5fb
commit
ee0b827c71
14 changed files with 545 additions and 38 deletions
|
|
@ -1,4 +1,8 @@
|
|||
import {dailyCalorieDemand, daysOfFood, findCaloriesKey, findCountKey, totalCalories} from "./RationCalculator";
|
||||
import React from "react";
|
||||
import {fireEvent, render, screen, within} from "@testing-library/react";
|
||||
import {
|
||||
dailyCalorieDemand, daysOfFood, findCaloriesKey, findCountKey, RationEstimatePanel, totalCalories,
|
||||
} from "./RationCalculator";
|
||||
|
||||
describe('findCaloriesKey', () => {
|
||||
test('detects a calories-type field', () => {
|
||||
|
|
@ -71,3 +75,76 @@ describe('daysOfFood', () => {
|
|||
expect(daysOfFood(14000, 0)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('RationEstimatePanel supply plan', () => {
|
||||
const FIELDS = [
|
||||
{key: 'name', label: 'Name', type: 'text', order: 0},
|
||||
{key: 'count', label: 'Count', type: 'number', order: 1},
|
||||
{key: 'calories', label: 'kcal', type: 'calories', order: 2},
|
||||
];
|
||||
// 100 cans × 450 kcal = 45,000 kcal; with a survival ration for one man (1,500/day) that lasts exactly 30 days.
|
||||
const ITEMS = [{name: 'Beans', count: '100', calories: '450'}];
|
||||
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
// Deterministic household: one man on the Survival preset → 1,500 kcal/day.
|
||||
window.localStorage.setItem('ration_preset', JSON.stringify('survival'));
|
||||
window.localStorage.setItem('ration_counts', JSON.stringify({men: '1', women: '', children: ''}));
|
||||
});
|
||||
|
||||
test('dragging the target slider produces a round-up shopping list', () => {
|
||||
render(<RationEstimatePanel items={ITEMS} fields={FIELDS} caloriesKey='calories' countKey='count'/>);
|
||||
|
||||
// Current estimate is 1 month, so the slider starts there with no purchases yet.
|
||||
expect(screen.getByText(/drag the slider/i)).toBeTruthy();
|
||||
|
||||
// Drag to a 2-month target: scale ×2 → 100 → 200 cans, buy 100.
|
||||
fireEvent.change(screen.getByLabelText(/target duration/i), {target: {value: '2'}});
|
||||
|
||||
// Scope to the on-screen table (the hidden print block duplicates these texts in the DOM).
|
||||
const plan = within(document.querySelector('table.sortable'));
|
||||
expect(plan.getByText('Beans')).toBeTruthy();
|
||||
expect(plan.getByText('+100')).toBeTruthy();
|
||||
// Summary line reports the total to buy.
|
||||
expect(screen.getByText(/additional packages/).textContent).toMatch(/100 additional packages/);
|
||||
});
|
||||
|
||||
test('clicking a column header re-sorts the shopping list', () => {
|
||||
const items = [
|
||||
{name: 'Apples', count: '10', calories: '100'},
|
||||
{name: 'Beans', count: '200', calories: '100'},
|
||||
];
|
||||
render(<RationEstimatePanel items={items} fields={FIELDS} caloriesKey='calories' countKey='count'/>);
|
||||
fireEvent.change(screen.getByLabelText(/target duration/i), {target: {value: '2'}});
|
||||
|
||||
const itemNames = () =>
|
||||
[...document.querySelectorAll('table.sortable tbody tr td:first-child')].map(td => td.textContent);
|
||||
// Default sort is largest purchase first (Beans buys far more than Apples).
|
||||
expect(itemNames()).toEqual(['Beans', 'Apples']);
|
||||
|
||||
// Sorting by Item ascending flips to alphabetical (scope to the sortable table; the print block also has an
|
||||
// "Item" header).
|
||||
fireEvent.click(within(document.querySelector('table.sortable')).getByText('Item'));
|
||||
expect(itemNames()).toEqual(['Apples', 'Beans']);
|
||||
});
|
||||
|
||||
test('a populated plan offers CSV and PDF export, and printing scopes to the shopping list', () => {
|
||||
const printSpy = jest.spyOn(window, 'print').mockImplementation(() => {});
|
||||
render(<RationEstimatePanel name='Food Storage' items={ITEMS} fields={FIELDS}
|
||||
caloriesKey='calories' countKey='count'/>);
|
||||
fireEvent.change(screen.getByLabelText(/target duration/i), {target: {value: '2'}});
|
||||
|
||||
expect(screen.getByText(/Download CSV/i)).toBeTruthy();
|
||||
|
||||
// Printing toggles the body class the @media print rules use to show only the shopping list.
|
||||
fireEvent.click(screen.getByText(/Print/i));
|
||||
expect(printSpy).toHaveBeenCalled();
|
||||
expect(document.body.classList.contains('printing-shopping')).toBe(true);
|
||||
printSpy.mockRestore();
|
||||
});
|
||||
|
||||
test('without a count field, the plan asks for one instead of a list', () => {
|
||||
render(<RationEstimatePanel items={ITEMS} fields={FIELDS} caloriesKey='calories' countKey={null}/>);
|
||||
expect(screen.getByText(/add a/i).textContent.toLowerCase()).toContain('count');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue