2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-04-13 00:45:14 +00:00
|
|
|
import { assert } from 'chai';
|
2018-04-13 20:27:06 +00:00
|
|
|
import { shuffle } from 'lodash';
|
2018-04-13 00:45:14 +00:00
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
import { IMAGE_JPEG } from '../../../types/MIME';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Section } from '../../../components/conversation/media-gallery/groupMediaItemsByDate';
|
|
|
|
import { groupMediaItemsByDate } from '../../../components/conversation/media-gallery/groupMediaItemsByDate';
|
|
|
|
import type { MediaItemType } from '../../../types/MediaItem';
|
2021-10-05 22:10:08 +00:00
|
|
|
import { fakeAttachment } from '../../../test-both/helpers/fakeAttachment';
|
2018-04-13 00:45:14 +00:00
|
|
|
|
2021-09-01 20:33:08 +00:00
|
|
|
const testDate = (
|
|
|
|
year: number,
|
|
|
|
month: number,
|
|
|
|
day: number,
|
|
|
|
hour: number,
|
|
|
|
minute: number,
|
|
|
|
second = 0
|
|
|
|
): Date => new Date(Date.UTC(year, month - 1, day, hour, minute, second, 0));
|
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
const toMediaItem = (date: Date): MediaItemType => ({
|
|
|
|
objectURL: date.toUTCString(),
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: date.getTime(),
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: date.getTime(),
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: date.getTime(),
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
});
|
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
describe('groupMediaItemsByDate', () => {
|
|
|
|
it('should group mediaItems', () => {
|
2021-09-01 20:33:08 +00:00
|
|
|
const referenceTime = testDate(2018, 4, 12, 18, 0, 0).getTime(); // Thu
|
2018-11-14 18:47:19 +00:00
|
|
|
const input: Array<MediaItemType> = shuffle([
|
2018-04-13 00:45:14 +00:00
|
|
|
// Today
|
2021-09-01 20:33:08 +00:00
|
|
|
toMediaItem(testDate(2018, 4, 12, 12, 0)), // Thu
|
|
|
|
toMediaItem(testDate(2018, 4, 12, 0, 1)), // Thu
|
2018-04-13 00:45:14 +00:00
|
|
|
// This week
|
2021-09-01 20:33:08 +00:00
|
|
|
toMediaItem(testDate(2018, 4, 11, 23, 59)), // Wed
|
|
|
|
toMediaItem(testDate(2018, 4, 9, 0, 1)), // Mon
|
2018-04-13 00:45:14 +00:00
|
|
|
// This month
|
2021-09-01 20:33:08 +00:00
|
|
|
toMediaItem(testDate(2018, 4, 8, 23, 59)), // Sun
|
|
|
|
toMediaItem(testDate(2018, 4, 1, 0, 1)),
|
2018-04-13 00:45:14 +00:00
|
|
|
// March 2018
|
2021-09-01 20:33:08 +00:00
|
|
|
toMediaItem(testDate(2018, 3, 31, 23, 59)),
|
|
|
|
toMediaItem(testDate(2018, 3, 1, 14, 0)),
|
2018-04-13 00:45:14 +00:00
|
|
|
// February 2011
|
2021-09-01 20:33:08 +00:00
|
|
|
toMediaItem(testDate(2011, 2, 28, 23, 59)),
|
|
|
|
toMediaItem(testDate(2011, 2, 1, 10, 0)),
|
2018-04-13 20:27:06 +00:00
|
|
|
]);
|
2018-04-13 00:45:14 +00:00
|
|
|
|
2018-04-13 20:27:06 +00:00
|
|
|
const expected: Array<Section> = [
|
|
|
|
{
|
|
|
|
type: 'today',
|
2018-11-14 18:47:19 +00:00
|
|
|
mediaItems: [
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Thu, 12 Apr 2018 12:00:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1523534400000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1523534400000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1523534400000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Thu, 12 Apr 2018 00:01:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1523491260000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1523491260000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1523491260000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yesterday',
|
2018-11-14 18:47:19 +00:00
|
|
|
mediaItems: [
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Wed, 11 Apr 2018 23:59:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1523491140000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1523491140000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1523491140000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'thisWeek',
|
2018-11-14 18:47:19 +00:00
|
|
|
mediaItems: [
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Mon, 09 Apr 2018 00:01:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1523232060000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1523232060000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1523232060000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'thisMonth',
|
2018-11-14 18:47:19 +00:00
|
|
|
mediaItems: [
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Sun, 08 Apr 2018 23:59:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1523231940000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1523231940000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1523231940000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Sun, 01 Apr 2018 00:01:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1522540860000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1522540860000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1522540860000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yearMonth',
|
|
|
|
year: 2018,
|
|
|
|
month: 2,
|
2018-11-14 18:47:19 +00:00
|
|
|
mediaItems: [
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Sat, 31 Mar 2018 23:59:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1522540740000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1522540740000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1522540740000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Thu, 01 Mar 2018 14:00:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1519912800000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1519912800000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1519912800000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'yearMonth',
|
|
|
|
year: 2011,
|
|
|
|
month: 1,
|
2018-11-14 18:47:19 +00:00
|
|
|
mediaItems: [
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Mon, 28 Feb 2011 23:59:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1298937540000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1298937540000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1298937540000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
{
|
2018-11-14 18:47:19 +00:00
|
|
|
objectURL: 'Tue, 01 Feb 2011 10:00:00 GMT',
|
|
|
|
index: 0,
|
|
|
|
message: {
|
2021-08-23 23:14:53 +00:00
|
|
|
conversationId: '1234',
|
2018-11-14 18:47:19 +00:00
|
|
|
id: 'id',
|
|
|
|
received_at: 1296554400000,
|
2021-03-04 21:44:57 +00:00
|
|
|
received_at_ms: 1296554400000,
|
2018-11-14 18:47:19 +00:00
|
|
|
attachments: [],
|
2021-08-26 20:42:25 +00:00
|
|
|
sent_at: 1296554400000,
|
2018-11-14 18:47:19 +00:00
|
|
|
},
|
2021-10-05 22:10:08 +00:00
|
|
|
attachment: fakeAttachment({
|
2018-11-14 18:47:19 +00:00
|
|
|
fileName: 'fileName',
|
|
|
|
contentType: IMAGE_JPEG,
|
|
|
|
url: 'url',
|
2021-10-05 22:10:08 +00:00
|
|
|
}),
|
2018-04-13 00:45:14 +00:00
|
|
|
},
|
2018-04-13 20:27:06 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2018-04-13 00:45:14 +00:00
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
const actual = groupMediaItemsByDate(referenceTime, input);
|
2018-04-13 00:45:14 +00:00
|
|
|
assert.deepEqual(actual, expected);
|
|
|
|
});
|
|
|
|
});
|