2021-11-26 18:39:07 +08:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2021-12-03 22:00:56 +08:00
|
|
|
import { linkify } from '../src/linkify-matrix';
|
2021-11-26 18:39:07 +08:00
|
|
|
|
|
|
|
describe('linkify-matrix', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const linkTypesByInitialCharacter = {
|
|
|
|
'#': 'roomalias',
|
|
|
|
'@': 'userid',
|
|
|
|
'+': 'groupid',
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param testName Due to all the tests using the same logic underneath, it makes to generate it in a bit smarter way
|
|
|
|
* @param char
|
|
|
|
*/
|
|
|
|
function genTests(char: '#' | '@' | '+') {
|
|
|
|
const type = linkTypesByInitialCharacter[char];
|
|
|
|
it('should not parse ' + char + 'foo without domain', () => {
|
|
|
|
const test = char + "foo";
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([]));
|
|
|
|
});
|
|
|
|
describe('ip v4 tests', () => {
|
|
|
|
it('should properly parse IPs v4 as the domain name', () => {
|
|
|
|
const test = char + 'potato:1.2.3.4';
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
|
|
|
href: char + 'potato:1.2.3.4',
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + 'potato:1.2.3.4',
|
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('should properly parse IPs v4 with port as the domain name with attached', () => {
|
|
|
|
const test = char + 'potato:1.2.3.4:1337';
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
|
|
|
href: char + 'potato:1.2.3.4:1337',
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + 'potato:1.2.3.4:1337',
|
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('should properly parse IPs v4 as the domain name while ignoring missing port', () => {
|
|
|
|
const test = char + 'potato:1.2.3.4:';
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
|
|
|
href: char + 'potato:1.2.3.4',
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length - 1,
|
|
|
|
value: char + 'potato:1.2.3.4',
|
|
|
|
}]));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// Currently those tests are failing, as there's missing implementation.
|
|
|
|
describe.skip('ip v6 tests', () => {
|
|
|
|
it('should properly parse IPs v6 as the domain name', () => {
|
|
|
|
const test = char + "username:[1234:5678::abcd]";
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual([{
|
|
|
|
href: char + 'username:[1234:5678::abcd]',
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + 'username:[1234:5678::abcd]',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should properly parse IPs v6 with port as the domain name', () => {
|
|
|
|
const test = char + "username:[1234:5678::abcd]:1337";
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual([{
|
|
|
|
href: char + 'username:[1234:5678::abcd]:1337',
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + 'username:[1234:5678::abcd]:1337',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
it('should properly parse IPs v6 while ignoring dangling comma when without port name as the domain name', () => {
|
|
|
|
const test = char + "username:[1234:5678::abcd]:";
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual([{
|
|
|
|
href: char + 'username:[1234:5678::abcd]:',
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length - 1,
|
|
|
|
value: char + 'username:[1234:5678::abcd]:',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('properly parses ' + char + '_foonetic_xkcd:matrix.org', () => {
|
|
|
|
const test = '' + char + '_foonetic_xkcd:matrix.org';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "_foonetic_xkcd:matrix.org",
|
|
|
|
type,
|
|
|
|
value: char + "_foonetic_xkcd:matrix.org",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
2022-01-19 01:24:16 +08:00
|
|
|
it('properly parses ' + char + 'foo:localhost', () => {
|
|
|
|
const test = char + "foo:localhost";
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo:localhost",
|
|
|
|
type,
|
|
|
|
value: char + "foo:localhost",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
2022-01-19 01:24:16 +08:00
|
|
|
it('accept ' + char + 'foo:bar.com', () => {
|
|
|
|
const test = '' + char + 'foo:bar.com';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo:bar.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
2022-01-19 01:24:16 +08:00
|
|
|
it('accept ' + char + 'foo:com (mostly for (TLD|DOMAIN)+ mixing)', () => {
|
|
|
|
const test = '' + char + 'foo:com';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo:com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('accept repeated TLDs (e.g .org.uk)', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const test = '' + char + 'foo:bar.org.uk';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo:bar.org.uk",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.org.uk",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('ignores trailing `:`', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const test = '' + char + 'foo:bar.com:';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com",
|
|
|
|
href: char + 'foo:bar.com',
|
|
|
|
start: 0,
|
|
|
|
end: test.length - ":".length,
|
|
|
|
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('accept :NUM (port specifier)', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const test = '' + char + 'foo:bar.com:2225';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo:bar.com:2225",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com:2225",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('ignores all the trailing :', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const test = '' + char + 'foo:bar.com::::';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo:bar.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com",
|
|
|
|
end: test.length - 4,
|
|
|
|
start: 0,
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('properly parses room alias with dots in name', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const test = '' + char + 'foo.asdf:bar.com::::';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo.asdf:bar.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo.asdf:bar.com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length - ":".repeat(4).length,
|
|
|
|
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('does not parse room alias with too many separators', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const test = '' + char + 'foo:::bar.com';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
|
|
|
href: "http://bar.com",
|
|
|
|
type: "url",
|
|
|
|
value: "bar.com",
|
2022-01-19 01:24:16 +08:00
|
|
|
isLink: true,
|
|
|
|
start: 7,
|
|
|
|
end: test.length,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
|
|
|
it('does not parse multiple room aliases in one string', () => {
|
2022-01-19 01:24:16 +08:00
|
|
|
const test = '' + char + 'foo:bar.com-baz.com';
|
2021-11-26 18:39:07 +08:00
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual(([{
|
2022-01-19 01:24:16 +08:00
|
|
|
href: char + "foo:bar.com-baz.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com-baz.com",
|
|
|
|
end: 20,
|
|
|
|
start: 0,
|
|
|
|
isLink: true,
|
2021-11-26 18:39:07 +08:00
|
|
|
}]));
|
|
|
|
});
|
2022-01-19 01:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('roomalias plugin', () => {
|
|
|
|
genTests('#');
|
2021-11-26 18:39:07 +08:00
|
|
|
});
|
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
describe('groupid plugin', () => {
|
|
|
|
genTests('+');
|
2021-11-26 18:39:07 +08:00
|
|
|
});
|
|
|
|
|
2022-01-19 01:24:16 +08:00
|
|
|
describe('userid plugin', () => {
|
|
|
|
genTests('@');
|
2021-11-26 18:39:07 +08:00
|
|
|
});
|
|
|
|
});
|