diff --git a/web/database/queries/message-to-device-queries.test.js b/web/database/queries/message-to-device-queries.test.js new file mode 100644 --- /dev/null +++ b/web/database/queries/message-to-device-queries.test.js @@ -0,0 +1,127 @@ +// @flow + +import { getDatabaseModule } from '../db-module.js'; +import type { EmscriptenModule } from '../types/module.js'; +import { + type ClientMessageToDevice, + type SQLiteQueryExecutor, +} from '../types/sqlite-query-executor.js'; +import { clearSensitiveData } from '../utils/db-utils.js'; + +const FILE_PATH = 'test.sqlite'; + +const timestamp1 = new Date('2023-01-01T00:00:00').getTime().toString(); +const timestamp2 = new Date('2023-01-02T00:00:00').getTime().toString(); +const timestamp3 = new Date('2023-01-03T00:00:00').getTime().toString(); +const timestamp4 = new Date('2023-01-04T00:00:00').getTime().toString(); + +const device1 = 'device-1'; +const device2 = 'device-2'; + +const TEST_MSG_1: ClientMessageToDevice = { + messageID: 'id-1', + deviceID: device1, + userID: 'user-1', + timestamp: timestamp2, + content: 'encrypted-1', +}; +const TEST_MSG_2: ClientMessageToDevice = { + messageID: 'id-2', + deviceID: device2, + userID: 'user-2', + timestamp: timestamp3, + content: 'encrypted-2', +}; + +const TEST_MSG_3: ClientMessageToDevice = { + messageID: 'id-3', + deviceID: device1, + userID: 'user-1', + timestamp: timestamp1, + content: 'encrypted-3', +}; + +const TEST_MSG_4: ClientMessageToDevice = { + messageID: 'id-4', + deviceID: device1, + userID: 'user-1', + timestamp: timestamp4, + content: 'encrypted-4', +}; + +const device1MessagesOrdered = [TEST_MSG_3, TEST_MSG_1, TEST_MSG_4]; + +describe('Message to device queries', () => { + let queryExecutor: ?SQLiteQueryExecutor = null; + let dbModule: ?EmscriptenModule = null; + + beforeAll(async () => { + dbModule = getDatabaseModule(); + }); + + beforeEach(() => { + if (!dbModule) { + return; + } + queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + queryExecutor?.addMessagesToDevice([ + TEST_MSG_1, + TEST_MSG_2, + TEST_MSG_3, + TEST_MSG_4, + ]); + }); + + afterEach(() => { + if (!dbModule || !queryExecutor) { + return; + } + clearSensitiveData(dbModule, FILE_PATH, queryExecutor); + }); + + it('should return all messages', () => { + expect(queryExecutor?.getAllMessagesToDevice(device1).length).toBe(3); + expect(queryExecutor?.getAllMessagesToDevice(device2).length).toBe(1); + }); + + it('should return messages in correct order', () => { + const messages = queryExecutor?.getAllMessagesToDevice(device1); + expect(messages).toStrictEqual(device1MessagesOrdered); + }); + + it('should remove when there is only one message', () => { + queryExecutor?.removeMessagesToDeviceOlderThan(TEST_MSG_2); + expect(queryExecutor?.getAllMessagesToDevice(device2).length).toBe(0); + }); + + it('should remove older messages', () => { + queryExecutor?.removeMessagesToDeviceOlderThan(TEST_MSG_1); + expect(queryExecutor?.getAllMessagesToDevice(device1)).toStrictEqual([ + TEST_MSG_4, + ]); + }); + + it('should remove all messages for given device', () => { + queryExecutor?.removeAllMessagesForDevice(device1); + expect(queryExecutor?.getAllMessagesToDevice(device1).length).toBe(0); + queryExecutor?.removeAllMessagesForDevice(device2); + expect(queryExecutor?.getAllMessagesToDevice(device2).length).toBe(0); + }); + + it('should duplicate messages for given device', () => { + const newDeviceID = 'newDeviceID'; + queryExecutor?.duplicateMessagesForDevice(device1, newDeviceID); + + const device1Messages = queryExecutor?.getAllMessagesToDevice(device1); + expect(device1Messages).toStrictEqual(device1MessagesOrdered); + + const newMessages = device1MessagesOrdered.map(msg => ({ + ...msg, + deviceID: newDeviceID, + })); + + const newDeviceMessages = + queryExecutor?.getAllMessagesToDevice(newDeviceID); + expect(newDeviceMessages).toStrictEqual(newMessages); + }); +});