75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { toTrustedMapAction } from "./map-actions";
|
|
|
|
describe("toTrustedMapAction", () => {
|
|
it("rejects zoom_to_map actions without a valid center", () => {
|
|
expect(toTrustedMapAction("zoom_to_map", { zoom: 14 })).toBeNull();
|
|
expect(toTrustedMapAction("zoom_to_map", { center: ["east", 39.1] })).toBeNull();
|
|
});
|
|
|
|
it("rejects unsupported map actions", () => {
|
|
expect(toTrustedMapAction("run_javascript", { code: "alert(1)" })).toBeNull();
|
|
});
|
|
|
|
it("rejects malformed or empty apply_layer_style params", () => {
|
|
expect(
|
|
toTrustedMapAction("apply_layer_style", {
|
|
layer_group_id: 123,
|
|
layer_id: [],
|
|
visible: "true"
|
|
})
|
|
).toBeNull();
|
|
expect(toTrustedMapAction("apply_layer_style", {})).toBeNull();
|
|
});
|
|
|
|
it("rejects out-of-range coordinates and zoom", () => {
|
|
expect(toTrustedMapAction("zoom_to_map", { center: [181, 39.1] })).toBeNull();
|
|
expect(toTrustedMapAction("zoom_to_map", { center: [117.2, -91] })).toBeNull();
|
|
expect(toTrustedMapAction("zoom_to_map", { center: [117.2, 39.1], zoom: 25 })).toBeNull();
|
|
});
|
|
|
|
it("rejects empty and oversized locate requests", () => {
|
|
expect(toTrustedMapAction("locate_features", { ids: [] })).toBeNull();
|
|
expect(toTrustedMapAction("locate_features", { ids: Array.from({ length: 101 }, (_, index) => `P${index}`) })).toBeNull();
|
|
});
|
|
|
|
it("accepts valid zoom_to_map coordinates", () => {
|
|
expect(toTrustedMapAction("zoom_to_map", { lng: 117.2, lat: 39.1, zoom: 13 })).toEqual({
|
|
type: "zoom_to_map",
|
|
center: [117.2, 39.1],
|
|
zoom: 13,
|
|
fallbackText: undefined
|
|
});
|
|
});
|
|
|
|
it("accepts legacy zoom coordinate aliases and numeric strings", () => {
|
|
expect(toTrustedMapAction("zoom_to_map", { coordinate: ["117.2", "39.1"], zoom: "13" })).toEqual({
|
|
type: "zoom_to_map",
|
|
center: [117.2, 39.1],
|
|
zoom: 13,
|
|
fallbackText: undefined
|
|
});
|
|
expect(toTrustedMapAction("zoom_to_map", { x: "117.2", y: "39.1" })).toMatchObject({
|
|
type: "zoom_to_map",
|
|
center: [117.2, 39.1]
|
|
});
|
|
});
|
|
|
|
it("normalizes legacy locate tool actions", () => {
|
|
expect(toTrustedMapAction("locate_pipes", { pipe_ids: "P1,P2" })).toEqual({
|
|
type: "locate_features",
|
|
featureIds: ["P1", "P2"],
|
|
layer: "geo_pipes_mat",
|
|
fallbackText: undefined
|
|
});
|
|
});
|
|
|
|
it("normalizes numeric locate ids", () => {
|
|
expect(toTrustedMapAction("locate_junctions", { junction_id: 42 })).toMatchObject({
|
|
type: "locate_features",
|
|
featureIds: ["42"],
|
|
layer: "geo_junctions_mat"
|
|
});
|
|
});
|
|
});
|