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

79 lines
2.2 KiB
TypeScript
Raw Normal View History

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