diff --git a/scripts/nutrition-import-loseit.test.js b/scripts/nutrition-import-loseit.test.js new file mode 100644 index 00000000..b65a17a2 --- /dev/null +++ b/scripts/nutrition-import-loseit.test.js @@ -0,0 +1,21 @@ +/** + * Lose It! food-log export. Calories and nutrients are the totals for the + * logged amount (50 g of oats is 185 kcal), so the amount must not become the + * serving count that the diary multiplies by. + */ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { parseLoseit } from '../server/lib/nutrition-import/loseit.js'; + +const CSV = [ + 'Date,Name,Icon,Type,Quantity,Units,Calories,Deleted,Fat (g),Protein (g),Carbohydrates (g),Saturated Fat (g),Sugars (g),Fiber (g),Cholesterol (mg),Sodium (mg)', + '08/27/2025,Porridge Oats,Cereal,Breakfast,50.0,Grams,185,0,3.90,6,29.50,0.60,0.45,4.40,0,15', +].join('\n') + '\n'; + +test('the logged amount is the portion, not the number of servings', () => { + const [item] = parseLoseit(CSV); + assert.equal(item.quantity, 1); + assert.equal(item.portion, 50); + assert.equal(item.unit, 'Grams'); + assert.equal(item.nutrition.calories, 185); +}); diff --git a/server/lib/nutrition-import/loseit.js b/server/lib/nutrition-import/loseit.js index 356680e7..b7bd15a9 100644 --- a/server/lib/nutrition-import/loseit.js +++ b/server/lib/nutrition-import/loseit.js @@ -39,7 +39,9 @@ export function parseLoseit(text) { const calories = parseNumber(getField(row, 'calories')); if (calories == null) continue; - const quantity = parseNumber(getField(row, 'quantity')) ?? 1; + // Calories and nutrients are the totals for the logged amount, so the item is one + // serving with the amount as its portion (as the other importers do). + const amount = parseNumber(getField(row, 'quantity')); const unit = getField(row, 'units') || null; const nutrition = { calories }; @@ -58,8 +60,9 @@ export function parseLoseit(text) { mealLabel: getField(row, 'type') || '', name, brand: null, - quantity, - portion: unit ? `${quantity} ${unit}` : null, + quantity: 1, + portion: amount, + unit, nutrition, notes: null, sourceRow: row._rowNum,