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";
|
2022-12-28 13:57:36 +00:00
|
|
|
import MessageStorage, {
|
|
|
|
currentSchemaVersion,
|
|
|
|
migrations,
|
|
|
|
necessaryMigrations,
|
|
|
|
} from "../../server/plugins/messageStorage/sqlite";
|
|
|
|
import Client from "../../server/client";
|
|
|
|
import sqlite3 from "sqlite3";
|
|
|
|
|
|
|
|
const orig_schema = [
|
|
|
|
// Schema version #1
|
|
|
|
// DO NOT CHANGE THIS IN ANY WAY, it's needed to properly test migrations
|
|
|
|
"CREATE TABLE IF NOT EXISTS options (name TEXT, value TEXT, CONSTRAINT name_unique UNIQUE (name))",
|
|
|
|
"CREATE TABLE IF NOT EXISTS messages (network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
|
|
|
|
"CREATE INDEX IF NOT EXISTS network_channel ON messages (network, channel)",
|
|
|
|
"CREATE INDEX IF NOT EXISTS time ON messages (time)",
|
|
|
|
];
|
|
|
|
|
|
|
|
const v1_schema_version = 1520239200;
|
|
|
|
|
|
|
|
const v1_dummy_messages = [
|
|
|
|
{
|
|
|
|
network: "8f650427-79a2-4950-b8af-94088b61b37c",
|
|
|
|
channel: "##linux",
|
|
|
|
time: 1594845354280,
|
|
|
|
type: "message",
|
|
|
|
msg: '{"from":{"mode":"","nick":"rascul"},"text":"db on a flash drive doesn\'t sound very nice though","self":false,"highlight":false,"users":[]}',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "8f650427-79a2-4950-b8af-94088b61b37c",
|
|
|
|
channel: "##linux",
|
|
|
|
time: 1594845357234,
|
|
|
|
type: "message",
|
|
|
|
msg: '{"from":{"mode":"","nick":"GrandPa-G"},"text":"that\'s the point of changing to make sure.","self":false,"highlight":false,"users":[]}',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "8f650427-79a2-4950-b8af-94088b61b37c",
|
|
|
|
channel: "#pleroma-dev",
|
|
|
|
time: 1594845358464,
|
|
|
|
type: "message",
|
|
|
|
msg: '{"from":{"mode":"@","nick":"rinpatch"},"text":"it\'s complicated","self":false,"highlight":false,"users":[]}',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
describe("SQLite migrations", function () {
|
|
|
|
let db: sqlite3.Database;
|
|
|
|
|
|
|
|
function serialize_run(stmt: string, ...params: any[]): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.serialize(() => {
|
|
|
|
db.run(stmt, params, (err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
db = new sqlite3.Database(":memory:");
|
|
|
|
|
|
|
|
for (const stmt of orig_schema) {
|
|
|
|
await serialize_run(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const msg of v1_dummy_messages) {
|
|
|
|
await serialize_run(
|
|
|
|
"INSERT INTO messages(network, channel, time, type, msg) VALUES(?, ?, ?, ?, ?)",
|
|
|
|
msg.network,
|
|
|
|
msg.channel,
|
|
|
|
msg.time,
|
|
|
|
msg.type,
|
|
|
|
msg.msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function (done) {
|
|
|
|
db.close(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("has working migrations", async function () {
|
|
|
|
const to_execute = necessaryMigrations(v1_schema_version);
|
|
|
|
expect(to_execute.length).to.eq(migrations.length);
|
|
|
|
await serialize_run("BEGIN EXCLUSIVE TRANSACTION");
|
|
|
|
|
|
|
|
for (const stmt of to_execute.map((m) => m.stmts).flat()) {
|
|
|
|
await serialize_run(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
await serialize_run("COMMIT TRANSACTION");
|
|
|
|
});
|
|
|
|
});
|
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) {
|
2022-12-30 15:40:12 +00:00
|
|
|
store = new MessageStorage("testUser");
|
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
|
|
|
});
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
it("should create database file", async function () {
|
2018-03-02 08:42:12 +00:00
|
|
|
expect(store.isEnabled).to.be.false;
|
|
|
|
expect(fs.existsSync(expectedPath)).to.be.false;
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
await store.enable();
|
2018-03-02 08:42:12 +00:00
|
|
|
expect(store.isEnabled).to.be.true;
|
|
|
|
});
|
|
|
|
|
2022-11-26 12:52:27 +00:00
|
|
|
it("should resolve an empty array when disabled", async function () {
|
|
|
|
store.isEnabled = false;
|
2022-12-30 15:40:12 +00:00
|
|
|
const messages = await store.getMessages(null as any, null as any, null as any);
|
2022-11-26 12:52:27 +00:00
|
|
|
expect(messages).to.be.empty;
|
|
|
|
store.isEnabled = true;
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
// compared as string because it's returned as such from the database
|
2022-12-28 12:52:24 +00:00
|
|
|
expect(row.value).to.equal(currentSchemaVersion.toString());
|
2021-10-28 07:48:11 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
it("should store a message", async function () {
|
|
|
|
await store.index(
|
2021-10-28 07:48:11 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
it("should retrieve previously stored message", async function () {
|
2022-12-30 15:40:12 +00:00
|
|
|
let msgid = 0;
|
2022-11-01 21:51:40 +00:00
|
|
|
const messages = await store.getMessages(
|
|
|
|
{
|
|
|
|
uuid: "this-is-a-network-guid",
|
|
|
|
} as any,
|
|
|
|
{
|
|
|
|
name: "#thisisaCHANNEL",
|
2022-12-30 15:40:12 +00:00
|
|
|
} as any,
|
|
|
|
() => msgid++
|
2022-11-01 21:51:40 +00:00
|
|
|
);
|
|
|
|
expect(messages).to.have.lengthOf(1);
|
|
|
|
const msg = messages[0];
|
|
|
|
expect(msg.text).to.equal("Hello from sqlite world!");
|
|
|
|
expect(msg.type).to.equal(MessageType.MESSAGE);
|
|
|
|
expect(msg.time.getTime()).to.equal(123456789);
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
it("should retrieve latest LIMIT messages in order", async 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) {
|
2022-11-01 21:51:40 +00:00
|
|
|
await 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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-30 15:40:12 +00:00
|
|
|
let msgId = 0;
|
2022-11-01 21:51:40 +00:00
|
|
|
const messages = await store.getMessages(
|
|
|
|
{uuid: "retrieval-order-test-network"} as any,
|
2022-12-30 15:40:12 +00:00
|
|
|
{name: "#channel"} as any,
|
|
|
|
() => msgId++
|
2022-11-01 21:51:40 +00:00
|
|
|
);
|
|
|
|
expect(messages).to.have.lengthOf(2);
|
|
|
|
expect(messages.map((i_1) => i_1.text)).to.deep.equal(["msg 198", "msg 199"]);
|
2021-10-28 07:48:11 +00:00
|
|
|
} 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
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
it("should search messages", async 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
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
const search = await store.search({
|
|
|
|
searchTerm: "msg",
|
|
|
|
networkUuid: "retrieval-order-test-network",
|
2022-11-24 08:45:01 +00:00
|
|
|
channelName: "",
|
|
|
|
offset: 0,
|
|
|
|
});
|
2022-11-01 21:51:40 +00:00
|
|
|
expect(search.results).to.have.lengthOf(100);
|
|
|
|
const expectedMessages: string[] = [];
|
2021-10-28 07:48:11 +00:00
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
for (let i = 100; i < 200; ++i) {
|
|
|
|
expectedMessages.push(`msg ${i}`);
|
|
|
|
}
|
2021-10-28 07:48:11 +00:00
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
expect(search.results.map((i_1) => i_1.text)).to.deep.equal(expectedMessages);
|
2021-10-28 07:48:11 +00:00
|
|
|
} 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-11-01 21:51:40 +00:00
|
|
|
it("should search messages with escaped wildcards", async function () {
|
|
|
|
async function assertResults(query: string, expected: string[]) {
|
|
|
|
const search = await store.search({
|
|
|
|
searchTerm: query,
|
|
|
|
networkUuid: "this-is-a-network-guid2",
|
2022-11-24 08:45:01 +00:00
|
|
|
channelName: "",
|
|
|
|
offset: 0,
|
|
|
|
});
|
2022-11-01 21:51:40 +00:00
|
|
|
expect(search.results.map((i) => i.text)).to.deep.equal(expected);
|
2022-04-12 00:49:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
await 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
|
|
|
);
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
await 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
|
|
|
);
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
await 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
|
|
|
);
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
await assertResults("foo", ["foo % bar _ baz", "foo bar x baz"]);
|
|
|
|
await assertResults("%", ["foo % bar _ baz"]);
|
|
|
|
await assertResults("foo % bar ", ["foo % bar _ baz"]);
|
|
|
|
await assertResults("_", ["foo % bar _ baz"]);
|
|
|
|
await assertResults("bar _ baz", ["foo % bar _ baz"]);
|
|
|
|
await assertResults("%%", []);
|
|
|
|
await assertResults("@%", []);
|
|
|
|
await assertResults("@", ["bar @ baz"]);
|
2022-04-12 00:49:13 +00:00
|
|
|
} finally {
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.values.maxHistory = originalMaxHistory;
|
2022-04-12 00:49:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
it("should close database", async function () {
|
|
|
|
await store.close();
|
|
|
|
expect(fs.existsSync(expectedPath)).to.be.true;
|
2018-03-10 16:49:16 +00:00
|
|
|
});
|
2018-03-02 08:42:12 +00:00
|
|
|
});
|