SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
test: added tests for uplotv2 utils (#10253)
Abhi kumar4a20e93b2038
diff --git a/frontend/src/lib/uPlotV2/utils/__tests__/dataUtils.test.ts b/frontend/src/lib/uPlotV2/utils/__tests__/dataUtils.test.tsnew file mode 100644index 0000000000..f869c1d28c--- /dev/null+++ b/frontend/src/lib/uPlotV2/utils/__tests__/dataUtils.test.ts@@ -0,0 +1,62 @@+import { isInvalidPlotValue, normalizePlotValue } from '../dataUtils';++describe('dataUtils', () => {+ describe('isInvalidPlotValue', () => {+ it('treats null and undefined as invalid', () => {+ expect(isInvalidPlotValue(null)).toBe(true);+ expect(isInvalidPlotValue(undefined)).toBe(true);+ });++ it('treats finite numbers as valid and non-finite as invalid', () => {+ expect(isInvalidPlotValue(0)).toBe(false);+ expect(isInvalidPlotValue(123.45)).toBe(false);+ expect(isInvalidPlotValue(Number.NaN)).toBe(true);+ expect(isInvalidPlotValue(Infinity)).toBe(true);+ expect(isInvalidPlotValue(-Infinity)).toBe(true);+ });++ it('treats well-formed numeric strings as valid', () => {+ expect(isInvalidPlotValue('0')).toBe(false);+ expect(isInvalidPlotValue('123.45')).toBe(false);+ expect(isInvalidPlotValue('-1')).toBe(false);+ });++ it('treats Infinity/NaN string variants and non-numeric strings as invalid', () => {+ expect(isInvalidPlotValue('+Inf')).toBe(true);+ expect(isInvalidPlotValue('-Inf')).toBe(true);+ expect(isInvalidPlotValue('Infinity')).toBe(true);+ expect(isInvalidPlotValue('-Infinity')).toBe(true);+ expect(isInvalidPlotValue('NaN')).toBe(true);+ expect(isInvalidPlotValue('not-a-number')).toBe(true);+ });++ it('treats non-number, non-string values as valid (left to caller)', () => {+ expect(isInvalidPlotValue({})).toBe(false);+ expect(isInvalidPlotValue([])).toBe(false);+ expect(isInvalidPlotValue(true)).toBe(false);+ });+ });++ describe('normalizePlotValue', () => {+ it('returns null for invalid values detected by isInvalidPlotValue', () => {+ expect(normalizePlotValue(null)).toBeNull();+ expect(normalizePlotValue(undefined)).toBeNull();+ expect(normalizePlotValue(NaN)).toBeNull();+ expect(normalizePlotValue(Infinity)).toBeNull();+ expect(normalizePlotValue('-Infinity')).toBeNull();+ expect(normalizePlotValue('not-a-number')).toBeNull();+ });++ it('parses valid numeric strings into numbers', () => {+ expect(normalizePlotValue('0')).toBe(0);+ expect(normalizePlotValue('123.45')).toBe(123.45);+ expect(normalizePlotValue('-1')).toBe(-1);+ });++ it('passes through valid numbers unchanged', () => {+ expect(normalizePlotValue(0)).toBe(0);+ expect(normalizePlotValue(123)).toBe(123);+ expect(normalizePlotValue(42.5)).toBe(42.5);+ });+ });+});diff --git a/frontend/src/lib/uPlotV2/utils/__tests__/scale.test.ts b/frontend/src/lib/uPlotV2/utils/__tests__/scale.test.tsnew file mode 100644index 0000000000..7225445013--- /dev/null+++ b/frontend/src/lib/uPlotV2/utils/__tests__/scale.test.ts@@ -0,0 +1,201 @@+import uPlot from 'uplot';++import { DistributionType } from '../../config/types';+import * as scaleUtils from '../scale';++describe('scale utils', () => {+ describe('normalizeLogScaleLimits', () => {+ it('returns limits unchanged when distribution is not logarithmic', () => {+ const limits = {+ min: 1,+ max: 100,+ softMin: 5,+ softMax: 50,+ };++ const result = scaleUtils.normalizeLogScaleLimits({+ distr: DistributionType.Linear,+ logBase: 10,+ limits,+ });++ expect(result).toEqual(limits);+ });++ it('snaps positive limits to powers of the log base when distribution is logarithmic', () => {+ const result = scaleUtils.normalizeLogScaleLimits({+ distr: DistributionType.Logarithmic,+ logBase: 10,+ limits: {+ min: 3,+ max: 900,+ softMin: 12,+ softMax: 85,+ },+ });++ expect(result.min).toBe(1); // 10^0+ expect(result.max).toBe(1000); // 10^3+ expect(result.softMin).toBe(10); // 10^1+ expect(result.softMax).toBe(100); // 10^2+ });+ });++ describe('getDistributionConfig', () => {+ it('returns empty config for time scales', () => {+ const config = scaleUtils.getDistributionConfig({+ time: true,+ distr: DistributionType.Linear,+ logBase: 2,+ });++ expect(config).toEqual({});+ });++ it('returns linear distribution settings for non-time scales', () => {+ const config = scaleUtils.getDistributionConfig({+ time: false,+ distr: DistributionType.Linear,+ logBase: 2,+ });++ expect(config.distr).toBe(1);+ expect(config.log).toBe(2);+ });++ it('returns log distribution settings for non-time scales', () => {+ const config = scaleUtils.getDistributionConfig({+ time: false,+ distr: DistributionType.Logarithmic,+ logBase: 10,+ });++ expect(config.distr).toBe(3);+ expect(config.log).toBe(10);+ });+ });++ describe('getRangeConfig', () => {+ it('computes range config and fixed range flags correctly', () => {+ const {+ rangeConfig,+ hardMinOnly,+ hardMaxOnly,+ hasFixedRange,+ } = scaleUtils.getRangeConfig(0, 100, null, null, 0.1, 0.2);++ expect(rangeConfig.min).toEqual({+ pad: 0.1,+ hard: 0,+ soft: undefined,+ mode: 3,+ });+ expect(rangeConfig.max).toEqual({+ pad: 0.2,+ hard: 100,+ soft: undefined,+ mode: 3,+ });+ expect(hardMinOnly).toBe(true);+ expect(hardMaxOnly).toBe(true);+ expect(hasFixedRange).toBe(true);+ });+ });++ describe('createRangeFunction', () => {+ it('returns [dataMin, dataMax] when no fixed range and no data', () => {+ const params = {+ rangeConfig: {} as uPlot.Range.Config,+ hardMinOnly: false,+ hardMaxOnly: false,+ hasFixedRange: false,+ min: null,+ max: null,+ };++ const rangeFn = scaleUtils.createRangeFunction(params);++ const u = ({+ scales: {+ y: {+ distr: 1,+ log: 10,+ },+ },+ } as unknown) as uPlot;++ const result = rangeFn(+ u,+ (null as unknown) as number,+ (null as unknown) as number,+ 'y',+ );++ expect(result).toEqual([null, null]);+ });++ it('applies hard min/max for linear scale when only hard limits are set', () => {+ const params = {+ rangeConfig: {} as uPlot.Range.Config,+ hardMinOnly: true,+ hardMaxOnly: true,+ hasFixedRange: true,+ min: 0,+ max: 100,+ };++ const rangeFn = scaleUtils.createRangeFunction(params);++ // Use an undefined distr so the range function skips calling uPlot.rangeNum+ // and we can focus on the behavior of applyHardLimits.+ const u = ({+ scales: {+ y: {+ distr: undefined,+ log: 10,+ },+ },+ } as unknown) as uPlot;++ const result = rangeFn(u, 10, 20, 'y');++ // After applyHardLimits, the returned range should respect configured min/max+ expect(result).toEqual([0, 100]);+ });+ });++ describe('adjustSoftLimitsWithThresholds', () => {+ it('returns original soft limits when there are no thresholds', () => {+ const result = scaleUtils.adjustSoftLimitsWithThresholds(1, 5, [], 'ms');++ expect(result).toEqual({ softMin: 1, softMax: 5 });+ });++ it('expands soft limits to include threshold min/max values', () => {+ const result = scaleUtils.adjustSoftLimitsWithThresholds(+ 3,+ 6,+ [{ thresholdValue: 2 }, { thresholdValue: 8 }],+ 'ms',+ );++ // min should be pulled down to the smallest threshold value+ expect(result.softMin).toBe(2);+ // max should be pushed up to the largest threshold value+ expect(result.softMax).toBe(8);+ });+ });++ describe('getFallbackMinMaxTimeStamp', () => {+ it('returns a 24-hour window ending at approximately now', () => {+ const { fallbackMin, fallbackMax } = scaleUtils.getFallbackMinMaxTimeStamp();++ // Difference should be exactly one day in seconds+ expect(fallbackMax - fallbackMin).toBe(86400);++ // Both should be reasonable timestamps (not NaN or negative)+ expect(fallbackMin).toBeGreaterThan(0);+ expect(fallbackMax).toBeGreaterThan(fallbackMin);+ });+ });+});diff --git a/frontend/src/lib/uPlotV2/utils/__tests__/threshold.test.ts b/frontend/src/lib/uPlotV2/utils/__tests__/threshold.test.tsnew file mode 100644index 0000000000..6f1f7427a3--- /dev/null+++ b/frontend/src/lib/uPlotV2/utils/__tests__/threshold.test.ts@@ -0,0 +1,34 @@+import { findMinMaxThresholdValues } from '../threshold';++describe('findMinMaxThresholdValues', () => {+ it('returns [null, null] when thresholds array is empty or missing', () => {+ expect(findMinMaxThresholdValues([], 'ms')).toEqual([null, null]);+ });++ it('returns min and max from thresholdValue when units are not provided', () => {+ const thresholds = [+ { thresholdValue: 5 },+ { thresholdValue: 1 },+ { thresholdValue: 10 },+ ];++ const [min, max] = findMinMaxThresholdValues(thresholds);++ expect(min).toBe(1);+ expect(max).toBe(10);+ });++ it('ignores thresholds without a value or with unconvertible units', () => {+ const thresholds = [+ // Should be ignored: convertValue returns null for unknown unit+ { thresholdValue: 100, thresholdUnit: 'unknown-unit' },+ // Should be used+ { thresholdValue: 4 },+ ];++ const [min, max] = findMinMaxThresholdValues(thresholds, 'ms');++ expect(min).toBe(4);+ expect(max).toBe(4);+ });+});