SPB Git

Repository Public

group.test.ts 2.3 KB 52 lines typescript

1import { describe, expect, it } from "vitest";2import type { Schemas } from "@/api/client";3import { groupByNode, isIpAddress, usedPct } from "./group";45type V = Schemas["Volume"];6const vol = (p: Partial<V>): V => ({ id: Math.random().toString(36), freshness: "fresh", kind: "internal", last_seen_at: "2026-11-18T00:00:00Z", mount: "/", node_alias: "e2e-node-01", node_id: "n1", ...p });78describe("usedPct", () => {9  it("is undefined when a figure is missing, never zero", () => {10    expect(usedPct({ total_bytes: 100 })).toBeUndefined();11    expect(usedPct({ used_bytes: 10 })).toBeUndefined();12    expect(usedPct({ used_bytes: 10, total_bytes: 0 })).toBeUndefined();13    expect(usedPct({ used_bytes: 25, total_bytes: 100 })).toBe(25);14  });15});1617describe("groupByNode", () => {18  it("groups by node in natural alias order and sorts mounts", () => {19    const g = groupByNode([20      vol({ node_id: "n10", node_alias: "e2e-node-10", mount: "/" }),21      vol({ node_id: "n2", node_alias: "e2e-node-02", mount: "/Volumes/ssd" }),22      vol({ node_id: "n2", node_alias: "e2e-node-02", mount: "/" }),23    ]);24    expect(g.map((x) => x.alias)).toEqual(["e2e-node-02", "e2e-node-10"]);25    expect(g[0]?.volumes.map((v) => v.mount)).toEqual(["/", "/Volumes/ssd"]);26  });27  it("totals only observed local volumes", () => {28    const [g] = groupByNode([29      vol({ used_bytes: 40, total_bytes: 100 }),30      vol({ mount: "/Volumes/ssd", kind: "external", used_bytes: 10, total_bytes: 50 }),31      vol({ mount: "/nas", kind: "network", used_bytes: 500, total_bytes: 1000 }),32      vol({ mount: "/old", used_bytes: 1, total_bytes: 2, gone_at: "2026-11-17T00:00:00Z" }),33      vol({ mount: "/unknown" }),34    ]);35    expect(g).toMatchObject({ usedBytes: 50, totalBytes: 150, counted: 2 });36    expect(g?.volumes).toHaveLength(5);37  });38});3940describe("isIpAddress", () => {41  it("accepts IPv4 and IPv6 literals only", () => {42    expect(isIpAddress("192.168.2.173")).toBe(true);43    expect(isIpAddress(" 10.0.0.1 ")).toBe(true);44    expect(isIpAddress("fe80::1")).toBe(true);45    expect(isIpAddress("2001:db8:0:0:0:0:0:1")).toBe(true);46    expect(isIpAddress("256.1.1.1")).toBe(false);47    expect(isIpAddress("nas.local")).toBe(false);48    expect(isIpAddress("1.2.3")).toBe(false);49    expect(isIpAddress("")).toBe(false);50  });51});52
Files main
Menu

Theme