Repository Public
1import { describe, expect, it } from "vitest";2import { DEFAULT_FILTERS, activeFilterCount, distinct, filtersToQuery, filtersToSearch, formatPerfLevels, groupByMetric, healthCounts, healthStrip, parseCompareIds, parseFilters, parseTags, usedPct } from "./logic";34describe("node filters", () => {5 it("round-trips through the URL and omits defaults", () => {6 expect(filtersToSearch(DEFAULT_FILTERS)).toBe("");7 const f = { ...DEFAULT_FILTERS, q: " m4 ", site: "lan", kind: "mac" as const, freshness: "stale" as const, maintenance: "true" as const, retired: true, sort: "cpu" as const, order: "desc" as const };8 const search = filtersToSearch(f);9 expect(parseFilters(new URLSearchParams(search))).toEqual({ ...f, q: "m4" });10 });11 it("ignores invalid values", () => {12 const f = parseFilters(new URLSearchParams("kind=windows&freshness=x&sort=evil&order=up&maintenance=maybe"));13 expect(f).toEqual(DEFAULT_FILTERS);14 });15 it("builds the API query with booleans and without empty filters", () => {16 expect(filtersToQuery(DEFAULT_FILTERS)).toEqual({ limit: 100, sort: "alias", order: "asc" });17 expect(filtersToQuery({ ...DEFAULT_FILTERS, maintenance: "false", tag: "gpu" })).toMatchObject({ maintenance: false, tag: "gpu" });18 expect(activeFilterCount({ ...DEFAULT_FILTERS, q: "a", retired: true })).toBe(2);19 });20 it("keeps extra parameters (view state) in the URL", () => {21 expect(filtersToSearch(DEFAULT_FILTERS, { tab: "x" })).toBe("?tab=x");22 });23});2425describe("helpers", () => {26 it("lists perf levels by their real names", () => {27 expect(formatPerfLevels([{ name: "Super", cores: 2 }, { name: "Performance", cores: 4 }, { name: "Efficiency", cores: 6 }])).toBe("Super 2 · Performance 4 · Efficiency 6");28 });29 it("parses compare ids: valid, unique, capped", () => {30 const a = "019a9c1e-7b10-7c3a-9d54-2f6f1f0f3a11", b = "019a9c1e-7b10-7c3a-9d54-2f6f1f0f3a12";31 expect(parseCompareIds(`${a}, nope,${b},${a.toUpperCase()}`)).toEqual([a, b]);32 expect(parseCompareIds(null)).toEqual([]);33 const many = Array.from({ length: 20 }, (_, i) => `019a9c1e-7b10-7c3a-9d54-2f6f1f0f3a${String(i).padStart(2, "0")}`).join(",");34 expect(parseCompareIds(many)).toHaveLength(12);35 });36 it("distinct, tags, used percentage", () => {37 expect(distinct(["b", undefined, "a", "b", ""], "z")).toEqual(["a", "b", "z"]);38 expect(parseTags("gpu, lan gpu")).toEqual(["gpu", "lan"]);39 expect(usedPct(50, 200)).toBe(25);40 expect(usedPct(undefined, 200)).toBeUndefined();41 expect(usedPct(1, 0)).toBeUndefined();42 });43 it("groups series by metric", () => {44 const s = (metric: string, label: string) => ({ metric, label, unit: "bytes", points: [] });45 const g = groupByMetric([s("net.rx", "en0"), s("net.tx", "en0"), s("net.rx", "en1")]);46 expect([...g.keys()]).toEqual(["net.rx", "net.tx"]);47 expect(g.get("net.rx")?.map((x) => x.label)).toEqual(["en0", "en1"]);48 });49});5051describe("health strip", () => {52 const b = (start: string, end: string, state: "up" | "down" | "unknown") => ({ start, end, state, alerts_firing: 0 });53 const buckets = [b("2026-11-18T00:00:00Z", "2026-11-18T01:00:00Z", "up"), b("2026-11-18T01:00:00Z", "2026-11-18T02:00:00Z", "down"), b("2026-11-18T02:00:00Z", "2026-11-18T04:00:00Z", "unknown")];54 it("lays buckets out proportionally with state-dependent heights", () => {55 const r = healthStrip(buckets, 400, 20);56 expect(r).toHaveLength(3);57 expect(r[0]?.x).toBe(0);58 expect(r[1]?.x).toBe(100);59 expect(r[2]?.x).toBe(200);60 expect(r[0]?.h).toBe(20);61 expect(r[1]?.h).toBeLessThan(r[0]?.h ?? 0);62 expect((r[2]?.w ?? 0) > (r[1]?.w ?? 0)).toBe(true);63 expect((r[1]?.y ?? 0) + (r[1]?.h ?? 0)).toBe(20);64 });65 it("is empty without a usable time span and counts states", () => {66 expect(healthStrip([], 400, 20)).toEqual([]);67 expect(healthCounts(buckets)).toEqual({ up: 1, degraded: 0, down: 1, maintenance: 0, unknown: 1 });68 });69});70