// WF-P8 S02 "Adressdaten" — payload parsing of the `updateAddresses` intent. // The section posts all three slots in one JSON field, and `null` for a slot is // a real instruction (clear it), so the parser has to keep that apart from // "slot not mentioned" and from a payload it cannot read at all. import { describe, expect, it } from "vitest"; import { parseAddressSlots } from "./projectDetailRoute.server"; describe("parseAddressSlots", () => { it("keeps a null slot as an explicit clear and leaves absent slots untouched", () => { const parsed = parseAddressSlots(JSON.stringify({ projekt: { strasse: "Hauptstr.", ort: "Freiburg" }, rechnung: null })); expect(parsed.projekt).toEqual({ strasse: "Hauptstr.", hausnummer: undefined, plz: undefined, ort: "Freiburg", land: undefined }); expect("rechnung" in parsed).toBe(true); expect(parsed.rechnung).toBeNull(); expect("bauherr" in parsed).toBe(false); }); it("writes nothing for a payload it cannot read instead of clearing every slot", () => { for (const raw of [null, "", "not json", "[]", '"projekt"']) { expect(parseAddressSlots(raw)).toEqual({}); } }); it("carries the slot extras, including the ones only one slot shows", () => { const parsed = parseAddressSlots( JSON.stringify({ projekt: { ort: "Freiburg", zufahrt: "Hofeinfahrt", notizen: "Anlieferung 7-16 Uhr" }, rechnung: { ort: "Freiburg", rechnungsEmail: "rechnung@example.de", zahlungsziel: "30 Tage" }, bauherr: { ort: "Freiburg", telefon: "+49 761 1", ansprechpartnerVorOrt: "Frau Schneider", postErwuenscht: true }, }) ); expect(parsed.projekt).toMatchObject({ zufahrt: "Hofeinfahrt", notizen: "Anlieferung 7-16 Uhr" }); expect(parsed.rechnung).toMatchObject({ rechnungsEmail: "rechnung@example.de", zahlungsziel: "30 Tage" }); expect(parsed.bauherr).toMatchObject({ telefon: "+49 761 1", ansprechpartnerVorOrt: "Frau Schneider", postErwuenscht: true }); }); it("leaves an extra undefined when the slot did not post it, so omission cannot clear it", () => { const parsed = parseAddressSlots(JSON.stringify({ projekt: { ort: "Freiburg" } })); expect(parsed.projekt?.notizen).toBeUndefined(); expect(parsed.projekt?.rechnungsEmail).toBeUndefined(); expect(parsed.projekt?.postErwuenscht).toBeUndefined(); }); it("drops unknown slots and non-string members", () => { const parsed = parseAddressSlots(JSON.stringify({ lieferung: { ort: "Freiburg" }, projekt: { ort: 42, plz: "79098" } })); expect(Object.keys(parsed)).toEqual(["projekt"]); expect(parsed.projekt).toEqual({ strasse: undefined, hausnummer: undefined, plz: "79098", ort: undefined, land: undefined }); }); });