Repository Public
1import { describe, expect, it } from "vitest";2import { COLS, addWidget, addableTypes, bottom, clampWidget, collides, defaultLayout, moveBy, moveTo, readingOrder, removeWidget, resizeBy, resizeTo, sameLayout, sanitizeLayout, setConfig, settle, trendConfig, trendRange, type Widget } from "./layout";34const noOverlap = (l: Widget[]) => l.every((a) => l.every((b) => !collides(a, b)));5const inBounds = (l: Widget[]) => l.every((w) => w.x >= 0 && w.x + w.w <= COLS && w.y >= 0 && w.w >= 1 && w.h >= 1);6const w = (id: string, x: number, y: number, ww: number, h: number, type = "jobs"): Widget => ({ id, type, x, y, w: ww, h, config: {} });78describe("overview layout", () => {9 it("default layout is valid, stable under settle and has unique ids", () => {10 const d = defaultLayout();11 expect(noOverlap(d)).toBe(true);12 expect(inBounds(d)).toBe(true);13 expect(new Set(d.map((x) => x.id)).size).toBe(d.length);14 expect(sameLayout(settle(d), d)).toBe(true);15 });1617 it("clamps a widget into the 12 columns and to its minimum size", () => {18 expect(clampWidget(w("a", 11, -3, 20, 0))).toMatchObject({ x: 0, y: 0, w: 12, h: 2 });19 expect(clampWidget(w("a", 11, 2, 4, 2))).toMatchObject({ x: 8, w: 4 });20 expect(clampWidget(w("t", 0, 0, 1, 1, "trends"))).toMatchObject({ w: 4, h: 4 });21 });2223 it("moving a widget keeps its cell and pushes the others down", () => {24 const l = [w("a", 0, 0, 4, 2), w("b", 4, 0, 4, 2), w("c", 0, 2, 4, 2)];25 const out = moveTo(l, "b", 0, 0);26 expect(out.find((x) => x.id === "b")).toMatchObject({ x: 0, y: 0 });27 expect(out.find((x) => x.id === "a")).toMatchObject({ x: 0, y: 2 });28 expect(out.find((x) => x.id === "c")).toMatchObject({ x: 0, y: 4 });29 expect(noOverlap(out)).toBe(true);30 });3132 it("gravity closes vertical holes, including under the moved widget", () => {33 const out = moveTo([w("a", 0, 0, 4, 2), w("b", 4, 0, 4, 2)], "b", 4, 9);34 expect(out.find((x) => x.id === "b")?.y).toBe(0);35 expect(bottom(removeWidget([w("a", 0, 0, 12, 2), w("b", 0, 2, 12, 2)], "a"))).toBe(2);36 });3738 it("moveBy / resizeBy stay in bounds and never overlap", () => {39 let l = defaultLayout();40 for (const [dx, dy] of [[-20, 0], [20, 0], [0, -20], [3, 2], [-1, 5]] as const) {41 l = moveBy(l, "trends-cpu", dx, dy);42 expect(noOverlap(l) && inBounds(l)).toBe(true);43 }44 l = resizeBy(l, "nodes", 30, 30);45 expect(noOverlap(l) && inBounds(l)).toBe(true);46 const n = l.find((x) => x.id === "nodes");47 expect(n && n.x + n.w).toBeLessThanOrEqual(COLS);48 expect(resizeTo(l, "nodes", 0, 0).find((x) => x.id === "nodes")).toMatchObject({ w: 3, h: 2 });49 expect(moveBy(l, "missing", 1, 1)).toEqual(l);50 });5152 it("is deterministic", () => {53 const a = moveTo(defaultLayout(), "actions", 0, 0);54 const b = moveTo(defaultLayout().reverse(), "actions", 0, 0);55 expect(a).toEqual(b);56 expect(readingOrder(a)).toEqual(a);57 });5859 it("adds at the bottom with a unique id; single-instance types are not offered twice", () => {60 const d = defaultLayout();61 expect(addableTypes(d)).toEqual(["trends"]);62 const more = addWidget(d, "trends");63 expect(more.length).toBe(d.length + 1);64 expect(more.some((x) => x.id === "trends")).toBe(true);65 expect(addWidget(more, "trends").some((x) => x.id === "trends-2")).toBe(true);66 expect(addWidget(d, "nodes").length).toBe(d.length);67 const less = removeWidget(d, "nodes");68 expect(addableTypes(less)).toContain("nodes");69 expect(noOverlap(addWidget(less, "nodes"))).toBe(true);70 });7172 it("sanitizes what the server returns: drops junk, dedupes, resolves overlaps, keeps unknown types", () => {73 const out = sanitizeLayout([74 null, 3, { id: "", type: "nodes" }, { id: "a", type: "nodes", x: 0, y: 0, w: 4, h: 3, config: {} },75 { id: "a", type: "alerts", x: 0, y: 0, w: 4, h: 3, config: {} },76 { id: "b", type: "alerts", x: 2, y: 1, w: 99, h: 3, config: [] },77 { id: "x", type: "future.widget", x: "1", y: 0, w: 2, h: 1 },78 ]);79 expect(out.map((x) => x.id).sort()).toEqual(["a", "b", "x"]);80 expect(noOverlap(out) && inBounds(out)).toBe(true);81 expect(out.find((x) => x.id === "b")?.config).toEqual({});82 expect(sanitizeLayout("nope")).toEqual([]);83 });8485 it("compares layouts regardless of order and tracks config changes", () => {86 const d = defaultLayout();87 expect(sameLayout(d, [...d].reverse())).toBe(true);88 expect(sameLayout(d, setConfig(d, "trends-cpu", { metric: "load.1" }))).toBe(false);89 expect(sameLayout(d, moveBy(d, "storage", 4, 0))).toBe(false);90 });9192 it("reads a trends config defensively and maps periods to the presets of the endpoint", () => {93 expect(trendConfig({})).toEqual({ metric: "cpu.usage", groupBy: "cluster" });94 expect(trendConfig({ metric: "mem.used", group_by: "site" })).toEqual({ metric: "mem.used", groupBy: "site" });95 expect(trendConfig({ metric: 4, group_by: "tag" })).toEqual({ metric: "cpu.usage", groupBy: "cluster" });96 expect(trendRange("7d", 1)).toBe("7d");97 expect(trendRange(null, 3600_000)).toBe("1h");98 expect(trendRange(null, 86400_000)).toBe("24h");99 expect(trendRange(null, 5 * 86400_000)).toBe("7d");100 expect(trendRange(null, 20 * 86400_000)).toBe("30d");101 });102});103