@netdata/charts
Version:
Netdata frontend SDK and chart utilities
329 lines (323 loc) • 16.2 kB
JavaScript
;
var _conversableUnits = _interopRequireWildcard(require("./conversableUnits"));
var _testUtilities = require("@jest/testUtilities");
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 _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("conversableUnits", function () {
describe("makeConversableKey", function () {
it("creates key from unit and scale", function () {
expect((0, _conversableUnits.makeConversableKey)("seconds", "ms")).toBe("seconds-ms");
expect((0, _conversableUnits.makeConversableKey)("bytes", "KB")).toBe("bytes-KB");
});
it("handles empty strings", function () {
expect((0, _conversableUnits.makeConversableKey)("", "")).toBe("-");
expect((0, _conversableUnits.makeConversableKey)("unit", "")).toBe("unit-");
expect((0, _conversableUnits.makeConversableKey)("", "scale")).toBe("-scale");
});
});
describe("keys export", function () {
it("contains expected unit types", function () {
expect(_conversableUnits.keys).toHaveProperty("Cel");
expect(_conversableUnits.keys).toHaveProperty("ns");
expect(_conversableUnits.keys).toHaveProperty("ms");
expect(_conversableUnits.keys).toHaveProperty("s");
expect(_conversableUnits.keys).toHaveProperty("h");
expect(_conversableUnits.keys).toHaveProperty("d");
});
it("contains correct Celsius conversion options", function () {
expect(_conversableUnits.keys.Cel).toEqual(["[degF]"]);
});
it("contains correct nanosecond conversion options", function () {
expect(_conversableUnits.keys.ns).toEqual(["ns", "us", "ms", "s"]);
});
it("contains correct millisecond conversion options", function () {
expect(_conversableUnits.keys.ms).toEqual(["ns", "us", "ms", "s", "a:mo:d", "mo:d:h", "d:h:mm", "h:mm:ss", "mm:ss"]);
});
it("contains correct second conversion options", function () {
expect(_conversableUnits.keys.s).toEqual(["ns", "us", "ms", "s", "a:mo:d", "mo:d:h", "d:h:mm", "h:mm:ss", "mm:ss", "dHH:MM:ss"]);
});
it("uses the second conversion options for larger source duration units", function () {
expect(_conversableUnits.keys.min).toEqual(_conversableUnits.keys.s);
expect(_conversableUnits.keys.h).toEqual(_conversableUnits.keys.s);
expect(_conversableUnits.keys.d).toEqual(_conversableUnits.keys.s);
expect(_conversableUnits.keys.wk).toEqual(_conversableUnits.keys.s);
expect(_conversableUnits.keys.mo).toEqual(_conversableUnits.keys.s);
expect(_conversableUnits.keys.a).toEqual(_conversableUnits.keys.s);
});
});
describe("Celsius conversions", function () {
var chart;
beforeEach(function () {
chart = (0, _testUtilities.makeTestChart)().chart;
});
it("converts Celsius to Fahrenheit when temperature is fahrenheit", function () {
chart.updateAttribute("temperature", "fahrenheit");
var converter = _conversableUnits["default"].Cel["[degF]"];
expect(converter.check(chart)).toBe(true);
expect(converter.convert(0)).toBe(32); // 0°C = 32°F
expect(converter.convert(100)).toBe(212); // 100°C = 212°F
expect(converter.convert(-40)).toBe(-40); // -40°C = -40°F
});
it("does not convert when temperature is not fahrenheit", function () {
chart.updateAttribute("temperature", "celsius");
var converter = _conversableUnits["default"].Cel["[degF]"];
expect(converter.check(chart)).toBe(false);
});
});
describe("nanosecond conversions", function () {
var chart;
beforeEach(function () {
chart = (0, _testUtilities.makeTestChart)({
attributes: {
secondsAsTime: true
}
}).chart;
});
it("converts to nanoseconds for values < 1000", function () {
var converter = _conversableUnits["default"].ns.ns;
expect(converter.check(chart, 999)).toBe(true);
expect(converter.check(chart, 1000)).toBe(false);
var result = converter.convert(123);
expect(_typeof(result)).toBe("string");
expect(result).toMatch(/^\d+\.\d+$/);
});
it("converts to microseconds for values 1000-999999", function () {
var converter = _conversableUnits["default"].ns.us;
expect(converter.check(chart, 1000)).toBe(true);
expect(converter.check(chart, 999999)).toBe(true);
expect(converter.check(chart, 1000000)).toBe(false);
var result = converter.convert(1500);
expect(_typeof(result)).toBe("string");
expect(result).toMatch(/^\d+\.\d+$/);
});
it("converts to milliseconds for values 1M-999M", function () {
var converter = _conversableUnits["default"].ns.ms;
expect(converter.check(chart, 1000000)).toBe(true);
expect(converter.check(chart, 999999999)).toBe(true);
expect(converter.check(chart, 1000000000)).toBe(false);
var result = converter.convert(1500000);
expect(_typeof(result)).toBe("string");
expect(result).toMatch(/^\d+\.\d+$/);
});
it("converts to seconds for values >= 1B", function () {
var converter = _conversableUnits["default"].ns.s;
expect(converter.check(chart, 1000000000)).toBe(true);
expect(converter.check(chart, 5000000000)).toBe(true);
var result = converter.convert(1500000000);
expect(_typeof(result)).toBe("string");
expect(result).toMatch(/^\d+\.\d+$/);
});
it("does not convert when secondsAsTime is false", function () {
chart.updateAttribute("secondsAsTime", false);
expect(_conversableUnits["default"].ns.ns.check(chart, 500)).toBe(false);
expect(_conversableUnits["default"].ns.us.check(chart, 5000)).toBe(false);
});
});
describe("millisecond conversions", function () {
var chart;
beforeEach(function () {
chart = (0, _testUtilities.makeTestChart)({
attributes: {
secondsAsTime: true
}
}).chart;
});
it("converts to nanoseconds for values < 1us", function () {
var converter = _conversableUnits["default"].ms.ns;
expect(converter.check(chart, 0.0005)).toBe(true);
expect(converter.check(chart, 0.001)).toBe(false);
expect(converter.convert(0.000025)).toBe(25);
});
it("converts to microseconds for values < 1", function () {
var converter = _conversableUnits["default"].ms.us;
expect(converter.check(chart, 0.001)).toBe(true);
expect(converter.check(chart, 0.5)).toBe(true);
expect(converter.check(chart, 1)).toBe(false);
expect(converter.convert(0.5)).toBe(500);
});
it("converts to milliseconds for values 1-999", function () {
var converter = _conversableUnits["default"].ms.ms;
expect(converter.check(chart, 1)).toBe(true);
expect(converter.check(chart, 999)).toBe(true);
expect(converter.check(chart, 1000)).toBe(false);
expect(converter.convert(500)).toBe(500);
});
it("converts to seconds for values 1000-59999", function () {
var converter = _conversableUnits["default"].ms.s;
expect(converter.check(chart, 1000)).toBe(true);
expect(converter.check(chart, 59999)).toBe(true);
expect(converter.check(chart, 60000)).toBe(false);
expect(converter.convert(2000)).toBe(2);
});
it("converts to mm:ss for values 60s-59min", function () {
var converter = _conversableUnits["default"].ms["mm:ss"];
expect(converter.check(chart, 60000)).toBe(true);
expect(converter.check(chart, 3599999)).toBe(true);
expect(converter.check(chart, 3600000)).toBe(false);
var result = converter.convert(90000); // 1.5 minutes
expect(_typeof(result)).toBe("string");
expect(result).toBe("1m30s");
});
it("converts to h:mm:ss for values 1h-23h", function () {
var converter = _conversableUnits["default"].ms["h:mm:ss"];
expect(converter.check(chart, 3600000)).toBe(true);
expect(converter.check(chart, 86399999)).toBe(true);
expect(converter.check(chart, 86400000)).toBe(false);
var result = converter.convert(7200000); // 2 hours
expect(_typeof(result)).toBe("string");
expect(result).toBe("2h");
});
it("converts to longer time formats for larger values", function () {
var dayConverter = _conversableUnits["default"].ms["d:h:mm"];
expect(dayConverter.check(chart, 86400000)).toBe(true); // 1 day
var monthConverter = _conversableUnits["default"].ms["mo:d:h"];
expect(monthConverter.check(chart, 86400000 * 30)).toBe(true); // 30 days
var yearConverter = _conversableUnits["default"].ms["a:mo:d"];
expect(yearConverter.check(chart, 86400000 * 365)).toBe(true); // 365 days
});
});
describe("second conversions", function () {
var chart;
beforeEach(function () {
chart = (0, _testUtilities.makeTestChart)({
attributes: {
secondsAsTime: true
}
}).chart;
});
it("converts to nanoseconds for values below 1us", function () {
var converter = _conversableUnits["default"].s.ns;
expect(converter.check(chart, 25e-9)).toBe(true);
expect(converter.check(chart, 1e-6)).toBe(false);
expect(converter.convert(25e-9)).toBe(25);
});
it("converts to microseconds for very small values", function () {
var converter = _conversableUnits["default"].s.us;
expect(converter.check(chart, 1e-6)).toBe(true);
expect(converter.check(chart, 0.0005)).toBe(true);
expect(converter.check(chart, 0.001)).toBe(false);
expect(converter.convert(0.0005)).toBe(500);
});
it("converts to milliseconds for small values", function () {
var converter = _conversableUnits["default"].s.ms;
expect(converter.check(chart, 0.001)).toBe(true);
expect(converter.check(chart, 0.999)).toBe(true);
expect(converter.check(chart, 1)).toBe(false);
expect(converter.convert(0.5)).toBe(500);
});
it("converts to seconds for values 1-59", function () {
var converter = _conversableUnits["default"].s.s;
expect(converter.check(chart, 1)).toBe(true);
expect(converter.check(chart, 59)).toBe(true);
expect(converter.check(chart, 60)).toBe(false);
expect(converter.convert(30)).toBe(30);
});
it("converts to time formats for larger values", function () {
var mmssConverter = _conversableUnits["default"].s["mm:ss"];
expect(mmssConverter.check(chart, 60)).toBe(true);
expect(mmssConverter.check(chart, 3599)).toBe(true);
var result = mmssConverter.convert(90); // 1.5 minutes
expect(_typeof(result)).toBe("string");
expect(result).toBe("1m30s");
});
it("has dHH:MM:ss format that never auto-selects", function () {
var converter = _conversableUnits["default"].s["dHH:MM:ss"];
expect(converter.check()).toBe(false); // always false for auto-selection
var result = converter.convert(90061); // > 1 day
expect(_typeof(result)).toBe("string");
expect(result).toBe("1d1h1m1s");
});
});
describe("larger source duration conversions", function () {
var chart;
beforeEach(function () {
chart = (0, _testUtilities.makeTestChart)({
attributes: {
secondsAsTime: true
}
}).chart;
});
it("converts source minutes through the seconds formatter", function () {
expect(_conversableUnits["default"].min["h:mm:ss"].check(chart, 90)).toBe(true);
expect(_conversableUnits["default"].min["h:mm:ss"].convert(90)).toBe("1h30m");
});
it("converts source hours through the seconds formatter", function () {
expect(_conversableUnits["default"].h["d:h:mm"].check(chart, 26)).toBe(true);
expect(_conversableUnits["default"].h["d:h:mm"].convert(26)).toBe("1d2h");
});
it("converts source days, weeks, months, and years through the same path", function () {
expect(_conversableUnits["default"].d["d:h:mm"].convert(2)).toBe("2d");
expect(_conversableUnits["default"].wk["d:h:mm"].convert(2)).toBe("14d");
expect(_conversableUnits["default"].mo["mo:d:h"].convert(2)).toBe("2mo");
expect(_conversableUnits["default"].a["a:mo:d"].convert(2)).toBe("2yr");
});
});
describe("conversion function behavior", function () {
it("handles edge cases in time conversion", function () {
// Test mm:ss conversion
var mmssConverter = _conversableUnits["default"].s["mm:ss"];
var result = mmssConverter.convert(125); // 2:05
expect(_typeof(result)).toBe("string");
// Test h:mm:ss conversion
var hmmssConverter = _conversableUnits["default"].s["h:mm:ss"];
var hourResult = hmmssConverter.convert(3725); // 1:02:05
expect(_typeof(hourResult)).toBe("string");
expect(hourResult).toBe("1h2m5s");
expect(hmmssConverter.convert(33820.22)).toBe("9h23m40s.22");
});
it("applies multipliers correctly", function () {
// Test microsecond conversion (multiplier = 1000000)
var usConverter = _conversableUnits["default"].s.us;
var result = usConverter.convert(0.001); // 1ms = 1000µs
expect(result).toBe(1000);
// Test millisecond conversion (multiplier = 1000)
var msConverter = _conversableUnits["default"].s.ms;
var msResult = msConverter.convert(0.5); // 0.5s = 500ms
expect(msResult).toBe(500);
});
});
describe("structure validation", function () {
it("exports object with expected unit types", function () {
expect(_conversableUnits["default"]).toHaveProperty("Cel");
expect(_conversableUnits["default"]).toHaveProperty("ns");
expect(_conversableUnits["default"]).toHaveProperty("ms");
expect(_conversableUnits["default"]).toHaveProperty("s");
});
it("all converters have check and convert functions", function () {
Object.values(_conversableUnits["default"]).forEach(function (unitGroup) {
Object.values(unitGroup).forEach(function (converter) {
expect(converter).toHaveProperty("check");
expect(converter).toHaveProperty("convert");
expect(_typeof(converter.check)).toBe("function");
expect(_typeof(converter.convert)).toBe("function");
});
});
});
it("check functions return boolean values", function () {
var chart = (0, _testUtilities.makeTestChart)({
attributes: {
secondsAsTime: true,
temperature: "fahrenheit"
}
}).chart;
Object.values(_conversableUnits["default"]).forEach(function (unitGroup) {
Object.values(unitGroup).forEach(function (converter) {
var result = converter.check(chart, 1000);
expect(_typeof(result)).toBe("boolean");
});
});
});
});
describe("temperature conversion edge cases", function () {
it("handles negative temperatures correctly", function () {
var converter = _conversableUnits["default"].Cel["[degF]"];
expect(converter.convert(-273.15)).toBeCloseTo(-459.67, 1); // Absolute zero
expect(converter.convert(-17.78)).toBeCloseTo(0, 1); // 0°F
});
it("handles fractional temperatures", function () {
var converter = _conversableUnits["default"].Cel["[degF]"];
expect(converter.convert(36.67)).toBeCloseTo(98, 1); // ~98°F (body temp)
expect(converter.convert(100.56)).toBeCloseTo(213, 1); // Slightly above boiling
});
});
});