import { describe, expect, it } from "vitest"; import { MAX_PIPE_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({ pipes: "id", junctions: "id", valves: "id", reservoirs: "id", pumps: "id", tanks: "id" }); expect(createMapFeatureWfsUrl({ sourceId: "junctions", featureIds: ["J-1"] }).searchParams.get("cql_filter")) .toBe("\"id\" IN ('J-1')"); }); it("escapes CQL literals and uses the new materialized WFS layers", () => { expect(escapeCqlLiteral("a'b")).toBe("'a''b'"); const url = createMapFeatureWfsUrl({ sourceId: "pipes", featureIds: ["a'b"] }); expect(url.searchParams.get("typeNames")).toContain("tjwater_next:pipes"); expect(url.searchParams.get("srsName")).toBe("EPSG:4326"); expect(url.searchParams.get("cql_filter")).toBe("\"id\" IN ('a''b')"); }); it("uses WFS only for authoritative supply asset layers", () => { expect(createMapFeatureWfsUrl({ sourceId: "valves", featureIds: ["V-1"] }).searchParams.get("typeNames")).toContain("tjwater_next:valves"); expect(createMapFeatureWfsUrl({ sourceId: "reservoirs", featureIds: ["R-1"] }).searchParams.get("typeNames")).toContain("tjwater_next:reservoirs"); expect(parseMapFeatureQuery({ sourceId: "scada", featureIds: ["S-1"] })).toMatchObject({ ok: false, code: "UNSUPPORTED_SOURCE" }); }); it("limits ids and permits full-network requests only for pipes", () => { expect(parseMapFeatureQuery({ sourceId: "pipes" })).toEqual({ ok: true, value: { sourceId: "pipes" } }); expect(createMapFeatureWfsUrl({ sourceId: "pipes" }).searchParams.get("count")).toBe(String(MAX_PIPE_FEATURES)); expect(parseMapFeatureQuery({ sourceId: "junctions" })).toMatchObject({ ok: false, status: 400 }); expect(parseMapFeatureQuery({ sourceId: "unknown", featureIds: ["1"] })).toMatchObject({ ok: false, status: 422 }); expect(parseMapFeatureQuery({ sourceId: "pumps", featureIds: ["P-1"] })).toEqual({ ok: true, value: { sourceId: "pumps", featureIds: ["P-1"] } }); expect(parseMapFeatureQuery({ sourceId: "tanks", featureIds: ["T-1"] })).toEqual({ ok: true, value: { sourceId: "tanks", featureIds: ["T-1"] } }); expect(parseMapFeatureQuery({ sourceId: "pipes", featureIds: Array.from({ length: 101 }, (_, index) => String(index)) })) .toMatchObject({ ok: false, status: 400 }); expect(parseMapFeatureQuery({ sourceId: "pipes", 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(); }); });