Files
client/test/chat-options.test.mjs
T

119 lines
7.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
MARKDOWN_HELP_IDS, showsMessage, formatClock, opensIncoming, isIgnoredUid, isIgnoredText, withIgnored, withoutIgnored,
} from '../app/renderer/js/chat-options.js';
import { parseMarkdown, toPlainText } from '../app/renderer/js/markdown/parse.js';
import { t, setLanguage, hasTranslation } from '../app/renderer/js/i18n.js';
const BAD = 'QkJCQkJCQkJCQkJCQkJCQkJCQkI=';
const SELF = 'QUFBQUFBQUFBQUFBQUFBQUFBQUE=';
const conn = { snap: { own_client_id: 1, clients: [{ id: 1, uid: SELF }, { id: 2, uid: BAD }, { id: 3, uid: 'other=' }] } };
const ignoring = { chatIgnoredUsers: [{ uid: BAD, name: 'Spammer' }] };
test('moderation events are hidden only when the option is off; messages, pokes and notices always show', () => {
const moderation = { system: true, moderation: true };
const notice = { system: true };
const text = { author: 'A', text: 'hi' };
for (const prefs of [undefined, {}, { chatShowModerationEvents: true }]) assert.equal(showsMessage(moderation, prefs), true);
assert.equal(showsMessage(moderation, { chatShowModerationEvents: false }), false);
assert.equal(showsMessage(notice, { chatShowModerationEvents: false }), true);
assert.equal(showsMessage(text, { chatShowModerationEvents: false }), true);
});
test('timestamps default to 24-hour and switch to the locale 12-hour clock', () => {
const date = new Date(2026, 10, 1, 15, 5);
for (const prefs of [undefined, {}, { chatUse24hTime: true }]) assert.equal(formatClock(date, 'en', prefs), '15:05');
assert.match(formatClock(date, 'en', { chatUse24hTime: false }), /^3:05\sPM$/u);
assert.equal(formatClock(new Date(2026, 10, 1, 9, 7), 'ru', {}), '09:07');
const ru12 = formatClock(date, 'ru', { chatUse24hTime: false });
assert.match(ru12, /^3:05\s/u);
assert.notEqual(ru12, formatClock(date, 'ru', {}));
});
test('incoming chats open in the sidebar by default', () => {
assert.equal(opensIncoming(undefined), true);
assert.equal(opensIncoming({ chatOpenIncoming: true }), true);
assert.equal(opensIncoming({ chatOpenIncoming: false }), false);
});
test('ignored users: only inbound texts and pokes carrying an ignored invoker UID are suppressed', () => {
for (const target of ['channel', 'server', 'client', 'poke']) {
const evt = Object.freeze({ event: 'text', target, from_client_id: 2, from_uid: BAD, from_nickname: 'Spammer', message: 'x', to_client_id: 1 });
assert.equal(isIgnoredText(evt, conn, ignoring), true, target);
assert.equal(isIgnoredText(evt, conn, {}), false, target);
}
// Other senders, our own echoes and events without a reported UID are never dropped.
assert.equal(isIgnoredText({ target: 'channel', from_client_id: 3, from_uid: 'other=' }, conn, ignoring), false);
assert.equal(isIgnoredText({ target: 'client', from_client_id: 1, from_uid: SELF, to_client_id: 2 }, conn, { chatIgnoredUsers: [{ uid: SELF, name: '' }] }), false);
assert.equal(isIgnoredText({ target: 'channel', from_client_id: 2 }, conn, ignoring), false);
assert.equal(isIgnoredText({ target: 'server', from_client_id: 0, from_uid: null }, conn, ignoring), false);
assert.equal(isIgnoredUid(ignoring, BAD), true);
assert.equal(isIgnoredUid(ignoring, ''), false);
assert.equal(isIgnoredUid(ignoring, undefined), false);
});
test('ignore list edits are explicit, deduplicated and never mutate the current prefs', () => {
const prefs = Object.freeze({ chatIgnoredUsers: Object.freeze([Object.freeze({ uid: BAD, name: 'Spammer' })]) });
const added = withIgnored(prefs, 'other=', 'Ot\u0007her'.padEnd(200, 'x'));
assert.deepEqual(added.map((entry) => entry.uid), [BAD, 'other=']);
assert.equal(added[1].name.length, 128);
assert.equal(added[1].name.startsWith('Other'), true);
const renamed = withIgnored({ chatIgnoredUsers: added }, BAD, 'Renamed');
assert.deepEqual(renamed.map((entry) => [entry.uid, entry.name.slice(0, 7)]), [[BAD, 'Renamed'], ['other=', 'Otherxx']]);
assert.deepEqual(withoutIgnored({ chatIgnoredUsers: renamed }, BAD).map((entry) => entry.uid), ['other=']);
assert.deepEqual(withoutIgnored(prefs, 'missing='), prefs.chatIgnoredUsers);
assert.deepEqual(withIgnored(undefined, BAD, 'A'), [{ uid: BAD, name: 'A' }]);
});
function types(value, found = new Set()) {
if (!value || typeof value !== 'object') return found;
if (typeof value.type === 'string') found.add(value.type);
for (const child of Object.values(value)) if (child && typeof child === 'object') types(child, found);
return found;
}
// What the TeamsPeach renderer must produce for each official Markdown Help example.
const EXPECTED = {
heading: ['heading'], setext_heading: ['heading'], emphasis_italic: ['em'], strong_bold: ['strong'],
strikethrough: ['del'], underline: ['u'], highlight: ['mark'], superscript: ['sup'], subscript: ['sub'],
inline_code: ['code'], inline_math: ['math'], link: ['link'], reference_link: ['link'],
link_reference_definition: ['em', 'link'], sized_image_pipe: ['image'], sized_image_equals: ['image'],
autolink: ['link'], plain_url: ['link'], blockquote: ['blockquote'], callout: ['blockquote'],
unordered_list: ['list'], ordered_list: ['list'], task_list_item: ['list'], fenced_code_block: ['code'],
indented_code_block: ['paragraph'], fenced_math_block: ['math_block'], mermaid_diagram: ['mermaid'], table: ['table'],
thematic_break: ['hr'], details_summary: ['details'], align: ['align'], footnote_reference: ['fnref'],
footnote_definition: [], spoiler: ['spoiler'], escape_sequence: ['paragraph'], html_entity: ['paragraph'], emoji: ['emoji'],
};
for (const language of ['en', 'ru']) test(`every Markdown Help example is translated and rendered as its syntax (${language})`, () => {
setLanguage(language);
assert.deepEqual(Object.keys(EXPECTED), MARKDOWN_HELP_IDS);
for (const key of ['title', 'intro.title', 'intro.body', 'syntax.title', 'card.syntax', 'card.example']) {
assert.notEqual(t(`settings.chat.markdown_help.${key}`), `settings.chat.markdown_help.${key}`, key);
}
for (const id of MARKDOWN_HELP_IDS) {
for (const part of ['name', 'syntax', 'example']) {
const key = `settings.chat.markdown_help.syntax.${id}.${part}`;
assert.equal(hasTranslation(key), true, key);
assert.notEqual(t(key), key, key);
}
const doc = parseMarkdown(t(`settings.chat.markdown_help.syntax.${id}.example`));
assert.equal(doc.tooComplex, undefined, id);
const found = types(doc.children);
for (const type of EXPECTED[id]) assert.equal(found.has(type), true, `${id}: ${type} in ${[...found]}`);
if (id === 'callout') assert.ok(doc.children[0].callout, id);
if (id === 'task_list_item') assert.equal(doc.children[0].items[0].task, true, id);
if (id === 'indented_code_block') assert.equal(found.has('code'), false, 'the chat dialect has no indented code blocks');
if (id === 'footnote_reference' || id === 'footnote_definition') assert.equal(doc.footnotes.length, 1, id);
if (id === 'escape_sequence') { assert.equal(found.has('em'), false); assert.match(toPlainText(doc).trim(), /^\*.+\*$/u); }
if (id === 'html_entity') assert.match(toPlainText(doc), /&/u);
}
setLanguage('en');
});
test('a document without footnote references still renders its definitions; references keep first-use order', () => {
assert.deepEqual(parseMarkdown('[^a]: first\n[^b]: second').footnotes.map((note) => note.label), ['a', 'b']);
assert.deepEqual(parseMarkdown('x[^b]\n\n[^a]: first\n[^b]: second').footnotes.map((note) => [note.label, note.number]), [['b', 1]]);
});