env.esq/src/parser/parser.test.ts

65 lines
2 KiB
TypeScript
Raw Normal View History

2020-08-10 13:35:15 +02:00
/* eslint-disable no-fallthrough */
2020-06-11 02:53:49 +02:00
import {
2020-08-10 13:35:15 +02:00
parseHash,
parsePermalink,
parseArgs,
verifiers,
identifyTypeFromRegex,
toURL,
2020-08-18 12:16:31 +02:00
} from './parser';
2020-08-06 15:49:33 +02:00
2020-08-18 12:16:31 +02:00
import { LinkKind } from './types';
2020-06-11 02:53:49 +02:00
2020-08-10 13:35:15 +02:00
const identifierType = (id: string): LinkKind =>
identifyTypeFromRegex(id, verifiers, LinkKind.ParseFailed);
2020-06-11 02:53:49 +02:00
2020-08-18 12:16:31 +02:00
it('types identifiers correctly', () => {
expect(identifierType('@user:matrix.org')).toEqual(LinkKind.UserId);
expect(identifierType('!room:matrix.org')).toEqual(LinkKind.RoomId);
expect(identifierType('!somewhere:example.org/$event:example.org')).toEqual(
2020-08-10 13:35:15 +02:00
LinkKind.Permalink
);
2020-08-18 12:16:31 +02:00
expect(identifierType('+group:matrix.org')).toEqual(LinkKind.GroupId);
expect(identifierType('#alias:matrix.org')).toEqual(LinkKind.Alias);
2020-06-11 02:53:49 +02:00
});
2020-08-18 12:16:31 +02:00
it('types garbage as such', () => {
expect(identifierType('sdfa;fdlkja')).toEqual(LinkKind.ParseFailed);
expect(identifierType('$event$matrix.org')).toEqual(LinkKind.ParseFailed);
expect(identifierType('/user:matrix.org')).toEqual(LinkKind.ParseFailed);
2020-08-06 15:49:33 +02:00
});
2020-08-18 12:16:31 +02:00
it('parses args correctly', () => {
2020-08-10 13:35:15 +02:00
expect(
2020-08-18 12:16:31 +02:00
parseArgs('via=example.org&via=alt.example.org')
).toHaveProperty('vias', ['example.org', 'alt.example.org']);
expect(parseArgs('sharer=blah')).toHaveProperty('sharer', 'blah');
expect(parseArgs('client=blah.com')).toHaveProperty('client', 'blah.com');
2020-06-11 02:53:49 +02:00
});
2020-08-18 12:16:31 +02:00
it('parses permalinks', () => {
expect(parsePermalink('!somewhere:example.org/$event:example.org')).toEqual(
2020-08-10 13:35:15 +02:00
{
roomKind: LinkKind.RoomId,
2020-08-18 12:16:31 +02:00
roomLink: '!somewhere:example.org',
eventId: '$event:example.org',
2020-08-10 13:35:15 +02:00
}
);
2020-06-11 02:53:49 +02:00
});
2020-08-18 12:16:31 +02:00
it('formats links correctly', () => {
2020-08-10 13:35:15 +02:00
const bigLink =
2020-08-18 12:16:31 +02:00
'!somewhere:example.org/$event:example.org?via=dfasdf&via=jfjafjaf';
const origin = 'https://matrix.org';
const prefix = origin + '/#/';
2020-08-10 13:35:15 +02:00
const parse = parseHash(bigLink);
2020-06-11 02:53:49 +02:00
2020-08-10 13:35:15 +02:00
switch (parse.kind) {
case LinkKind.ParseFailed:
2020-08-18 12:16:31 +02:00
fail('Parse failed');
2020-08-10 13:35:15 +02:00
default:
expect(toURL(origin, parse).toString()).toEqual(prefix + bigLink);
}
2020-06-11 02:53:49 +02:00
});