import { describe, expect, it } from "vitest";
import {
  AVAILABLE_WATER_NETWORK_SOURCE_IDS,
  SUPPLY_LAYER_CATALOG,
  createWaterNetworkSources,
  SOURCE_LAYERS,
  WATER_NETWORK_SOURCE_IDS
} from "./sources";

describe("createWaterNetworkSources", () => {
  it("catalogs published and pre-registered supply system layers", () => {
    expect(SOURCE_LAYERS).toEqual({
      pipes: "geo_pipes_mat",
      junctions: "geo_junctions_mat",
      valves: "geo_valves",
      reservoirs: "geo_reservoirs",
      scada: "geo_scada",
      pumps: "geo_pumps",
      tanks: "geo_tanks"
    });
    expect(WATER_NETWORK_SOURCE_IDS).toEqual(["pipes", "junctions", "valves", "reservoirs", "scada", "pumps", "tanks"]);
    expect(SUPPLY_LAYER_CATALOG.map((layer) => [layer.id, layer.available])).toEqual([
      ["pipes", true],
      ["junctions", true],
      ["valves", true],
      ["reservoirs", true],
      ["scada", true],
      ["pumps", false],
      ["tanks", false]
    ]);
    expect(SUPPLY_LAYER_CATALOG.find((layer) => layer.id === "valves")).toMatchObject({
      geometry: "point",
      sourceLayer: "geo_valves"
    });
  });

  it("loads only available supply system layers from GeoServer WMTS", () => {
    expect(AVAILABLE_WATER_NETWORK_SOURCE_IDS).toEqual(["pipes", "junctions", "valves", "reservoirs", "scada"]);
    expect(SUPPLY_LAYER_CATALOG.find((layer) => layer.id === "reservoirs")).toMatchObject({
      label: "水库",
      icon: "reservoir"
    });

    const sources = createWaterNetworkSources();
    expect(Object.keys(sources)).toEqual(AVAILABLE_WATER_NETWORK_SOURCE_IDS);
    AVAILABLE_WATER_NETWORK_SOURCE_IDS.forEach((sourceId) => {
      expect(sources[sourceId].tiles).toEqual([
        expect.stringContaining(`/tjwater:${SOURCE_LAYERS[sourceId]}/WebMercatorQuad/`)
      ]);
      expect(sources[sourceId].promoteId).toBe("id");
    });
    expect("pumps" in sources).toBe(false);
    expect("tanks" in sources).toBe(false);
  });
});
