7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 1 | import assert from 'node:assert/strict'; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 2 | import { test } from 'node:test'; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 3 | import { moveBefore, pinnedFirst, sortSpaces } from '../src/lib/rail-order.ts'; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 4 | |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 5 | const space = (room_id: string, space_order: string | null) => ({ room_id, space_order }); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 6 | |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 7 | test('orders spaces the MSC3230 way', () => { |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 8 | const spaces = [space('!d', null), space('!c', 'b'), space('!b', null), space('!a', 'a'), space('!e', 'b')]; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 9 | assert.deepEqual(sortSpaces(spaces, null).map((s) => s.room_id), ['!a', '!c', '!e', '!b', '!d']); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 10 | }); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 11 | |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 12 | test('a local override wins until the server catches up', () => { |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 13 | const spaces = [space('!a', 'a'), space('!b', 'b'), space('!c', null)]; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 14 | assert.deepEqual(sortSpaces(spaces, ['!c', '!a', '!b']).map((s) => s.room_id), ['!c', '!a', '!b']); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 15 | }); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 16 | |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 17 | test('moves an id before another or to the end', () => { |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 18 | assert.deepEqual(moveBefore(['a', 'b', 'c', 'd'], 'd', 'b'), ['a', 'd', 'b', 'c']); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 19 | assert.deepEqual(moveBefore(['a', 'b', 'c'], 'a', null), ['b', 'c', 'a']); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 20 | assert.deepEqual(moveBefore(['a', 'b'], 'a', 'zz'), ['a', 'b']); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 21 | }); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 22 | |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 23 | test('pinned rooms come first and keep their relative order', () => { |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 24 | const rooms = [ |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 25 | { id: 1, favourite: false }, |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 26 | { id: 2, favourite: true }, |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 27 | { id: 3, favourite: false }, |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 28 | { id: 4, favourite: true } |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 29 | ]; |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 30 | assert.deepEqual(pinnedFirst(rooms).map((r) => r.id), [2, 4, 1, 3]); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 31 | }); |