import test from 'node:test'; import assert from 'node:assert/strict'; import { WAVEFORM_POINTS, WAVEFORM_MAX_LEVEL, compressLevels, decodeWaveform, encodeWaveform, resampleWaveform, rmsLevel, waveformFromChannelData, } from '../js/components/dm-voice-waveform.js'; test('compressLevels: всегда 64 значения 0..31, пик громкости = 31', () => { const levels = Array.from({ length: 500 }, (_, i) => (i === 250 ? 0.4 : 0.1)); const wave = compressLevels(levels); assert.equal(wave.length, WAVEFORM_POINTS); assert.ok(wave.every((v) => Number.isInteger(v) && v >= 0 && v <= WAVEFORM_MAX_LEVEL)); assert.equal(Math.max(...wave), WAVEFORM_MAX_LEVEL); assert.ok(wave.filter((v) => v === WAVEFORM_MAX_LEVEL).length === 1, 'пик не размазывается по всей волне'); }); test('compressLevels: тишина и пустой ввод дают нули, короткий ввод растягивается', () => { assert.deepEqual([...compressLevels([])], new Array(WAVEFORM_POINTS).fill(0)); assert.deepEqual([...compressLevels(new Array(100).fill(0.0001))], new Array(WAVEFORM_POINTS).fill(0)); const short = compressLevels([0.1, 0.2, 0.4]); assert.equal(short.length, WAVEFORM_POINTS); assert.ok(short[0] < short[WAVEFORM_POINTS - 1], 'форма сохраняется при растяжении'); }); test('compressLevels: возрастающая громкость даёт неубывающую волну, мусор не ломает', () => { const ramp = compressLevels(Array.from({ length: 640 }, (_, i) => i / 640)); for (let i = 1; i < ramp.length; i += 1) assert.ok(ramp[i] >= ramp[i - 1]); const dirty = compressLevels([NaN, -1, Infinity, 0.2, '0.3', null]); assert.ok(dirty.every((v) => v >= 0 && v <= WAVEFORM_MAX_LEVEL)); }); test('encodeWaveform/decodeWaveform: точный круговой обход, 54 символа base64url', () => { const values = Uint8Array.from({ length: WAVEFORM_POINTS }, (_, i) => (i * 7) % 32); const encoded = encodeWaveform(values); assert.equal(encoded.length, 54); assert.match(encoded, /^[A-Za-z0-9_-]+$/); assert.deepEqual([...decodeWaveform(encoded)].slice(0, WAVEFORM_POINTS), [...values]); }); test('encodeWaveform ограничивает значения и длину', () => { const decoded = decodeWaveform(encodeWaveform([99, -5, 3.6, 31])); assert.deepEqual([...decoded].slice(0, 4), [31, 0, 4, 31]); assert.equal(encodeWaveform(new Array(200).fill(5)).length, 54); }); test('decodeWaveform отвергает чужой мусор', () => { for (const bad of ['', null, undefined, 42, 'abc$', 'a b', '', 'A'.repeat(65), 'A']) { assert.equal(decodeWaveform(bad), null, String(bad)); } }); test('resampleWaveform: нужное число столбиков 0..1, пик сохраняется', () => { const values = new Uint8Array(WAVEFORM_POINTS).fill(3); values[33] = 31; const bars = resampleWaveform(values, 40); assert.equal(bars.length, 40); assert.ok(bars.every((v) => v >= 0 && v <= 1)); assert.equal(Math.max(...bars), 1); assert.equal(resampleWaveform(Uint8Array.from([31, 0]), 6).length, 6); assert.deepEqual(resampleWaveform(null, 3), [0, 0, 0]); }); test('rmsLevel и waveformFromChannelData: громкая половина выше тихой', () => { assert.equal(rmsLevel(new Float32Array(16)), 0); assert.ok(Math.abs(rmsLevel(Float32Array.from([1, -1, 1, -1])) - 1) < 1e-9); const data = new Float32Array(48_000); for (let i = 24_000; i < data.length; i += 1) data[i] = Math.sin(i / 7) * 0.5; const wave = waveformFromChannelData(data); assert.equal(wave.length, WAVEFORM_POINTS); const avg = (part) => part.reduce((s, v) => s + v, 0) / part.length; assert.ok(avg([...wave.slice(32)]) > avg([...wave.slice(0, 32)]) + 20); });