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