2021-12-08 19:52:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// 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';
|
|
|
|
|
|
|
|
import type { MessageAttributesType } from '../../model-types.d';
|
|
|
|
|
2022-10-22 06:26:16 +00:00
|
|
|
const { removeAll, _getAllMessages, saveMessages, getAllStories } =
|
2021-12-08 19:52:46 +00:00
|
|
|
dataInterface;
|
|
|
|
|
|
|
|
function getUuid(): UUIDStringType {
|
|
|
|
return UUID.generate().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('sql/stories', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await removeAll();
|
|
|
|
});
|
|
|
|
|
2022-10-22 06:26:16 +00:00
|
|
|
describe('getAllStories', () => {
|
2021-12-08 19:52:46 +00:00
|
|
|
it('returns N most recent stories overall, or in converation, or by author', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const now = Date.now();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const sourceUuid = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const story1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story 1',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
sourceUuid: getUuid(),
|
|
|
|
};
|
|
|
|
const story2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story 2',
|
|
|
|
type: 'story',
|
|
|
|
conversationId: getUuid(),
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
sourceUuid,
|
|
|
|
};
|
|
|
|
const story3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'incoming',
|
|
|
|
conversationId: getUuid(),
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
sourceUuid,
|
|
|
|
};
|
|
|
|
const story4: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story 4',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
sourceUuid: getUuid(),
|
|
|
|
};
|
|
|
|
const story5: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story 5',
|
|
|
|
type: 'story',
|
|
|
|
conversationId: getUuid(),
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
sourceUuid,
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveMessages([story1, story2, story3, story4, story5], {
|
|
|
|
forceSave: true,
|
2021-12-20 21:04:02 +00:00
|
|
|
ourUuid,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 5);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2022-10-22 06:26:16 +00:00
|
|
|
const stories = await getAllStories({});
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.lengthOf(stories, 4, 'expect four total stories');
|
|
|
|
|
2022-03-29 01:10:08 +00:00
|
|
|
// They are in ASC order
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
stories[0].id,
|
2022-03-29 01:10:08 +00:00
|
|
|
story1.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
'stories first should be story5'
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
stories[3].id,
|
2022-03-29 01:10:08 +00:00
|
|
|
story5.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
'stories last should be story1'
|
|
|
|
);
|
|
|
|
|
2022-10-22 06:26:16 +00:00
|
|
|
const storiesInConversation = await getAllStories({
|
2021-12-08 19:52:46 +00:00
|
|
|
conversationId,
|
|
|
|
});
|
|
|
|
assert.lengthOf(
|
|
|
|
storiesInConversation,
|
|
|
|
2,
|
|
|
|
'expect two stories in conversaton'
|
|
|
|
);
|
|
|
|
|
2022-03-29 01:10:08 +00:00
|
|
|
// They are in ASC order
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
storiesInConversation[0].id,
|
2022-03-29 01:10:08 +00:00
|
|
|
story1.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
'storiesInConversation first should be story4'
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
storiesInConversation[1].id,
|
2022-03-29 01:10:08 +00:00
|
|
|
story4.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
'storiesInConversation last should be story1'
|
|
|
|
);
|
|
|
|
|
2022-10-22 06:26:16 +00:00
|
|
|
const storiesByAuthor = await getAllStories({
|
2021-12-08 19:52:46 +00:00
|
|
|
sourceUuid,
|
|
|
|
});
|
|
|
|
assert.lengthOf(storiesByAuthor, 2, 'expect two stories by author');
|
|
|
|
|
2022-03-29 01:10:08 +00:00
|
|
|
// They are in ASC order
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
storiesByAuthor[0].id,
|
2022-03-29 01:10:08 +00:00
|
|
|
story2.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
'storiesByAuthor first should be story5'
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
storiesByAuthor[1].id,
|
2022-03-29 01:10:08 +00:00
|
|
|
story5.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
'storiesByAuthor last should be story2'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|