#!/usr/bin/env node
// Unit test for the pure helpers in web-files/js/whp-table.js
// (parseBytes / cellSortKey / compareValues). DOM behavior is verified in-browser.
'use strict';
var T = require('../web-files/js/whp-table.js');

var ok = true;
function eq(label, got, expected) {
    if (JSON.stringify(got) !== JSON.stringify(expected)) {
        ok = false;
        console.error('FAIL ' + label + ': got ' + JSON.stringify(got) + ' expected ' + JSON.stringify(expected));
    }
}
function truthy(label, cond) { if (!cond) { ok = false; console.error('FAIL ' + label); } }

// parseBytes: human-readable sizes -> bytes
eq('bytes-gb', T.parseBytes('3.60 GB'), 3.6e9);
eq('bytes-b',  T.parseBytes('320 B'), 320);
eq('bytes-kib-as-k', T.parseBytes('1 KiB'), 1000); // unit normalized, i-suffix stripped
eq('bytes-none', T.parseBytes('hello'), null);
eq('bytes-plain-number-is-not-bytes', T.parseBytes('42'), null); // no unit => not a byte value

// compareValues: numbers sort numerically, before strings; strings natural-compare
truthy('num-asc', T.compareValues(320, 3.6e9) < 0);
truthy('num-desc', T.compareValues(3.6e9, 320) > 0);
truthy('numbers-before-strings', T.compareValues(5, 'abc') < 0);
truthy('string-natural', T.compareValues('item2', 'item10') < 0); // numeric-aware
truthy('string-eqcase', T.compareValues('Abc', 'abc') === 0);

// cellSortKey: data-sort wins; byte strings -> number; percent/number -> number; else lowercased string
eq('key-explicit-number', T.cellSortKey({ dataset: { sort: '900' }, textContent: 'nine hundred' }), 900);
eq('key-explicit-string', T.cellSortKey({ dataset: { sort: 'zeta' }, textContent: 'x' }), 'zeta');
eq('key-bytes', T.cellSortKey({ dataset: {}, textContent: '3.60 GB' }), 3.6e9);
eq('key-percent', T.cellSortKey({ dataset: {}, textContent: '48%' }), 48);
eq('key-string', T.cellSortKey({ dataset: {}, textContent: 'Apache' }), 'apache');

console.log(ok ? 'whp-table OK' : 'whp-table FAIL');
process.exit(ok ? 0 : 1);
