import { describe, expect, it } from "vitest"; import { MAX_CONDUIT_FEATURES, MAP_FEATURE_ID_FIELDS, createMapFeatureWfsUrl, escapeCqlLiteral, normalizeMapFeatureCollection, parseMapFeatureQuery } from "./map-feature-query"; describe("map feature WFS query", () => { it("maps source identifiers to authoritative feature fields", () => { expect(MAP_FEATURE_ID_FIELDS).toMatchObject({ conduits: "id", junctions: "id", scada: "sensor_id" }); expect(createMapFeatureWfsUrl({ sourceId: "scada", featureIds: ["S-1"] }).searchParams.get("cql_filter")) .toBe("sensor_id IN ('S-1')"); }); it("escapes CQL literals and uses materialized WFS layers", () => { expect(escapeCqlLiteral("a'b")).toBe("'a''b'"); const url = createMapFeatureWfsUrl({ sourceId: "conduits", featureIds: ["a'b"] }); expect(url.searchParams.get("typeNames")).toContain("geo_conduits_mat"); expect(url.searchParams.get("srsName")).toBe("EPSG:4326"); expect(url.searchParams.get("cql_filter")).toBe("id IN ('a''b')"); }); it("limits ids and permits full-network requests only for conduits", () => { expect(parseMapFeatureQuery({ sourceId: "conduits" })).toEqual({ ok: true, value: { sourceId: "conduits" } }); expect(createMapFeatureWfsUrl({ sourceId: "conduits" }).searchParams.get("count")).toBe(String(MAX_CONDUIT_FEATURES)); expect(parseMapFeatureQuery({ sourceId: "junctions" })).toMatchObject({ ok: false, status: 400 }); expect(parseMapFeatureQuery({ sourceId: "unknown", featureIds: ["1"] })).toMatchObject({ ok: false, status: 422 }); expect(parseMapFeatureQuery({ sourceId: "conduits", featureIds: Array.from({ length: 101 }, (_, index) => String(index)) })) .toMatchObject({ ok: false, status: 400 }); expect(parseMapFeatureQuery({ sourceId: "conduits", featureIds: ["x".repeat(129)] })) .toMatchObject({ ok: false, status: 400 }); }); it("preserves an empty result and rejects malformed upstream data", () => { expect(normalizeMapFeatureCollection({ type: "FeatureCollection", features: [] }, 100)).toEqual({ type: "FeatureCollection", features: [] }); expect(normalizeMapFeatureCollection({ features: [] }, 100)).toBeNull(); }); });