// The admin log console's filters in the address bar: a link carries them, a hand-edited value the // API would refuse degrades to "no filter", and a filter at its default leaves no key behind. import { describe, expect, it } from "vitest"; import { DEFAULT_LOGS_STATE, logsParamsToState, logsParamUpdates, type LogsUrlState, } from "./logsUrlState"; const params = (updates: Record) => new URLSearchParams(Object.entries(updates).filter((e): e is [string, string] => e[1] !== null)); describe("logsUrlState", () => { it("round-trips every filter", () => { const state: LogsUrlState = { group: "error", sources: ["worker"], user: "none", event: "download.job.failed", requestId: "req-abcdef12", jobId: 42, q: "100% done", window: "7d", }; expect(logsParamsToState(params(logsParamUpdates(state)))).toEqual(state); }); it("leaves no key for a filter at its default", () => { expect(Object.values(logsParamUpdates(DEFAULT_LOGS_STATE)).every((v) => v === null)).toBe(true); }); it("drops a value the API would refuse instead of sending it", () => { const state = logsParamsToState( new URLSearchParams( "lgrp=loud&lsrc=api,WORKER,x y&lu=me&lev=Not.A.Code&lreq=short&ljob=0&lwin=1y", ), ); expect(state).toEqual({ ...DEFAULT_LOGS_STATE, sources: ["api"] }); }); });