83 lines
4.5 KiB
JavaScript
83 lines
4.5 KiB
JavaScript
// node --test ide/ui/js/board-merge.test.mjs: several people on one jumphp.net board (board-merge.js): the three-way merge of a card
|
|
// field by field, the live events applied to the board, who is here and what they edit, the reconnection delays.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mergeCard, resolve, applyEvents, presence, same, backoff, clientId } from './board-merge.js';
|
|
|
|
const base = { id: 12, title: 'Menu', body: 'a', labels: [{ name: 'ui', color: '#f00' }], priority: 'low', due: null, version: 1 };
|
|
|
|
test('a field only I changed is sent, one only they changed stays theirs', () => {
|
|
const theirs = { ...base, body: 'b', version: 2 };
|
|
const m = mergeCard(base, { title: 'Main menu', body: 'a' }, theirs);
|
|
assert.deepEqual(m, { fields: { title: 'Main menu' }, conflicts: [] });
|
|
});
|
|
|
|
test('a field we both changed the same way is no conflict', () => {
|
|
const m = mergeCard(base, { title: 'Main menu' }, { ...base, title: 'Main menu', version: 2 });
|
|
assert.deepEqual(m.conflicts, []);
|
|
assert.deepEqual(m.fields, { title: 'Main menu' });
|
|
});
|
|
|
|
test('a field we both changed differently is a conflict the person resolves', () => {
|
|
const theirs = { ...base, title: 'Start screen', labels: [{ name: 'ux', color: '#0f0' }], version: 3 };
|
|
const m = mergeCard(base, { title: 'Main menu', labels: [{ name: 'ui', color: '#f00' }], priority: 'high' }, theirs);
|
|
assert.deepEqual(m.conflicts.map((c) => [c.field, c.mine, c.theirs]), [['title', 'Main menu', 'Start screen']]);
|
|
assert.deepEqual(m.fields, { priority: 'high' }, 'labels untouched by me: theirs stay');
|
|
assert.deepEqual(resolve(m, { title: 'theirs' }), { priority: 'high' });
|
|
assert.deepEqual(resolve(m, { title: 'mine' }), { priority: 'high', title: 'Main menu' });
|
|
assert.deepEqual(resolve(m, {}), { priority: 'high', title: 'Main menu' }, 'mine by default');
|
|
});
|
|
|
|
test('empty, null and missing are the same value; lists compare by value', () => {
|
|
assert.ok(same('', null));
|
|
assert.ok(same(undefined, null));
|
|
assert.ok(same([{ text: 'a', done: false }], [{ text: 'a', done: false }]));
|
|
assert.ok(!same([{ text: 'a', done: false }], [{ text: 'a', done: true }]));
|
|
assert.ok(!same(0, null));
|
|
});
|
|
|
|
test('live events replace cards by id, never with an older version, and delete them', () => {
|
|
const board = { cards: [{ id: '1', title: 'A', version: 3 }, { id: '2', title: 'B', version: 1 }], columns: [{ id: '9', name: 'Todo' }] };
|
|
const changed = applyEvents(board, [
|
|
{ type: 'card.saved', card: { id: '1', title: 'old', version: 2 } },
|
|
{ type: 'card.saved', card: { id: '2', title: 'B2', version: 2 } },
|
|
{ type: 'card.saved', card: { id: '3', title: 'C', version: 1 } },
|
|
{ type: 'card.deleted', id: '1' },
|
|
{ type: 'column.saved', column: { id: '9', name: 'To do' } },
|
|
{ type: 'note.saved', note: { id: '4' } },
|
|
{ type: 'unknown' },
|
|
]);
|
|
assert.deepEqual(board.cards.map((c) => [c.id, c.title]), [['2', 'B2'], ['3', 'C']]);
|
|
assert.equal(board.columns[0].name, 'To do');
|
|
assert.deepEqual([...changed.cards].sort(), ['1', '2', '3']);
|
|
assert.equal(changed.columns, true);
|
|
assert.equal(changed.notes.length, 1);
|
|
});
|
|
|
|
test('ids compare as the board keeps them (numbers on the site, strings in the IDE)', () => {
|
|
const board = { cards: [{ id: 5, title: 'x', version: 1 }], columns: [] };
|
|
applyEvents(board, [{ type: 'card.saved', card: { id: 5, title: 'y', version: 2 } }], Number);
|
|
assert.deepEqual(board.cards, [{ id: 5, title: 'y', version: 2 }]);
|
|
});
|
|
|
|
test('presence: one avatar per person, the IDE noted, my own window left out of "editing"', () => {
|
|
const p = presence([
|
|
{ login: 'ada', name: 'Ada', client: 'w1', editing: 'card:12', via: 'web' },
|
|
{ login: 'ada', name: 'Ada', client: 'w2', editing: null, via: 'ide' },
|
|
{ login: 'me', client: 'mine', editing: 'card:12', via: 'ide' },
|
|
{ login: 'bob', client: 'w3', editing: 'card:12', via: 'web' },
|
|
], 'mine');
|
|
assert.deepEqual(p.people.map((x) => [x.login, [...x.via].sort()]), [['ada', ['ide', 'web']], ['me', ['ide']], ['bob', ['web']]]);
|
|
assert.deepEqual(p.editing.get('card:12').map((w) => w.login), ['ada', 'bob']);
|
|
});
|
|
|
|
test('reconnections wait longer and longer, at most about 30 seconds', () => {
|
|
assert.deepEqual([0, 1, 2, 3, 4, 5, 9].map((n) => backoff(n, () => 0.5)), [1, 2, 4, 8, 15, 30, 30]);
|
|
assert.ok(backoff(0, () => 0) >= 0.8 && backoff(0, () => 1) <= 1.2 + 1e-9);
|
|
});
|
|
|
|
test('a window id is random hex', () => {
|
|
const a = clientId();
|
|
assert.match(a, /^[0-9a-f]{24}$/);
|
|
assert.notEqual(a, clientId());
|
|
});
|