thelounge/test/tests/customhighlights.ts

151 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

import {expect} from "chai";
import log from "../../server/log";
import Client from "../../server/client";
import TestUtil from "../util";
import sinon from "ts-sinon";
2019-02-19 15:21:54 +01:00
describe("Custom highlights", function () {
2019-10-23 11:42:01 +02:00
let userLoadedLog = "";
const logInfoStub = sinon.stub(log, "info");
logInfoStub.callsFake(TestUtil.sanitizeLog((str) => (userLoadedLog += str)));
2019-10-23 11:42:01 +02:00
const client = new Client(
{
clients: [],
getDataToSave() {
return {
newUser: "",
newHash: "",
};
},
} as any,
"test",
{
2020-07-22 17:28:12 +02:00
clientSettings: {
highlights: "foo, @all, sp ace , 고",
highlightExceptions: "foo bar, bar @all, test sp ace test",
},
} as any
);
client.connect();
logInfoStub.restore();
2019-10-23 11:42:01 +02:00
expect(userLoadedLog).to.equal("User test loaded\n");
it("should NOT highlight", function () {
2019-02-19 15:21:54 +01:00
const teststrings = [
"and foos stuff",
"test foobar",
"testfoo bar",
"fooö",
"wtf@all",
"foo고",
"test고",
"space",
"sp:ace",
];
for (const teststring of teststrings) {
expect(teststring).to.not.match(client.highlightRegex!);
2019-02-19 15:21:54 +01:00
}
});
it("should highlight", function () {
2019-02-19 15:21:54 +01:00
const teststrings = [
"Hey foo hello",
"hey Foo: hi",
"hey Foo, hi",
"<foo> testing",
"foo",
"hey @all test",
"testing foo's stuff",
2019-07-17 11:33:59 +02:00
'"foo"',
'"@all"',
2019-02-19 15:21:54 +01:00
"foo!",
"www.foo.bar",
"www.bar.foo/page",
"고",
"test 고",
"고!",
"www.고.com",
"hey @Foo",
"hey ~Foo",
"hey +Foo",
"hello &foo",
"@all",
"@all wtf",
"wtf @all",
"@@all",
"@고",
"f00 sp ace: bar",
];
for (const teststring of teststrings) {
expect(teststring).to.match(client.highlightRegex!);
2019-02-19 15:21:54 +01:00
}
});
it("should trim custom highlights in the compiled regex", function () {
2019-02-19 15:21:54 +01:00
expect(client.highlightRegex).to.match(/\(\?:foo\|@all\|sp ace\|고\)/);
});
it("should NOT compile a regex", function () {
2019-02-19 15:21:54 +01:00
// test updating the regex and invalid custom hl inputs
client.config.clientSettings.highlights = ",,";
client.compileCustomHighlights();
expect(client.highlightRegex).to.be.null;
client.config.clientSettings.highlights = " ";
client.compileCustomHighlights();
expect(client.highlightRegex).to.be.null;
});
2020-07-22 17:28:12 +02:00
// tests for highlight exceptions
it("should NOT highlight due to highlight exceptions", function () {
const teststrings = [
"foo bar baz",
"test foo bar",
"foo bar @all test",
"with a test sp ace test",
];
for (const teststring of teststrings) {
expect(teststring).to.match(client.highlightExceptionRegex!);
2020-07-22 17:28:12 +02:00
}
});
it("should highlight regardless of highlight exceptions", function () {
const teststrings = [
"Hey foo hello",
"hey Foo: hi",
"hey Foo, hi",
"<foo> testing",
"foo",
"hey @all test",
"testing foo's stuff",
'"foo"',
'"@all"',
"foo!",
"www.foo.bar",
"www.bar.foo/page",
"고",
"test 고",
"고!",
"www.고.com",
"hey @Foo",
"hey ~Foo",
"hey +Foo",
"hello &foo",
"@all",
"@all wtf",
"wtfbar @all",
"@@all",
"@고",
"f00 sp ace: bar",
];
for (const teststring of teststrings) {
expect(teststring).to.not.match(client.highlightExceptionRegex!);
2020-07-22 17:28:12 +02:00
}
});
2019-02-19 15:21:54 +01:00
});