From 4cd3da8f5b7ff8dd6cb3d43b232c99ec011103cf Mon Sep 17 00:00:00 2001
From: Evan Hahn <69474926+EvanHahn-Signal@users.noreply.github.com>
Date: Fri, 2 Jul 2021 15:40:36 -0500
Subject: [PATCH] Convert ConversationModel tests to TypeScript
---
test/index.html | 2 -
test/keychange_listener_test.js | 60 +++++++++----------
.../models/conversations_test.ts | 37 ++++++++----
3 files changed, 54 insertions(+), 45 deletions(-)
rename test/models/conversations_test.js => ts/test-electron/models/conversations_test.ts (74%)
diff --git a/test/index.html b/test/index.html
index 03ac0f40e3..3bd167f4e5 100644
--- a/test/index.html
+++ b/test/index.html
@@ -408,8 +408,6 @@
-
-
diff --git a/test/keychange_listener_test.js b/test/keychange_listener_test.js
index 26c5ae011e..ab5dbfccfa 100644
--- a/test/keychange_listener_test.js
+++ b/test/keychange_listener_test.js
@@ -1,4 +1,4 @@
-// Copyright 2017-2020 Signal Messenger, LLC
+// Copyright 2017-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* global ConversationController, SignalProtocolStore, Whisper */
@@ -10,37 +10,38 @@ describe('KeyChangeListener', () => {
const newKey = window.Signal.Crypto.getRandomBytes(33);
let store;
+ let convo;
+
beforeEach(async () => {
+ window.ConversationController.reset();
+ await window.ConversationController.load();
+ await window.ConversationController.loadPromise();
+
+ convo = window.ConversationController.dangerouslyCreateAndAdd({
+ id: phoneNumberWithKeyChange,
+ type: 'private',
+ });
+ await window.Signal.Data.saveConversation(convo.attributes);
+
store = new SignalProtocolStore();
await store.hydrateCaches();
Whisper.KeyChangeListener.init(store);
return store.saveIdentity(addressString, oldKey);
});
- afterEach(() => {
- return store.removeIdentityKey(phoneNumberWithKeyChange);
+ afterEach(async () => {
+ await window.Signal.Data.removeAllMessagesInConversation(convo.id, {
+ logId: phoneNumberWithKeyChange,
+ MessageCollection: Whisper.MessageCollection,
+ });
+ await window.Signal.Data.removeConversation(convo.id, {
+ Conversation: Whisper.Conversation,
+ });
+
+ await store.removeIdentityKey(phoneNumberWithKeyChange);
});
describe('When we have a conversation with this contact', () => {
- let convo;
- before(async () => {
- convo = ConversationController.dangerouslyCreateAndAdd({
- id: phoneNumberWithKeyChange,
- type: 'private',
- });
- await window.Signal.Data.saveConversation(convo.attributes);
- });
-
- after(async () => {
- await window.Signal.Data.removeAllMessagesInConversation(convo.id, {
- logId: phoneNumberWithKeyChange,
- MessageCollection: Whisper.MessageCollection,
- });
- await window.Signal.Data.removeConversation(convo.id, {
- Conversation: Whisper.Conversation,
- });
- });
-
it('generates a key change notice in the private conversation with this contact', done => {
const original = convo.addKeyChange;
convo.addKeyChange = keyChangedId => {
@@ -54,21 +55,17 @@ describe('KeyChangeListener', () => {
describe('When we have a group with this contact', () => {
let groupConvo;
- let convo;
- before(async () => {
- convo = ConversationController.dangerouslyCreateAndAdd({
- id: phoneNumberWithKeyChange,
- type: 'private',
- });
+
+ beforeEach(async () => {
groupConvo = ConversationController.dangerouslyCreateAndAdd({
id: 'groupId',
type: 'group',
members: [convo.id],
});
- await window.Signal.Data.saveConversation(convo.attributes);
await window.Signal.Data.saveConversation(groupConvo.attributes);
});
- after(async () => {
+
+ afterEach(async () => {
await window.Signal.Data.removeAllMessagesInConversation(groupConvo.id, {
logId: phoneNumberWithKeyChange,
MessageCollection: Whisper.MessageCollection,
@@ -76,9 +73,6 @@ describe('KeyChangeListener', () => {
await window.Signal.Data.removeConversation(groupConvo.id, {
Conversation: Whisper.Conversation,
});
- await window.Signal.Data.removeConversation(convo.id, {
- Conversation: Whisper.Conversation,
- });
});
it('generates a key change notice in the group conversation with this contact', done => {
diff --git a/test/models/conversations_test.js b/ts/test-electron/models/conversations_test.ts
similarity index 74%
rename from test/models/conversations_test.js
rename to ts/test-electron/models/conversations_test.ts
index 3851851606..d32866eab5 100644
--- a/test/models/conversations_test.js
+++ b/ts/test-electron/models/conversations_test.ts
@@ -1,7 +1,18 @@
-// Copyright 2014-2020 Signal Messenger, LLC
+// Copyright 2014-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
+import { assert } from 'chai';
+
describe('Conversations', () => {
+ async function resetConversationController(): Promise {
+ window.ConversationController.reset();
+ await window.ConversationController.load();
+ }
+
+ beforeEach(resetConversationController);
+
+ afterEach(resetConversationController);
+
it('updates lastMessage even in race conditions with db', async () => {
const ourNumber = '+15550000000';
const ourUuid = window.getGuid();
@@ -12,6 +23,14 @@ describe('Conversations', () => {
e164: '+15551234567',
uuid: '2f2734aa-f69d-4c1c-98eb-50eb0fc512d7',
type: 'private',
+ inbox_position: 0,
+ isPinned: false,
+ markedUnread: false,
+ lastMessageDeletedForEveryone: false,
+ messageCount: 0,
+ sentMessageCount: 0,
+ profileSharing: true,
+ version: 0,
});
const destinationE164 = '+15557654321';
@@ -21,7 +40,7 @@ describe('Conversations', () => {
'my device'
);
window.textsecure.storage.user.setUuidAndDeviceId(ourUuid, 2);
- window.ConversationController._initialFetchComplete = true;
+ await window.ConversationController.loadPromise();
// Creating a fake message
const now = Date.now();
@@ -33,9 +52,9 @@ describe('Conversations', () => {
delivered_to: [destinationE164],
destination: destinationE164,
expirationStartTimestamp: now,
- hasAttachments: 0,
- hasFileAttachments: 0,
- hasVisualMediaAttachments: 0,
+ hasAttachments: false,
+ hasFileAttachments: false,
+ hasVisualMediaAttachments: false,
id: 'd8f2b435-e2ef-46e0-8481-07e68af251c6',
received_at: now,
recipients: [destinationE164],
@@ -52,9 +71,7 @@ describe('Conversations', () => {
Message: window.Whisper.Message,
});
message = window.MessageController.register(message.id, message);
- await window.Signal.Data.saveConversation(conversation.attributes, {
- Conversation: window.Whisper.Conversation,
- });
+ await window.Signal.Data.saveConversation(conversation.attributes);
await conversation.updateLastMessage();
// Should be set to bananas because that's the last message sent.
@@ -66,9 +83,9 @@ describe('Conversations', () => {
body: '',
bodyRanges: undefined,
attachments: [],
- quote: null,
+ quote: undefined,
contact: [],
- sticker: null,
+ sticker: undefined,
preview: [],
});