atagen

malice

public
Star0
Watch0
43c2cc753052f1810753e9a47ed12f9b68ced64d
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/test/rail-order.test.ts

7cca1d5628d1atagen2026-10-24 07:36 UTC1
import assert from 'node:assert/strict';
7cca1d5628d1atagen2026-10-24 07:36 UTC2
import { test } from 'node:test';
7cca1d5628d1atagen2026-10-24 07:36 UTC3
import { moveBefore, pinnedFirst, sortSpaces } from '../src/lib/rail-order.ts';
7cca1d5628d1atagen2026-10-24 07:36 UTC4
7cca1d5628d1atagen2026-10-24 07:36 UTC5
const space = (room_id: string, space_order: string | null) => ({ room_id, space_order });
7cca1d5628d1atagen2026-10-24 07:36 UTC6
7cca1d5628d1atagen2026-10-24 07:36 UTC7
test('orders spaces the MSC3230 way', () => {
7cca1d5628d1atagen2026-10-24 07:36 UTC8
  const spaces = [space('!d', null), space('!c', 'b'), space('!b', null), space('!a', 'a'), space('!e', 'b')];
7cca1d5628d1atagen2026-10-24 07:36 UTC9
  assert.deepEqual(sortSpaces(spaces, null).map((s) => s.room_id), ['!a', '!c', '!e', '!b', '!d']);
7cca1d5628d1atagen2026-10-24 07:36 UTC10
});
7cca1d5628d1atagen2026-10-24 07:36 UTC11
7cca1d5628d1atagen2026-10-24 07:36 UTC12
test('a local override wins until the server catches up', () => {
7cca1d5628d1atagen2026-10-24 07:36 UTC13
  const spaces = [space('!a', 'a'), space('!b', 'b'), space('!c', null)];
7cca1d5628d1atagen2026-10-24 07:36 UTC14
  assert.deepEqual(sortSpaces(spaces, ['!c', '!a', '!b']).map((s) => s.room_id), ['!c', '!a', '!b']);
7cca1d5628d1atagen2026-10-24 07:36 UTC15
});
7cca1d5628d1atagen2026-10-24 07:36 UTC16
7cca1d5628d1atagen2026-10-24 07:36 UTC17
test('moves an id before another or to the end', () => {
7cca1d5628d1atagen2026-10-24 07:36 UTC18
  assert.deepEqual(moveBefore(['a', 'b', 'c', 'd'], 'd', 'b'), ['a', 'd', 'b', 'c']);
7cca1d5628d1atagen2026-10-24 07:36 UTC19
  assert.deepEqual(moveBefore(['a', 'b', 'c'], 'a', null), ['b', 'c', 'a']);
7cca1d5628d1atagen2026-10-24 07:36 UTC20
  assert.deepEqual(moveBefore(['a', 'b'], 'a', 'zz'), ['a', 'b']);
7cca1d5628d1atagen2026-10-24 07:36 UTC21
});
7cca1d5628d1atagen2026-10-24 07:36 UTC22
7cca1d5628d1atagen2026-10-24 07:36 UTC23
test('pinned rooms come first and keep their relative order', () => {
7cca1d5628d1atagen2026-10-24 07:36 UTC24
  const rooms = [
7cca1d5628d1atagen2026-10-24 07:36 UTC25
    { id: 1, favourite: false },
7cca1d5628d1atagen2026-10-24 07:36 UTC26
    { id: 2, favourite: true },
7cca1d5628d1atagen2026-10-24 07:36 UTC27
    { id: 3, favourite: false },
7cca1d5628d1atagen2026-10-24 07:36 UTC28
    { id: 4, favourite: true }
7cca1d5628d1atagen2026-10-24 07:36 UTC29
  ];
7cca1d5628d1atagen2026-10-24 07:36 UTC30
  assert.deepEqual(pinnedFirst(rooms).map((r) => r.id), [2, 4, 1, 3]);
7cca1d5628d1atagen2026-10-24 07:36 UTC31
});