import { test } from 'node:test'; import assert from 'node:assert/strict'; import { DEFAULT_SETTINGS, normalizeSettings } from '../src/lib/storage.js'; test('normalizeSettings falls back to defaults for missing input', () => { assert.deepEqual(DEFAULT_SETTINGS, { threshold: 5, minViews: 10000 }); assert.deepEqual(normalizeSettings(), DEFAULT_SETTINGS); assert.deepEqual(normalizeSettings({}), DEFAULT_SETTINGS); }); test('normalizeSettings coerces numeric strings', () => { assert.deepEqual(normalizeSettings({ threshold: '7.5', minViews: '20000' }), { threshold: 7.5, minViews: 20000 }); }); test('normalizeSettings replaces invalid thresholds with the default', () => { for (const threshold of [-1, 0, 'abc', '', null, NaN, Infinity]) { assert.equal(normalizeSettings({ threshold }).threshold, 5, String(threshold)); } }); test('normalizeSettings replaces invalid minViews with the default and floors decimals', () => { for (const minViews of [-5, 'abc', '', null, NaN]) { assert.equal(normalizeSettings({ minViews }).minViews, 10000, String(minViews)); } assert.equal(normalizeSettings({ minViews: 1500.7 }).minViews, 1500); assert.equal(normalizeSettings({ minViews: 0 }).minViews, 0); }); test('normalizeSettings drops fields it does not know (the old apiKey)', () => { assert.deepEqual(normalizeSettings({ apiKey: 'secret', threshold: 6 }), { threshold: 6, minViews: 10000 }); });