Files
cinny/src/app/plugins/matrix-to.test.ts
T

78 lines
2.9 KiB
TypeScript
Raw Normal View History

import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
getMatrixToUser,
getMatrixToRoom,
getMatrixToRoomEvent,
testMatrixTo,
parseMatrixToUser,
parseMatrixToRoom,
parseMatrixToRoomEvent,
} from './matrix-to';
test('getMatrixToUser builds a user permalink', () => {
assert.equal(getMatrixToUser('@alice:example.org'), 'https://matrix.to/#/@alice:example.org');
});
test('getMatrixToRoom builds room links with optional via servers', () => {
assert.equal(getMatrixToRoom('#room:example.org'), 'https://matrix.to/#/#room:example.org');
assert.equal(
getMatrixToRoom('!abc:example.org', ['a.org', 'b.org']),
'https://matrix.to/#/!abc:example.org?via=a.org&via=b.org',
);
// empty via array → no query string
assert.equal(getMatrixToRoom('!abc:example.org', []), 'https://matrix.to/#/!abc:example.org');
});
test('getMatrixToRoomEvent builds event links', () => {
assert.equal(
getMatrixToRoomEvent('!abc:example.org', '$evt', ['a.org']),
'https://matrix.to/#/!abc:example.org/$evt?via=a.org',
);
assert.equal(
getMatrixToRoomEvent('!abc:example.org', '$evt'),
'https://matrix.to/#/!abc:example.org/$evt',
);
});
test('testMatrixTo recognizes matrix.to hrefs', () => {
assert.equal(testMatrixTo('https://matrix.to/#/@a:b.org'), true);
assert.equal(testMatrixTo('http://matrix.to/#/!r:b.org'), true);
assert.equal(testMatrixTo('https://example.org/#/@a:b.org'), false);
});
test('parseMatrixToUser round-trips and rejects non-user links', () => {
assert.equal(parseMatrixToUser(getMatrixToUser('@a:b.org')), '@a:b.org');
assert.equal(parseMatrixToUser('https://matrix.to/#/@a:b.org/'), '@a:b.org'); // trailing slash ok
assert.equal(parseMatrixToUser('https://matrix.to/#/#room:b.org'), undefined);
assert.equal(parseMatrixToUser('https://example.org'), undefined);
});
test('parseMatrixToRoom extracts alias/id and via servers', () => {
assert.deepEqual(parseMatrixToRoom('https://matrix.to/#/#room:b.org'), {
roomIdOrAlias: '#room:b.org',
viaServers: undefined,
});
assert.deepEqual(parseMatrixToRoom(getMatrixToRoom('!abc:b.org', ['a.org', 'c.org'])), {
roomIdOrAlias: '!abc:b.org',
viaServers: ['a.org', 'c.org'],
});
// a user link is not a room link
assert.equal(parseMatrixToRoom('https://matrix.to/#/@a:b.org'), undefined);
});
test('parseMatrixToRoomEvent extracts room, event, and via servers', () => {
assert.deepEqual(parseMatrixToRoomEvent(getMatrixToRoomEvent('!abc:b.org', '$e', ['a.org'])), {
roomIdOrAlias: '!abc:b.org',
eventId: '$e',
viaServers: ['a.org'],
});
assert.deepEqual(parseMatrixToRoomEvent('https://matrix.to/#/!abc:b.org/$e'), {
roomIdOrAlias: '!abc:b.org',
eventId: '$e',
viaServers: undefined,
});
// a room-only link has no event
assert.equal(parseMatrixToRoomEvent('https://matrix.to/#/!abc:b.org'), undefined);
});