diff --git a/src/lib/storage.js b/src/lib/storage.js new file mode 100644 index 0000000..7cbc100 --- /dev/null +++ b/src/lib/storage.js @@ -0,0 +1,34 @@ +export const DEFAULT_SETTINGS = { apiKey: '', threshold: 5, minViews: 10000 }; + +const toNumber = (value) => (value === null || value === undefined || value === '' ? NaN : Number(value)); + +export function normalizeSettings(raw = {}) { + const threshold = toNumber(raw.threshold); + const minViews = toNumber(raw.minViews); + return { + apiKey: typeof raw.apiKey === 'string' ? raw.apiKey.trim() : DEFAULT_SETTINGS.apiKey, + threshold: Number.isFinite(threshold) && threshold > 0 ? threshold : DEFAULT_SETTINGS.threshold, + minViews: Number.isFinite(minViews) && minViews >= 0 ? Math.floor(minViews) : DEFAULT_SETTINGS.minViews, + }; +} + +export async function getSettings() { + const { settings } = await chrome.storage.local.get('settings'); + return normalizeSettings(settings); +} + +export async function saveSettings(raw) { + const settings = normalizeSettings(raw); + await chrome.storage.local.set({ settings }); + return settings; +} + +// Status is { state: 'ok' | 'no-key' | 'invalid-key' | 'quota', message?, until? }. +export async function getStatus() { + const { status } = await chrome.storage.local.get('status'); + return status ?? { state: 'ok' }; +} + +export async function setStatus(status) { + await chrome.storage.local.set({ status }); +} diff --git a/test/storage.test.js b/test/storage.test.js new file mode 100644 index 0000000..9712546 --- /dev/null +++ b/test/storage.test.js @@ -0,0 +1,35 @@ +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(normalizeSettings(), DEFAULT_SETTINGS); + assert.deepEqual(normalizeSettings({}), DEFAULT_SETTINGS); + assert.deepEqual(DEFAULT_SETTINGS, { apiKey: '', threshold: 5, minViews: 10000 }); +}); + +test('normalizeSettings trims the key and coerces numeric strings', () => { + assert.deepEqual(normalizeSettings({ apiKey: ' abc123 ', threshold: '7.5', minViews: '20000' }), { + apiKey: 'abc123', + 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 ignores a non-string key', () => { + assert.equal(normalizeSettings({ apiKey: 12345 }).apiKey, ''); +});