2022-06-19 00:25:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import {expect} from "chai";
|
|
|
|
import util from "../util";
|
|
|
|
import Msg, {MessageType} from "../../server/models/msg";
|
|
|
|
import Config from "../../server/config";
|
|
|
|
import MessageStorage from "../../server/plugins/messageStorage/sqlite";
|
|
|
|
import Client from "../../server/client";
|
2018-03-02 08:42:12 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("SQLite Message Storage", function () {
|
2019-11-27 18:25:29 +00:00
|
|
|
// Increase timeout due to unpredictable I/O on CI services
|
2020-02-09 12:21:45 +00:00
|
|
|
this.timeout(util.isRunningOnCI() ? 25000 : 5000);
|
|
|
|
this.slow(300);
|
2019-11-27 18:25:29 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
const expectedPath = path.join(Config.getHomePath(), "logs", "testUser.sqlite3");
|
2022-06-19 00:25:21 +00:00
|
|
|
let store: MessageStorage;
|
2018-03-02 08:42:12 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
before(function (done) {
|
2018-04-27 10:16:23 +00:00
|
|
|
store = new MessageStorage({
|
2018-04-17 08:06:08 +00:00
|
|
|
name: "testUser",
|
2018-04-27 10:16:23 +00:00
|
|
|
idMsg: 1,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as Client);
|
2018-03-02 08:42:12 +00:00
|
|
|
|
2020-03-16 12:40:21 +00:00
|
|
|
// Delete database file from previous test run
|
2018-03-02 08:42:12 +00:00
|
|
|
if (fs.existsSync(expectedPath)) {
|
|
|
|
fs.unlink(expectedPath, done);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-16 12:40:21 +00:00
|
|
|
after(function (done) {
|
|
|
|
// After tests run, remove the logs folder
|
|
|
|
// so we return to the clean state
|
|
|
|
fs.unlinkSync(expectedPath);
|
2022-05-01 19:12:39 +00:00
|
|
|
fs.rmdir(path.join(Config.getHomePath(), "logs"), done);
|
2020-03-16 12:40:21 +00:00
|
|
|
});
|
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
it("should resolve an empty array when disabled", function () {
|
2022-06-19 00:25:21 +00:00
|
|
|
return store.getMessages(null as any, null as any).then((messages) => {
|
2018-03-02 08:42:12 +00:00
|
|
|
expect(messages).to.be.empty;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should create database file", function () {
|
2018-03-02 08:42:12 +00:00
|
|
|
expect(store.isEnabled).to.be.false;
|
|
|
|
expect(fs.existsSync(expectedPath)).to.be.false;
|
|
|
|
|
2018-04-17 08:06:08 +00:00
|
|
|
store.enable();
|
2018-03-02 08:42:12 +00:00
|
|
|
|
|
|
|
expect(store.isEnabled).to.be.true;
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should create tables", function (done) {
|
2021-10-28 07:48:11 +00:00
|
|
|
store.database.all(
|
|
|
|
"SELECT name, tbl_name, sql FROM sqlite_master WHERE type = 'table'",
|
|
|
|
(err, row) => {
|
|
|
|
expect(err).to.be.null;
|
|
|
|
expect(row).to.deep.equal([
|
|
|
|
{
|
|
|
|
name: "options",
|
|
|
|
tbl_name: "options",
|
2022-02-09 23:27:34 +00:00
|
|
|
sql: "CREATE TABLE options (name TEXT, value TEXT, CONSTRAINT name_unique UNIQUE (name))",
|
2021-10-28 07:48:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "messages",
|
|
|
|
tbl_name: "messages",
|
2022-02-09 23:27:34 +00:00
|
|
|
sql: "CREATE TABLE messages (network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
|
2021-10-28 07:48:11 +00:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
2018-03-02 08:42:12 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should insert schema version to options table", function (done) {
|
2021-10-28 07:48:11 +00:00
|
|
|
store.database.get(
|
|
|
|
"SELECT value FROM options WHERE name = 'schema_version'",
|
|
|
|
(err, row) => {
|
|
|
|
expect(err).to.be.null;
|
2019-07-17 09:33:59 +00:00
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
// Should be sqlite.currentSchemaVersion,
|
|
|
|
// compared as string because it's returned as such from the database
|
|
|
|
expect(row.value).to.equal("1520239200");
|
2019-07-17 09:33:59 +00:00
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should store a message", function () {
|
|
|
|
store.index(
|
|
|
|
{
|
|
|
|
uuid: "this-is-a-network-guid",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2021-10-28 07:48:11 +00:00
|
|
|
{
|
|
|
|
name: "#thisISaCHANNEL",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2021-10-28 07:48:11 +00:00
|
|
|
new Msg({
|
|
|
|
time: 123456789,
|
|
|
|
text: "Hello from sqlite world!",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any)
|
2018-03-02 08:42:12 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
it("should retrieve previously stored message", function () {
|
|
|
|
return store
|
|
|
|
.getMessages(
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
uuid: "this-is-a-network-guid",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any,
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
2021-10-28 07:48:11 +00:00
|
|
|
name: "#thisisaCHANNEL",
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any
|
2021-10-28 07:48:11 +00:00
|
|
|
)
|
|
|
|
.then((messages) => {
|
|
|
|
expect(messages).to.have.lengthOf(1);
|
2018-03-02 08:42:12 +00:00
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
const msg = messages[0];
|
|
|
|
|
|
|
|
expect(msg.text).to.equal("Hello from sqlite world!");
|
2022-06-19 00:25:21 +00:00
|
|
|
expect(msg.type).to.equal(MessageType.MESSAGE);
|
2021-10-28 07:48:11 +00:00
|
|
|
expect(msg.time.getTime()).to.equal(123456789);
|
|
|
|
});
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
it("should retrieve latest LIMIT messages in order", function () {
|
2022-05-01 19:12:39 +00:00
|
|
|
const originalMaxHistory = Config.values.maxHistory;
|
2021-10-28 07:48:11 +00:00
|
|
|
|
|
|
|
try {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.maxHistory = 2;
|
2021-10-28 07:48:11 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < 200; ++i) {
|
|
|
|
store.index(
|
2022-06-19 00:25:21 +00:00
|
|
|
{uuid: "retrieval-order-test-network"} as any,
|
|
|
|
{name: "#channel"} as any,
|
2021-10-28 07:48:11 +00:00
|
|
|
new Msg({
|
|
|
|
time: 123456789 + i,
|
|
|
|
text: `msg ${i}`,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any)
|
2021-10-28 07:48:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return store
|
2022-06-19 00:25:21 +00:00
|
|
|
.getMessages(
|
|
|
|
{uuid: "retrieval-order-test-network"} as any,
|
|
|
|
{name: "#channel"} as any
|
|
|
|
)
|
2019-07-17 09:33:59 +00:00
|
|
|
.then((messages) => {
|
2021-10-28 07:48:11 +00:00
|
|
|
expect(messages).to.have.lengthOf(2);
|
|
|
|
expect(messages.map((i) => i.text)).to.deep.equal(["msg 198", "msg 199"]);
|
|
|
|
});
|
|
|
|
} finally {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.maxHistory = originalMaxHistory;
|
2021-10-28 07:48:11 +00:00
|
|
|
}
|
|
|
|
});
|
2019-07-17 09:33:59 +00:00
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
it("should search messages", function () {
|
2022-05-01 19:12:39 +00:00
|
|
|
const originalMaxHistory = Config.values.maxHistory;
|
2019-07-17 09:33:59 +00:00
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
try {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.maxHistory = 2;
|
2019-07-17 09:33:59 +00:00
|
|
|
|
2021-10-28 07:48:11 +00:00
|
|
|
return store
|
|
|
|
.search({
|
|
|
|
searchTerm: "msg",
|
|
|
|
networkUuid: "retrieval-order-test-network",
|
2022-11-12 21:50:56 +00:00
|
|
|
channelName: "",
|
|
|
|
offset: 0,
|
|
|
|
})
|
2021-10-28 07:48:11 +00:00
|
|
|
.then((messages) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
// @ts-expect-error Property 'results' does not exist on type '[]'.
|
2021-10-28 07:48:11 +00:00
|
|
|
expect(messages.results).to.have.lengthOf(100);
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const expectedMessages: string[] = [];
|
2021-10-28 07:48:11 +00:00
|
|
|
|
|
|
|
for (let i = 100; i < 200; ++i) {
|
|
|
|
expectedMessages.push(`msg ${i}`);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
// @ts-expect-error Property 'results' does not exist on type '[]'.
|
2021-10-28 07:48:11 +00:00
|
|
|
expect(messages.results.map((i) => i.text)).to.deep.equal(expectedMessages);
|
|
|
|
});
|
|
|
|
} finally {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.maxHistory = originalMaxHistory;
|
2021-10-28 07:48:11 +00:00
|
|
|
}
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|
2018-03-10 16:49:16 +00:00
|
|
|
|
2022-04-12 00:49:13 +00:00
|
|
|
it("should search messages with escaped wildcards", function () {
|
|
|
|
function assertResults(query, expected) {
|
|
|
|
return store
|
|
|
|
.search({
|
|
|
|
searchTerm: query,
|
|
|
|
networkUuid: "this-is-a-network-guid2",
|
2022-11-12 21:50:56 +00:00
|
|
|
channelName: "",
|
|
|
|
offset: 0,
|
|
|
|
})
|
2022-04-12 00:49:13 +00:00
|
|
|
.then((messages) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
// @ts-expect-error Property 'results' does not exist on type '[]'.
|
2022-04-12 00:49:13 +00:00
|
|
|
expect(messages.results.map((i) => i.text)).to.deep.equal(expected);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
const originalMaxHistory = Config.values.maxHistory;
|
2022-04-12 00:49:13 +00:00
|
|
|
|
|
|
|
try {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.maxHistory = 3;
|
2022-04-12 00:49:13 +00:00
|
|
|
|
|
|
|
store.index(
|
2022-06-19 00:25:21 +00:00
|
|
|
{uuid: "this-is-a-network-guid2"} as any,
|
|
|
|
{name: "#channel"} as any,
|
2022-04-12 00:49:13 +00:00
|
|
|
new Msg({
|
|
|
|
time: 123456790,
|
|
|
|
text: `foo % bar _ baz`,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any)
|
2022-04-12 00:49:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
store.index(
|
2022-06-19 00:25:21 +00:00
|
|
|
{uuid: "this-is-a-network-guid2"} as any,
|
|
|
|
{name: "#channel"} as any,
|
2022-04-12 00:49:13 +00:00
|
|
|
new Msg({
|
|
|
|
time: 123456791,
|
|
|
|
text: `foo bar x baz`,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any)
|
2022-04-12 00:49:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
store.index(
|
2022-06-19 00:25:21 +00:00
|
|
|
{uuid: "this-is-a-network-guid2"} as any,
|
|
|
|
{name: "#channel"} as any,
|
2022-04-12 00:49:13 +00:00
|
|
|
new Msg({
|
|
|
|
time: 123456792,
|
|
|
|
text: `bar @ baz`,
|
2022-06-19 00:25:21 +00:00
|
|
|
} as any)
|
2022-04-12 00:49:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
store
|
2022-06-19 00:25:21 +00:00
|
|
|
.getMessages(
|
|
|
|
{uuid: "this-is-a-network-guid2"} as any,
|
|
|
|
{name: "#channel"} as any
|
|
|
|
)
|
2022-04-12 00:49:13 +00:00
|
|
|
// .getMessages() waits for store.index() transactions to commit
|
|
|
|
.then(() => assertResults("foo", ["foo % bar _ baz", "foo bar x baz"]))
|
|
|
|
.then(() => assertResults("%", ["foo % bar _ baz"]))
|
|
|
|
.then(() => assertResults("foo % bar ", ["foo % bar _ baz"]))
|
|
|
|
.then(() => assertResults("_", ["foo % bar _ baz"]))
|
|
|
|
.then(() => assertResults("bar _ baz", ["foo % bar _ baz"]))
|
|
|
|
.then(() => assertResults("%%", []))
|
|
|
|
.then(() => assertResults("@%", []))
|
|
|
|
.then(() => assertResults("@", ["bar @ baz"]))
|
|
|
|
);
|
|
|
|
} finally {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.maxHistory = originalMaxHistory;
|
2022-04-12 00:49:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should close database", function (done) {
|
2018-03-10 16:49:16 +00:00
|
|
|
store.close((err) => {
|
|
|
|
expect(err).to.be.null;
|
|
|
|
expect(fs.existsSync(expectedPath)).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|