import { describe, it, expect } from "vitest"; import { parseLocations } from "./dhl-locations"; /** * Ein Treffer im Format der DHL Location Finder API. Die Packstation-Nummer * steht in location.keywordId — location.ids[0].locationId ist ein * zusammengesetzter Systemcode und ausdruecklich NICHT die Nummer, die * Kundschaft am Geraet eingibt. */ function dhlLocation(overrides: Record = {}) { return { url: "https://api.dhl.com/location-finder/v1/locations/8003-453113145", location: { ids: [{ locationId: "8003-453113145", provider: "parcel-de" }], keyword: "DHL Packstation", keywordId: "145", type: "locker", }, name: "Packstation 145", distance: 820, place: { address: { countryCode: "DE", postalCode: "86450", addressLocality: "Altenmünster", streetAddress: "Bahnhofstr. 1", }, }, ...overrides, }; } describe("parseLocations", () => { it("nimmt die Packstation-Nummer aus keywordId, nicht aus locationId", () => { const [packstation] = parseLocations([dhlLocation()]); // locationId waere "8003-453113145" — damit koennte niemand etwas anfangen, // und DHL wuerde die Versandmarke ablehnen. expect(packstation.number).toBe("145"); expect(packstation.zip).toBe("86450"); expect(packstation.city).toBe("Altenmünster"); expect(packstation.street).toBe("Bahnhofstr. 1"); expect(packstation.distanceMeters).toBe(820); }); it("sortiert nach Entfernung aufsteigend", () => { const parsed = parseLocations([ dhlLocation({ distance: 3000, location: { keywordId: "300", keyword: "DHL Packstation" } }), dhlLocation({ distance: 120, location: { keywordId: "120", keyword: "DHL Packstation" } }), ]); expect(parsed.map((p) => p.number)).toEqual(["120", "300"]); }); it("ueberspringt einen Eintrag ohne Packstation-Nummer, statt die ganze Suche scheitern zu lassen", () => { const parsed = parseLocations([ { location: { keyword: "DHL Packstation" }, place: { address: {} } }, dhlLocation(), ]); // Ein Standort ohne Nummer ist unbrauchbar — die uebrigen bleiben gueltig. expect(parsed).toHaveLength(1); expect(parsed[0].number).toBe("145"); }); it("faellt auf einen zusammengesetzten Namen zurueck, wenn DHL keinen liefert", () => { const [packstation] = parseLocations([dhlLocation({ name: undefined })]); expect(packstation.name).toBe("Packstation 145"); }); it("kommt ohne Strasse und ohne Entfernung zurecht", () => { const [packstation] = parseLocations([ dhlLocation({ distance: undefined, place: { address: { postalCode: "10115", addressLocality: "Berlin" } }, }), ]); expect(packstation.street).toBe(""); expect(packstation.distanceMeters).toBeNull(); // Ohne Entfernung darf der Eintrag nicht verschwinden, nur ans Ende rutschen. expect(packstation.city).toBe("Berlin"); }); it("liefert fuer eine leere Trefferliste ein leeres Ergebnis", () => { expect(parseLocations([])).toEqual([]); }); });