import { describe, expect, it } from "vitest"; import { defaultUrlState, navigationKey, parseUrlState, serializeUrlState, type UrlState, } from "./urlState"; describe("URL state", () => { it("round-trips every field and omits defaults", () => { const state: UrlState = { ...defaultUrlState(), panel: "services", serviceId: "svc-1", incidentId: "inc-9", snapshotId: "snap-2", topology: { level: "components", domainId: "commerce", applicationId: "billing" }, filters: { ...defaultUrlState().filters, search: "api gateway", status: "down", environment: "prod" }, }; const hash = serializeUrlState(state); expect(hash).toBe( "#/services?service=svc-1&incident=inc-9&snapshot=snap-2&in_domain=commerce&in_app=billing&q=api+gateway&status=down&env=prod", ); expect(parseUrlState(hash)).toEqual(state); expect(serializeUrlState(defaultUrlState())).toBe("#/topology"); }); it("falls back to defaults for unknown or hostile input", () => { expect(parseUrlState("")).toEqual(defaultUrlState()); expect(parseUrlState("#/not-a-panel?service=")).toEqual(defaultUrlState()); const long = parseUrlState(`#/topology?service=${"x".repeat(500)}`); expect(long.serviceId).toBeNull(); }); it("derives the topology level from the scope parameters", () => { expect(parseUrlState("#/topology?in_domain=commerce").topology).toEqual({ level: "applications", domainId: "commerce", applicationId: null, }); expect(parseUrlState("#/topology?in_app=billing").topology.level).toBe("components"); }); it("separates navigation from filter changes", () => { const base = parseUrlState("#/services?service=svc-1"); const filtered = { ...base, filters: { ...base.filters, search: "db" } }; const navigated = { ...base, serviceId: "svc-2" }; expect(navigationKey(filtered)).toBe(navigationKey(base)); expect(navigationKey(navigated)).not.toBe(navigationKey(base)); }); });