155 lines
7.4 KiB
TypeScript
155 lines
7.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { formatKitchen, formatQuantity, fromBase, impliedKitchenUnit, niceQty, parseNumber, scale, shoppingQuantity, sizeOf, toBase, type BaseUnit, type KitchenUnit } from "@/lib/units";
|
|
|
|
describe("units", () => {
|
|
it("scales per-portion amounts", () => {
|
|
expect(scale(125, 4)).toBe(500);
|
|
expect(scale(0.25, 4)).toBe(1);
|
|
});
|
|
|
|
it("formats weights and volumes the way a kitchen reads them", () => {
|
|
expect(formatQuantity(125, "g")).toEqual({ amount: "125", unit: "g" });
|
|
expect(formatQuantity(1500, "g")).toEqual({ amount: "1,5", unit: "kg" });
|
|
expect(formatQuantity(1000, "ml")).toEqual({ amount: "1", unit: "l" });
|
|
expect(formatQuantity(482, "g")).toEqual({ amount: "480", unit: "g" });
|
|
expect(formatQuantity(2.3, "g")).toEqual({ amount: "2,5", unit: "g" });
|
|
expect(formatQuantity(1500, "g", "en")).toEqual({ amount: "1.5", unit: "kg" });
|
|
});
|
|
|
|
it("shows pieces as kitchen fractions", () => {
|
|
expect(formatQuantity(0.5, "piece").amount).toBe("½");
|
|
expect(formatQuantity(1.25, "piece").amount).toBe("1¼");
|
|
expect(formatQuantity(0.75, "piece").amount).toBe("¾");
|
|
expect(formatQuantity(2, "piece").amount).toBe("2");
|
|
expect(formatQuantity(1.4, "piece").amount).toBe("1,4");
|
|
});
|
|
|
|
it("rounds pieces up for shopping", () => {
|
|
expect(shoppingQuantity(0.25, "piece")).toBe(1);
|
|
expect(shoppingQuantity(2, "piece")).toBe(2);
|
|
expect(shoppingQuantity(480, "g")).toBe(480);
|
|
});
|
|
|
|
it("converts kitchen measures into the base unit, and refuses what does not fit", () => {
|
|
expect(toBase(1.5, "kg", "g")).toBe(1500);
|
|
expect(toBase(2, "tbsp", "ml")).toBe(30);
|
|
expect(toBase(1, "l", "ml")).toBe(1000);
|
|
expect(toBase(2, "tsp", "g")).toBe(8);
|
|
expect(toBase(3, "piece", "piece")).toBe(3);
|
|
expect(toBase(200, "g", "piece")).toBeNull();
|
|
expect(toBase(2, "piece", "g")).toBeNull();
|
|
expect(toBase(-1, "g", "g")).toBeNull();
|
|
});
|
|
|
|
it("reads numbers in German and English notation", () => {
|
|
expect(parseNumber("1,5")).toBe(1.5);
|
|
expect(parseNumber("1.5")).toBe(1.5);
|
|
expect(parseNumber("1 1/2")).toBe(1.5);
|
|
expect(parseNumber("½")).toBe(0.5);
|
|
expect(parseNumber("2½")).toBe(2.5);
|
|
expect(parseNumber("3/4")).toBe(0.75);
|
|
expect(parseNumber("")).toBeNull();
|
|
expect(parseNumber("etwas")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("spoons and packs (26.11.39)", () => {
|
|
const de: Record<string, [string, string]> = { tbsp: ["EL", "EL"], tsp: ["TL", "TL"], pinch: ["Prise", "Prisen"], can: ["Dose", "Dosen"], jar: ["Glas", "Gläser"], pack: ["Packung", "Packungen"], bunch: ["Bund", "Bund"] };
|
|
const word = (u: KitchenUnit, n: number) => de[u]![n <= 1 + 1e-9 ? 0 : 1]!;
|
|
const show = (qty: number, base: BaseUnit, display: KitchenUnit | null, size: number | null = null) => {
|
|
const f = formatKitchen(qty, base, display, size, "de", word);
|
|
return f.extra ? `${f.main} (${f.extra})` : f.main;
|
|
};
|
|
|
|
it("a pack needs its size; without one it is not an amount", () => {
|
|
expect(toBase(1, "can", "g", 400)).toBe(400);
|
|
expect(toBase(1.5, "jar", "g", 190)).toBe(285);
|
|
expect(toBase(1, "can", "g")).toBeNull();
|
|
expect(toBase(1, "bunch", "piece", 6)).toBe(6);
|
|
});
|
|
|
|
it("spoons first with the weight in brackets, packs the other way round", () => {
|
|
expect(show(30, "ml", "tbsp")).toBe("2 EL (30 ml)");
|
|
expect(show(6, "g", "tsp")).toBe("1½ TL (6 g)");
|
|
expect(show(0.5, "g", "pinch")).toBe("1 Prise");
|
|
expect(show(400, "g", "can", 400)).toBe("400 g (1 Dose)");
|
|
expect(show(800, "g", "can", 400)).toBe("800 g (2 Dosen)");
|
|
expect(show(6, "piece", "bunch", 6)).toBe("6 (1 Bund)");
|
|
});
|
|
|
|
it("scales with kitchen fractions", () => {
|
|
// 1 Dose für 4 Portionen → 2 Portionen
|
|
expect(show((400 / 4) * 2, "g", "can", 400)).toBe("200 g (½ Dose)");
|
|
expect(show((45 / 4) * 2, "ml", "tbsp")).toBe("1½ EL (23 ml)");
|
|
// ein Achtel Packung Butter: nur Gramm
|
|
expect(show(30, "g", "pack", 250)).toBe("30 g");
|
|
});
|
|
|
|
it("back to the unit that was typed, for editing", () => {
|
|
expect(fromBase(30, "tbsp", "ml")).toBe(2);
|
|
expect(fromBase(800, "can", "g", 400)).toBe(2);
|
|
expect(fromBase(30, "tbsp", "piece")).toBeNull();
|
|
});
|
|
|
|
it("old dishes get spoons or packs where they fit, never pinches", () => {
|
|
expect(impliedKitchenUnit(30, "ml", "tbsp", null, null)).toBe("tbsp");
|
|
expect(impliedKitchenUnit(500, "ml", "tbsp", null, null)).toBeNull();
|
|
expect(impliedKitchenUnit(400, "g", "can", "can", 400)).toBe("can");
|
|
expect(impliedKitchenUnit(20, "g", null, "pack", 250)).toBeNull();
|
|
expect(impliedKitchenUnit(2, "g", "pinch", null, null)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("more measures (26.11.49)", () => {
|
|
const word = (u: KitchenUnit, n: number) => ({ clove: n === 1 ? "Zehe" : "Zehen", piece: "Stück", cup: n === 1 ? "Tasse" : "Tassen", tub: "Becher", knifepoint: "Msp." })[u as string] ?? u;
|
|
it("fixed measures need no size", () => {
|
|
expect(toBase(1, "cup", "ml")).toBe(240);
|
|
expect(toBase(1, "dash", "ml")).toBe(10);
|
|
expect(toBase(2, "knifepoint", "g")).toBe(1);
|
|
expect(toBase(1, "cup", "piece")).toBeNull();
|
|
});
|
|
it("counted measures take the size of the ingredient; cloves of a piece ingredient are one piece", () => {
|
|
expect(toBase(3, "piece", "g", 200)).toBe(600);
|
|
expect(toBase(3, "piece", "g")).toBeNull();
|
|
expect(toBase(2, "clove", "piece")).toBe(2);
|
|
expect(toBase(1, "tub", "g", 150)).toBe(150);
|
|
expect(fromBase(600, "piece", "g", 200)).toBe(3);
|
|
expect(sizeOf("pack", "g", { packKind: "can", packSize: 400, sizes: { pack: 500 } })).toBe(500);
|
|
expect(sizeOf("can", "g", { packKind: "can", packSize: 400 })).toBe(400);
|
|
expect(sizeOf("slice", "g", {})).toBeNull();
|
|
});
|
|
it("shows pieces and cloves in front, tubs behind", () => {
|
|
expect(formatKitchen(6, "g", "clove", 3, "de", word)).toEqual({ main: "2 Zehen", extra: "6 g" });
|
|
expect(formatKitchen(600, "g", "piece", 200, "de", word)).toEqual({ main: "3 Stück", extra: "600 g" });
|
|
expect(formatKitchen(150, "g", "tub", 150, "de", word)).toEqual({ main: "150 g", extra: "1 Becher" });
|
|
expect(formatKitchen(0.5, "g", "knifepoint", null, "de", word)).toEqual({ main: "1 Msp.", extra: "" });
|
|
});
|
|
it("rounds amounts the way one would type them", () => {
|
|
expect(niceQty(333.333, "g")).toBe(333);
|
|
expect(niceQty(2.666, "g")).toBe(2.67);
|
|
expect(niceQty(1.4999, "can")).toBe(1.5);
|
|
expect(niceQty(0.1, "tbsp")).toBe(0.25);
|
|
});
|
|
});
|
|
|
|
describe("millilitres and litres everywhere (26.11.50)", () => {
|
|
const word = (u: KitchenUnit, n: number) => (u === "piece" ? "Stück" : u) + (n > 1 ? "" : "");
|
|
it("a piece ingredient in ml takes the size per piece", () => {
|
|
expect(toBase(750, "ml", "piece", 500)).toBe(1.5);
|
|
expect(toBase(1, "l", "piece", 500)).toBe(2);
|
|
expect(toBase(750, "ml", "piece")).toBeNull();
|
|
expect(fromBase(1.5, "ml", "piece", 500)).toBe(750);
|
|
expect(sizeOf("l", "piece", { sizes: { ml: 500 } })).toBe(500);
|
|
});
|
|
it("grams and millilitres are one to one", () => {
|
|
expect(toBase(200, "ml", "g")).toBe(200);
|
|
expect(toBase(1, "kg", "ml")).toBe(1000);
|
|
expect(fromBase(200, "ml", "g")).toBe(200);
|
|
});
|
|
it("shows the typed measure, with the pieces of a piece ingredient", () => {
|
|
expect(formatKitchen(1.5, "piece", "ml", 500, "de", word)).toEqual({ main: "750 ml", extra: "1½ Stück" });
|
|
expect(formatKitchen(1500, "ml", "l", null, "de", word).main).toBe("1,5 l");
|
|
expect(formatKitchen(200, "g", "ml", null, "de", word)).toEqual({ main: "200 ml", extra: "" });
|
|
});
|
|
});
|