wrolpi/app/src/components/inventory/inventoryExport.test.js

82 lines
3.3 KiB
JavaScript
Raw Normal View History

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>
2026-06-19 12:39:58 -06:00
import {inventoryExportFilename, shoppingListCSV, toCSV} from "./inventoryExport";
Add CSV/PDF export and a search filter to Inventory New Export tab with two client-side formats (no new dependencies): - CSV: the raw items table (RFC-4180 quoting, UTF-8 BOM for Excel), generated entirely in the browser. The download anchor is attached to the DOM and the object URL is revoked on the next tick for cross-browser robustness. - PDF: a printable full-inventory table followed by a group-by summary table, via the browser's print dialog ("Save as PDF"). The summary's grouping is chosen in the Export tab. Search filter: a box on its own line under the inventory selector filters the active inventory across every column's displayed value (case-insensitive; whitespace-separated terms are AND-ed). It narrows the whole view — Items table, Summary aggregation, and Export/PDF — while edits/adds in the table still operate on the full inventory, so filtering never drops data. Summary/export grouping: - The group-by aggregation is extracted into a shared summarize.js (field-role detection + count-aware totals via units.js + calories), now the single source of truth for the Summary tab, the PDF summary, and the ration calculator. findCaloriesKey/findCountKey moved here from RationCalculator (re-exported for back-compat). - The Sum selector now offers number and calories fields in addition to quantity: number/calories are summed as a plain column total, while quantity stays count-aware and unit-compacted and the dedicated Calories column stays count-aware. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:00:01 -06:00
const FIELDS = [
{key: 'name', label: 'Name', type: 'text', order: 0},
{key: 'item_size', label: 'Size', type: 'quantity', unit: 'lb', order: 1},
{key: 'count', label: 'Count', type: 'number', order: 2},
];
describe('toCSV', () => {
test('emits a header row of labels then one row per item, in field order', () => {
const items = [
{id: 1, name: 'White Rice', item_size: '30', item_size_unit: 'lb', count: '3'},
{id: 2, name: 'Honey', item_size: '12', item_size_unit: 'lb', count: '1'},
];
expect(toCSV(FIELDS, items)).toBe(
'Name,Size,Count\r\n' +
'White Rice,30 lb,3\r\n' +
'Honey,12 lb,1'
);
});
test('orders columns by field order, not array order', () => {
const unordered = [
{key: 'count', label: 'Count', type: 'number', order: 2},
{key: 'name', label: 'Name', type: 'text', order: 0},
];
const csv = toCSV(unordered, [{id: 1, name: 'Salt', count: '5'}]);
expect(csv.split('\r\n')[0]).toBe('Name,Count');
expect(csv.split('\r\n')[1]).toBe('Salt,5');
});
test('quotes values containing commas, quotes, or newlines (RFC 4180)', () => {
const fields = [{key: 'name', label: 'Name', type: 'text', order: 0}];
expect(toCSV(fields, [{id: 1, name: 'Beans, dried'}]).split('\r\n')[1])
.toBe('"Beans, dried"');
expect(toCSV(fields, [{id: 1, name: 'Bob\'s "Best"'}]).split('\r\n')[1])
.toBe('"Bob\'s ""Best"""');
expect(toCSV(fields, [{id: 1, name: 'line1\nline2'}]).split('\r\n').slice(1).join('\r\n'))
.toBe('"line1\nline2"');
});
test('blank/missing values render as empty cells', () => {
expect(toCSV(FIELDS, [{id: 1, name: 'Salt'}])).toBe('Name,Size,Count\r\nSalt,,');
});
test('no items yields just the header row', () => {
expect(toCSV(FIELDS, [])).toBe('Name,Size,Count');
});
});
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>
2026-06-19 12:39:58 -06:00
describe('shoppingListCSV', () => {
test('emits the Item/Have/Buy/New Total columns for each plan row', () => {
const rows = [
{name: 'Canned Beans', current: 96, additional: 29, target: 125},
{name: 'Mixed Vegetables', current: 180, additional: 55, target: 235},
];
expect(shoppingListCSV(rows)).toBe(
'Item,Have,Buy,New Total\r\n' +
'Canned Beans,96,29,125\r\n' +
'Mixed Vegetables,180,55,235'
);
});
test('quotes a name containing a comma, and handles no rows', () => {
expect(shoppingListCSV([{name: 'Beans, dried', current: 1, additional: 1, target: 2}]).split('\r\n')[1])
.toBe('"Beans, dried",1,1,2');
expect(shoppingListCSV([])).toBe('Item,Have,Buy,New Total');
});
});
Add CSV/PDF export and a search filter to Inventory New Export tab with two client-side formats (no new dependencies): - CSV: the raw items table (RFC-4180 quoting, UTF-8 BOM for Excel), generated entirely in the browser. The download anchor is attached to the DOM and the object URL is revoked on the next tick for cross-browser robustness. - PDF: a printable full-inventory table followed by a group-by summary table, via the browser's print dialog ("Save as PDF"). The summary's grouping is chosen in the Export tab. Search filter: a box on its own line under the inventory selector filters the active inventory across every column's displayed value (case-insensitive; whitespace-separated terms are AND-ed). It narrows the whole view — Items table, Summary aggregation, and Export/PDF — while edits/adds in the table still operate on the full inventory, so filtering never drops data. Summary/export grouping: - The group-by aggregation is extracted into a shared summarize.js (field-role detection + count-aware totals via units.js + calories), now the single source of truth for the Summary tab, the PDF summary, and the ration calculator. findCaloriesKey/findCountKey moved here from RationCalculator (re-exported for back-compat). - The Sum selector now offers number and calories fields in addition to quantity: number/calories are summed as a plain column total, while quantity stays count-aware and unit-compacted and the dedicated Calories column stays count-aware. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:00:01 -06:00
describe('inventoryExportFilename', () => {
test('slugifies the inventory name with the given extension', () => {
expect(inventoryExportFilename('Food Storage', 'csv')).toBe('food-storage.csv');
expect(inventoryExportFilename(' My Tools!! ', 'csv')).toBe('my-tools.csv');
});
test('falls back to "inventory" for an empty name', () => {
expect(inventoryExportFilename('', 'csv')).toBe('inventory.csv');
expect(inventoryExportFilename(null, 'csv')).toBe('inventory.csv');
});
});