UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

614 lines (613 loc) 29.9 kB
"use strict"; var _ = _interopRequireWildcard(require(".")); var _scalableUnits = _interopRequireDefault(require("./scalableUnits")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } describe("units helpers", function () { describe("unitsMissing", function () { it("returns true for undefined units", function () { expect((0, _.unitsMissing)("nonexistent_unit")).toBe(true); }); it("returns false for existing units", function () { expect((0, _.unitsMissing)("By")).toBe(false); expect((0, _.unitsMissing)("s")).toBe(false); expect((0, _.unitsMissing)("%")).toBe(false); expect((0, _.unitsMissing)("requests/s")).toBe(false); expect((0, _.unitsMissing)("unknown")).toBe(false); expect((0, _.unitsMissing)("m3/min")).toBe(false); }); it("returns boolean for any input", function () { expect(_typeof((0, _.unitsMissing)("any_unit"))).toBe("boolean"); expect(_typeof((0, _.unitsMissing)(""))).toBe("boolean"); expect(_typeof((0, _.unitsMissing)(null))).toBe("boolean"); expect(_typeof((0, _.unitsMissing)(undefined))).toBe("boolean"); }); }); describe("getUnitConfig", function () { it("returns config object with required properties", function () { var config = (0, _.getUnitConfig)("By"); expect(config).toHaveProperty("is_scalable", true); expect(config).toHaveProperty("is_metric", true); expect(config).toHaveProperty("is_binary", true); expect(config).toHaveProperty("print_symbol", "B"); expect(config).toHaveProperty("name", "bytes"); // is_bit is added by getUnitConfig for unknown units, but By doesn't have it expect(config.is_bit).toBeUndefined(); }); it("returns actual unit config for known units", function () { var config = (0, _.getUnitConfig)("s"); expect(config.is_metric).toBe(true); expect(config.is_scalable).toBe(true); expect(config.print_symbol).toBe("s"); expect(config.name).toBe("seconds"); }); it("uses the micro sign for microsecond display labels", function () { var config = (0, _.getUnitConfig)("us"); expect(config.print_symbol).toBe("\xB5s"); expect(config.print_symbol).not.toBe("\u03BCs"); }); it("returns default config for unknown units", function () { var config = (0, _.getUnitConfig)("unknown_unit"); expect(config).toEqual({ is_scalable: true, is_metric: false, is_binary: false, is_bit: false, print_symbol: "unknown_unit", name: "unknown_unit" }); }); it("handles null units", function () { var config = (0, _.getUnitConfig)(null); expect(config.print_symbol).toBe(""); expect(config.name).toBe(""); }); it("handles undefined units", function () { var config = (0, _.getUnitConfig)(undefined); expect(config.print_symbol).toBe(""); expect(config.name).toBe(""); }); it("preserves unit string for unknown units", function () { var config = (0, _.getUnitConfig)("custom/second"); expect(config.print_symbol).toBe("custom/second"); expect(config.name).toBe("custom/second"); }); }); describe("getAlias", function () { it("returns alias for known aliases", function () { expect((0, _.getAlias)("% of time working")).toBe("%"); expect((0, _.getAlias)("seconds")).toBe("s"); expect((0, _.getAlias)("bytes")).toBe("By"); }); it("accepts both microsecond characters as aliases", function () { expect((0, _.getAlias)("\xB5s")).toBe("us"); expect((0, _.getAlias)("\u03BCs")).toBe("us"); }); it("aliases source-scaled Agent units without losing their denominator", function () { expect((0, _.getAlias)("KB")).toBe("KBy"); expect((0, _.getAlias)("MiB")).toBe("MiBy"); expect((0, _.getAlias)("MB")).toBe("MBy"); expect((0, _.getAlias)("GB")).toBe("GBy"); expect((0, _.getAlias)("TB")).toBe("TBy"); expect((0, _.getAlias)("PB")).toBe("PBy"); expect((0, _.getAlias)("GBy")).toBe("GBy"); expect((0, _.getAlias)("TiB")).toBe("TiBy"); expect((0, _.getAlias)("PiB")).toBe("PiBy"); expect((0, _.getAlias)("MB/s")).toBe("MBy/s"); expect((0, _.getAlias)("GB/s")).toBe("GBy/s"); expect((0, _.getAlias)("kilobytes/s")).toBe("KBy/s"); expect((0, _.getAlias)("kilobytes per operation")).toBe("KBy/{operation}"); expect((0, _.getAlias)("bits")).toBe("bit"); expect((0, _.getAlias)("bits/s")).toBe("bit/s"); expect((0, _.getAlias)("kilobits")).toBe("Kibit"); expect((0, _.getAlias)("kbit/s")).toBe("Kibit/s"); expect((0, _.getAlias)("megabits")).toBe("Mibit"); expect((0, _.getAlias)("Mbps")).toBe("Mibit/s"); expect((0, _.getAlias)("MHz")).toBe("MHz"); expect((0, _.getAlias)("microsecond")).toBe("us"); expect((0, _.getAlias)("millisecond")).toBe("ms"); expect((0, _.getAlias)("nanosecond")).toBe("ns"); expect((0, _.getAlias)("nanoseconds")).toBe("ns"); expect((0, _.getAlias)("second")).toBe("s"); expect((0, _.getAlias)("sec")).toBe("s"); expect((0, _.getAlias)("secs")).toBe("s"); expect((0, _.getAlias)("hr")).toBe("h"); expect((0, _.getAlias)("hrs")).toBe("h"); expect((0, _.getAlias)("Celcius")).toBe("Cel"); expect((0, _.getAlias)("Joules")).toBe("J"); expect((0, _.getAlias)("pcent")).toBe("%"); expect((0, _.getAlias)("minutes")).toBe("min"); expect((0, _.getAlias)("hours")).toBe("h"); expect((0, _.getAlias)("weeks")).toBe("wk"); expect((0, _.getAlias)("months")).toBe("mo"); expect((0, _.getAlias)("years")).toBe("a"); expect((0, _.getAlias)("KiB/operation")).toBe("KiBy/{operation}"); expect((0, _.getAlias)("milliseconds/request")).toBe("ms/{request}"); expect((0, _.getAlias)("milliseconds/run")).toBe("ms/{run}"); expect((0, _.getAlias)("milliseconds/operation")).toBe("ms/{operation}"); expect((0, _.getAlias)("usec/s")).toBe("us/s"); expect((0, _.getAlias)("gigabytes")).toBe("GBy"); expect((0, _.getAlias)("GiB/s")).toBe("GiBy/s"); expect((0, _.getAlias)("millicpu")).toBe("m[CPU]"); expect((0, _.getAlias)("milliamps")).toBe("mA"); expect((0, _.getAlias)("millivolts")).toBe("mV"); expect((0, _.getAlias)("mJ/s")).toBe("mW"); expect((0, _.getAlias)("dBm")).toBe("dB[mW]"); }); it("aliases the additional verified Netdata unit strings", function () { expect((0, _.getAlias)("scopes")).toBe("{scope}"); expect((0, _.getAlias)("values")).toBe("{value}"); expect((0, _.getAlias)("fields")).toBe("{field}"); expect((0, _.getAlias)("buckets")).toBe("{bucket}"); expect((0, _.getAlias)("buckets/s")).toBe("{bucket}/s"); expect((0, _.getAlias)("count/s")).toBe("{count}/s"); expect((0, _.getAlias)("unknown")).toBe("{item}"); expect((0, _.getAlias)("m3/min")).toBe("m3/min"); expect((0, _.getAlias)("dimensions")).toBe("{dimension}"); expect((0, _.getAlias)("models")).toBe("{model}"); expect((0, _.getAlias)("profile_units")).toBe("{profile unit}"); expect((0, _.getAlias)("vus")).toBe("{virtual user}"); expect((0, _.getAlias)("iterations")).toBe("{iteration}"); expect((0, _.getAlias)("kb/s")).toBe("Kibit/s"); expect((0, _.getAlias)("tests/s")).toBe("{test}/s"); expect((0, _.getAlias)("worker processes")).toBe("{worker process}"); expect((0, _.getAlias)("app pool status")).toBe("{status}"); expect((0, _.getAlias)("app pool worker processes")).toBe("{worker process}"); expect((0, _.getAlias)("app pool failures")).toBe("{app pool failure}"); expect((0, _.getAlias)("app pool recycles")).toBe("{app pool recycle}"); expect((0, _.getAlias)("app pool uptime")).toBe("app pool uptime"); expect((0, _.getAlias)("w3svc w3wp")).toBe("w3svc w3wp"); expect((0, _.getAlias)("failed servers")).toBe("{failed server}"); expect((0, _.getAlias)("health servers")).toBe("{health server}"); expect((0, _.getAlias)("maintenance servers")).toBe("{maintenance server}"); expect((0, _.getAlias)("responses")).toBe("{response}"); }); it("returns original unit if no alias exists but unit is known", function () { expect((0, _.getAlias)("By")).toBe("By"); expect((0, _.getAlias)("s")).toBe("s"); expect((0, _.getAlias)("%")).toBe("%"); }); it("finds curly brace variants", function () { // Test with actual curly brace units from all.js var result = (0, _.getAlias)("custom"); // If {custom} doesn't exist, it should return "custom" expect(_typeof(result)).toBe("string"); }); it("handles units with slashes", function () { // Mock the all.js structure to test slash handling // Test with existing slash patterns from all.js // actions/s is aliased to {action}/s expect((0, _.getAlias)("actions/s")).toBe("{action}/s"); }); it("returns original unit if no match found", function () { expect((0, _.getAlias)("completely_unknown")).toBe("completely_unknown"); }); }); describe("getNormalizedUnit", function () { it("returns the base unit for pre-scaled binary and metric units", function () { expect((0, _.getNormalizedUnit)("KiBy")).toBe("By"); expect((0, _.getNormalizedUnit)("KiBy/s")).toBe("By/s"); expect((0, _.getNormalizedUnit)("KiBy/{operation}")).toBe("By/{operation}"); expect((0, _.getNormalizedUnit)("GiBy")).toBe("By"); expect((0, _.getNormalizedUnit)("GiBy/s")).toBe("By/s"); expect((0, _.getNormalizedUnit)("TiBy")).toBe("By"); expect((0, _.getNormalizedUnit)("PiBy")).toBe("By"); expect((0, _.getNormalizedUnit)("KBy")).toBe("By"); expect((0, _.getNormalizedUnit)("MBy")).toBe("By"); expect((0, _.getNormalizedUnit)("GBy")).toBe("By"); expect((0, _.getNormalizedUnit)("TBy")).toBe("By"); expect((0, _.getNormalizedUnit)("PBy")).toBe("By"); expect((0, _.getNormalizedUnit)("GBy/s")).toBe("By/s"); expect((0, _.getNormalizedUnit)("KBy/{operation}")).toBe("By/{operation}"); expect((0, _.getNormalizedUnit)("ms")).toBe("s"); expect((0, _.getNormalizedUnit)("ms/{request}")).toBe("s/{request}"); expect((0, _.getNormalizedUnit)("Mibit/s")).toBe("bit/s"); expect((0, _.getNormalizedUnit)("kb/s")).toBe("bit/s"); expect((0, _.getNormalizedUnit)("MHz")).toBe("Hz"); expect((0, _.getNormalizedUnit)("m[CPU]")).toBe("[CPU]"); expect((0, _.getNormalizedUnit)("min")).toBe("s"); expect((0, _.getNormalizedUnit)("h")).toBe("s"); expect((0, _.getNormalizedUnit)("d")).toBe("s"); expect((0, _.getNormalizedUnit)("wk")).toBe("s"); expect((0, _.getNormalizedUnit)("mo")).toBe("s"); expect((0, _.getNormalizedUnit)("a")).toBe("s"); expect((0, _.getNormalizedUnit)("mA")).toBe("Ampere"); expect((0, _.getNormalizedUnit)("mW")).toBe("W"); expect((0, _.getNormalizedUnit)("unknown")).toBe("{item}"); }); it("keeps non-scalable special units in their source unit", function () { expect((0, _.getNormalizedUnit)("dB[mW]")).toBe("dB[mW]"); }); it("returns normalized unit config labels", function () { expect((0, _.getNormalizedUnitConfig)("KiBy").name).toBe("bytes"); expect((0, _.getNormalizedUnitConfig)("ms/{request}").name).toBe("seconds per request"); expect((0, _.getNormalizedUnitConfig)("m[CPU]").name).toBe("cores"); expect((0, _.getNormalizedUnitConfig)("dB[mW]").name).toBe("decibel milliwatts"); expect((0, _.getNormalizedUnitConfig)("unknown").name).toBe("items"); }); }); describe("isScalable", function () { it("returns true for scalable units", function () { expect((0, _.isScalable)("By")).toBe(true); expect((0, _.isScalable)("s")).toBe(true); expect((0, _.isScalable)("bit")).toBe(true); expect((0, _.isScalable)("unknown")).toBe(true); }); it("returns false for non-scalable units", function () { expect((0, _.isScalable)("%")).toBe(false); expect((0, _.isScalable)("m3/min")).toBe(false); // Most units default to scalable, find actual non-scalable ones var config = (0, _.getUnitConfig)("%"); expect(config.is_scalable).toBe(false); }); it("handles string input", function () { expect((0, _.isScalable)("unknown_unit")).toBe(true); // defaults to scalable }); it("handles unit config object input", function () { var scalableConfig = { is_scalable: true }; var nonScalableConfig = { is_scalable: false }; expect((0, _.isScalable)(scalableConfig)).toBe(true); expect((0, _.isScalable)(nonScalableConfig)).toBe(false); }); it("handles empty or undefined input", function () { expect((0, _.isScalable)("")).toBe(true); expect((0, _.isScalable)()).toBe(true); expect((0, _.isScalable)(null)).toBeUndefined(); }); }); describe("isMetric", function () { it("returns true for metric units", function () { expect((0, _.isMetric)("s")).toBe(true); expect((0, _.isMetric)("By")).toBe(true); // bytes is metric in all.js }); it("returns false for non-metric units", function () { expect((0, _.isMetric)("%")).toBe(false); // Note: By (bytes) is actually metric:true in all.js }); it("handles unit config object input", function () { var metricConfig = { is_metric: true }; var nonMetricConfig = { is_metric: false }; expect((0, _.isMetric)(metricConfig)).toBe(true); expect((0, _.isMetric)(nonMetricConfig)).toBe(false); }); it("handles empty or undefined input", function () { expect((0, _.isMetric)("")).toBe(false); expect((0, _.isMetric)()).toBe(false); }); }); describe("isBinary", function () { it("returns true for binary units", function () { expect((0, _.isBinary)("By")).toBe(true); }); it("returns false for non-binary units", function () { expect((0, _.isBinary)("s")).toBe(false); expect((0, _.isBinary)("bit")).toBe(false); expect((0, _.isBinary)("%")).toBe(false); }); it("handles unit config object input", function () { var binaryConfig = { is_binary: true }; var nonBinaryConfig = { is_binary: false }; expect((0, _.isBinary)(binaryConfig)).toBe(true); expect((0, _.isBinary)(nonBinaryConfig)).toBe(false); }); it("handles empty or undefined input", function () { expect((0, _.isBinary)("")).toBe(false); expect((0, _.isBinary)()).toBe(false); }); }); describe("isBit", function () { it("returns true for bit units", function () { expect((0, _.isBit)("bit")).toBe(true); }); it("returns false for non-bit units", function () { // Units without is_bit property return undefined expect((0, _.isBit)("By")).toBeUndefined(); expect((0, _.isBit)("s")).toBeUndefined(); expect((0, _.isBit)("%")).toBeUndefined(); }); it("handles unit config object input", function () { var bitConfig = { is_bit: true }; var nonBitConfig = { is_bit: false }; expect((0, _.isBit)(bitConfig)).toBe(true); expect((0, _.isBit)(nonBitConfig)).toBe(false); }); it("handles empty or undefined input", function () { expect((0, _.isBit)("")).toBe(false); expect((0, _.isBit)()).toBe(false); }); }); describe("isDecimalByte", function () { it("returns true for decimal byte units", function () { expect((0, _.isDecimalByte)("KBy")).toBe(true); expect((0, _.isDecimalByte)("MBy")).toBe(true); expect((0, _.isDecimalByte)("GBy")).toBe(true); expect((0, _.isDecimalByte)("TBy")).toBe(true); expect((0, _.isDecimalByte)("PBy")).toBe(true); expect((0, _.isDecimalByte)("MBy/s")).toBe(true); expect((0, _.isDecimalByte)("KBy/{operation}")).toBe(true); }); it("returns false for binary byte units", function () { expect((0, _.isDecimalByte)("KiBy")).toBe(false); expect((0, _.isDecimalByte)("MiBy")).toBe(false); expect((0, _.isDecimalByte)("GiBy")).toBe(false); expect((0, _.isDecimalByte)("TiBy")).toBe(false); expect((0, _.isDecimalByte)("PiBy")).toBe(false); }); }); describe("getScales", function () { it("returns empty arrays for non-scalable units", function () { var _getScales = (0, _.getScales)("%"), _getScales2 = _slicedToArray(_getScales, 2), keys = _getScales2[0], values = _getScales2[1]; expect(keys).toEqual([]); expect(values).toEqual({}); }); it("returns binary scales for binary units", function () { var _getScales3 = (0, _.getScales)("By"), _getScales4 = _slicedToArray(_getScales3, 2), keys = _getScales4[0], values = _getScales4[1]; expect(keys).toContain("1"); expect(keys).toContain("Ki"); expect(keys).toContain("Mi"); expect(values).toBe(_scalableUnits["default"].binary); }); it("returns decimal byte scales for decimal byte units", function () { var _getScales5 = (0, _.getScales)("MBy"), _getScales6 = _slicedToArray(_getScales5, 2), keys = _getScales6[0], values = _getScales6[1]; expect(keys).toContain("K"); expect(keys).toContain("G"); expect(values).toBe(_scalableUnits["default"].decimalBytes); }); it("returns num scales for bit units", function () { var _getScales7 = (0, _.getScales)("bit"), _getScales8 = _slicedToArray(_getScales7, 2), keys = _getScales8[0], values = _getScales8[1]; expect(keys).toContain("1"); expect(keys).toContain("k"); expect(keys).toContain("M"); expect(values).toBe(_scalableUnits["default"].num); }); it("returns num scales for metric units", function () { var _getScales9 = (0, _.getScales)("s"), _getScales0 = _slicedToArray(_getScales9, 2), keys = _getScales0[0], values = _getScales0[1]; expect(keys).toContain("1"); expect(keys).toContain("k"); expect(keys).toContain("m"); expect(keys).toContain("u"); expect(values).toBe(_scalableUnits["default"].num); }); it("returns decimal scales for other scalable units", function () { var _getScales1 = (0, _.getScales)("unknown_scalable"), _getScales10 = _slicedToArray(_getScales1, 2), keys = _getScales10[0], values = _getScales10[1]; expect(keys).toEqual(["1", "K", "M", "B", "T"]); expect(values).toBe(_scalableUnits["default"].decimal); }); }); describe("getUnitsString", function () { it("returns base label for non-scalable units", function () { var result = (0, _.getUnitsString)("%", "", "%"); expect(result).toBe("%"); }); it("returns base label with config for non-scalable units", function () { var config = { print_symbol: "%", name: "percentage" }; var result = (0, _.getUnitsString)(config, "", "percent"); expect(result).toBe("%"); }); it("returns long name when requested for non-scalable units", function () { var config = { print_symbol: "%", name: "percentage" }; var result = (0, _.getUnitsString)(config, "", "percent", true); expect(result).toBe("percentage"); }); it("combines prefix and base for metric units", function () { var result = (0, _.getUnitsString)("s", "k", "s"); expect(result).toBe("ks"); }); it("combines prefix and base for binary units", function () { var result = (0, _.getUnitsString)("By", "Ki", "B"); expect(result).toBe("KiB"); }); it("combines prefix and base for bit units", function () { var result = (0, _.getUnitsString)("bit", "k", "bit"); expect(result).toBe("kbit"); }); it("uses scale-only format for counted scalable units", function () { var result = (0, _.getUnitsString)("unknown_scalable", "K", "items"); expect(result).toBe("K"); }); it("uses scale-only format for request rate units", function () { var result = (0, _.getUnitsString)("{request}/s", "M", "requests/s"); expect(result).toBe("M"); }); it("uses scale-only format for raw alias request rate units", function () { var result = (0, _.getUnitsString)("requests/s", "M", "requests/s"); expect(result).toBe("M"); }); it("uses scale-only format for verified count rate aliases", function () { expect((0, _.getUnitsString)("buckets/s", "M", "buckets/s")).toBe("M"); expect((0, _.getUnitsString)("count/s", "M", "count/s")).toBe("M"); }); it("treats the unknown sentinel as a generic scalable item count", function () { expect((0, _.getUnitsString)("unknown", "", "items")).toBe(""); expect((0, _.getUnitsString)("unknown", "M", "items")).toBe("M"); }); it("keeps verified literal units non-scaled", function () { expect((0, _.getUnitsString)("m3/min", "K", "m3/min")).toBe("m3/min"); }); it("can force full decimal prefix format for counted scalable units", function () { var result = (0, _.getUnitsString)("unknown_scalable", "K", "items", false, { mode: "full" }); expect(result).toBe("K items"); }); it("handles empty prefix", function () { var result = (0, _.getUnitsString)("s", "", "s"); expect(result).toBe("s"); }); it("hides unit labels for compact duration values", function () { var result = (0, _.getUnitsString)("s", "", "h:mm:ss"); expect(result).toBe(""); }); it("handles long format for prefixes", function () { var result = (0, _.getUnitsString)("s", "k", "seconds", true); expect(result).toBe("kiloseconds"); }); it("uses the generic item label when full units are requested", function () { var result = (0, _.getUnitsString)("unknown", "", "", false, { mode: "full" }); expect(result).toBe("items"); }); it("hides units with empty print_symbol like {boolean}", function () { var result = (0, _.getUnitsString)("{boolean}", "", "{boolean}"); expect(result).toBe(""); }); it("shows non-scalable units like percentage when base is empty", function () { var result = (0, _.getUnitsString)("%", "", ""); expect(result).toBe("%"); }); it("uses base over print_symbol for scaled units", function () { var result = (0, _.getUnitsString)("By", "Mi", "B"); expect(result).toBe("MiB"); }); it("shows the prefix once for pre-scaled units with normalized base units", function () { expect((0, _.getUnitsString)("KiBy/{operation}", "Mi", "By/{operation}")).toBe("MiB/op"); expect((0, _.getUnitsString)("MBy", "G", "By")).toBe("GB"); expect((0, _.getUnitsString)("MBy/s", "K", "By/s")).toBe("KB/s"); expect((0, _.getUnitsString)("kb/s", "M", "bit/s")).toBe("Mbit/s"); expect((0, _.getUnitsString)("KBy/{operation}", "M", "By/{operation}")).toBe("MB/op"); expect((0, _.getUnitsString)("ms/{request}", "u", "s/{request}")).toBe("\xB5s/req"); expect((0, _.getUnitsString)("ms/{operation}", "", "s/{operation}")).toBe("s/op"); expect((0, _.getUnitsString)("mA", "k", "Ampere")).toBe("kA"); }); it("keeps full compound units when explicitly requested", function () { expect((0, _.getUnitsString)("KiBy/{operation}", "Mi", "By/{operation}", false, { mode: "full" })).toBe("MiB/operation"); expect((0, _.getUnitsString)("ms/{request}", "u", "s/{request}", true)).toBe("microseconds per request"); }); it("does not compact per-second duration rate units", function () { expect((0, _.getUnitsString)("ms/s", "m", "s/s")).toBe("ms/s"); expect((0, _.getUnitsString)("ms/s", "", "s/s")).toBe("s/s"); }); it("labels CPU cores without using CPU for whole-core scale", function () { expect((0, _.getUnitsString)("m[CPU]", "m", "[CPU]")).toBe("mCPU"); expect((0, _.getUnitsString)("c[CPU]", "c", "[CPU]")).toBe("cCPU"); expect((0, _.getUnitsString)("m[CPU]", "", "[CPU]")).toBe("core"); expect((0, _.getUnitsString)("m[CPU]", "", "[CPU]", true)).toBe("cores"); }); }); describe("default export (unitConverter)", function () { var mockChart; beforeEach(function () { mockChart = { getAttribute: jest.fn(), updateAttribute: jest.fn() }; }); it("returns original value for original method", function () { var result = (0, _["default"])(mockChart, "original", 100); expect(result).toBe(100); }); it("divides value for divide method", function () { var result = (0, _["default"])(mockChart, "divide", 1000, 10); expect(result).toBe(100); }); it("applies make function for adjust method", function () { var makeFn = jest.fn(function (value) { return value * 2; }); var result = (0, _["default"])(mockChart, "adjust", 50, makeFn); expect(result).toBe(100); expect(makeFn).toHaveBeenCalledWith(50); }); it("falls back to original for unknown methods", function () { var result = (0, _["default"])(mockChart, "unknown_method", 42); expect(result).toBe(42); }); it("handles conversable unit methods", function () { // This would be tested more thoroughly with actual conversableUnits data var result = (0, _["default"])(mockChart, "seconds-ms", 1000); expect(_typeof(result)).toBe("number"); }); it("handles null/undefined values gracefully", function () { expect((0, _["default"])(mockChart, "original", null)).toBeNull(); expect((0, _["default"])(mockChart, "original", undefined)).toBeUndefined(); expect((0, _["default"])(mockChart, "original", 0)).toBe(0); }); }); describe("edge cases and error handling", function () { it("handles malformed unit strings", function () { expect(function () { return (0, _.getUnitConfig)(""); }).not.toThrow(); expect(function () { return (0, _.getAlias)(""); }).not.toThrow(); expect(function () { return (0, _.isScalable)(""); }).not.toThrow(); }); it("handles numeric inputs where strings expected", function () { expect(function () { return (0, _.getUnitConfig)(123); }).not.toThrow(); expect(function () { return (0, _.getAlias)(123); }).not.toThrow(); }); it("handles complex slash patterns in getAlias", function () { // Test deep slash patterns var complexUnit = "requests/per/second/avg"; var result = (0, _.getAlias)(complexUnit); expect(_typeof(result)).toBe("string"); }); it("preserves object references for scale data", function () { var _getScales11 = (0, _.getScales)("By"), _getScales12 = _slicedToArray(_getScales11, 2), binaryScales = _getScales12[1]; var _getScales13 = (0, _.getScales)("s"), _getScales14 = _slicedToArray(_getScales13, 2), metricScales = _getScales14[1]; expect(binaryScales).toBe(_scalableUnits["default"].binary); expect(metricScales).toBe(_scalableUnits["default"].num); }); }); });