diff --git a/web/database/queries/communities-queries.test.js b/web/database/queries/communities-queries.test.js index d7671ac73..3e337ecf0 100644 --- a/web/database/queries/communities-queries.test.js +++ b/web/database/queries/communities-queries.test.js @@ -1,132 +1,134 @@ // @flow import { convertCommunityInfoToClientDBCommunityInfo, communityStoreOpsHandlers, } from 'lib/ops/community-store-ops.js'; import type { CommunityInfo } from 'lib/types/community-types.js'; import { getDatabaseModule } from '../db-module.js'; import type { EmscriptenModule } from '../types/module.js'; import type { SQLiteQueryExecutor } from '../types/sqlite-query-executor.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; const TEST_COMMUNITY_1: CommunityInfo = { enabledApps: { calendar: false, wiki: false, tasks: true, files: true, }, }; const TEST_COMMUNITY_2: CommunityInfo = { enabledApps: { calendar: true, wiki: false, tasks: false, files: false, }, }; describe('Community Store queries', () => { let queryExecutor: ?SQLiteQueryExecutor = null; let dbModule: ?EmscriptenModule = null; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { if (!dbModule) { - return; + throw new Error('Database module is missing'); } - queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor?.replaceCommunity( convertCommunityInfoToClientDBCommunityInfo({ communityInfo: TEST_COMMUNITY_1, id: '1', }), ); queryExecutor?.replaceCommunity( convertCommunityInfoToClientDBCommunityInfo({ communityInfo: TEST_COMMUNITY_2, id: '2', }), ); }); afterEach(() => { if (!dbModule || !queryExecutor) { return; } clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all communities', () => { const communities = queryExecutor?.getAllCommunities(); expect(communities).toHaveLength(2); }); it('should remove all communities', () => { queryExecutor?.removeAllCommunities(); const communities = queryExecutor?.getAllCommunities(); expect(communities).toHaveLength(0); }); it('should update community enabled apps', () => { const community2Updated: CommunityInfo = { enabledApps: { calendar: true, wiki: true, tasks: true, files: true, }, }; queryExecutor?.replaceCommunity( convertCommunityInfoToClientDBCommunityInfo({ communityInfo: community2Updated, id: '2', }), ); const communities = queryExecutor?.getAllCommunities(); if (!communities) { throw new Error('communities not defined'); } expect(communities).toHaveLength(2); const communitiesFromDB = communityStoreOpsHandlers.translateClientDBData(communities); expect(communitiesFromDB['2']).toBeDefined(); expect(communitiesFromDB['2'].enabledApps.calendar).toBe(true); expect(communitiesFromDB['2'].enabledApps.wiki).toBe(true); expect(communitiesFromDB['2'].enabledApps.tasks).toBe(true); expect(communitiesFromDB['2'].enabledApps.files).toBe(true); }); it('should remove community', () => { queryExecutor?.removeCommunities(['2']); const communities = queryExecutor?.getAllCommunities(); if (!communities) { throw new Error('communities not defined'); } expect(communities.length).toBe(1); const communitiesFromDB = communityStoreOpsHandlers.translateClientDBData(communities); expect(communitiesFromDB['1']).toBeDefined(); }); }); diff --git a/web/database/queries/draft-queries.test.js b/web/database/queries/draft-queries.test.js index 6b65a056b..3217d4ab0 100644 --- a/web/database/queries/draft-queries.test.js +++ b/web/database/queries/draft-queries.test.js @@ -1,113 +1,119 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; describe('Draft Store queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.updateDraft('thread_a', 'draft a'); queryExecutor.updateDraft('thread_b', 'draft b'); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all drafts', () => { const drafts = queryExecutor.getAllDrafts(); expect(drafts.length).toBe(2); }); it('should remove all drafts', () => { queryExecutor.removeAllDrafts(); const drafts = queryExecutor.getAllDrafts(); expect(drafts.length).toBe(0); }); it('should update draft text', () => { const key = 'thread_b'; const text = 'updated message'; queryExecutor.updateDraft(key, text); const drafts = queryExecutor.getAllDrafts(); expect(drafts.length).toBe(2); const draft = drafts.find(d => d.key === key); expect(draft?.text).toBe(text); }); it('should insert not existing draft', () => { const key = 'new_key'; const text = 'some message'; queryExecutor.updateDraft(key, text); const drafts = queryExecutor.getAllDrafts(); expect(drafts.length).toBe(3); const draft = drafts.find(d => d.key === key); expect(draft?.text).toBe(text); }); it('should move draft to a new key', () => { const newKey = 'new_key'; const oldKey = 'thread_a'; const draftText = 'draft a'; queryExecutor.moveDraft(oldKey, newKey); const drafts = queryExecutor.getAllDrafts(); expect(drafts.length).toBe(2); const oldKeyDraft = drafts.find(d => d.key === oldKey); expect(oldKeyDraft).toBeUndefined(); const newKeyDraft = drafts.find(d => d.key === newKey); expect(newKeyDraft?.text).toBe(draftText); }); it('should not change anything if oldKey not exists', () => { const newKey = 'new_key'; const oldKey = 'missing_key'; queryExecutor.moveDraft(oldKey, newKey); const drafts = queryExecutor.getAllDrafts(); expect(drafts.length).toBe(2); const oldKeyDraft = drafts.find(d => d.key === oldKey); expect(oldKeyDraft).toBeUndefined(); const newKeyDraft = drafts.find(d => d.key === newKey); expect(newKeyDraft).toBeUndefined(); }); it('should move and replace if newKey exists', () => { const newKey = 'thread_b'; const oldKey = 'thread_a'; const draftText = 'draft a'; queryExecutor.moveDraft(oldKey, newKey); const drafts = queryExecutor.getAllDrafts(); expect(drafts.length).toBe(1); const oldKeyDraft = drafts.find(d => d.key === oldKey); expect(oldKeyDraft).toBeUndefined(); const newKeyDraft = drafts.find(d => d.key === newKey); expect(newKeyDraft?.text).toBe(draftText); }); it('should remove drafts with specified keys', () => { queryExecutor.removeDrafts(['thread_a']); const drafts = queryExecutor.getAllDrafts(); expect(drafts).toEqual([{ key: 'thread_b', text: 'draft b' }]); }); }); diff --git a/web/database/queries/keyservers-queries.test.js b/web/database/queries/keyservers-queries.test.js index a64fe5b15..3e7660211 100644 --- a/web/database/queries/keyservers-queries.test.js +++ b/web/database/queries/keyservers-queries.test.js @@ -1,132 +1,135 @@ // @flow import { convertKeyserverInfoToClientDBKeyserverInfo, keyserverStoreOpsHandlers, } from 'lib/ops/keyserver-store-ops.js'; import { defaultCalendarFilters } from 'lib/types/filter-types.js'; import type { KeyserverInfo } from 'lib/types/keyserver-types.js'; import { defaultConnectionInfo } from 'lib/types/socket-types.js'; import { getDatabaseModule } from '../db-module.js'; import type { EmscriptenModule } from '../types/module.js'; import { type SQLiteQueryExecutor } from '../types/sqlite-query-executor.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; const TEST_KEYSERVER_1: KeyserverInfo = { cookie: 'testCookie1', updatesCurrentAsOf: 0, urlPrefix: 'localhost', connection: { ...defaultConnectionInfo, }, deviceToken: 'token', lastCommunicatedPlatformDetails: null, actualizedCalendarQuery: { startDate: '', endDate: '', filters: defaultCalendarFilters, }, }; const TEST_KEYSERVER_2: KeyserverInfo = { cookie: 'testCookie2', updatesCurrentAsOf: 0, urlPrefix: 'localhost', connection: { ...defaultConnectionInfo, }, deviceToken: 'token', lastCommunicatedPlatformDetails: null, actualizedCalendarQuery: { startDate: '', endDate: '', filters: defaultCalendarFilters, }, }; describe('Keyserver Store queries', () => { let queryExecutor: ?SQLiteQueryExecutor = null; let dbModule: ?EmscriptenModule = null; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { if (!dbModule) { - return; + throw new Error('Database module is missing'); } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor?.replaceKeyserver( convertKeyserverInfoToClientDBKeyserverInfo({ keyserverInfo: TEST_KEYSERVER_1, id: '1', }), ); queryExecutor?.replaceKeyserver( convertKeyserverInfoToClientDBKeyserverInfo({ keyserverInfo: TEST_KEYSERVER_2, id: '2', }), ); }); afterEach(() => { if (!dbModule || !queryExecutor) { return; } clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all keyservers', () => { const keyservers = queryExecutor?.getAllKeyservers(); expect(keyservers?.length).toBe(2); }); it('should remove all keyservers', () => { queryExecutor?.removeAllKeyservers(); const keyservers = queryExecutor?.getAllKeyservers(); expect(keyservers?.length).toBe(0); }); it('should update keyserver cookie', () => { const keyserver2Updated: KeyserverInfo = { ...TEST_KEYSERVER_1, cookie: 'updatedCookie', }; queryExecutor?.replaceKeyserver( convertKeyserverInfoToClientDBKeyserverInfo({ keyserverInfo: keyserver2Updated, id: '1', }), ); const keyservers = queryExecutor?.getAllKeyservers(); if (!keyservers) { throw new Error('keyservers not defined'); } expect(keyservers.length).toBe(2); const keyserversFromDB = keyserverStoreOpsHandlers.translateClientDBData(keyservers); expect(keyserversFromDB['1']).toBeDefined(); expect(keyserversFromDB['1'].cookie).toBe('updatedCookie'); }); it('should remove keyserver', () => { queryExecutor?.removeKeyservers(['1']); const keyservers = queryExecutor?.getAllKeyservers(); if (!keyservers) { throw new Error('keyservers not defined'); } expect(keyservers.length).toBe(1); const keyserversFromDB = keyserverStoreOpsHandlers.translateClientDBData(keyservers); expect(keyserversFromDB['2']).toBeDefined(); }); }); diff --git a/web/database/queries/message-store-threads-queries.test.js b/web/database/queries/message-store-threads-queries.test.js index 7e509ab78..d6364aac7 100644 --- a/web/database/queries/message-store-threads-queries.test.js +++ b/web/database/queries/message-store-threads-queries.test.js @@ -1,46 +1,52 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; describe('Message store threads queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.replaceMessageStoreThreads([ { id: '1', startReached: 0 }, { id: '2', startReached: 0 }, { id: '3', startReached: 0 }, { id: '4', startReached: 0 }, ]); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all message store threads', () => { const threads = queryExecutor.getAllMessageStoreThreads(); expect(threads.length).toBe(4); }); it('should remove all message store threads', () => { queryExecutor.removeAllMessageStoreThreads(); const threads = queryExecutor.getAllMessageStoreThreads(); expect(threads.length).toBe(0); }); it('should remove a subset of message store threads', () => { queryExecutor.removeMessageStoreThreads(['2', '3']); const threads = queryExecutor.getAllMessageStoreThreads(); expect(threads.length).toBe(2); }); }); diff --git a/web/database/queries/message-to-device-queries.test.js b/web/database/queries/message-to-device-queries.test.js index 981b6923b..ee599c442 100644 --- a/web/database/queries/message-to-device-queries.test.js +++ b/web/database/queries/message-to-device-queries.test.js @@ -1,114 +1,117 @@ // @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, plaintext: 'decrypted-1', ciphertext: 'encrypted-1', }; const TEST_MSG_2: ClientMessageToDevice = { messageID: 'id-2', deviceID: device2, userID: 'user-2', timestamp: timestamp3, plaintext: 'decrypted-2', ciphertext: 'encrypted-2', }; const TEST_MSG_3: ClientMessageToDevice = { messageID: 'id-3', deviceID: device1, userID: 'user-1', timestamp: timestamp1, plaintext: 'decrypted-3', ciphertext: 'encrypted-3', }; const TEST_MSG_4: ClientMessageToDevice = { messageID: 'id-4', deviceID: device1, userID: 'user-1', timestamp: timestamp4, plaintext: 'decrypted-4', ciphertext: '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; + throw new Error('Database module is missing'); } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } 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); }); }); diff --git a/web/database/queries/messages-and-media-queries.test.js b/web/database/queries/messages-and-media-queries.test.js index d41678fbd..e837b0aa7 100644 --- a/web/database/queries/messages-and-media-queries.test.js +++ b/web/database/queries/messages-and-media-queries.test.js @@ -1,176 +1,182 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; describe('Message and media store queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.replaceMessageWeb({ id: '1', localID: { value: '', isNull: true }, thread: '1', user: '1', type: 0, futureType: { value: 0, isNull: true }, content: { value: '', isNull: true }, time: '0', }); queryExecutor.replaceMessageWeb({ id: '2', localID: { value: '', isNull: true }, thread: '1', user: '1', type: 0, futureType: { value: 0, isNull: true }, content: { value: '', isNull: true }, time: '0', }); queryExecutor.replaceMessageWeb({ id: '3', localID: { value: '', isNull: true }, thread: '2', user: '1', type: 0, futureType: { value: 5, isNull: false }, content: { value: '', isNull: true }, time: '0', }); queryExecutor.replaceMedia({ id: '1', container: '1', thread: '1', uri: '1', type: '1', extras: '1', }); queryExecutor.replaceMedia({ id: '2', container: '1', thread: '1', uri: '1', type: '1', extras: '1', }); queryExecutor.replaceMedia({ id: '3', container: '3', thread: '2', uri: '1', type: '1', extras: '1', }); queryExecutor.replaceMedia({ id: '4', container: '3', thread: '2', uri: '1', type: '1', extras: '1', }); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all messages with media', () => { const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages.length).toBe(3); expect(allMessages[0].medias.length).toBe(2); expect(allMessages[1].medias.length).toBe(0); expect(allMessages[2].medias.length).toBe(2); }); it('should remove all messages', () => { queryExecutor.removeAllMessages(); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages.length).toBe(0); }); it('should remove all media', () => { queryExecutor.removeAllMedia(); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages[0].medias.length).toBe(0); expect(allMessages[1].medias.length).toBe(0); expect(allMessages[2].medias.length).toBe(0); }); it('should remove all messages for threads', () => { queryExecutor.removeMessagesForThreads(['1']); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages.length).toBe(1); }); it('should remove all messages with ids', () => { queryExecutor.removeMessages(['1']); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages.length).toBe(2); }); it('should remove all media for message', () => { queryExecutor.removeMediaForMessage('1'); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages[0].medias.length).toBe(0); expect(allMessages[1].medias.length).toBe(0); expect(allMessages[2].medias.length).toBe(2); }); it('should remove all media for messages', () => { queryExecutor.removeMediaForMessages(['3']); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages[0].medias.length).toBe(2); expect(allMessages[1].medias.length).toBe(0); expect(allMessages[2].medias.length).toBe(0); }); it('should remove all media for threads', () => { queryExecutor.removeMediaForThreads(['2']); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages[0].medias.length).toBe(2); expect(allMessages[1].medias.length).toBe(0); expect(allMessages[2].medias.length).toBe(0); }); it('should rekey media containers', () => { queryExecutor.rekeyMediaContainers('1', '3'); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages[0].medias.length).toBe(0); expect(allMessages[1].medias.length).toBe(0); expect(allMessages[2].medias.length).toBe(4); }); it('should rekey message', () => { queryExecutor.rekeyMessage('3', '2'); const allMessages = queryExecutor.getAllMessagesWeb(); expect(allMessages.length).toBe(2); const rekeyedMessage = allMessages.find( messageWithMedia => messageWithMedia.message.id === '2', ); expect(rekeyedMessage?.message.thread).toBe('2'); }); it('should correctly handle nullable integer', () => { const allMessages = queryExecutor.getAllMessagesWeb(); const messageWithNullFutureType = allMessages.find( messageWithMedia => messageWithMedia.message.id === '1', ); const messageWithNonNullIFutureType = allMessages.find( messageWithMedia => messageWithMedia.message.id === '3', ); expect(messageWithNullFutureType?.message.futureType.isNull).toBe(true); expect(messageWithNonNullIFutureType?.message.futureType.isNull).toBe( false, ); expect(messageWithNonNullIFutureType?.message.futureType.value).toBe(5); }); }); diff --git a/web/database/queries/metadata-queries.test.js b/web/database/queries/metadata-queries.test.js index 74308e88f..327a422b6 100644 --- a/web/database/queries/metadata-queries.test.js +++ b/web/database/queries/metadata-queries.test.js @@ -1,62 +1,68 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; const TEST_USER_ID_KEY = 'current_user_id'; const TEST_USER_ID_VAL = 'qwerty1234'; describe('Metadata queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.setMetadata(TEST_USER_ID_KEY, TEST_USER_ID_VAL); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return the data of an existing name', () => { expect(queryExecutor.getMetadata(TEST_USER_ID_KEY)).toBe(TEST_USER_ID_VAL); }); it('should return undefined for a non-existing name', () => { const nonExistingName = 'non_existing_name'; expect(queryExecutor.getMetadata(nonExistingName)).toBe(''); }); it('should set the data of an existing name', () => { const newUserID = 'newID123'; queryExecutor.setMetadata(TEST_USER_ID_KEY, newUserID); expect(queryExecutor.getMetadata(TEST_USER_ID_KEY)).toBe(newUserID); }); it('should set the data of a non-existing name', () => { const newEntry = 'testEntry'; const newData = 'testData'; queryExecutor.setMetadata(newEntry, newData); expect(queryExecutor.getMetadata(newEntry)).toBe(newData); expect(queryExecutor.getMetadata(TEST_USER_ID_KEY)).toBe(TEST_USER_ID_VAL); }); it('should clear an existing entry', () => { queryExecutor.clearMetadata(TEST_USER_ID_KEY); expect(queryExecutor.getMetadata(TEST_USER_ID_KEY)).toBe(''); }); it('should do nothing when clearing a non-existing entry', () => { const nonExistingName = 'non_existing_name'; queryExecutor.clearMetadata(nonExistingName); expect(queryExecutor.getMetadata(nonExistingName)).toBe(''); expect(queryExecutor.getMetadata(TEST_USER_ID_KEY)).toBe(TEST_USER_ID_VAL); }); }); diff --git a/web/database/queries/olm-persist-data-queries.test.js b/web/database/queries/olm-persist-data-queries.test.js index 893bce882..88140782c 100644 --- a/web/database/queries/olm-persist-data-queries.test.js +++ b/web/database/queries/olm-persist-data-queries.test.js @@ -1,47 +1,53 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; describe('Olm Tables queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.storeOlmPersistAccount('accountData'); queryExecutor.storeOlmPersistSession({ targetUserID: '1', sessionData: '1', }); queryExecutor.storeOlmPersistSession({ targetUserID: '2', sessionData: '2', }); queryExecutor.storeOlmPersistSession({ targetUserID: '3', sessionData: '3', }); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return olm account data', () => { const olmAccount = queryExecutor.getOlmPersistAccountDataWeb(); expect(olmAccount.value).toBe('accountData'); }); it('should return all olm sessions', () => { const olmSessions = queryExecutor.getOlmPersistSessionsData(); expect(olmSessions.length).toBe(3); }); }); diff --git a/web/database/queries/report-queries.test.js b/web/database/queries/report-queries.test.js index 7b3efdb98..6330c3864 100644 --- a/web/database/queries/report-queries.test.js +++ b/web/database/queries/report-queries.test.js @@ -1,59 +1,65 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; describe('Report Store queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.replaceReport({ id: '1', report: '{report_content_1}' }); queryExecutor.replaceReport({ id: '2', report: '{report_content_2}' }); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all reports', () => { const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(2); }); it('should remove all reports', () => { queryExecutor.removeAllReports(); const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(0); }); it('should insert not existing report', () => { const id = '3'; const report = '{some_content}'; queryExecutor.replaceReport({ id, report }); const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(3); expect(reports).toContainEqual({ id, report }); }); it('should reports with given id', () => { const id = '2'; queryExecutor.removeReports([id]); const reports = queryExecutor.getAllReports(); expect(reports).toHaveLength(1); expect(reports).not.toContain( expect.objectContaining({ id, }), ); }); }); diff --git a/web/database/queries/storage-engine-queries.test.js b/web/database/queries/storage-engine-queries.test.js index 1fff967b9..e898a3055 100644 --- a/web/database/queries/storage-engine-queries.test.js +++ b/web/database/queries/storage-engine-queries.test.js @@ -1,62 +1,68 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; const TEST_KEY = 'redux-persist'; const TEST_ITEM = '[[{}]]'; describe('redux-persist storage engine queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.setPersistStorageItem(TEST_KEY, TEST_ITEM); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return the item of an existing key', () => { expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(TEST_ITEM); }); it('should return empty string for a non-existing key', () => { const nonExistingKey = 'non_existing_key'; expect(queryExecutor.getPersistStorageItem(nonExistingKey)).toBe(''); }); it('should set the item of an existing key', () => { const newItem = '[[{a:2]]'; queryExecutor.setPersistStorageItem(TEST_KEY, newItem); expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(newItem); }); it('should set the item of a non-existing key', () => { const newEntry = 'testEntry'; const newData = 'testData'; queryExecutor.setPersistStorageItem(newEntry, newData); expect(queryExecutor.getPersistStorageItem(newEntry)).toBe(newData); expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(TEST_ITEM); }); it('should remove an existing key', () => { queryExecutor.removePersistStorageItem(TEST_KEY); expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(''); }); it('should do nothing when removing a non-existing key', () => { const nonExistingName = 'non_existing_name'; queryExecutor.removePersistStorageItem(nonExistingName); expect(queryExecutor.getPersistStorageItem(nonExistingName)).toBe(''); expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(TEST_ITEM); }); }); diff --git a/web/database/queries/threads-queries.test.js b/web/database/queries/threads-queries.test.js index 6aa2e1e8f..c1ff60d62 100644 --- a/web/database/queries/threads-queries.test.js +++ b/web/database/queries/threads-queries.test.js @@ -1,94 +1,100 @@ // @flow import { getDatabaseModule } from '../db-module.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; describe('Threads queries', () => { let queryExecutor; let dbModule; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { + if (!dbModule) { + throw new Error('Database module is missing'); + } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor.replaceThreadWeb({ id: '1', type: 1, name: { value: '', isNull: true }, avatar: { value: '', isNull: true }, description: { value: '', isNull: true }, color: '1', creationTime: '1', parentThreadID: { value: '', isNull: true }, containingThreadID: { value: '', isNull: true }, community: { value: '', isNull: true }, members: '1', roles: '1', currentUser: '1', sourceMessageID: { value: '', isNull: true }, repliesCount: 1, pinnedCount: 1, }); queryExecutor.replaceThreadWeb({ id: '2', type: 1, name: { value: '', isNull: true }, avatar: { value: '', isNull: true }, description: { value: '', isNull: true }, color: '1', creationTime: '1', parentThreadID: { value: '', isNull: true }, containingThreadID: { value: '', isNull: true }, community: { value: '', isNull: true }, members: '1', roles: '1', currentUser: '1', sourceMessageID: { value: '', isNull: true }, repliesCount: 1, pinnedCount: 1, }); queryExecutor.replaceThreadWeb({ id: '3', type: 1, name: { value: '', isNull: true }, avatar: { value: '', isNull: true }, description: { value: '', isNull: true }, color: '1', creationTime: '1', parentThreadID: { value: '', isNull: true }, containingThreadID: { value: '', isNull: true }, community: { value: '', isNull: true }, members: '1', roles: '1', currentUser: '1', sourceMessageID: { value: '', isNull: true }, repliesCount: 1, pinnedCount: 1, }); }); afterEach(() => { clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all threads', () => { const threads = queryExecutor.getAllThreadsWeb(); expect(threads.length).toBe(3); }); it('should remove all threads', () => { queryExecutor.removeAllThreads(); const threads = queryExecutor.getAllThreadsWeb(); expect(threads.length).toBe(0); }); it('should remove subset of threads', () => { queryExecutor.removeThreads(['2']); const threads = queryExecutor.getAllThreadsWeb(); expect(threads.length).toBe(2); }); }); diff --git a/web/database/queries/user-quries.test.js b/web/database/queries/user-quries.test.js index b18c87ec9..5371e7d08 100644 --- a/web/database/queries/user-quries.test.js +++ b/web/database/queries/user-quries.test.js @@ -1,106 +1,109 @@ // @flow import { convertUserInfoToClientDBUserInfo, userStoreOpsHandlers, } from 'lib/ops/user-store-ops.js'; import type { UserInfo } from 'lib/types/user-types.js'; import { getDatabaseModule } from '../db-module.js'; import type { EmscriptenModule } from '../types/module.js'; import { type SQLiteQueryExecutor } from '../types/sqlite-query-executor.js'; import { clearSensitiveData } from '../utils/db-utils.js'; const FILE_PATH = 'test.sqlite'; const TEST_USER_1: UserInfo = { id: '256', username: 'Test1', relationshipStatus: 2, avatar: { type: 'image', uri: 'av1', }, }; const TEST_USER_2: UserInfo = { id: '512', username: 'Test2', relationshipStatus: 3, avatar: { type: 'image', uri: 'av2', }, }; describe('User Store queries', () => { let queryExecutor: ?SQLiteQueryExecutor = null; let dbModule: ?EmscriptenModule = null; beforeAll(async () => { dbModule = getDatabaseModule(); }); beforeEach(() => { if (!dbModule) { - return; + throw new Error('Database module is missing'); } queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); + if (!queryExecutor) { + throw new Error('SQLiteQueryExecutor is missing'); + } queryExecutor?.replaceUser(convertUserInfoToClientDBUserInfo(TEST_USER_1)); queryExecutor?.replaceUser(convertUserInfoToClientDBUserInfo(TEST_USER_2)); }); afterEach(() => { if (!dbModule || !queryExecutor) { return; } clearSensitiveData(dbModule, FILE_PATH, queryExecutor); }); it('should return all users', () => { const users = queryExecutor?.getAllUsers(); expect(users?.length).toBe(2); }); it('should remove all users', () => { queryExecutor?.removeAllUsers(); const users = queryExecutor?.getAllUsers(); expect(users?.length).toBe(0); }); it('should update user avatar', () => { const user2Updated: UserInfo = { id: '512', username: 'Test2', avatar: { type: 'image', uri: 'new_av', }, }; queryExecutor?.replaceUser(convertUserInfoToClientDBUserInfo(user2Updated)); const dbUsers = queryExecutor?.getAllUsers(); if (!dbUsers) { throw new Error('users not defined'); } expect(dbUsers.length).toBe(2); const users = userStoreOpsHandlers.translateClientDBData(dbUsers); expect(users['512']).toBeDefined(); expect(users['512'].avatar?.uri).toBe(user2Updated.avatar?.uri); expect(users['512'].relationshipStatus).not.toBeDefined(); }); it('should remove user', () => { queryExecutor?.removeUsers(['512']); const dbUsers = queryExecutor?.getAllUsers(); if (!dbUsers) { throw new Error('users not defined'); } expect(dbUsers.length).toBe(1); const users = userStoreOpsHandlers.translateClientDBData(dbUsers); expect(users['256']).toBeDefined(); }); });