Files
Betriebsf-hrungssoftware/test/formel.test.ts
Claude 8d0f88a9fa
Some checks failed
Prüfung / tests (push) Has been cancelled
Prüfung / container (push) Has been cancelled
0.30.1: Wandliste in der Position bearbeitbar, Fassade aus der Wandliste
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
2026-10-22 13:02:17 +00:00

109 lines
5.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import { benenneUm, berechne, FormelFehler, kuerzelIn, zahlDeutsch } from "../src/domain/formel.js";
import { werteBlatt, textMitWerten } from "../src/domain/mengenblatt.js";
import { STANDARD_VORLAGEN } from "../src/domain/mengenvorlagen.js";
import { umgebungVon } from "../src/domain/kalkulation.js";
const r = (f: string, w: Record<string, number> = {}) => berechne(f, { wert: (n) => { if (n in w) return w[n]!; throw new FormelFehler(`„${n}“ unbekannt`); } });
describe("Formeln", () => {
it("rechnet mit Komma, Punkt-vor-Strich und Klammern wie Plancraft", () => {
expect(r("100+(5,1*4)/2")).toBeCloseTo(110.2);
expect(r("10,00 * 0,30")).toBeCloseTo(3);
expect(r("2 × 8 · 0,3")).toBeCloseTo(4.8);
expect(r("-2^2")).toBe(-4);
expect(r("10 %")).toBeCloseTo(0.1);
});
it("Winkel in Grad, Funktionen mit Semikolon", () => {
expect(r("cos(60)")).toBeCloseTo(0.5);
expect(r("(B/2+UET)/cos(DN)", { B: 8, UET: 0.5, DN: 35 })).toBeCloseTo(5.4935, 3);
expect(r("aufrunden(10/2,6)")).toBe(4);
expect(r("aufrunden(10/2,5)")).toBe(4);
expect(r("runden(2,675; 2)")).toBe(2.68);
expect(r("wenn(3 > 2; 1; 0)")).toBe(1);
expect(r('wenn("G" = "g"; 5)')).toBe(5);
expect(r("max(1; 7; 3)")).toBe(7);
expect(r("ROUNDUP(4,2;0)")).toBe(5);
});
it("Einheit hinter einer Zahl wird überlesen", () => {
expect(r("2,5 m * 2")).toBe(5);
expect(r("31°")).toBe(31);
});
it("meldet Fehler verständlich", () => {
expect(() => r("L * FZ", { L: 2 })).toThrow("„FZ“ unbekannt");
expect(() => r("(1+2")).toThrow("Klammer zu fehlt");
expect(() => r("1/0")).toThrow("Teilen durch 0");
expect(() => r("2 2")).toThrow(/Rechenzeichen/);
expect(() => r("30 mm")).toThrow();
});
it("Kürzel finden und umbenennen", () => {
expect(kuerzelIn("aufrunden(L / TL) - 1")).toEqual(["L", "TL"]);
expect(benenneUm("L + STO * L2 + summe(BT.L)", "L", "LA")).toBe("LA + STO * L2 + summe(BT.L)");
});
it("Zahlen deutsch", () => {
expect(zahlDeutsch(200, "m")).toBe("200,00");
expect(zahlDeutsch(8, "St")).toBe("8");
expect(zahlDeutsch(3.1524, "m³")).toBe("3,152");
});
});
describe("Mengenblatt", () => {
it("freie Aufmaßzeilen ergeben die Menge (Bild 5)", () => {
const a = werteBlatt({ werte: [], listen: [], zeilen: [{ text: "Nord", formel: "10,00 * 0,30" }, { text: "Süd", formel: "10*0,3" }, { formel: "2 * 8,00 * 0,30" }, { formel: "" }] });
expect(a.menge).toBeCloseTo(10.8);
expect(a.ergebnis["z:3"]).toBeNull();
});
it("Kreisbezug und doppelte Kürzel", () => {
const a = werteBlatt({ werte: [{ kz: "A", formel: "B + 1" }, { kz: "B", formel: "A * 2" }, { kz: "C", formel: "1" }, { kz: "C", formel: "2" }], listen: [], zeilen: [], menge: "C" });
expect(a.fehler["w:0"]).toMatch(/Kreisbezug|Fehler/);
expect(a.fehler["w:3"]).toMatch(/zweimal/);
expect(a.menge).toBe(1);
});
it("Platzhalter im Text", () => {
const a = werteBlatt({ werte: [{ kz: "Z", formel: "0,3" }], listen: [], zeilen: [] });
expect(textMitWerten("Z = {Z} m, {X}", a, { Z: "m" })).toBe("Z = 0,30 m, {X}");
});
});
describe("mitgelieferte Vorlagen", () => {
const umg = umgebungVon({ mengen: { verschnitt: 10, oeffnung_ab: 2.5 } });
for (const v of STANDARD_VORLAGEN) {
it(`${v.name} rechnet ohne Fehler`, () => {
const a = werteBlatt(v.inhalt, umg);
expect(a.fehler).toEqual({});
expect(a.menge).toBeGreaterThan(0);
for (const z of v.inhalt.ansaetze) if (z.bezug) expect(a.rechne(z.bezug)).toBeGreaterThan(0);
});
}
it("Pfettendach: 200 m Holz, 3,152 m³, 8 Stützen (Bild 3)", () => {
const a = werteBlatt(STANDARD_VORLAGEN.find((v) => v.id === "std-pfettendach")!.inhalt, umg);
expect(a.werte.ST).toBe(8);
expect(a.menge).toBe(200);
expect(a.werte.HV).toBeCloseTo(3.152);
expect(a.werte.SPL).toBe(140);
});
it("Wandliste: Giebel als Dreieck, Öffnungen erst über 2,5 m² abgezogen (Bild 4)", () => {
const a = werteBlatt(STANDARD_VORLAGEN.find((v) => v.id === "std-holzrahmenwand")!.inhalt, umg);
expect(a.werte.AB).toBeCloseTo(118.2);
expect(a.werte.AO).toBeCloseTo(7.3265, 3);
expect(a.werte.GR).toBeCloseTo(18.66, 1);
expect(a.werte.NOE).toBe(3);
});
it("Abdeckblech: LG 12,25 m (Bild 6)", () => {
const a = werteBlatt(STANDARD_VORLAGEN.find((v) => v.id === "std-abdeckblech")!.inhalt, umg);
expect(a.werte.STO).toBe(5);
expect(a.menge).toBeCloseTo(12.25);
expect(a.werte.HA).toBe(25);
});
});
describe("Fassade mit Wandliste (Henning, 22.10.2026)", () => {
it("Fassadenfläche, Umfang und Öffnungen kommen aus der Wandliste", () => {
const a = werteBlatt(STANDARD_VORLAGEN.find((v) => v.id === "std-fassade")!.inhalt, umgebungVon({}));
// 2×10×2,75 + 2×8×2,75 + 2 Giebel 8×2,4/2 − Terrassentür 2,01×2,26 − Fenster Wohnen 2,01×1,385 (Küche unter 2,5 m²)
expect(a.menge).toBeCloseTo(99 + 19.2 - 4.5426 - 2.78385, 3);
expect(a.werte.UF).toBeCloseTo(36);
expect(a.werte.BOEF).toBeCloseTo(5.28);
});
});