UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

130 lines (129 loc) 7.54 kB
"use strict"; var _heatmapScale = require("./heatmapScale"); describe("heatmapScale", function () { describe("parseHeatmapValue", function () { it("parses integer and decimal bucket labels", function () { expect((0, _heatmapScale.parseHeatmapValue)("0")).toBe(0); expect((0, _heatmapScale.parseHeatmapValue)("5")).toBe(5); expect((0, _heatmapScale.parseHeatmapValue)("10000")).toBe(10000); expect((0, _heatmapScale.parseHeatmapValue)("0.005")).toBe(0.005); expect((0, _heatmapScale.parseHeatmapValue)("2.5")).toBe(2.5); expect((0, _heatmapScale.parseHeatmapValue)(".5")).toBe(0.5); }); it("parses prefixed bucket labels", function () { expect((0, _heatmapScale.parseHeatmapValue)("bucket_1")).toBe(1); expect((0, _heatmapScale.parseHeatmapValue)("latency_0.005")).toBe(0.005); expect((0, _heatmapScale.parseHeatmapValue)("bucket_+Inf")).toBe(Infinity); }); it("parses positive infinity variants", function () { expect((0, _heatmapScale.parseHeatmapValue)("+Inf")).toBe(Infinity); expect((0, _heatmapScale.parseHeatmapValue)("+inf")).toBe(Infinity); expect((0, _heatmapScale.parseHeatmapValue)("+INF")).toBe(Infinity); }); it("rejects non-bucket labels", function () { var rejected = ["5.", "-Inf", "Inf", "5ms", "5e3", "0x10", "abc", "", null, undefined, " "]; rejected.forEach(function (value) { return expect((0, _heatmapScale.parseHeatmapValue)(value)).toBeNaN(); }); }); }); describe("isHeatmapNumeric", function () { it("accepts pure numeric and infinity labels", function () { expect((0, _heatmapScale.isHeatmapNumeric)(["0.005", "0.01", "+Inf"])).toBe(true); expect((0, _heatmapScale.isHeatmapNumeric)(["1", "2", "5", "10", "+Inf"])).toBe(true); expect((0, _heatmapScale.isHeatmapNumeric)(["bucket_1", "bucket_2", "bucket_+Inf"])).toBe(true); expect((0, _heatmapScale.isHeatmapNumeric)(["+Inf"])).toBe(true); expect((0, _heatmapScale.isHeatmapNumeric)([])).toBe(true); }); it("rejects mixed non-numeric labels", function () { expect((0, _heatmapScale.isHeatmapNumeric)(["5ms", "1", "+Inf"])).toBe(false); }); }); describe("isHeatmapBinary", function () { it("detects integer powers of two above one", function () { expect((0, _heatmapScale.isHeatmapBinary)(["1024", "2048", "4096", "+Inf"])).toBe(true); expect((0, _heatmapScale.isHeatmapBinary)(["1", "2", "4", "8"])).toBe(true); expect((0, _heatmapScale.isHeatmapBinary)(["0.25", "0.5", "1", "2", "4"])).toBe(true); }); it("rejects non-powers of two and empty candidate sets", function () { expect((0, _heatmapScale.isHeatmapBinary)(["0", "5", "10"])).toBe(false); expect((0, _heatmapScale.isHeatmapBinary)(["1000", "2000"])).toBe(false); expect((0, _heatmapScale.isHeatmapBinary)(["3", "5", "+Inf"])).toBe(false); expect((0, _heatmapScale.isHeatmapBinary)(["0.25", "0.5", "1", "+Inf"])).toBe(false); }); it("handles powers larger than 32-bit integers", function () { expect((0, _heatmapScale.isHeatmapBinary)(["4294967296", "1099511627776"])).toBe(true); }); }); describe("detectHeatmapScale", function () { it("selects binary only for power-of-two bucket sets", function () { expect((0, _heatmapScale.detectHeatmapScale)(["1024", "2048", "+Inf"])).toBe("binary"); }); it("selects SI for numeric non-binary bucket sets", function () { expect((0, _heatmapScale.detectHeatmapScale)(["0.005", "0.01", "+Inf"])).toBe("num"); expect((0, _heatmapScale.detectHeatmapScale)(["0", "5", "10", "+Inf"])).toBe("num"); }); it("returns null for non-numeric or empty bucket sets", function () { expect((0, _heatmapScale.detectHeatmapScale)(["5ms", "1"])).toBe(null); expect((0, _heatmapScale.detectHeatmapScale)([])).toBe(null); }); }); describe("sortHeatmapValues", function () { it("sorts numeric labels with positive infinity last", function () { expect((0, _heatmapScale.sortHeatmapValues)(["5", "1", "10", "+Inf"])).toEqual(["1", "5", "10", "+Inf"]); expect((0, _heatmapScale.sortHeatmapValues)(["10", "+Inf", "1", "5"])).toEqual(["1", "5", "10", "+Inf"]); expect((0, _heatmapScale.sortHeatmapValues)(["0.25", "0.005", "1", "+Inf"])).toEqual(["0.005", "0.25", "1", "+Inf"]); }); it("sorts the live Prometheus bucket shape", function () { expect((0, _heatmapScale.sortHeatmapValues)(["+Inf", "0.3", "10", "120", "15", "2", "2.5"])).toEqual(["0.3", "2", "2.5", "10", "15", "120", "+Inf"]); }); it("sorts prefixed compatibility labels while preserving original ids", function () { expect((0, _heatmapScale.sortHeatmapValues)(["bucket_+Inf", "bucket_10", "bucket_2"])).toEqual(["bucket_2", "bucket_10", "bucket_+Inf"]); }); it("falls back for non-numeric labels and does not mutate input", function () { var values = ["10", "1", "+Inf"]; var sorted = (0, _heatmapScale.sortHeatmapValues)(values); expect(sorted).toEqual(["1", "10", "+Inf"]); expect(sorted).not.toBe(values); expect(values).toEqual(["10", "1", "+Inf"]); expect((0, _heatmapScale.sortHeatmapValues)(["5ms", "1"])).toBe(null); }); }); describe("formatScaledValue", function () { it("formats SI values compactly", function () { expect((0, _heatmapScale.formatScaledValue)(0.005, "num")).toBe("5m"); expect((0, _heatmapScale.formatScaledValue)(0.0001, "num")).toBe("100u"); expect((0, _heatmapScale.formatScaledValue)(500, "num")).toBe("500"); expect((0, _heatmapScale.formatScaledValue)(1000, "num")).toBe("1k"); expect((0, _heatmapScale.formatScaledValue)(1500, "num")).toBe("1.5k"); expect((0, _heatmapScale.formatScaledValue)(1250, "num")).toBe("1.25k"); expect((0, _heatmapScale.formatScaledValue)(1000000, "num")).toBe("1M"); expect((0, _heatmapScale.formatScaledValue)(0, "num")).toBe("0"); }); it("formats binary values compactly", function () { expect((0, _heatmapScale.formatScaledValue)(500, "binary")).toBe("500"); expect((0, _heatmapScale.formatScaledValue)(1023, "binary")).toBe("1023"); expect((0, _heatmapScale.formatScaledValue)(1024, "binary")).toBe("1Ki"); expect((0, _heatmapScale.formatScaledValue)(1536, "binary")).toBe("1.5Ki"); expect((0, _heatmapScale.formatScaledValue)(1048576, "binary")).toBe("1Mi"); expect((0, _heatmapScale.formatScaledValue)(0, "binary")).toBe("0"); }); }); describe("formatHeatmapLabel", function () { it("keeps infinity literal", function () { expect((0, _heatmapScale.formatHeatmapLabel)("+Inf", "num")).toBe("+Inf"); expect((0, _heatmapScale.formatHeatmapLabel)("+Inf", "binary")).toBe("+Inf"); expect((0, _heatmapScale.formatHeatmapLabel)("+Inf", null)).toBe("+Inf"); expect((0, _heatmapScale.formatHeatmapLabel)("+inf", "num")).toBe("+Inf"); }); it("formats numeric labels using the detected scale", function () { expect((0, _heatmapScale.formatHeatmapLabel)("0.005", "num")).toBe("5m"); expect((0, _heatmapScale.formatHeatmapLabel)("1024", "binary")).toBe("1Ki"); expect((0, _heatmapScale.formatHeatmapLabel)("bucket_1024", "binary")).toBe("1Ki"); }); it("passes through fallback labels", function () { expect((0, _heatmapScale.formatHeatmapLabel)("5ms", "num")).toBe("5ms"); expect((0, _heatmapScale.formatHeatmapLabel)("0.005", null)).toBe("0.005"); }); }); });