// SPDX-License-Identifier: MIT // Copyright contributors to the kepler.gl project import test from 'tape'; import sinon from 'sinon'; import * as arrow from 'apache-arrow'; import {TIME_INTERVALS} from '@kepler.gl/constants'; import {KeplerTable} from '@kepler.gl/table'; import { getBinThresholds, getTimeBins, histogramFromThreshold, histogramFromValues, histogramFromTimeIntervals, getNumericBinThresholds, getNumericPlotInterval, getNumericRangeMinimum, intervalBinFromMarks, nextDown, nextUp, mergePolygonLayerIndexes, runGpuFilterForPlot, getLineChart, getCategoricalHistogram, mergePlotGroupBy, lineChartSeriesLegend, PLOT_GROUP_OTHERS_NAME, PLOT_NUM_GROUPS_ALL, MAX_PLOT_NUM_GROUPS } from '@kepler.gl/utils'; const values1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; test('Utils -> time thresholds bound allocation before expanding a millisecond domain', t => { const every = TIME_INTERVALS.millisecond.every; const stub = sinon.stub(TIME_INTERVALS.millisecond, 'every').callsFake(step => { const interval = every(step); return { ...interval, range(start, stop) { const requested = Math.ceil((stop - start) / step); if (requested > 2049) { throw new Error(`Intercepted unsafe allocation of ${requested} Dates`); } return interval.range(start, stop); } }; }); try { const start = Date.UTC(2020, 0, 1); const end = start + 86400000; const thresholds = getBinThresholds('1-millisecond', [start, end]); t.ok(thresholds.length <= 2049, 'should bound the number of bins including endpoint padding'); t.equal(thresholds[0], start, 'should keep the full domain start'); t.ok(thresholds[thresholds.length - 1] > end, 'should include the maximum in a complete bin'); t.equal( histogramFromThreshold(thresholds, [start, end]).reduce((sum, bin) => sum + bin.count, 0), 2, 'should retain both endpoint rows instead of truncating the domain' ); } catch (error) { t.fail(error.message); } finally { stub.restore(); } t.end(); }); test('Utils -> time thresholds preserve safe calendar intervals and reject invalid domains', t => { t.equal( getBinThresholds('1-millisecond', [0, 2047]).length, 2049, 'should keep exactly 2048 bins' ); for (const end of [2048, 3000]) { const thresholds = getBinThresholds('1-millisecond', [0, end]); t.ok(thresholds.length > 1 && thresholds.length <= 2049, 'should coarsen beyond the cap'); t.equal(thresholds[0], 0, 'should retain the domain start'); t.ok(thresholds[thresholds.length - 1] > end, 'should include the domain maximum'); t.equal( histogramFromThreshold(thresholds, [0, end]).reduce((sum, bin) => sum + bin.count, 0), 2, 'should retain both endpoint rows after fallback' ); } const dates = [Date.UTC(2020, 0, 1), Date.UTC(2020, 1, 1), Date.UTC(2020, 2, 1)]; t.deepEqual( getBinThresholds('1-month', [dates[0], dates[1] + 1]), dates, 'should preserve leap-year calendar boundaries' ); t.deepEqual( getBinThresholds('1-month', [dates[0], dates[1]]), dates, 'should retain inclusive maximum padding' ); for (const interval of [ '5-second', '15-minute', '3-hour', '2-day', '2-week', '3-month', '5-year' ]) { const [step, unitName] = interval.split('-'); const unit = TIME_INTERVALS[unitName].every(Number(step)); const start = Date.UTC(2020, 0, 2, 1, 2, 3); const end = Number(unit.offset(start, 3)); const expected = unit.range(unit.floor(start), Number(unit.ceil(end)) + 1).map(Number); if (expected[expected.length - 1] === end) expected.push(Number(unit.offset(end))); t.deepEqual( getBinThresholds(interval, [start, end]), expected, `${interval} should keep calendar alignment` ); } t.ok( getBinThresholds('1000000-week', [0, 86400000]).length <= 2049, 'should bound large calendar steps' ); for (const domain of [[], [NaN, 1], [0, Infinity], [2, 1], [0, 9e15]]) { t.deepEqual( getBinThresholds('1-millisecond', domain), [], 'should reject an invalid time domain' ); } for (const interval of [undefined, '', '0-day', '-1-day', 'no-such-unit']) { const thresholds = getBinThresholds(interval, [0, 86400000]); t.ok( thresholds.length > 1 && thresholds.length <= 2049, 'should safely replace an invalid interval' ); t.ok(thresholds[thresholds.length - 1] >= 86400000, 'should cover the full domain'); } t.end(); }); test('Utils -> time thresholds stop before calendar padding overflows Date', t => { const setMonth = Date.prototype.setUTCMonth; let operations = 0; const stub = sinon.stub(Date.prototype, 'setUTCMonth').callsFake(function (...args) { if (++operations > 100) throw new Error('Intercepted unbounded calendar stepping'); return setMonth.apply(this, args); }); try { const july = Date.UTC(275760, 6, 1); const september = Date.UTC(275760, 8, 1); t.deepEqual( getBinThresholds('2-month', [july, july + 86400000]), [july, september], 'should keep finite calendar boundaries near the Date limit' ); t.deepEqual( getBinThresholds('2-month', [8640000000000000 - 86400000, 8640000000000000]), [], 'should reject unrepresentable upper padding' ); const minimum = -8640000000000000; const thresholds = getBinThresholds('2-month', [minimum, minimum + 86400000]); t.equal( thresholds[0], minimum, 'should replace unrepresentable lower padding with a finite interval' ); t.ok( thresholds.length <= 2049 && thresholds[thresholds.length - 1] > minimum + 86400000, 'should bound the fallback and cover the entire domain' ); } catch (error) { t.fail(error.message); } finally { stub.restore(); } t.end(); }); test('Utils -> time bins reuse validated boundaries and invalidate changed domains', t => { const mappedValue = [0, 30000, 60000]; const dataset = { ...mockGpuPlotDataset( mappedValue.map(value => [value]), [] ), getColumnField: () => ({filterProps: {mappedValue}}) }; const filter = { type: 'timeRange', dataId: ['dsA'], name: ['time'], domain: [0, 30000 * 2047] }; const datasets = {dsA: dataset}; filter.timeBins = getTimeBins(filter, datasets, '30-second'); const cachedBins = filter.timeBins.dsA['30-second']; const marks = getBinThresholds('30-second', filter.domain); const every = sinon.spy(TIME_INTERVALS.second, 'every'); try { t.equal( getTimeBins(filter, datasets, '30-second'), filter.timeBins, 'should reuse cached bins' ); t.deepEqual(getBinThresholds('30-second', filter.domain), marks, 'should reuse matching marks'); t.notOk(every.called, 'a validated cache hit should not traverse calendar intervals again'); marks.length = 0; t.equal( getBinThresholds('30-second', filter.domain).length, 2049, 'caller changes to returned marks must not corrupt cached boundaries' ); const minuteBins = getTimeBins(filter, datasets, '1-minute').dsA['1-minute']; t.notEqual(minuteBins, cachedBins, 'a changed interval should create different bins'); t.deepEqual( minuteBins.map(bin => bin.count), [2, 1], 'should aggregate with the new interval' ); filter.domain[0] = 30000; const changed = getTimeBins(filter, datasets, '30-second'); t.notEqual(changed.dsA['30-second'], cachedBins, 'should invalidate an in-place domain change'); t.deepEqual( changed.dsA['30-second'].flatMap(bin => bin.indexes), [1, 2], 'should exclude rows outside the new domain' ); const replaced = getTimeBins({...filter, domain: [60000, 90000]}, datasets, '30-second'); t.deepEqual( replaced.dsA['30-second'].flatMap(bin => bin.indexes), [2], 'should rebuild bins when the domain array is replaced' ); const growing = {...filter, domain: [0, 1]}; growing.timeBins = getTimeBins(growing, datasets, '1-millisecond'); growing.domain[1] = 86400000; const coarsened = getTimeBins(growing, datasets, '1-millisecond'); t.notOk(coarsened.dsA['1-millisecond'], 'must not reuse unsafe bins after domain growth'); t.ok(coarsened.dsA['15-minute'], 'should cache under the effective safe interval'); t.equal( coarsened.dsA['15-minute'].reduce((sum, bin) => sum + bin.count, 0), 3, 'should retain every row after domain growth' ); t.deepEqual( getTimeBins({...filter, domain: {}}, datasets, '30-second').dsA['30-second'], [], 'should reject a non-array domain without throwing while caching metadata' ); } finally { every.restore(); } t.end(); }); test('Utils -> getNumericBinThresholds uses numeric units', t => { t.deepEqual( getNumericBinThresholds([-1.5, 1], 0.5), [-1.5, -1, -0.5, 0, 0.5, 1], 'should preserve negative and fractional numeric boundaries' ); t.deepEqual( getNumericBinThresholds([7, 8], 0.25), [7, 7.25, 7.5, 7.75, 8], 'should bin the padded domain of a constant numeric field' ); t.deepEqual( getNumericBinThresholds([-0.2, -0.19], 0.01), [-0.2, -0.19], 'should not duplicate a decimal domain end because of floating-point division' ); const maximumBins = getNumericBinThresholds([-4.42, 16.060000000000002], 0.01); t.equal(maximumBins.length, 2049, 'should keep exactly 2048 bins at the safety cap'); t.equal(maximumBins[0], -4.42, 'should preserve the capped domain start'); t.equal( maximumBins[maximumBins.length - 1], 16.060000000000002, 'should preserve the capped domain end' ); t.ok( maximumBins.every((boundary, index) => index === 0 || boundary > maximumBins[index - 1]), 'should generate only strictly increasing numeric boundaries' ); const fallback = getNumericBinThresholds([0, 1], -1); t.ok(fallback.length > 2 && fallback.length < 100, 'should safely replace an invalid bin size'); t.equal(fallback[0], 0, 'should keep the numeric domain start'); t.equal(fallback[fallback.length - 1], 1, 'should keep the numeric domain end'); t.end(); }); test('Utils -> getNumericPlotInterval ignores padded edge bins', t => { const filter = { domain: [-0.20000000000000004, -0.18999], step: 0.00001, plotType: {}, bins: { dataset: [ {x0: -0.20000000000000004, x1: -0.2}, {x0: -0.2, x1: -0.1995}, {x0: -0.1995, x1: -0.199} ] } }; t.equal( getNumericPlotInterval(filter), 0.0005, 'should use the median positive width instead of a floating-point edge sliver' ); t.equal( getNumericPlotInterval({...filter, plotType: {numericInterval: 0.0025}}), 0.0025, 'should preserve an explicit numeric interval' ); t.end(); }); test('Utils -> numeric range precision follows representable domain spacing', t => { const domains = [ [0, 1e-20], [1e12, 1e12 + 0.001], [0, 0.1] ]; for (const domain of domains) { const [start, end] = domain; const span = end - start; const minimum = getNumericRangeMinimum(domain); t.ok(minimum > 0, `${domain} should have a positive representable minimum`); t.ok(minimum <= span, `${domain} minimum should not exceed its span`); t.ok(start + minimum > start, `${domain} minimum should advance the lower endpoint`); t.ok(end - minimum < end, `${domain} minimum should retreat the upper endpoint`); } t.equal(nextDown(nextUp(1e12)), 1e12, 'adjacent numeric values should round-trip'); t.equal(nextDown(0), -Number.MIN_VALUE, 'zero should have a negative predecessor'); t.end(); }); test('Utils -> mergePolygonLayerIndexes', t => { const baseIndex = [0, 1, 2, 3]; t.deepEqual( mergePolygonLayerIndexes(baseIndex, {}), baseIndex, 'should return base index when no layers are polygon-filtered' ); t.deepEqual( mergePolygonLayerIndexes(baseIndex, {layerA: [0, 2]}), [0, 2], 'should keep rows visible on a single targeted layer' ); t.deepEqual( mergePolygonLayerIndexes(baseIndex, {layerA: [0, 2], layerB: [1]}), [0, 1, 2], 'should keep the union of rows visible on any targeted layer' ); t.deepEqual( mergePolygonLayerIndexes(baseIndex, {layerA: [], layerB: []}), [], 'should export no rows when all targeted layers are empty' ); t.end(); }); function mockGpuPlotDataset(rows, fieldNames = ['colA', 'colB']) { return { id: 'dsA', filteredIndex: rows.map((_, i) => i), filteredIndexByLayer: {}, dataContainer: {}, gpuFilter: { filterRange: fieldNames.map(() => [0, 10]), filterValueUpdateTriggers: fieldNames.reduce( (triggers, name, index) => ({...triggers, [`gpu${index}`]: {name}}), {} ), filterValueAccessor: () => () => ({index}) => rows[index] } }; } test('Utils -> runGpuFilterForPlot applies polygon layer indexes', t => { const dataset = { id: 'puppy', filteredIndex: [0, 1, 2, 3], filteredIndexByLayer: {layerA: [0, 2]}, dataContainer: {}, gpuFilter: { filterRange: [], filterValueUpdateTriggers: {}, filterValueAccessor: () => () => () => [] } }; t.deepEqual( runGpuFilterForPlot(dataset), [0, 2], 'should start plots from polygon-visible rows when filteredIndexByLayer is set' ); t.deepEqual( runGpuFilterForPlot({...dataset, filteredIndexByLayer: {}}), [0, 1, 2, 3], 'should fall back to filteredIndex when no polygon layer indexes exist' ); t.deepEqual( runGpuFilterForPlot(dataset, undefined, undefined, [1, 3]), [1, 3], 'should plot from an explicit polygon-filtered base instead of stale polygon indexes' ); t.end(); }); test('Utils -> runGpuFilterForPlot skips the plotted filter GPU channels', t => { // Channels 0 and 1 belong to the plotted interval filter. Channel 2 belongs // to a sibling filter that deliberately uses the same start field name. const dataset = mockGpuPlotDataset( [ [5, 5, 5], [5, 100, 5], [100, 5, 100], [100, 100, 5] ], ['start', 'end', 'start'] ); t.deepEqual( runGpuFilterForPlot(dataset, { gpu: true, dataId: ['dsA'], name: ['start'], endName: ['end'], gpuChannel: [0], gpuEndChannel: [1] }), [0, 1, 3], 'should skip both own interval channels and still apply a same-field sibling channel' ); t.deepEqual( runGpuFilterForPlot(mockGpuPlotDataset([[5], [100]], ['start']), { gpu: false, dataId: ['dsA'], name: ['start'] }), [0], 'should apply a same-field GPU sibling when the plotted filter runs on the CPU' ); t.end(); }); test('Utils -> runGpuFilterForPlot skips only this dataset column on multi-dataset filters', t => { // GPU channel 0 = colA, channel 1 = colB. Range [0, 10] on both. // Row 0: both in range. Row 1: colB out. Row 2: colA out. Row 3: both out. const dataset = mockGpuPlotDataset([ [5, 5], [5, 100], [100, 5], [100, 100] ]); t.deepEqual( runGpuFilterForPlot(dataset, { gpu: true, dataId: ['dsB', 'dsA'], name: ['colB', 'colA'], gpuChannel: [3, 0] }), [0, 2], 'should use the channel paired with the current dataset in a multi-dataset filter' ); t.deepEqual( runGpuFilterForPlot(dataset, { dataId: ['dsA', 'dsB'], name: ['colA', 'colB'] }), [0, 2], 'should skip only this dataset column and still apply sibling GPU channels' ); t.deepEqual( runGpuFilterForPlot(dataset, { dataId: ['dsA'], name: ['colA'] }), [0, 2], 'should skip the plotted field on a single-dataset filter and still apply other GPU channels' ); t.deepEqual( runGpuFilterForPlot(dataset, undefined, ['colA', 'colB']), [0, 1, 2, 3], 'should skip every extra field name passed by charts without changing filter pairing' ); t.end(); }); test('Utils -> histogramFromThreshold', t => { const thresholds1 = [1, 3, 6, 13]; const bins1 = histogramFromThreshold(thresholds1, values1); const expectedHistogram1 = [ { count: 2, indexes: [1, 2], x0: 1, x1: 3 }, { count: 3, indexes: [3, 4, 5], x0: 3, x1: 6 }, { count: 7, indexes: [6, 7, 8, 9, 10, 11, 12], x0: 6, x1: 13 }, { count: 1, indexes: [13], x0: 13, x1: 13 } ]; expectedHistogram1.forEach(bin => { bin.indexes.x0 = bin.x0; bin.indexes.x1 = bin.x1; }); t.deepEqual(bins1.length, 4, 'should create histogram with 4 bins.'); t.deepEqual(expectedHistogram1, bins1, 'should create histogram as expectedHistogram1.'); const bins2 = histogramFromThreshold([], values1); t.deepEqual(bins2.length, 0, 'should create no histogram no threshold.'); const bins3 = histogramFromThreshold(thresholds1, []); t.deepEqual(bins3.length, 0, 'should create no histogram no values.'); const valueAccessor = idx => values1[idx]; const filteredIndex = [0, 1, 3, 5, 7, 9, 11]; const bins4 = histogramFromThreshold(thresholds1, filteredIndex, valueAccessor); const expectedHistogram4 = [ { count: 2, indexes: [0, 1], x0: 1, x1: 3 }, { count: 1, indexes: [3], x0: 3, x1: 6 }, { count: 4, indexes: [5, 7, 9, 11], x0: 6, x1: 13 } ]; expectedHistogram4.forEach(bin => { bin.indexes.x0 = bin.x0; bin.indexes.x1 = bin.x1; }); t.deepEqual(bins4, expectedHistogram4, 'should create histogram with valueAccessor.'); t.end(); }); test('Utils -> histogramFromValues', t => { const numBins = 4; const bins5 = histogramFromValues(values1, numBins); const expectedHistogram5 = [ { count: 4, indexes: [1, 2, 3, 4], x0: 0, x1: 5 }, { count: 5, indexes: [5, 6, 7, 8, 9], x0: 5, x1: 10 }, { count: 4, indexes: [10, 11, 12, 13], x0: 10, x1: 15 } ]; expectedHistogram5.forEach(bin => { bin.indexes.x0 = bin.x0; bin.indexes.x1 = bin.x1; }); // d3.histogram uses ticks() to find nice number of breaks (bins), so the // number of returned bins may be different than the input number of bins t.deepEqual(bins5, expectedHistogram5, 'should create histogram with 3 bins from values.'); t.end(); }); test('Utils -> histogramFromTimeIntervals', t => { const thresholds = [0, 10, 20, 30]; const starts = [5, 15, 0, 25]; const ends = [15, 15, 30, null]; const bins = histogramFromTimeIntervals( thresholds, [0, 1, 2, 3], idx => starts[idx], idx => ends[idx] ); t.deepEqual( bins.map(b => ({count: b.count, indexes: b.indexes, x0: b.x0, x1: b.x1})), [ {count: 2, indexes: [0, 2], x0: 0, x1: 10}, {count: 3, indexes: [0, 1, 2], x0: 10, x1: 20}, {count: 2, indexes: [2, 3], x0: 20, x1: 30} ], 'should count a feature in every bin that overlaps [start, end]' ); t.deepEqual( histogramFromTimeIntervals( thresholds, [0], idx => 10, idx => 10 ), [{count: 1, indexes: [0], x0: 10, x1: 20}], 'an instant at a bin boundary should land in the following bin' ); t.deepEqual( histogramFromTimeIntervals( thresholds, [0], () => null, () => 20 ), [], 'should skip rows with no start time' ); t.deepEqual( histogramFromTimeIntervals( [], [0], () => 5, () => 15 ), [], 'should return no bins without thresholds' ); t.deepEqual( histogramFromTimeIntervals( thresholds, [0], () => 15, () => 5 ), [], 'should skip inverted intervals (end < start)' ); t.end(); }); const groupedRows = [ {v: 10, g: 'a'}, {v: 30, g: 'b'}, {v: 20, g: 'a'}, {v: 5, g: 'c'}, {v: 7, g: null} ]; function groupedDataset() { return { fields: [ {name: 'v', type: 'integer', valueAccessor: ({index}) => groupedRows[index].v}, {name: 'g', type: 'string', valueAccessor: ({index}) => groupedRows[index].g} ] }; } const groupedBins = [ {count: 3, indexes: [0, 1, 4], x0: 0, x1: 1}, {count: 1, indexes: [2], x0: 1, x1: 2}, {count: 1, indexes: [3], x0: 2, x1: 3} ]; function groupedFilter(groupBy) { return { dataId: ['ds'], yAxis: {name: 'v', type: 'integer'}, plotType: { aggregation: 'sum', interval: '1-day', type: 'lineChart', groupBy }, timeBins: {ds: {'1-day': groupedBins}} }; } function seriesSummary(lineChart) { return { names: lineChart.series.names, colors: lineChart.series.colors, ys: lineChart.series.lines.map(line => line.map(point => point.y)) }; } test('Utils -> getCategoricalHistogram splits bin counts without changing totals or input', t => { const bins = {ds: groupedBins}; const original = JSON.parse(JSON.stringify(bins)); const plotType = { groupBy: { fieldName: 'g', numGroups: 3, groupOthers: false, colorRange: {colors: ['#111111', '#222222', '#333333']} } }; const result = getCategoricalHistogram(bins, {ds: groupedDataset()}, plotType); t.deepEqual( result.bins.ds.map(bin => bin.segments), [ [ {name: 'a', color: '#111111', count: 1}, {name: 'b', color: '#222222', count: 1} ], [{name: 'a', color: '#111111', count: 1}], [{name: 'c', color: '#333333', count: 1}] ], 'should count each category within its bin and use the same color across bins' ); t.deepEqual( result.bins.ds.map(({segments, ...bin}) => bin), groupedBins, 'should preserve total counts, membership, and boundaries including missing categories' ); t.equal( result.bins.ds[0].segments[0].count / result.bins.ds[0].count, 1 / 3, 'should express category proportions relative to the full bin count' ); t.deepEqual(bins, original, 'should leave source bins unchanged'); t.end(); }); test('Utils -> getCategoricalHistogram shares category colors across linked datasets', t => { const rows = ['b', 'b', 'd']; const linkedBins = [ {count: 2, indexes: [0, 1], x0: 0, x1: 1}, {count: 1, indexes: [2], x0: 1, x1: 2} ]; const result = getCategoricalHistogram( {ds: groupedBins, linked: linkedBins, missing: linkedBins}, { ds: groupedDataset(), linked: {fields: [{name: 'g', valueAccessor: ({index}) => rows[index]}]}, missing: {fields: []} }, { groupBy: { fieldName: 'g', numGroups: 4, colorRange: {colors: ['#111111', '#222222', '#333333', '#444444']} } } ); t.deepEqual( result.legend, [ {name: 'a', color: '#111111'}, {name: 'b', color: '#222222'}, {name: 'c', color: '#333333'}, {name: 'd', color: '#444444'} ], 'should use one first-seen category order for all datasets' ); t.deepEqual( result.bins.linked.map(bin => bin.segments), [[{name: 'b', color: '#222222', count: 2}], [{name: 'd', color: '#444444', count: 1}]], 'should count linked categories with the shared color mapping' ); t.deepEqual( result.bins.missing, linkedBins, 'should leave a dataset without the grouping column as a plain histogram' ); t.end(); }); test('Utils -> getCategoricalHistogram caps categories and optionally folds Others', t => { const bins = {ds: groupedBins}; const datasets = {ds: groupedDataset()}; const groupBy = { fieldName: 'g', numGroups: 1, groupOthers: true, colorRange: {colors: ['#111111'], colorMap: [[PLOT_GROUP_OTHERS_NAME, '#abcdef']]} }; const grouped = getCategoricalHistogram(bins, datasets, {groupBy}); t.deepEqual( grouped.legend, [ {name: 'a', color: '#111111'}, {name: PLOT_GROUP_OTHERS_NAME, color: '#abcdef'} ], 'should honor the category cap and mapped Others color' ); t.deepEqual( grouped.bins.ds.map(bin => bin.segments.map(segment => [segment.name, segment.count])), [ [ ['a', 1], [PLOT_GROUP_OTHERS_NAME, 1] ], [['a', 1]], [[PLOT_GROUP_OTHERS_NAME, 1]] ], 'should fold remaining non-missing categories into Others in every bin' ); const capped = getCategoricalHistogram(bins, datasets, { groupBy: {...groupBy, groupOthers: false} }); t.deepEqual( capped.bins.ds.map(bin => bin.segments), [[{name: 'a', color: '#111111', count: 1}], [{name: 'a', color: '#111111', count: 1}], []], 'should leave capped categories unrepresented when Others is disabled' ); t.deepEqual( capped.bins.ds.map(bin => bin.count), [3, 1, 1], 'should preserve full bar heights when categories are missing or capped' ); t.end(); }); test('Utils -> getCategoricalHistogram keeps colors stable when categories are filtered out', t => { const linkedRows = ['b', 'd']; const datasets = { ds: {...groupedDataset(), allIndexes: [0, 1, 2, 3, 4]}, linked: { allIndexes: [0, 1], fields: [{name: 'g', valueAccessor: ({index}) => linkedRows[index]}] } }; const bins = { ds: groupedBins, linked: [{count: 2, indexes: [0, 1], x0: 0, x1: 1}] }; const filteredBins = { ...bins, ds: [{count: 2, indexes: [1, 3], x0: 0, x1: 1}] }; const groupBy = { fieldName: 'g', numGroups: 4, colorRange: {colors: ['#111111', '#222222', '#333333', '#444444']} }; const initial = getCategoricalHistogram(bins, datasets, {groupBy}); const filtered = getCategoricalHistogram(filteredBins, datasets, {groupBy}); t.deepEqual( filtered.legend, initial.legend.filter(({name}) => name !== 'a'), 'should hide an absent category without shifting remaining colors across datasets' ); t.deepEqual( filtered.bins.ds[0].segments, [ {name: 'b', color: '#222222', count: 1}, {name: 'c', color: '#333333', count: 1} ], 'should preserve stable category colors within the filtered bin' ); t.deepEqual( getCategoricalHistogram(bins, datasets, {groupBy}), initial, 'should restore the original categorical histogram when the filter is cleared' ); const cappedPlot = {groupBy: {...groupBy, numGroups: 1, groupOthers: true}}; const initialCapped = getCategoricalHistogram(bins, datasets, cappedPlot); const filteredCapped = getCategoricalHistogram(filteredBins, datasets, cappedPlot); t.deepEqual( filteredCapped.legend.find(({name}) => name === PLOT_GROUP_OTHERS_NAME), initialCapped.legend.find(({name}) => name === PLOT_GROUP_OTHERS_NAME), 'should keep the Others color stable when a preceding category disappears' ); t.equal( filteredCapped.legend[0].color, '#222222', 'should use the stable color when a formerly folded category enters the visible group limit' ); t.end(); }); test('Utils -> getCategoricalHistogram handles missing grouping and empty categories', t => { const datasets = {ds: groupedDataset()}; const bins = {ds: groupedBins}; const plotType = {groupBy: {fieldName: 'g'}}; for (const groupBy of [null, {}, {fieldName: 'unknown'}]) { t.equal( getCategoricalHistogram(bins, datasets, {groupBy}), null, 'should retain plain bins when no valid grouping field is selected' ); } t.equal(getCategoricalHistogram({}, datasets, plotType), null, 'should accept no datasets'); t.equal(getCategoricalHistogram({ds: []}, datasets, plotType), null, 'should accept empty bins'); const missingRows = [null, undefined, '']; t.equal( getCategoricalHistogram( {ds: [{count: 3, indexes: [0, 1, 2], x0: 0, x1: 1}]}, {ds: {fields: [{name: 'g', valueAccessor: ({index}) => missingRows[index]}]}}, plotType ), null, 'should keep a plain histogram when every category is missing' ); t.end(); }); test('Utils -> getCategoricalHistogram distinguishes literal Others from folded categories', t => { const rows = [PLOT_GROUP_OTHERS_NAME, 'Others (grouped)', 'a', 'b']; const result = getCategoricalHistogram( {ds: [{count: rows.length, indexes: [0, 1, 2, 3], x0: 0, x1: 1}]}, {ds: {fields: [{name: 'g', valueAccessor: ({index}) => rows[index]}]}}, {groupBy: {fieldName: 'g', numGroups: 2, groupOthers: true}} ); t.deepEqual( result.bins.ds[0].segments.map(({name, count}) => ({name, count})), [ {name: PLOT_GROUP_OTHERS_NAME, count: 1}, {name: 'Others (grouped)', count: 1}, {name: 'Others (grouped 2)', count: 2} ], 'should keep real category names and give folded counts an unambiguous name' ); t.equal( new Set(result.legend.map(({color}) => color)).size, 3, 'should distinguish the folded category with a separate color' ); t.end(); }); function numericGroupDataset(values, type = 'real') { return { allIndexes: values.map((_, index) => index), fields: [{name: 'g', type, valueAccessor: ({index}) => values[index]}] }; } test('Utils -> categorical histogram reuses domains across filters and palettes', t => { const rows = Array.from({length: 1000}, (_, index) => `category ${index}`); const valueAccessor = sinon.spy(({index}) => rows[index]); const dataset = { dataContainer: {}, dataRevision: 0, allIndexes: rows.map((_, index) => index), fields: [{name: 'g', type: 'string', valueAccessor}] }; const bins = {ds: [{x0: 0, x1: 1, count: 2, indexes: [1, 999]}]}; const groupBy = { fieldName: 'g', numGroups: 1, groupOthers: true, colorRange: {colors: ['#111111', '#222222', '#333333']} }; const initial = getCategoricalHistogram(bins, {ds: dataset}, {groupBy}); valueAccessor.reset(); t.deepEqual( getCategoricalHistogram(bins, {ds: {...dataset}}, {groupBy}), initial, 'playback wrappers should keep category and Others colors' ); t.ok(valueAccessor.callCount < 10, 'playback should read only binned rows, not the full domain'); const recoloredGroupBy = {...groupBy, colorRange: {colors: ['#aaaaaa', '#bbbbbb', '#cccccc']}}; valueAccessor.reset(); const recolored = getCategoricalHistogram(bins, {ds: dataset}, {groupBy: recoloredGroupBy}); t.equal( recolored.legend[0].color, '#bbbbbb', 'palette changes should use the stable category index' ); t.deepEqual( recolored.bins.ds[0].segments.map(({name, count}) => [name, count]), initial.bins.ds[0].segments.map(({name, count}) => [name, count]), 'recoloring should keep counts' ); t.ok(valueAccessor.callCount < 10, 'palette changes should not rescan the full domain'); valueAccessor.reset(); const filtered = getCategoricalHistogram( {ds: [{...bins.ds[0], count: 1, indexes: [999]}]}, {ds: {...dataset}}, {groupBy: recoloredGroupBy} ); t.deepEqual( filtered.legend, [{name: 'category 999', color: '#aaaaaa'}], 'cross-filters should promote the remaining category with its original color index' ); t.ok(valueAccessor.callCount < 10, 'cross-filters should reuse the complete category domain'); valueAccessor.reset(); t.equal( getCategoricalHistogram(bins, {ds: dataset}, {}), null, 'ungrouped bins should stay plain' ); t.equal(valueAccessor.callCount, 0, 'ungrouped bins should not read the grouping column'); t.end(); }); test('Utils -> histogram domain cache invalidates row edits, appends, replacements and fields', t => { let values = [0, 2, 4, 6, 8]; const dataset = {...numericGroupDataset(values), dataContainer: {}, dataRevision: 0}; dataset.fields[0].valueAccessor = ({index}) => values[index]; const bins = {ds: [{x0: 0, x1: 1, count: 1, indexes: [0]}]}; const plotType = {groupBy: {fieldName: 'g', numGroups: 2}}; const label = () => getCategoricalHistogram(bins, {ds: dataset}, plotType).legend[0].name; t.equal(label(), '0 ≤ x < 4', 'should start with the complete numeric domain'); values[4] = 12; dataset.dataRevision++; t.equal(label(), '0 ≤ x < 6', 'a revision change should invalidate in-place row edits'); values.push(16); dataset.allIndexes = [...dataset.allIndexes, 5]; t.equal(label(), '0 ≤ x < 8', 'new row indexes should invalidate the domain'); values = [0, 2, 4, 6, 8, 20]; dataset.dataContainer = {}; t.equal(label(), '0 ≤ x < 10', 'a replacement container should invalidate the domain'); dataset.fields[0] = {...dataset.fields[0], valueAccessor: ({index}) => values[index] * 2}; t.equal(label(), '0 ≤ x < 20', 'a replacement field accessor should invalidate the domain'); dataset.fields[0] = {...dataset.fields[0], type: 'string'}; t.equal(label(), '0', 'field type changes should switch back to exact categories'); delete dataset.dataRevision; dataset.fields[0] = {...dataset.fields[0], type: 'real'}; t.equal(label(), '0 ≤ x < 20', 'unversioned datasets should still support numeric ranges'); values[5] = 30; t.equal(label(), '0 ≤ x < 30', 'unversioned datasets should not retain stale domains'); dataset.fields = [{name: 'other', type: 'real', valueAccessor: ({index}) => values[index]}]; t.equal( getCategoricalHistogram(bins, {ds: dataset}, plotType), null, 'removing the grouping field should not reuse stale categories' ); t.end(); }); test('Utils -> histogram Others color preserves overrides and palette wrapping', t => { const rows = ['a', 'b', 'c', 'd', 'e']; const dataset = {...numericGroupDataset(rows, 'string'), dataContainer: {}, dataRevision: 0}; const bins = {ds: [{x0: 0, x1: 1, count: rows.length, indexes: dataset.allIndexes}]}; const groupBy = { fieldName: 'g', numGroups: 1, groupOthers: true, colorRange: { colors: ['#111111', '#222222', '#333333'], colorMap: [ [['a', 'd'], '#abcdef'], ['a', '#111111'] ] } }; const result = getCategoricalHistogram(bins, {ds: dataset}, {groupBy}); t.deepEqual( result.legend, [ {name: 'a', color: '#abcdef'}, {name: PLOT_GROUP_OTHERS_NAME, color: '#111111'} ], 'Others should use a palette color freed by overrides, respecting first-match precedence' ); const filtered = getCategoricalHistogram( {ds: [{...bins.ds[0], count: 2, indexes: [1, 2]}]}, {ds: dataset}, {groupBy} ); t.deepEqual( filtered.legend, [ {name: 'b', color: '#222222'}, {name: PLOT_GROUP_OTHERS_NAME, color: '#111111'} ], 'Others should keep its color when mapped categories are filtered out' ); t.end(); }); test('Utils -> numeric histogram groups use equal widths and include boundary values exactly once', t => { for (const [type, values] of [ ['integer', [8, 0, 2, 4, 6]], ['real', [8, 0, 2, 4, 6]], ['integer', [8n, 0n, 2n, 4n, 6n]] ]) { const datasets = {ds: numericGroupDataset(values, type)}; const bins = { ds: [ {x0: 0, x1: 1, count: 3, indexes: [0, 1, 2]}, {x0: 1, x1: 2, count: 2, indexes: [3, 4]} ] }; const original = JSON.stringify(bins); const groupBy = { fieldName: 'g', numGroups: 2, groupOthers: false, colorRange: {colors: ['#111111', '#222222']} }; const result = getCategoricalHistogram(bins, datasets, {groupBy}); t.deepEqual( result.legend, [ {name: '0 ≤ x < 4', color: '#111111'}, {name: '4 ≤ x ≤ 8', color: '#222222'} ], `${type}: should describe equal-width ranges in numeric order` ); t.deepEqual( result.bins.ds.map(bin => bin.segments.map(({name, count}) => [name, count])), [ [ ['0 ≤ x < 4', 2], ['4 ≤ x ≤ 8', 1] ], [['4 ≤ x ≤ 8', 2]] ], `${type}: should put interior boundaries in the next range and include the maximum` ); t.deepEqual( result.bins.ds.map(bin => bin.segments.reduce((total, segment) => total + segment.count, 0)), bins.ds.map(bin => bin.count), `${type}: should color every finite numeric row, even with Others disabled` ); t.deepEqual( getCategoricalHistogram(bins, datasets, {groupBy: {...groupBy, groupOthers: true}}), result, `${type}: should need no overflow group for numeric ranges` ); t.equal( getCategoricalHistogram(bins, datasets, {groupBy: {...groupBy, numGroups: 4}}).legend.length, 4, `${type}: should update the range count when Max groups changes` ); t.equal(JSON.stringify(bins), original, `${type}: should not mutate source bins`); } t.end(); }); test('Utils -> numeric histogram groups keep small domains exact and ignore invalid numbers', t => { const values = [2, 1, 2, 0, null, undefined, '', NaN, Infinity, -Infinity, 'invalid', '3', false]; const bins = {ds: [{x0: 0, x1: 1, count: values.length, indexes: values.map((_, i) => i)}]}; const datasets = {ds: numericGroupDataset(values)}; const groupBy = {fieldName: 'g', numGroups: 3}; const result = getCategoricalHistogram(bins, datasets, {groupBy}); t.deepEqual( result.bins.ds[0].segments.map(({name, count}) => [name, count]).sort(), [ ['0', 1], ['1', 1], ['2', 2] ], 'should keep exact categories when finite distinct values equal the limit' ); const ranges = getCategoricalHistogram(bins, datasets, {groupBy: {...groupBy, numGroups: 2}}); t.deepEqual( ranges.bins.ds[0].segments.map(({name, count}) => [name, count]), [ ['0 ≤ x < 1', 1], ['1 ≤ x ≤ 2', 3] ], 'should exclude missing and nonfinite values from the range domain and counts' ); t.equal( ranges.bins.ds[0].count, values.length, 'should preserve the total including invalid rows' ); t.deepEqual( getCategoricalHistogram( {ds: [{x0: 0, x1: 1, count: 2, indexes: [0, 1]}]}, {ds: numericGroupDataset([7, 7], 'integer')}, {groupBy: {...groupBy, numGroups: 1}} ).bins.ds[0].segments.map(({name, count}) => [name, count]), [['7', 2]], 'should keep a constant column as one exact category' ); t.equal( getCategoricalHistogram( {ds: [{x0: 0, x1: 1, count: 3, indexes: [0, 1, 2]}]}, {ds: numericGroupDataset([null, NaN, Infinity])}, {groupBy} ), null, 'should retain plain bins when no numeric values are valid' ); const text = getCategoricalHistogram( {ds: [{x0: 0, x1: 1, count: 3, indexes: [0, 1, 2]}]}, {ds: numericGroupDataset(['0', '1', '2'], 'string')}, {groupBy: {...groupBy, numGroups: 2}} ); t.deepEqual( text.legend.map(({name}) => name), ['0', '1'], 'should keep numeric text categorical' ); const largeIntegers = [9007199254740992n, 9007199254740993n]; const exactLargeIntegers = getCategoricalHistogram( {ds: [{x0: 0, x1: 1, count: 2, indexes: [0, 1]}]}, {ds: numericGroupDataset(largeIntegers, 'integer')}, {groupBy} ); t.deepEqual( exactLargeIntegers.legend.map(({name}) => name), largeIntegers.map(String), 'should not merge distinct unsafe bigint values through a lossy numeric conversion' ); t.end(); }); test('Utils -> categorical histograms preserve unsafe Arrow integers and refresh numeric ranges', async t => { for (const [type, largeIntegers] of [ [new arrow.Int64(), [-9007199254740992n, -9007199254740993n]], [new arrow.Uint64(), [18446744073709551614n, 18446744073709551615n]] ]) { const fields = [{name: 'g', type: 'integer', analyzerType: 'INT'}]; const arrowData = values => { const arrowTable = new arrow.Table({g: arrow.vectorFromArray(values, type)}); return {fields, cols: [arrowTable.getChildAt(0)], arrowTable}; }; const dataset = new KeplerTable({info: {id: 'ds'}, color: [0, 0, 0]}).updateSchema( arrowData([...largeIntegers, null]) ); const plotType = {groupBy: {fieldName: 'g', numGroups: 2}}; const histogram = () => getCategoricalHistogram( {ds: [{x0: 0, x1: 1, count: dataset.allIndexes.length, indexes: dataset.allIndexes}]}, {ds: dataset}, plotType ); const exact = histogram(); t.deepEqual( exact.legend.map(({name}) => name), largeIntegers.map(String), `${type}: should keep distinct unsafe integer categories from real table accessors` ); t.deepEqual( exact.bins.ds[0].segments.map(({name, count}) => [name, count]), largeIntegers.map(value => [String(value), 1]), `${type}: should count each integer exactly once and exclude null` ); t.equal(exact.bins.ds[0].count, 3, `${type}: should retain the total including null`); await dataset.update(arrowData([0n, 2n, 4n, 6n, 8n, null])); const ranges = histogram(); t.deepEqual( ranges.bins.ds[0].segments.map(({name, count}) => [name, count]), [ ['0 ≤ x < 4', 2], ['4 ≤ x ≤ 8', 3] ], `${type}: should invalidate the cached domain and group safe integers into ranges` ); t.equal(ranges.bins.ds[0].count, 6, `${type}: should preserve null in the updated total`); } t.end(); }); test('Utils -> numeric histogram ranges share a stable domain across datasets and cross-filters', t => { const datasets = { ds: numericGroupDataset([-10, 0, 5, 10], 'integer'), linked: numericGroupDataset([20, 30]) }; const bins = { ds: [{x0: 0, x1: 1, count: 4, indexes: [0, 1, 2, 3]}], linked: [{x0: 0, x1: 1, count: 2, indexes: [0, 1]}] }; const plotType = {groupBy: {fieldName: 'g', numGroups: 2}}; const initial = getCategoricalHistogram(bins, datasets, plotType); t.deepEqual( initial.legend.map(({name}) => name), ['-10 ≤ x < 10', '10 ≤ x ≤ 30'], 'should use the full shared numeric domain' ); t.deepEqual( initial.bins.linked[0].segments, [{...initial.legend[1], count: 2}], 'should use the same ranges and colors in linked datasets' ); const filtered = getCategoricalHistogram( {...bins, ds: [{x0: 0, x1: 1, count: 1, indexes: [3]}]}, datasets, plotType ); t.deepEqual( filtered.legend, [initial.legend[1]], 'should preserve upper-range boundaries and color when lower values are filtered out' ); t.deepEqual( filtered.bins.ds[0].segments, [{...initial.legend[1], count: 1}], 'should retain ranges even when the visible distinct count falls below the limit' ); t.deepEqual( getCategoricalHistogram(bins, datasets, plotType), initial, 'should restore the same grouping when the filter is cleared' ); t.end(); }); test('Utils -> numeric histogram range labels hide floating-point noise without changing membership', t => { const lower = 2.5; const upper = 7.39; const boundary = lower * 0.7 + upper * 0.3; const values = [ ...Array.from({length: 11}, (_, i) => lower * (1 - i / 10) + upper * (i / 10)), nextDown(boundary), boundary, nextUp(boundary) ]; const result = getCategoricalHistogram( { ds: [ {x0: 0, x1: 1, count: 11, indexes: values.slice(0, 11).map((_, i) => i)}, {x0: 1, x1: 2, count: 3, indexes: [11, 12, 13]} ] }, {ds: numericGroupDataset(values)}, {groupBy: {fieldName: 'g', numGroups: 10}} ); t.deepEqual( result.legend.map(({name}) => name), [ '2.5 ≤ x < 2.989', '2.989 ≤ x < 3.478', '3.478 ≤ x < 3.967', '3.967 ≤ x < 4.456', '4.456 ≤ x < 4.945', '4.945 ≤ x < 5.434', '5.434 ≤ x < 5.923', '5.923 ≤ x < 6.412', '6.412 ≤ x < 6.901', '6.901 ≤ x ≤ 7.39' ], 'should use compact labels instead of exposing binary floating-point digits' ); t.deepEqual( result.bins.ds[1].segments.map(({name, count}) => [name, count]), [ ['3.478 ≤ x < 3.967', 1], ['3.967 ≤ x < 4.456', 2] ], 'should still classify adjacent values using the original unrounded boundary' ); t.end(); }); test('Utils -> numeric histogram ranges handle tiny decimals and extreme finite spans', t => { for (const values of [ [1, 1 + Number.EPSILON, 1 + 2 * Number.EPSILON, 1 + 3 * Number.EPSILON], [-Number.MAX_VALUE, -1e308, 0, 1e308, Number.MAX_VALUE] ]) { const result = getCategoricalHistogram( {ds: [{x0: 0, x1: 1, count: values.length, indexes: values.map((_, i) => i)}]}, {ds: numericGroupDataset(values)}, {groupBy: {fieldName: 'g', numGroups: 2}} ); t.equal(result.legend.length, 2, 'should produce two distinguishable nonempty numeric ranges'); result.legend.forEach(({name}) => { const match = name.match(/^(.+) ≤ x [<≤] (.+)$/); t.ok(match && match[1] !== match[2], 'should not label different bounds as the same value'); t.notOk(/NaN|Infinity|undefined/.test(name), 'should describe finite boundaries'); }); t.equal( result.bins.ds[0].segments.reduce((sum, segment) => sum + segment.count, 0), values.length, 'should represent all finite values including the maximum' ); } t.end(); }); test('Utils -> getLineChart aggregates numeric range bins on a numeric X axis', t => { const rows = [ {x: -1.25, y: 2}, {x: -0.75, y: 3}, {x: -0.25, y: null}, {x: 0.25, y: 5} ]; const bins = [ {count: 1, indexes: [0], x0: -1.5, x1: -1}, {count: 1, indexes: [1], x0: -1, x1: -0.5}, {count: 1, indexes: [2], x0: -0.5, x1: 0}, {count: 1, indexes: [3], x0: 0, x1: 0.5} ]; const chart = getLineChart( { ds: { fields: [ {name: 'x', type: 'real', valueAccessor: ({index}) => rows[index].x}, {name: 'y', type: 'real', valueAccessor: ({index}) => rows[index].y} ] } }, { type: 'range', dataId: ['ds'], yAxis: {name: 'y', type: 'real'}, plotType: {type: 'lineChart', aggregation: 'sum', numericInterval: 0.5}, bins: {ds: bins} } ); t.deepEqual(chart.xDomain, [-1.5, 0.5], 'should keep numeric X-domain values'); t.equal(chart.interval, 0.5, 'should cache the numeric bin size without calendar conversion'); t.deepEqual( chart.series.lines.map(line => line.map(point => [point.x, point.y])), [ [ [-1.5, 2], [-1, 3], [-0.5, 0], [0, 5] ] ], 'should aggregate each numeric bin and tolerate missing Y values' ); t.end(); }); test('Utils -> getLineChart groupBy splits series and folds the rest into Others', t => { const datasets = {ds: groupedDataset()}; const colors = ['#111111', '#222222', '#333333']; const filter = groupedFilter({ fieldName: 'g', numGroups: 2, groupOthers: true, colorRange: {colors} }); const ungrouped = getLineChart(datasets, groupedFilter(null)); t.deepEqual( seriesSummary(ungrouped).ys, [[47, 20, 5]], 'should keep a single series when groupBy is cleared' ); const grouped = getLineChart(datasets, filter); t.deepEqual( seriesSummary(grouped), { names: ['a', 'b', PLOT_GROUP_OTHERS_NAME], colors, ys: [[10, 20], [30], [5]] }, 'should keep the first groups and aggregate the rest as Others' ); t.deepEqual( lineChartSeriesLegend(grouped), [ {name: 'a', color: '#111111'}, {name: 'b', color: '#222222'}, {name: PLOT_GROUP_OTHERS_NAME, color: '#333333'} ], 'should expose one legend entry per series' ); const capped = getLineChart(datasets, { ...filter, plotType: { ...filter.plotType, groupBy: {...filter.plotType.groupBy, groupOthers: false} } }); t.deepEqual( seriesSummary(capped).names, ['a', 'b'], 'should drop groups past numGroups when groupOthers is off' ); const collided = getLineChart( datasets, groupedFilter({ fieldName: 'g', numGroups: 2, groupOthers: true, colorRange: {colors: ['#111111', '#222222']} }) ); const collidedColors = seriesSummary(collided).colors; t.deepEqual( collidedColors.slice(0, 2), ['#111111', '#222222'], 'should keep the colors of the groups that fit' ); t.ok( !collidedColors.slice(0, 2).includes(collidedColors[2]), 'should give Others a color that is not already used' ); const mapped = getLineChart( datasets, groupedFilter({ fieldName: 'g', numGroups: 2, groupOthers: true, colorRange: { colors: ['#111111', '#222222'], colorMap: [[PLOT_GROUP_OTHERS_NAME, '#abcdef']] } }) ); t.equal( seriesSummary(mapped).colors[2], '#abcdef', 'should keep an explicit Others color from the color map' ); const cached = getLineChart(datasets, {...filter, lineChart: grouped}); t.equal(cached, grouped, 'should reuse the line chart when groupBy and bins are unchanged'); const narrowed = getLineChart(datasets, { ...filter, lineChart: grouped, plotType: { ...filter.plotType, groupBy: {...filter.plotType.groupBy, numGroups: 1, groupOthers: false} } }); t.deepEqual(seriesSummary(narrowed).names, ['a'], 'should recompute when numGroups changes'); t.end(); }); test('Utils -> getLineChart groupBy caps All at the palette size', t => { const rowCount = MAX_PLOT_NUM_GROUPS + 5; const rows = Array.from({length: rowCount}, (_, index) => ({v: 1, g: `g${index}`})); const datasets = { ds: { fields: [ {name: 'v', type: 'integer', valueAccessor: ({index}) => rows[index].v}, {name: 'g', type: 'string', valueAccessor: ({index}) => rows[index].g} ] } }; const filter = { dataId: ['ds'], yAxis: {name: 'v', type: 'integer'}, plotType: { aggregation: 'sum', interval: '1-day', type: 'lineChart', groupBy: { fieldName: 'g', numGroups: PLOT_NUM_GROUPS_ALL, groupOthers: true, colorRange: {colors: ['#111111']} } }, timeBins: { ds: { '1-day': [{count: rowCount, indexes: rows.map((_, index) => index), x0: 0, x1: 1}] } } }; const chart = getLineChart(datasets, filter); const names = seriesSummary(chart).names; t.equal(names.length, MAX_PLOT_NUM_GROUPS + 1, 'should keep the cap plus Others'); t.equal(names[names.length - 1], PLOT_GROUP_OTHERS_NAME, 'should fold the rest into Others'); t.deepEqual( names.slice(0, MAX_PLOT_NUM_GROUPS), rows.slice(0, MAX_PLOT_NUM_GROUPS).map(row => row.g), 'should keep the first groups in first-seen order' ); t.end(); }); test('Utils -> mergePlotGroupBy fills defaults and clears', t => { const created = mergePlotGroupBy(null, {fieldName: 'city'}); t.equal(created.fieldName, 'city', 'should keep the selected field'); t.equal(created.numGroups, 10, 'should default max groups to 10'); t.equal(created.groupOthers, false, 'should default group others off'); t.ok(created.colorRange.colors.length >= 2, 'should assign a qualitative color range'); t.equal( created.colorUI.colorRangeConfig.type, 'qualitative', 'should use a qualitative color UI' ); const updated = mergePlotGroupBy(created, {numGroups: 3, groupOthers: true}); t.equal(updated.fieldName, 'city', 'should keep the field when only the cap changes'); t.equal(updated.numGroups, 3, 'should apply the new cap'); t.equal(updated.groupOthers, true, 'should apply group others'); t.equal(updated.colorRange, created.colorRange, 'should keep the existing series colors'); t.notOk(created.source, 'should keep legacy grouping independent without a source setting'); const linked = mergePlotGroupBy(updated, {source: 'layer', layerId: 'points'}); t.equal(linked.source, 'layer', 'should enable layer grouping'); t.equal(linked.layerId, 'points', 'should retain the chosen layer'); t.equal(linked.fieldName, updated.fieldName, 'should preserve the independent field'); t.equal(linked.colorRange, updated.colorRange, 'should preserve the independent palette'); const independent = mergePlotGroupBy(linked, {source: 'independent'}); t.deepEqual( independent, {...updated, source: 'independent', layerId: 'points'}, 'should restore independent settings while remembering the layer' ); t.equal( mergePlotGroupBy(linked, {groupOthers: false}).source, 'layer', 'should preserve the source when updating another setting' ); t.equal(mergePlotGroupBy(updated, null), null, 'should clear groupBy'); t.end(); }); test('Utils -> intervalBinFromMarks snaps a brush position to one histogram bin', t => { const marks = [0, 100, 200, 300]; t.deepEqual(intervalBinFromMarks(marks, 0), [0, 99], 'should select the first bin'); t.deepEqual( intervalBinFromMarks(marks, 100), [100, 199], 'should select the bin that starts on the mark' ); t.deepEqual( intervalBinFromMarks(marks, 149), [100, 199], 'should snap toward the nearer bin start' ); t.deepEqual( intervalBinFromMarks(marks, 150), [200, 299], 'should snap to the next bin at the midpoint' ); t.deepEqual( intervalBinFromMarks(marks, 250), [200, 299], 'should keep the right half of the last bin on that bin' ); t.deepEqual( intervalBinFromMarks(marks, 300), [200, 299], 'should keep the final mark on the last bin' ); t.deepEqual(intervalBinFromMarks([10], 10), [10, 10], 'should keep a single mark as a point'); t.deepEqual( intervalBinFromMarks([], 10), [10, 10], 'should leave the value unchanged without marks' ); t.deepEqual( intervalBinFromMarks([-1.5, -1, -0.5], -1.3, 0), [-1.5, nextDown(-1)], 'should end a numeric bin at the representable value before its next boundary' ); t.deepEqual( intervalBinFromMarks([0, 0.05, 0.1], 0, 0), [0, nextDown(0.05)], 'should exclude the next fractional bin boundary without subtracting a nominal step' ); t.deepEqual( intervalBinFromMarks([0, 0.05, 0.1], 0.1, 0), [0.05, 0.1], 'should include the numeric domain maximum in the final interval' ); t.end(); }); test('Utils -> numeric interval selection matches histogram row membership', t => { const values = [0, 0.05, 0.075, 0.1]; const rowIds = values.map((_, index) => index); const thresholds = getNumericBinThresholds([0, 0.1], 0.05); const bins = histogramFromThreshold(thresholds, rowIds, index => values[index], false); const marks = [...bins.map(bin => bin.x0), bins[bins.length - 1].x1]; const dataset = { dataContainer: {}, filteredIndex: rowIds, filteredIndexByLayer: {}, gpuFilter: { filterValueUpdateTriggers: {gpuFilter_0: {name: 'x'}}, filterRange: [[0, 0]], filterValueAccessor: () => () => ({index}) => [values[index]] } }; t.deepEqual( bins.map(bin => bin.count), [1, 2, 1], 'fixture should exercise two half-open boundaries and the final maximum bin' ); bins.forEach((bin, index) => { dataset.gpuFilter.filterRange = [intervalBinFromMarks(marks, bin.x0, 0)]; t.deepEqual( runGpuFilterForPlot(dataset), [...bin.indexes], `interval ${index} should select exactly the row IDs assigned to its histogram bin` ); }); t.end(); });