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

116 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-03-20 22:23:53 +00:00
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { v4 as generateUuid } from 'uuid';
2023-03-20 22:23:53 +00:00
import { generateAci } from '../../types/ServiceId';
2024-07-22 18:16:33 +00:00
import { DataReader, DataWriter } from '../../sql/Client';
2023-03-20 22:23:53 +00:00
import type { MessageAttributesType } from '../../model-types';
2024-07-22 18:16:33 +00:00
const { _getAllMessages, getMessagesBetween } = DataReader;
const { saveMessages, _removeAllMessages } = DataWriter;
2023-03-20 22:23:53 +00:00
describe('sql/getMessagesBetween', () => {
beforeEach(async () => {
await _removeAllMessages();
});
it('finds all messages between two in-order messages', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = generateUuid();
const ourAci = generateAci();
2023-03-20 22:23:53 +00:00
function getMessage(body: string, offset: number): MessageAttributesType {
return {
id: generateUuid(),
2023-03-20 22:23:53 +00:00
body,
type: 'outgoing',
conversationId,
sent_at: now + offset,
received_at: now + offset,
timestamp: now + offset,
};
}
const message1 = getMessage('message 1', -50);
const message2 = getMessage('message 2', -40); // after
const message3 = getMessage('message 3', -30);
const message4 = getMessage('message 4', -20); // before
const message5 = getMessage('message 5', -10);
await saveMessages([message1, message2, message3, message4, message5], {
forceSave: true,
ourAci,
2023-03-20 22:23:53 +00:00
});
assert.lengthOf(await _getAllMessages(), 5);
const ids = await getMessagesBetween(conversationId, {
after: {
received_at: message2.received_at,
sent_at: message2.sent_at,
},
before: {
received_at: message4.received_at,
sent_at: message4.sent_at,
},
includeStoryReplies: false,
});
assert.lengthOf(ids, 1);
assert.deepEqual(ids, [message3.id]);
});
it('returns based on timestamps even if one message doesnt exist', async () => {
assert.lengthOf(await _getAllMessages(), 0);
const now = Date.now();
const conversationId = generateUuid();
const ourAci = generateAci();
2023-03-20 22:23:53 +00:00
function getMessage(body: string, offset: number): MessageAttributesType {
return {
id: generateUuid(),
2023-03-20 22:23:53 +00:00
body,
type: 'outgoing',
conversationId,
sent_at: now + offset,
received_at: now + offset,
timestamp: now + offset,
};
}
const message1 = getMessage('message 1', -50);
const message2 = getMessage('message 2', -40); // after
const message3 = getMessage('message 3', -30);
const message4 = getMessage('message 4', -20); // before, doesn't exist
const message5 = getMessage('message 5', -10);
await saveMessages([message1, message2, message3, message5], {
forceSave: true,
ourAci,
2023-03-20 22:23:53 +00:00
});
assert.lengthOf(await _getAllMessages(), 4);
const ids = await getMessagesBetween(conversationId, {
after: {
received_at: message2.received_at,
sent_at: message2.sent_at,
},
before: {
received_at: message4.received_at,
sent_at: message4.sent_at,
},
includeStoryReplies: false,
});
assert.lengthOf(ids, 1);
assert.deepEqual(ids, [message3.id]);
});
});