2021-12-08 19:52:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { v4 as generateUuid } from 'uuid';
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../../sql/Client';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci } from '../../types/ServiceId';
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
import type { StoryReadType } from '../../sql/Interface';
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const { _getAllStoryReads, getLastStoryReadsForAuthor } = DataReader;
|
|
|
|
|
|
|
|
const { _deleteAllStoryReads, addNewStoryRead } = DataWriter;
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
describe('sql/storyReads', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await _deleteAllStoryReads();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('roundtrips with create/fetch/delete', async () => {
|
|
|
|
assert.lengthOf(await _getAllStoryReads(), 0);
|
|
|
|
|
|
|
|
const read: StoryReadType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
authorId: generateAci(),
|
|
|
|
conversationId: generateUuid(),
|
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: Date.now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
await addNewStoryRead(read);
|
|
|
|
|
|
|
|
const allReads = await _getAllStoryReads();
|
|
|
|
assert.lengthOf(allReads, 1);
|
|
|
|
assert.deepEqual(allReads[0], read);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getLastStoryReadsForAuthor', () => {
|
|
|
|
it('returns n = limit items for author', async () => {
|
|
|
|
const now = Date.now();
|
2023-08-10 16:43:33 +00:00
|
|
|
const authorId = generateAci();
|
2021-12-08 19:52:46 +00:00
|
|
|
const read1: StoryReadType = {
|
|
|
|
authorId,
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now - 20,
|
|
|
|
};
|
|
|
|
const read2: StoryReadType = {
|
|
|
|
authorId,
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now - 10,
|
|
|
|
};
|
|
|
|
const read3: StoryReadType = {
|
|
|
|
authorId,
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now,
|
|
|
|
};
|
|
|
|
const read4: StoryReadType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
authorId: generateAci(),
|
|
|
|
conversationId: generateUuid(),
|
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now,
|
|
|
|
};
|
|
|
|
|
|
|
|
await addNewStoryRead(read1);
|
|
|
|
await addNewStoryRead(read2);
|
|
|
|
await addNewStoryRead(read3);
|
|
|
|
await addNewStoryRead(read4);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryReads(), 4);
|
|
|
|
|
|
|
|
const lastReads = await getLastStoryReadsForAuthor({
|
|
|
|
authorId,
|
|
|
|
limit: 2,
|
|
|
|
});
|
|
|
|
assert.lengthOf(lastReads, 2);
|
|
|
|
assert.deepEqual([read3, read2], lastReads);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns only items in provided conversation', async () => {
|
|
|
|
const now = Date.now();
|
2023-08-10 16:43:33 +00:00
|
|
|
const authorId = generateAci();
|
|
|
|
const conversationId = generateUuid();
|
2021-12-08 19:52:46 +00:00
|
|
|
const read1: StoryReadType = {
|
|
|
|
authorId,
|
|
|
|
conversationId,
|
2023-08-10 16:43:33 +00:00
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now - 20,
|
|
|
|
};
|
|
|
|
const read2: StoryReadType = {
|
|
|
|
authorId,
|
|
|
|
conversationId,
|
2023-08-10 16:43:33 +00:00
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now - 10,
|
|
|
|
};
|
|
|
|
const read3: StoryReadType = {
|
|
|
|
authorId,
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now,
|
|
|
|
};
|
|
|
|
const read4: StoryReadType = {
|
|
|
|
authorId,
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
storyReadDate: now,
|
|
|
|
};
|
|
|
|
|
|
|
|
await addNewStoryRead(read1);
|
|
|
|
await addNewStoryRead(read2);
|
|
|
|
await addNewStoryRead(read3);
|
|
|
|
await addNewStoryRead(read4);
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllStoryReads(), 4);
|
|
|
|
|
|
|
|
const lastReads = await getLastStoryReadsForAuthor({
|
|
|
|
authorId,
|
|
|
|
conversationId,
|
|
|
|
limit: 1,
|
|
|
|
});
|
|
|
|
assert.lengthOf(lastReads, 1);
|
|
|
|
assert.deepEqual([read2], lastReads);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|