192 lines
11 KiB
JavaScript
192 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const B = require('../app/renderer/shared/bookmarks.js');
|
|
|
|
const empty = () => ({ savedServers: [], bookmarkFolders: [], bookmarkOrder: [] });
|
|
const ids = (state, folder = null) => B.contents(state, folder).map((entry) => entry.item.id);
|
|
|
|
function sample() {
|
|
let s = empty();
|
|
s = B.addBookmark(s, { id: 'bmk001', name: 'One', address: 'one.example' });
|
|
s = B.createFolder(s, { id: 'fold01', name: 'Games' });
|
|
s = B.addBookmark(s, { id: 'bmk002', name: 'Two', address: 'two.example' }, 'fold01');
|
|
s = B.createFolder(s, { id: 'fold02', name: 'Nested' }, 'fold01');
|
|
s = B.addBookmark(s, { id: 'bmk003', name: '', address: 'three.example' }, 'fold02');
|
|
return s;
|
|
}
|
|
|
|
test('creation appends to the target container and keeps every invariant', () => {
|
|
const s = sample();
|
|
assert.ok(B.consistent(s));
|
|
assert.deepEqual(ids(s), ['bmk001', 'fold01']);
|
|
assert.deepEqual(ids(s, 'fold01'), ['bmk002', 'fold02']);
|
|
assert.deepEqual(ids(s, 'fold02'), ['bmk003']);
|
|
// An empty bookmark name falls back to the address, like the official display name.
|
|
assert.equal(s.savedServers.find((entry) => entry.id === 'bmk003').name, 'three.example');
|
|
assert.deepEqual(B.allBookmarks(s).map((entry) => entry.id), ['bmk001', 'bmk002', 'bmk003']);
|
|
assert.throws(() => B.createFolder(s, { id: 'fold03', name: 'x' }, 'nope00'), { code: 'missing' });
|
|
assert.throws(() => B.createFolder(s, { id: 'bmk001', name: 'dup' }), { code: 'id' });
|
|
assert.throws(() => B.addBookmark(s, { id: 'bmk009', name: 'bad', address: ' ' }), { code: 'address' });
|
|
});
|
|
|
|
test('moves reorder within a container, enter and leave folders, and update the reference', () => {
|
|
let s = sample();
|
|
s = B.move(s, 'fold01', null, 'bmk001');
|
|
assert.deepEqual(ids(s), ['fold01', 'bmk001']);
|
|
s = B.move(s, 'bmk001', 'fold02', 'bmk003');
|
|
assert.deepEqual(ids(s), ['fold01']);
|
|
assert.deepEqual(ids(s, 'fold02'), ['bmk001', 'bmk003']);
|
|
assert.equal(s.savedServers.find((entry) => entry.id === 'bmk001').folder, 'fold02');
|
|
s = B.move(s, 'bmk001', null, null);
|
|
assert.equal('folder' in s.savedServers.find((entry) => entry.id === 'bmk001'), false);
|
|
s = B.move(s, 'fold02', null, 'fold01');
|
|
assert.equal('parent' in s.bookmarkFolders.find((entry) => entry.id === 'fold02'), false);
|
|
assert.deepEqual(ids(s), ['fold02', 'fold01', 'bmk001']);
|
|
assert.ok(B.consistent(s));
|
|
// The anchor must be a sibling in the destination container.
|
|
assert.throws(() => B.move(s, 'bmk002', null, 'bmk003'), { code: 'missing' });
|
|
});
|
|
|
|
test('a folder can never be moved into itself or its own subtree', () => {
|
|
const s = sample();
|
|
assert.throws(() => B.move(s, 'fold01', 'fold01', null), { code: 'cycle' });
|
|
assert.throws(() => B.move(s, 'fold01', 'fold02', null), { code: 'cycle' });
|
|
assert.equal(B.consistent(s), true);
|
|
});
|
|
|
|
test('folder depth and count stay bounded', () => {
|
|
let s = empty();
|
|
let parent = null;
|
|
for (let level = 1; level <= B.MAX_FOLDER_DEPTH; level++) {
|
|
const id = `lvl${String(level).padStart(3, '0')}`;
|
|
s = B.createFolder(s, { id, name: `L${level}` }, parent);
|
|
parent = id;
|
|
}
|
|
assert.throws(() => B.createFolder(s, { id: 'toodeep', name: 'x' }, parent), { code: 'depth' });
|
|
s = B.createFolder(s, { id: 'side01', name: 'Side' });
|
|
// Moving the full eight-level chain under a root folder would make it nine levels deep.
|
|
assert.throws(() => B.move(s, 'lvl001', 'side01', null), { code: 'depth' });
|
|
assert.ok(B.consistent(B.move(s, 'lvl002', 'side01', null)));
|
|
let many = empty();
|
|
for (let i = 0; i < B.MAX_FOLDERS; i++) many = B.createFolder(many, { id: `f${String(i).padStart(5, '0')}`, name: '' });
|
|
assert.throws(() => B.createFolder(many, { id: 'fextra', name: 'x' }), { code: 'folder-limit' });
|
|
});
|
|
|
|
test('deleting a folder removes its whole subtree and nothing else', () => {
|
|
const s = B.remove(sample(), 'fold01');
|
|
assert.deepEqual(s.savedServers.map((entry) => entry.id), ['bmk001']);
|
|
assert.deepEqual(s.bookmarkFolders, []);
|
|
assert.deepEqual(s.bookmarkOrder, ['bmk001']);
|
|
assert.ok(B.consistent(s));
|
|
const single = B.remove(sample(), 'bmk002');
|
|
assert.deepEqual(ids(single, 'fold01'), ['fold02']);
|
|
assert.throws(() => B.remove(single, 'bmk002'), { code: 'missing' });
|
|
});
|
|
|
|
test('rename keeps identity; empty folder names are allowed, empty bookmark names show the address', () => {
|
|
let s = B.rename(sample(), 'fold01', ' ');
|
|
assert.equal(s.bookmarkFolders.find((entry) => entry.id === 'fold01').name, '');
|
|
s = B.rename(s, 'bmk001', '');
|
|
assert.equal(s.savedServers.find((entry) => entry.id === 'bmk001').name, 'one.example');
|
|
assert.throws(() => B.rename(s, 'bmk001', 'x'.repeat(B.NAME_MAX + 1)), { code: 'name' });
|
|
assert.throws(() => B.rename(s, 'bmk001', 'a\u0007b'), { code: 'name' });
|
|
});
|
|
|
|
test('duplicate places a copy right after the original in the same folder', () => {
|
|
const s = B.duplicate(sample(), 'bmk002', 'copy01');
|
|
assert.deepEqual(ids(s, 'fold01'), ['bmk002', 'copy01', 'fold02']);
|
|
const copy = s.savedServers.find((entry) => entry.id === 'copy01');
|
|
assert.deepEqual({ ...copy, id: 'bmk002' }, { ...s.savedServers.find((entry) => entry.id === 'bmk002'), name: 'Two Copy' });
|
|
assert.equal(B.duplicate(sample(), 'bmk002', 'copy02', '').savedServers.find((entry) => entry.id === 'copy02').name, 'Two');
|
|
assert.throws(() => B.duplicate(sample(), 'fold01', 'copy03'), { code: 'missing' });
|
|
});
|
|
|
|
test('the bookmark limit stays 50 regardless of folders', () => {
|
|
let s = empty();
|
|
for (let i = 0; i < B.MAX_BOOKMARKS; i++) s = B.addBookmark(s, { id: `b${String(i).padStart(5, '0')}`, name: 'x', address: `h${i}` });
|
|
assert.throws(() => B.addBookmark(s, { id: 'bextra', name: 'x', address: 'h' }), { code: 'bookmark-limit' });
|
|
assert.throws(() => B.duplicate(s, 'b00000', 'bcopy1'), { code: 'bookmark-limit' });
|
|
});
|
|
|
|
test('normalize keeps legacy bookmark order and moves orphans to the root without losing bookmarks', () => {
|
|
const legacy = { savedServers: [{ id: 'aaa111', name: 'A', address: 'a' }, { id: 'bbb222', name: 'B', address: 'b' }], bookmarkFolders: [], bookmarkOrder: [] };
|
|
assert.deepEqual(B.normalize(legacy).bookmarkOrder, ['aaa111', 'bbb222']);
|
|
const broken = {
|
|
savedServers: [{ id: 'aaa111', name: 'A', address: 'a', folder: 'gone00' }, { id: 'bbb222', name: 'B', address: 'b', folder: 'okay01' }],
|
|
bookmarkFolders: [
|
|
{ id: 'cyc001', name: 'x', parent: 'cyc002' }, { id: 'cyc002', name: 'y', parent: 'cyc001' },
|
|
{ id: 'okay01', name: 'ok' }, { id: 'kid001', name: 'kid', parent: 'okay01' },
|
|
{ id: 'aaa111', name: 'clash' }, { id: 'kid002', name: 'orphan', parent: 'aaa111' },
|
|
],
|
|
bookmarkOrder: ['cyc001', 'zzz999', 'bbb222'],
|
|
};
|
|
const repaired = B.normalize(broken);
|
|
assert.deepEqual(repaired, {
|
|
savedServers: [{ id: 'aaa111', name: 'A', address: 'a' }, { id: 'bbb222', name: 'B', address: 'b', folder: 'okay01' }],
|
|
bookmarkFolders: [{ id: 'cyc001', name: 'x' }, { id: 'cyc002', name: 'y' }, { id: 'okay01', name: 'ok' }, { id: 'kid001', name: 'kid', parent: 'okay01' }, { id: 'kid002', name: 'orphan' }],
|
|
bookmarkOrder: ['cyc001', 'bbb222', 'cyc002', 'okay01', 'kid001', 'kid002', 'aaa111'],
|
|
});
|
|
assert.ok(B.consistent(repaired));
|
|
});
|
|
|
|
const CIPHER = { server_password_encrypted: 'c2VydmVy', channel_password_encrypted: 'Y2hhbm5lbA==' };
|
|
|
|
function withOptions() {
|
|
const s = sample();
|
|
return { ...s, savedServers: s.savedServers.map((entry) => (entry.id === 'bmk002' ? { ...entry, nickname: 'Guest', channel: 'Lobby/Sub', auto_connect: true, ...CIPHER } : entry)) };
|
|
}
|
|
const find = (state, id) => state.savedServers.find((entry) => entry.id === id);
|
|
|
|
test('editor patches change only their fields and keep folder, position and stored ciphertext', () => {
|
|
const s = B.updateBookmark(withOptions(), 'bmk002', { name: 'Renamed', auto_connect_muted: true });
|
|
assert.deepEqual(find(s, 'bmk002'), { id: 'bmk002', name: 'Renamed', address: 'two.example', folder: 'fold01', nickname: 'Guest', channel: 'Lobby/Sub', auto_connect: true, auto_connect_muted: true, ...CIPHER });
|
|
assert.deepEqual(ids(s, 'fold01'), ['bmk002', 'fold02']);
|
|
assert.ok(B.validServers(s.savedServers) && B.consistent(s));
|
|
// '' and false clear an option, so the stored entry falls back to the default behaviour.
|
|
const cleared = find(B.updateBookmark(s, 'bmk002', { nickname: ' ', channel: '', auto_connect: false, auto_connect_muted: false }), 'bmk002');
|
|
assert.deepEqual(Object.keys(cleared), ['id', 'name', 'address', 'folder', ...Object.keys(CIPHER)]);
|
|
assert.equal(find(B.updateBookmark(s, 'bmk002', { nickname: ' Нико ' }), 'bmk002').nickname, 'Нико');
|
|
});
|
|
|
|
test('a changed address drops both address-bound passwords; the same address keeps them', () => {
|
|
const s = withOptions();
|
|
assert.deepEqual(find(B.updateBookmark(s, 'bmk002', { address: ' two.example ' }), 'bmk002').server_password_encrypted, CIPHER.server_password_encrypted);
|
|
const moved = find(B.updateBookmark(s, 'bmk002', { address: 'TWO.example' }), 'bmk002');
|
|
assert.equal(moved.server_password_encrypted, undefined);
|
|
assert.equal(moved.channel_password_encrypted, undefined);
|
|
assert.equal(moved.nickname, 'Guest');
|
|
});
|
|
|
|
test('the renderer can never write passwords or unknown fields through an editor patch', () => {
|
|
const s = withOptions();
|
|
for (const field of ['server_password_encrypted', 'channel_password_encrypted', 'server_password', 'folder', 'id']) {
|
|
assert.throws(() => B.updateBookmark(s, 'bmk002', { [field]: 'YWJj' }), { code: 'field' }, field);
|
|
}
|
|
assert.throws(() => B.updateBookmark(s, 'bmk002', { nickname: 'ab' }), { code: 'nickname-short' });
|
|
assert.throws(() => B.updateBookmark(s, 'bmk002', { nickname: 'n'.repeat(31) }), { code: 'nickname-long' });
|
|
assert.throws(() => B.updateBookmark(s, 'bmk002', { nickname: 'bad\u0085x' }), { code: 'nickname' });
|
|
assert.throws(() => B.updateBookmark(s, 'bmk002', { channel: 'a\nb' }), { code: 'channel' });
|
|
assert.throws(() => B.updateBookmark(s, 'bmk002', { auto_connect: 'yes' }), { code: 'flag' });
|
|
assert.throws(() => B.updateBookmark(s, 'bmk002', { identity_id: 'nope' }), { code: 'identity' });
|
|
// Code points, not UTF-16 units: three astral characters are a valid nickname.
|
|
assert.equal(find(B.updateBookmark(s, 'bmk002', { nickname: '😀😀😀' }), 'bmk002').nickname, '😀😀😀');
|
|
});
|
|
|
|
test('the edit panel duplicate copies options and the identical ciphertext of the same address', () => {
|
|
const s = B.duplicate(withOptions(), 'bmk002', 'copy01', '');
|
|
assert.deepEqual({ ...find(s, 'copy01'), id: 'bmk002' }, find(s, 'bmk002'));
|
|
assert.ok(B.validServers(s.savedServers));
|
|
});
|
|
|
|
test('channel paths join parent names from the current snapshot and reject unknown or cyclic chains', () => {
|
|
const channels = [{ id: 1, parent_id: 0, name: 'Lobby' }, { id: 2, parent_id: 1, name: 'Sub' }, { id: 3, parent_id: 2, name: 'Deep' }, { id: 7, parent_id: 99, name: 'Lost' }, { id: 8, parent_id: 9, name: 'a' }, { id: 9, parent_id: 8, name: 'b' }];
|
|
assert.equal(B.channelPath(channels, 1), 'Lobby');
|
|
assert.equal(B.channelPath(channels, 3), 'Lobby/Sub/Deep');
|
|
assert.equal(B.channelPath(channels, 42), null);
|
|
assert.equal(B.channelPath(channels, 7), null);
|
|
assert.equal(B.channelPath(channels, 8), null);
|
|
assert.equal(B.channelPath([{ id: 1, parent_id: 0, name: 'x'.repeat(B.CHANNEL_MAX + 1) }], 1), null);
|
|
});
|