Repository: thelounge/thelounge
Pull request #4345 (2021-10-28 07:52:49)

Small cleanup of messageStorage/sqlite.
* Extend test coverage to the `search` function.
* Test sort order of messages from `getMessages` and `search`
* Move reversal of `search` results from Vue to messageStorage.
* Remove unnecessary uses of `sqlite.serialize` in tests.
* Return promises from test functions where possible.

--

These changes were originally in #4334.


Patch:
diff --git a/client/components/Windows/SearchResults.vue b/client/components/Windows/SearchResults.vue
index 08ce21ff..159b702c 100644
--- a/client/components/Windows/SearchResults.vue
+++ b/client/components/Windows/SearchResults.vue
@@ -124,7 +124,7 @@ export default {
 				return [];
 			}
 
-			return this.search.results.slice().reverse();
+			return this.search.results;
 		},
 		chan() {
 			const chanId = parseInt(this.$route.params.id, 10);
diff --git a/src/plugins/messageStorage/sqlite.js b/src/plugins/messageStorage/sqlite.js
index cc8ccdf8..981a28b5 100644
--- a/src/plugins/messageStorage/sqlite.js
+++ b/src/plugins/messageStorage/sqlite.js
@@ -236,7 +236,7 @@ class MessageStorage {
 						target: query.channelName,
 						networkUuid: query.networkUuid,
 						offset: query.offset,
-						results: parseSearchRowsToMessages(query.offset, rows),
+						results: parseSearchRowsToMessages(query.offset, rows).reverse(),
 					};
 					resolve(response);
 				}


Test patch:
diff --git a/test/plugins/sqlite.js b/test/plugins/sqlite.js
index 0971d00f..e886d64e 100644
--- a/test/plugins/sqlite.js
+++ b/test/plugins/sqlite.js
@@ -37,10 +37,9 @@ describe("SQLite Message Storage", function () {
 		fs.rmdir(path.join(Helper.getHomePath(), "logs"), done);
 	});
 
-	it("should resolve an empty array when disabled", function (done) {
-		store.getMessages(null, null).then((messages) => {
+	it("should resolve an empty array when disabled", function () {
+		return store.getMessages(null, null).then((messages) => {
 			expect(messages).to.be.empty;
-			done();
 		});
 	});
 
@@ -54,91 +53,134 @@ describe("SQLite Message Storage", function () {
 	});
 
 	it("should create tables", function (done) {
-		store.database.serialize(() =>
-			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",
-							sql:
-								"CREATE TABLE options (name TEXT, value TEXT, CONSTRAINT name_unique UNIQUE (name))",
-						},
-						{
-							name: "messages",
-							tbl_name: "messages",
-							sql:
-								"CREATE TABLE messages (network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
-						},
-					]);
-
-					done();
-				}
-			)
+		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",
+						sql:
+							"CREATE TABLE options (name TEXT, value TEXT, CONSTRAINT name_unique UNIQUE (name))",
+					},
+					{
+						name: "messages",
+						tbl_name: "messages",
+						sql:
+							"CREATE TABLE messages (network TEXT, channel TEXT, time INTEGER, type TEXT, msg TEXT)",
+					},
+				]);
+
+				done();
+			}
 		);
 	});
 
 	it("should insert schema version to options table", function (done) {
-		store.database.serialize(() =>
-			store.database.get(
-				"SELECT value FROM options WHERE name = 'schema_version'",
-				(err, row) => {
-					expect(err).to.be.null;
+		store.database.get(
+			"SELECT value FROM options WHERE name = 'schema_version'",
+			(err, row) => {
+				expect(err).to.be.null;
 
-					// Should be sqlite.currentSchemaVersion,
-					// compared as string because it's returned as such from the database
-					expect(row.value).to.equal("1520239200");
+				// Should be sqlite.currentSchemaVersion,
+				// compared as string because it's returned as such from the database
+				expect(row.value).to.equal("1520239200");
 
-					done();
-				}
-			)
+				done();
+			}
+		);
+	});
+
+	it("should store a message", function () {
+		store.index(
+			{
+				uuid: "this-is-a-network-guid",
+			},
+			{
+				name: "#thisISaCHANNEL",
+			},
+			new Msg({
+				time: 123456789,
+				text: "Hello from sqlite world!",
+			})
 		);
 	});
 
-	it("should store a message", function (done) {
-		store.database.serialize(() => {
-			store.index(
+	it("should retrieve previously stored message", function () {
+		return store
+			.getMessages(
 				{
 					uuid: "this-is-a-network-guid",
 				},
 				{
-					name: "#thisISaCHANNEL",
-				},
-				new Msg({
-					time: 123456789,
-					text: "Hello from sqlite world!",
-				})
-			);
+					name: "#thisisaCHANNEL",
+				}
+			)
+			.then((messages) => {
+				expect(messages).to.have.lengthOf(1);
 
-			done();
-		});
+				const msg = messages[0];
+
+				expect(msg.text).to.equal("Hello from sqlite world!");
+				expect(msg.type).to.equal(Msg.Type.MESSAGE);
+				expect(msg.time.getTime()).to.equal(123456789);
+			});
 	});
 
-	it("should retrieve previously stored message", function (done) {
-		store.database.serialize(() =>
-			store
-				.getMessages(
-					{
-						uuid: "this-is-a-network-guid",
-					},
-					{
-						name: "#thisisaCHANNEL",
-					}
-				)
+	it("should retrieve latest LIMIT messages in order", function () {
+		const originalMaxHistory = Helper.config.maxHistory;
+
+		try {
+			Helper.config.maxHistory = 2;
+
+			for (let i = 0; i < 200; ++i) {
+				store.index(
+					{uuid: "retrieval-order-test-network"},
+					{name: "#channel"},
+					new Msg({
+						time: 123456789 + i,
+						text: `msg ${i}`,
+					})
+				);
+			}
+
+			return store
+				.getMessages({uuid: "retrieval-order-test-network"}, {name: "#channel"})
 				.then((messages) => {
-					expect(messages).to.have.lengthOf(1);
+					expect(messages).to.have.lengthOf(2);
+					expect(messages.map((i) => i.text)).to.deep.equal(["msg 198", "msg 199"]);
+				});
+		} finally {
+			Helper.config.maxHistory = originalMaxHistory;
+		}
+	});
 
-					const msg = messages[0];
+	it("should search messages", function () {
+		const originalMaxHistory = Helper.config.maxHistory;
 
-					expect(msg.text).to.equal("Hello from sqlite world!");
-					expect(msg.type).to.equal(Msg.Type.MESSAGE);
-					expect(msg.time.getTime()).to.equal(123456789);
+		try {
+			Helper.config.maxHistory = 2;
 
-					done();
+			return store
+				.search({
+					searchTerm: "msg",
+					networkUuid: "retrieval-order-test-network",
 				})
-		);
+				.then((messages) => {
+					expect(messages.results).to.have.lengthOf(100);
+
+					const expectedMessages = [];
+
+					for (let i = 100; i < 200; ++i) {
+						expectedMessages.push(`msg ${i}`);
+					}
+
+					expect(messages.results.map((i) => i.text)).to.deep.equal(expectedMessages);
+				});
+		} finally {
+			Helper.config.maxHistory = originalMaxHistory;
+		}
 	});
 
 	it("should close database", function (done) {