92 lines
3.4 KiB
TypeScript
92 lines
3.4 KiB
TypeScript
import { test } from 'node:test';
|
|||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import {
|
||
|
|
getLotusRoomPermalink,
|
||
|
|
getLotusSpacePermalink,
|
||
|
|
lotusPermalinkToMatrixTo,
|
||
|
|
parseLotusPermalink,
|
||
|
|
} from './lotus-permalink';
|
||
|
|
|
||
|
|
const BASE = 'https://chat.lotusguild.org/';
|
||
|
|
|
||
|
|
test('builds room / event / space permalinks with encoded ids and viaServers', () => {
|
||
|
|
assert.equal(
|
||
|
|
getLotusRoomPermalink(BASE, '!abc:matrix.lotusguild.org', '$ev1', ['matrix.lotusguild.org']),
|
||
|
|
'https://chat.lotusguild.org/home/!abc%3Amatrix.lotusguild.org/%24ev1?viaServers=matrix.lotusguild.org',
|
||
|
|
);
|
||
|
|
assert.equal(
|
||
|
|
getLotusRoomPermalink(BASE, '#general:matrix.lotusguild.org'),
|
||
|
|
'https://chat.lotusguild.org/home/%23general%3Amatrix.lotusguild.org',
|
||
|
|
);
|
||
|
|
assert.equal(
|
||
|
|
getLotusSpacePermalink('https://chat.lotusguild.org', '#guild:matrix.lotusguild.org'),
|
||
|
|
'https://chat.lotusguild.org/%23guild%3Amatrix.lotusguild.org',
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('parses its own output back (encoded and decoded forms)', () => {
|
||
|
|
const link = getLotusRoomPermalink(BASE, '!abc:lotus', '$ev1', ['a.org', 'b.org']);
|
||
|
|
assert.deepEqual(parseLotusPermalink(BASE, link), {
|
||
|
|
roomIdOrAlias: '!abc:lotus',
|
||
|
|
eventId: '$ev1',
|
||
|
|
viaServers: ['a.org', 'b.org'],
|
||
|
|
});
|
||
|
|
assert.deepEqual(parseLotusPermalink(BASE, 'https://chat.lotusguild.org/home/!abc:lotus/$ev1'), {
|
||
|
|
roomIdOrAlias: '!abc:lotus',
|
||
|
|
eventId: '$ev1',
|
||
|
|
viaServers: undefined,
|
||
|
|
});
|
||
|
|
assert.deepEqual(parseLotusPermalink(BASE, 'https://chat.lotusguild.org/direct/!dm:lotus/'), {
|
||
|
|
roomIdOrAlias: '!dm:lotus',
|
||
|
|
viaServers: undefined,
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
test('space routes: lobby → the space, nested room → the room', () => {
|
||
|
|
assert.deepEqual(parseLotusPermalink(BASE, 'https://chat.lotusguild.org/%23guild%3Alotus/'), {
|
||
|
|
roomIdOrAlias: '#guild:lotus',
|
||
|
|
viaServers: undefined,
|
||
|
|
});
|
||
|
|
assert.deepEqual(
|
||
|
|
parseLotusPermalink(BASE, 'https://chat.lotusguild.org/%23guild%3Alotus/!room%3Alotus/%24e'),
|
||
|
|
{ roomIdOrAlias: '!room:lotus', eventId: '$e', viaServers: undefined },
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('accepts the matrix.to Cinny adapter form (?via=a,b)', () => {
|
||
|
|
assert.deepEqual(
|
||
|
|
parseLotusPermalink(BASE, 'https://chat.lotusguild.org/home/!r:x?via=a.org,b.org'),
|
||
|
|
{
|
||
|
|
roomIdOrAlias: '!r:x',
|
||
|
|
viaServers: ['a.org', 'b.org'],
|
||
|
|
},
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('rejects other origins, non-room routes and malformed ids', () => {
|
||
|
|
assert.equal(parseLotusPermalink(BASE, 'https://app.cinny.in/home/!r:x'), undefined);
|
||
|
|
assert.equal(parseLotusPermalink(BASE, 'https://chat.lotusguild.org/home/settings'), undefined);
|
||
|
|
assert.equal(parseLotusPermalink(BASE, 'https://chat.lotusguild.org/explore/lotus'), undefined);
|
||
|
|
assert.equal(
|
||
|
|
parseLotusPermalink(BASE, 'https://chat.lotusguild.org/home/!r:x/notanevent'),
|
||
|
|
undefined,
|
||
|
|
);
|
||
|
|
assert.equal(
|
||
|
|
parseLotusPermalink(BASE, 'https://chat.lotusguild.org.evil.com/home/!r:x'),
|
||
|
|
undefined,
|
||
|
|
);
|
||
|
|
assert.equal(parseLotusPermalink(BASE, 'https://chat.lotusguild.org'), undefined);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('rewrites to the matrix.to form the existing mention pipeline understands', () => {
|
||
|
|
assert.equal(
|
||
|
|
lotusPermalinkToMatrixTo(BASE, 'https://chat.lotusguild.org/home/!r:x/$e?viaServers=a.org'),
|
||
|
|
'https://matrix.to/#/!r:x/$e?via=a.org',
|
||
|
|
);
|
||
|
|
assert.equal(
|
||
|
|
lotusPermalinkToMatrixTo(BASE, 'https://chat.lotusguild.org/%23guild%3Ax/'),
|
||
|
|
'https://matrix.to/#/#guild:x',
|
||
|
|
);
|
||
|
|
assert.equal(lotusPermalinkToMatrixTo(BASE, 'https://example.org/home/!r:x'), undefined);
|
||
|
|
});
|