Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.5 KB
Raw Blame
import { describe, expect, test } from "vitest";

import { formatBitRate } from "@homarr/common";

import { calculateBandwidth } from "./bandwidth";

describe("calculateBandwidth", () => {
  test("sorts samples before calculating a rate", () => {
    const result = calculateBandwidth([
      { timestamp: new Date("2026-01-01T00:00:00Z"), data: [{ name: "wan", receive: 100, transmit: 200 }] },
      { timestamp: new Date("2026-01-01T00:00:10Z"), data: [{ name: "wan", receive: 300, transmit: 500 }] },
    ]);

    expect(result.data).toEqual([{ name: "wan", receive: 160, transmit: 240 }]);
  });

  test("treats counter resets as zero instead of negative traffic", () => {
    const result = calculateBandwidth([
      { timestamp: new Date("2026-01-01T00:00:10Z"), data: [{ name: "wan", receive: 10, transmit: 10 }] },
      { timestamp: new Date("2026-01-01T00:00:00Z"), data: [{ name: "wan", receive: 100, transmit: 100 }] },
    ]);

    expect(result.data).toEqual([{ name: "wan", receive: 0, transmit: 0 }]);
  });

  test("rejects duplicate timestamps", () => {
    const timestamp = new Date("2026-01-01T00:00:00Z");
    expect(
      calculateBandwidth([
        { timestamp, data: [{ name: "wan", receive: 100, transmit: 100 }] },
        { timestamp, data: [{ name: "wan", receive: 50, transmit: 50 }] },
      ]).data,
    ).toEqual([]);
  });
});

describe("formatBitRate", () => {
  test("handles invalid and negative rates", () => {
    expect(formatBitRate(Number.NaN)).toBe("0 b/s");
    expect(formatBitRate(-1)).toBe("0 b/s");
  });
});