'use strict'; const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { Prefs } = require('../app/main/prefs'); const { MAX_IGNORED_USERS } = require('../app/main/chat-prefs'); function tempFile() { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'teamspeach-chat-prefs-')); return { file: path.join(dir, 'prefs.json'), cleanup: () => fs.rmSync(dir, { recursive: true, force: true }) }; } const UID = 'QkJCQkJCQkJCQkJCQkJCQkJCQkI='; test('new profiles get the official Settings > Chats defaults', () => { const { file, cleanup } = tempFile(); try { const values = new Prefs(file).get(); assert.equal(values.chatToolbarVisibility, 'always-visible'); assert.equal(values.chatMermaidAutoload, 'only-simple'); assert.equal(values.chatOpenIncoming, true); assert.equal(values.chatShowModerationEvents, true); assert.equal(values.chatUse24hTime, true); assert.equal(values.autoDownloadImages, false); assert.deepEqual(values.chatIgnoredUsers, []); } finally { cleanup(); } }); test('chat options and the ignore list persist across restarts', () => { const { file, cleanup } = tempFile(); try { new Prefs(file).set({ chatOpenIncoming: false, chatShowModerationEvents: false, chatUse24hTime: false, chatIgnoredUsers: [{ uid: UID, name: 'Spammer' }, { uid: 'serveradmin', name: '' }] }); const values = new Prefs(file).get(); assert.equal(values.chatOpenIncoming, false); assert.equal(values.chatShowModerationEvents, false); assert.equal(values.chatUse24hTime, false); assert.deepEqual(values.chatIgnoredUsers, [{ uid: UID, name: 'Spammer' }, { uid: 'serveradmin', name: '' }]); } finally { cleanup(); } }); test('malformed chat values are rejected without changing stored prefs', () => { const { file, cleanup } = tempFile(); try { const prefs = new Prefs(file); prefs.set({ chatIgnoredUsers: [{ uid: UID, name: 'Kept' }] }); const invalid = [ { chatOpenIncoming: 'yes' }, { chatShowModerationEvents: 1 }, { chatUse24hTime: null }, { chatIgnoredUsers: [{ uid: UID, name: 'A' }, { uid: UID, name: 'B' }] }, { chatIgnoredUsers: [{ uid: 'not a uid', name: '' }] }, { chatIgnoredUsers: [{ uid: UID, name: 'line\nbreak' }] }, { chatIgnoredUsers: [{ uid: UID, name: 'x'.repeat(129) }] }, { chatIgnoredUsers: [{ uid: UID, name: '', extra: true }] }, { chatIgnoredUsers: [{ uid: UID }] }, { chatIgnoredUsers: Array.from({ length: MAX_IGNORED_USERS + 1 }, (_, i) => ({ uid: `u${i}`, name: '' })) }, ]; for (const patch of invalid) assert.throws(() => prefs.set(patch), /prefs: invalid value/, JSON.stringify(patch).slice(0, 80)); assert.deepEqual(new Prefs(file).get().chatIgnoredUsers, [{ uid: UID, name: 'Kept' }]); } finally { cleanup(); } }); test('a hand-edited file with invalid chat values falls back to the defaults per key', () => { const { file, cleanup } = tempFile(); try { fs.writeFileSync(file, JSON.stringify({ chatUse24hTime: 'no', chatIgnoredUsers: [{ uid: '', name: 'x' }], chatShowModerationEvents: false })); const values = new Prefs(file).get(); assert.equal(values.chatUse24hTime, true); assert.deepEqual(values.chatIgnoredUsers, []); assert.equal(values.chatShowModerationEvents, false); } finally { cleanup(); } });