/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import test from 'node:test'; import assert from 'node:assert/strict'; import { parseStoredArray, parseStoredObject, readStorageValue, } from './storageJson.js'; import { getAnnouncementKey, getUnreadAnnouncementKeys, markAnnouncementsRead, mergeAnnouncementReadKeys, NOTICE_READ_KEYS_KEY, readNoticeReadKeys, } from './notificationReadState.js'; import { getTableCompactMode, parseTableCompactMode, setTableCompactMode, } from './tableCompactMode.js'; import { TABLE_COMPACT_MODES_KEY } from '../constants/common.constant.js'; function mockStorage(t, storage) { const descriptor = Object.getOwnPropertyDescriptor( globalThis, 'localStorage', ); Object.defineProperty(globalThis, 'localStorage', { configurable: true, value: storage, }); t.after(() => { if (descriptor) { Object.defineProperty(globalThis, 'localStorage', descriptor); } else { delete globalThis.localStorage; } }); } const invalidJson = [ null, undefined, '', '{broken', 'null', 'true', '42', '"text"', ]; test('stored arrays reject malformed JSON and non-array values', () => { for (const json of [...invalidJson, '{}', '{"0":"notice"}']) { assert.deepEqual(parseStoredArray(json), [], String(json)); } }); test('stored objects reject malformed JSON and non-object values', () => { for (const json of [...invalidJson, '[]', '[true]']) { assert.deepEqual(parseStoredObject(json), {}, String(json)); } }); test('valid stored containers keep their values without coercion or filtering', () => { const array = ['notice', 'notice', null, 1]; const object = { global: true, tokens: false, other: 'yes' }; assert.deepEqual(parseStoredArray(JSON.stringify(array)), array); assert.deepEqual(parseStoredObject(JSON.stringify(object)), object); }); test('fallback containers are fresh for each parse', () => { const array = parseStoredArray(null); const object = parseStoredObject(null); array.push('notice'); object.global = true; assert.deepEqual(parseStoredArray(null), []); assert.deepEqual(parseStoredObject(null), {}); }); test('storage reads return the original stored value', (t) => { mockStorage(t, { getItem(key) { assert.equal(key, 'preference'); return 'stored'; }, }); assert.equal(readStorageValue('preference'), 'stored'); }); test('storage read failures fall back to null', (t) => { mockStorage(t, { getItem() { throw new Error('Storage blocked'); }, }); assert.equal(readStorageValue('preference'), null); assert.deepEqual(readNoticeReadKeys(), []); assert.equal(getTableCompactMode(), false); }); test('announcement identity keeps the date and first 30 content characters', () => { assert.equal( getAnnouncementKey({ publishDate: '2025-01-01', content: 'a'.repeat(40) }), `2025-01-01-${'a'.repeat(30)}`, ); assert.equal(getAnnouncementKey({}), '-'); assert.equal(getAnnouncementKey(null), '-'); }); const first = { publishDate: '2025-01-01', content: 'First' }; const second = { publishDate: '2025-01-02', content: 'Second' }; test('unread keys preserve announcement order and duplicate counts', () => { const notices = [first, second, second]; const readKeys = [getAnnouncementKey(first)]; assert.deepEqual(getUnreadAnnouncementKeys(notices, readKeys), [ getAnnouncementKey(second), getAnnouncementKey(second), ]); assert.deepEqual( getUnreadAnnouncementKeys(notices, []), notices.map(getAnnouncementKey), ); assert.deepEqual( getUnreadAnnouncementKeys(notices, notices.map(getAnnouncementKey)), [], ); assert.deepEqual(getUnreadAnnouncementKeys([], readKeys), []); }); test('merging read keys retains history, deduplicates, and does not mutate inputs', () => { const notices = [first, second, first]; const readKeys = ['old', getAnnouncementKey(first), 'old']; assert.deepEqual(mergeAnnouncementReadKeys(notices, readKeys), [ 'old', getAnnouncementKey(first), getAnnouncementKey(second), ]); assert.deepEqual(readKeys, ['old', getAnnouncementKey(first), 'old']); assert.deepEqual(notices, [first, second, first]); }); test('notification persistence uses the existing key and merges the latest stored history', (t) => { let stored = '["old"]'; mockStorage(t, { getItem(key) { assert.equal(key, 'notice_read_keys'); return stored; }, setItem(key, value) { assert.equal(key, NOTICE_READ_KEYS_KEY); stored = value; }, }); markAnnouncementsRead([first, first]); assert.deepEqual(JSON.parse(stored), ['old', getAnnouncementKey(first)]); stored = JSON.stringify([...JSON.parse(stored), 'another-tab']); markAnnouncementsRead([second]); assert.deepEqual(JSON.parse(stored), [ 'old', getAnnouncementKey(first), 'another-tab', getAnnouncementKey(second), ]); }); test('invalid notice storage is treated as unread and can be repaired on write', (t) => { let stored; mockStorage(t, { getItem: () => stored, setItem: (_key, value) => { stored = value; }, }); for (const json of [...invalidJson, '{}']) { stored = json; assert.deepEqual(readNoticeReadKeys(), []); assert.deepEqual(getUnreadAnnouncementKeys([first], readNoticeReadKeys()), [ getAnnouncementKey(first), ]); markAnnouncementsRead([first]); assert.deepEqual(JSON.parse(stored), [getAnnouncementKey(first)]); } }); test('notification write failures propagate instead of reporting a saved read state', (t) => { const failure = new Error('Quota exceeded'); mockStorage(t, { getItem: () => '[]', setItem() { throw failure; }, }); assert.throws( () => markAnnouncementsRead([first]), (error) => error === failure, ); }); test('compact parsing preserves global defaults, per-table selection and truthiness', () => { const json = '{"global":true,"tokens":false,"channels":"enabled","logs":0}'; assert.equal(parseTableCompactMode(json), true); assert.equal(parseTableCompactMode(json, 'tokens'), false); assert.equal(parseTableCompactMode(json, 'channels'), true); assert.equal(parseTableCompactMode(json, 'logs'), false); assert.equal(parseTableCompactMode(json, 'missing'), false); }); test('compact storage-event payloads use the same fallback as initial reads', (t) => { let stored; mockStorage(t, { getItem: () => stored }); for (const json of [...invalidJson, '[]', '[true]', '{"global":true}']) { stored = json; assert.equal(parseTableCompactMode(json), getTableCompactMode()); } assert.equal(parseTableCompactMode(null), false); }); test('compact writes preserve other table preferences and the storage key', (t) => { let stored = '{"channels":true,"tokens":false}'; mockStorage(t, { getItem(key) { assert.equal(key, 'table_compact_modes'); return stored; }, setItem(key, value) { assert.equal(key, TABLE_COMPACT_MODES_KEY); stored = value; }, }); assert.equal(setTableCompactMode(true), undefined); setTableCompactMode(true, 'tokens'); assert.deepEqual(JSON.parse(stored), { channels: true, tokens: true, global: true, }); setTableCompactMode(false, 'channels'); assert.equal(getTableCompactMode('channels'), false); }); test('invalid compact preferences can be replaced by a valid object on write', (t) => { let stored; mockStorage(t, { getItem: () => stored, setItem: (_key, value) => { stored = value; }, }); for (const json of [...invalidJson, '[]', '[true]']) { stored = json; assert.equal(getTableCompactMode(), false); setTableCompactMode(true); assert.deepEqual(JSON.parse(stored), { global: true }); } }); test('compact write failures remain non-fatal', (t) => { mockStorage(t, { getItem: () => '{}', setItem() { throw new Error('Quota exceeded'); }, }); assert.doesNotThrow(() => setTableCompactMode(true)); }); test('blocked storage access is guarded for reads and compact writes but not notice writes', (t) => { mockStorage(t, undefined); Object.defineProperty(globalThis, 'localStorage', { configurable: true, get() { throw new Error('Storage blocked'); }, }); assert.deepEqual(readNoticeReadKeys(), []); assert.equal(getTableCompactMode(), false); assert.doesNotThrow(() => setTableCompactMode(true)); assert.throws(() => markAnnouncementsRead([first]), /Storage blocked/); });