You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
3.1 KiB
67 lines
3.1 KiB
|
2 months ago
|
const test = require('node:test');
|
||
|
|
const assert = require('node:assert/strict');
|
||
|
|
const dashboard = require('../frontend/web/static/js/dashboard.js');
|
||
|
|
|
||
|
|
test('storage reports real partition data separately from RAM and fails closed on unavailable readings', () => {
|
||
|
|
assert.equal(dashboard.storageSummary(null),null);
|
||
|
|
assert.equal(dashboard.storageSummary({memory_usage_percent:25}),null);
|
||
|
|
const storage={total_bytes:1000,used_bytes:200,available_bytes:750,usage_percent:20,path:'/userdata'};
|
||
|
|
assert.deepEqual(dashboard.storageSummary(storage),{total:1000,used:200,available:750,percent:20,path:'/userdata'});
|
||
|
|
for(const update of [{total_bytes:0},{used_bytes:-1},{available_bytes:900},{usage_percent:null},{used_bytes:2000}]) assert.equal(dashboard.storageSummary({...storage,...update}),null);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('missing or invalid readings are not presented as zero', () => {
|
||
|
|
for (const value of [null, undefined, '', 'invalid', NaN, Infinity]) {
|
||
|
|
assert.equal(dashboard.numeric(value), null);
|
||
|
|
assert.equal(dashboard.metricText(value, '%'), '—');
|
||
|
|
assert.equal(dashboard.percentage(value), null);
|
||
|
|
}
|
||
|
|
assert.equal(dashboard.metricText(0, '%'), '0%');
|
||
|
|
assert.equal(dashboard.metricText(45.555, '°C'), '45.6°C');
|
||
|
|
assert.equal(dashboard.percentage(-1), null);
|
||
|
|
assert.equal(dashboard.percentage(100.1), null);
|
||
|
|
assert.equal(dashboard.percentage(100), 100);
|
||
|
|
});
|
||
|
|
|
||
|
|
function port(channel, state, rx = 0, tx = 0) {
|
||
|
|
return { channel, __linkView: { visualKey: state }, runtime: { rx_bytes: rx, tx_bytes: tx } };
|
||
|
|
}
|
||
|
|
|
||
|
|
test('UP and session are active; missing ports remain unknown, never down', () => {
|
||
|
|
const result = dashboard.summarizePorts([
|
||
|
|
port('CH01', 'up', 100, 30), port('CH02', 'session', 50, 20),
|
||
|
|
port('CH03', 'down'), port('CH04', 'unknown'),
|
||
|
|
], 48);
|
||
|
|
assert.deepEqual(result.counts, { all: 48, up: 1, session: 1, down: 1, unknown: 45 });
|
||
|
|
assert.equal(result.active, 2);
|
||
|
|
assert.equal(result.rx, 150);
|
||
|
|
assert.equal(result.tx, 50);
|
||
|
|
assert.deepEqual(result.ranking.map(p => p.channel), ['CH01', 'CH02']);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('ranking excludes zero traffic and is stable for equal values', () => {
|
||
|
|
const result = dashboard.summarizePorts([
|
||
|
|
port('CH16', 'up', 100), port('CH02', 'unknown', 100), port('CH01', 'unknown'),
|
||
|
|
], 48);
|
||
|
|
assert.deepEqual(result.ranking.map(p => p.channel), ['CH02', 'CH16']);
|
||
|
|
assert.equal(dashboard.summarizePorts([], 48).ranking.length, 0);
|
||
|
|
assert.equal(dashboard.summarizePorts([], 48).counts.unknown, 48);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('invalid/duplicate channels and negative counters do not inflate totals', () => {
|
||
|
|
const result = dashboard.summarizePorts([
|
||
|
|
port('CH01', 'up', -2, 'bad'), port('CH01', 'up', 999),
|
||
|
|
port('CH00', 'up', 999), port('CH49', 'up', 999), port('bad', 'up', 999),
|
||
|
|
], 48);
|
||
|
|
assert.equal(result.active, 1);
|
||
|
|
assert.equal(result.rx, 0);
|
||
|
|
assert.equal(result.tx, 0);
|
||
|
|
assert.equal(result.counts.unknown, 47);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('at most five nonzero traffic entries are displayed', () => {
|
||
|
|
const result = dashboard.summarizePorts(Array.from({length: 48}, (_, i) => port(`CH${i + 1}`, 'unknown', i)), 48);
|
||
|
|
assert.equal(result.ranking.length, 5);
|
||
|
|
assert.equal(result.ranking[0].channel, 'CH48');
|
||
|
|
});
|