signal-desktop/ts/test-electron/sql/conversationSummary_test.ts

584 lines
16 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
2021-12-20 21:04:02 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import dataInterface from '../../sql/Client';
import { UUID } from '../../types/UUID';
import type { UUIDStringType } from '../../types/UUID';
2022-11-16 20:18:02 +00:00
import { DurationInSeconds } from '../../util/durations';
2021-12-20 21:04:02 +00:00
import type { MessageAttributesType } from '../../model-types.d';
const {
removeAll,
_getAllMessages,
saveMessages,
getConversationMessageStats,
2021-12-20 21:04:02 +00:00
} = dataInterface;
function getUuid(): UUIDStringType {
return UUID.generate().toString();
}
describe('sql/conversationSummary', () => {
beforeEach(async () => {
await removeAll();
});
describe('getConversationMessageStats', () => {
2021-12-20 21:04:02 +00:00
it('returns the latest message in current conversation', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
type: 'outgoing',
conversationId,
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'outgoing',
conversationId,
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
const message3: MessageAttributesType = {
id: getUuid(),
body: 'message 3',
type: 'outgoing',
conversationId: getUuid(),
sent_at: now + 3,
received_at: now + 3,
timestamp: now + 3,
};
await saveMessages([message1, message2, message3], {
forceSave: true,
ourUuid,
});
assert.lengthOf(await _getAllMessages(), 3);
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.activity?.body, message2.body, 'activity');
assert.strictEqual(messages.preview?.body, message2.body, 'preview');
assert.isTrue(messages.hasUserInitiatedMessages);
});
2022-04-20 23:33:38 +00:00
it('returns the latest message in current conversation excluding group story replies', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
type: 'outgoing',
conversationId,
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'outgoing',
conversationId,
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
storyId: getUuid(),
};
const message3: MessageAttributesType = {
id: getUuid(),
body: 'message 3',
type: 'incoming',
conversationId,
sent_at: now + 3,
received_at: now + 3,
timestamp: now + 3,
storyId: getUuid(),
};
await saveMessages([message1, message2, message3], {
forceSave: true,
ourUuid,
});
assert.lengthOf(await _getAllMessages(), 3);
const messages = await getConversationMessageStats({
conversationId,
includeStoryReplies: false,
2022-04-20 23:33:38 +00:00
ourUuid,
});
assert.strictEqual(messages.activity?.body, message1.body, 'activity');
assert.strictEqual(messages.preview?.body, message1.body, 'preview');
assert.isTrue(messages.hasUserInitiatedMessages);
});
2021-12-20 21:04:02 +00:00
it('preview excludes several message types, allows type = NULL', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
// @ts-expect-error We're forcing a null type here for testing
type: null,
conversationId,
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'change-number-notification',
conversationId,
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
const message3: MessageAttributesType = {
id: getUuid(),
body: 'message 3',
type: 'group-v1-migration',
conversationId,
sent_at: now + 3,
received_at: now + 3,
timestamp: now + 3,
};
const message4: MessageAttributesType = {
id: getUuid(),
body: 'message 5',
type: 'profile-change',
conversationId,
sent_at: now + 5,
received_at: now + 5,
timestamp: now + 5,
};
const message5: MessageAttributesType = {
2021-12-20 21:04:02 +00:00
id: getUuid(),
body: 'message 6',
type: 'story',
conversationId,
sent_at: now + 6,
received_at: now + 6,
timestamp: now + 6,
};
const message6: MessageAttributesType = {
2021-12-20 21:04:02 +00:00
id: getUuid(),
body: 'message 7',
type: 'universal-timer-notification',
conversationId,
sent_at: now + 7,
received_at: now + 7,
timestamp: now + 7,
};
const message7: MessageAttributesType = {
2021-12-20 21:04:02 +00:00
id: getUuid(),
body: 'message 8',
type: 'verified-change',
conversationId,
sent_at: now + 8,
received_at: now + 8,
timestamp: now + 8,
};
await saveMessages(
[message1, message2, message3, message4, message5, message6, message7],
2021-12-20 21:04:02 +00:00
{
forceSave: true,
ourUuid,
}
);
assert.lengthOf(await _getAllMessages(), 7);
2021-12-20 21:04:02 +00:00
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.preview?.body, message1.body);
});
it('activity excludes several message types, allows type = NULL', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
// @ts-expect-error We're forcing a null type here for testing
type: null,
conversationId,
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'change-number-notification',
conversationId,
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
const message3: MessageAttributesType = {
id: getUuid(),
body: 'message 3',
type: 'group-v1-migration',
conversationId,
sent_at: now + 3,
received_at: now + 3,
timestamp: now + 3,
};
const message4: MessageAttributesType = {
id: getUuid(),
body: 'message 4',
type: 'keychange',
conversationId,
sent_at: now + 4,
received_at: now + 4,
timestamp: now + 4,
};
const message5: MessageAttributesType = {
id: getUuid(),
body: 'message 6',
type: 'profile-change',
conversationId,
sent_at: now + 6,
received_at: now + 6,
timestamp: now + 6,
};
const message6: MessageAttributesType = {
2021-12-20 21:04:02 +00:00
id: getUuid(),
body: 'message 7',
type: 'story',
conversationId,
sent_at: now + 7,
received_at: now + 7,
timestamp: now + 7,
};
const message7: MessageAttributesType = {
2021-12-20 21:04:02 +00:00
id: getUuid(),
body: 'message 8',
type: 'universal-timer-notification',
conversationId,
sent_at: now + 8,
received_at: now + 8,
timestamp: now + 8,
};
const message8: MessageAttributesType = {
2021-12-20 21:04:02 +00:00
id: getUuid(),
body: 'message 9',
type: 'verified-change',
conversationId,
sent_at: now + 9,
received_at: now + 9,
timestamp: now + 9,
};
await saveMessages(
[
message1,
message2,
message3,
message4,
message5,
message6,
message7,
message8,
],
{
forceSave: true,
ourUuid,
}
);
assert.lengthOf(await _getAllMessages(), 8);
2021-12-20 21:04:02 +00:00
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.activity?.body, message1.body);
});
it('activity excludes expirationTimerUpdates with fromSync = true, includes fromSync = undefined', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
type: 'outgoing',
conversationId,
expirationTimerUpdate: {
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.fromSeconds(10),
2021-12-20 21:04:02 +00:00
source: 'you',
},
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'outgoing',
conversationId,
expirationTimerUpdate: {
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.fromSeconds(10),
2021-12-20 21:04:02 +00:00
fromSync: true,
},
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
await saveMessages([message1, message2], {
forceSave: true,
ourUuid,
});
assert.lengthOf(await _getAllMessages(), 2);
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.activity?.body, message1.body);
});
it('activity excludes expirationTimerUpdates with fromSync = true, includes fromSync = false', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
type: 'outgoing',
conversationId,
expirationTimerUpdate: {
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.fromSeconds(10),
2021-12-20 21:04:02 +00:00
source: 'you',
fromSync: false,
},
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'outgoing',
conversationId,
expirationTimerUpdate: {
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.fromSeconds(10),
2021-12-20 21:04:02 +00:00
fromSync: true,
},
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
await saveMessages([message1, message2], {
forceSave: true,
ourUuid,
});
assert.lengthOf(await _getAllMessages(), 2);
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.activity?.body, message1.body);
});
it('preview excludes expired message, includes non-disappearing message', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
type: 'outgoing',
conversationId,
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'outgoing',
conversationId,
expirationStartTimestamp: now - 2 * 1000,
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.fromSeconds(1),
2021-12-20 21:04:02 +00:00
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
await saveMessages([message1, message2], {
forceSave: true,
ourUuid,
});
assert.lengthOf(await _getAllMessages(), 2);
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.preview?.body, message1.body);
});
it('preview excludes expired message, includes non-disappearing message', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1',
type: 'outgoing',
conversationId,
expirationStartTimestamp: now,
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.fromSeconds(30),
2021-12-20 21:04:02 +00:00
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2',
type: 'outgoing',
conversationId,
expirationStartTimestamp: now - 2 * 1000,
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.fromSeconds(1),
2021-12-20 21:04:02 +00:00
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
await saveMessages([message1, message2], {
forceSave: true,
ourUuid,
});
assert.lengthOf(await _getAllMessages(), 2);
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.preview?.body, message1.body);
});
it('excludes group v2 change events where someone else leaves a group', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = getUuid();
const otherUuid = getUuid();
const ourUuid = getUuid();
const message1: MessageAttributesType = {
id: getUuid(),
body: 'message 1 - removing ourselves',
type: 'group-v2-change',
conversationId,
groupV2Change: {
from: ourUuid,
details: [
{
type: 'member-remove',
uuid: ourUuid,
},
],
},
sent_at: now + 1,
received_at: now + 1,
timestamp: now + 1,
};
const message2: MessageAttributesType = {
id: getUuid(),
body: 'message 2 - someone else leaving',
type: 'group-v2-change',
conversationId,
groupV2Change: {
from: otherUuid,
details: [
{
type: 'member-remove',
uuid: otherUuid,
},
],
},
sent_at: now + 2,
received_at: now + 2,
timestamp: now + 2,
};
await saveMessages([message1, message2], {
forceSave: true,
ourUuid,
});
assert.lengthOf(await _getAllMessages(), 2);
const messages = await getConversationMessageStats({
2021-12-20 21:04:02 +00:00
conversationId,
ourUuid,
includeStoryReplies: false,
2021-12-20 21:04:02 +00:00
});
assert.strictEqual(messages.activity?.body, message1.body, 'activity');
assert.strictEqual(messages.preview?.body, message1.body, 'preview');
assert.isFalse(messages.hasUserInitiatedMessages);
});
});
});