mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-22 03:18:12 +00:00
Pull-to-refresh on a channel does a plain page-0 fetch, which is exactly the thing that cannot recover a channel hidden by bad PostsInChannel bookkeeping: the interval it creates sorts below the bad one, so the list keeps rendering the bad interval and stays empty. Refreshing is also the moment the user is telling us the screen looks wrong, so it is a good place to escalate. refreshPostsForChannel now takes whether the list is rendering nothing. When it is, and the channel still has non-deleted posts stored, those posts are hidden rather than absent: clear the channel's cached posts and re-fetch from scratch, the same recovery as "Reset cached messages". Otherwise -- posts on screen, or a genuinely empty channel -- keep the previous page fetch. This covers the cases the interval prune cannot: an interval that holds posts none of which the list renders (all replies with CRT on, or only join/leave messages with that preference off) survives the prune but still shows nothing. The blankness signal is `orderedPosts`, i.e. what the list actually renders after preparePostList, so it accounts for the filtering the prune's DB-level check cannot see. The warning it logs (channel id and a count, no PII) is also the breadcrumb that was missing from the report that started this. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1378 lines
57 KiB
TypeScript
1378 lines
57 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
/* eslint-disable max-lines */
|
|
|
|
import {storePostsForChannel} from '@actions/local/post';
|
|
import {ActionType, Post, ServerErrors} from '@constants';
|
|
import {SYSTEM_IDENTIFIERS} from '@constants/database';
|
|
import DatabaseManager from '@database/manager';
|
|
import PostModel from '@database/models/server/post';
|
|
import NetworkManager from '@managers/network_manager';
|
|
import {getMyChannel} from '@queries/servers/channel';
|
|
import {getPostById, getRecentPostsInChannel, queryPostsInChannel} from '@queries/servers/post';
|
|
import TestHelper from '@test/test_helper';
|
|
import {getFullErrorMessage} from '@utils/errors';
|
|
|
|
import {
|
|
createPost,
|
|
retryFailedPost,
|
|
togglePinPost,
|
|
deletePost,
|
|
markPostAsUnread,
|
|
editPost,
|
|
acknowledgePost,
|
|
unacknowledgePost,
|
|
revealBoRPost,
|
|
fetchPostsForChannel,
|
|
fetchPostsForUnreadChannels,
|
|
refreshPostsForChannel,
|
|
fetchPosts,
|
|
fetchPostsBefore,
|
|
fetchPostsSince,
|
|
fetchPostAuthors,
|
|
fetchPostThread,
|
|
fetchPostsAround,
|
|
fetchMissingChannelsFromPosts,
|
|
fetchPostById,
|
|
fetchPostInfo,
|
|
fetchSavedPosts,
|
|
fetchPinnedPosts,
|
|
burnPostNow,
|
|
} from './post';
|
|
import * as PostAuxilaryFunctions from './post.auxiliary';
|
|
|
|
import type ServerDataOperator from '@database/operator/server_data_operator';
|
|
import type {Database} from '@nozbe/watermelondb';
|
|
|
|
const serverUrl = 'baseHandler.test.com';
|
|
let operator: ServerDataOperator;
|
|
|
|
const channelId = 'channelid1';
|
|
const teamId = 'teamid1';
|
|
|
|
const user1 = {id: 'userid1', username: 'user1', email: 'user1@mattermost.com', roles: ''} as UserProfile;
|
|
const user2 = {id: 'userid2', username: 'user2', email: 'user2@mattermost.com', roles: ''} as UserProfile;
|
|
|
|
const post1 = TestHelper.fakePost({channel_id: channelId, id: 'postid1', user_id: user1.id});
|
|
const post2 = TestHelper.fakePost({channel_id: channelId, id: 'postid2', user_id: user2.id});
|
|
const reply1 = TestHelper.fakePost({channel_id: channelId, id: 'replyid1', root_id: post1.id, user_id: user2.id});
|
|
|
|
const channel1 = {
|
|
id: channelId,
|
|
team_id: teamId,
|
|
total_msg_count: 12,
|
|
creator_id: user1.id,
|
|
delete_at: 0,
|
|
} as Channel;
|
|
|
|
const channelMember1 = {
|
|
id: 'memberid1',
|
|
channel_id: channelId,
|
|
user_id: user1.id,
|
|
msg_count: 10,
|
|
} as ChannelMembership;
|
|
|
|
const fileInfo1: FileInfo = {
|
|
id: 'fileid',
|
|
clientId: 'clientid',
|
|
localPath: 'path1',
|
|
} as FileInfo;
|
|
|
|
const mockPostModel = (overrides: Partial<PostModel> = {}): PostModel => ({
|
|
id: 'post-id',
|
|
channelId: 'channel-id',
|
|
createAt: Date.now(),
|
|
deleteAt: 0,
|
|
type: 'custom_post_type',
|
|
userId: 'user-id',
|
|
...overrides,
|
|
} as PostModel);
|
|
|
|
const throwFunc = () => {
|
|
throw Error('error');
|
|
};
|
|
|
|
const acknowledgedTime = Date.now();
|
|
|
|
const genericGetPostsMock = jest.fn((_channelId: string) => ({posts: {[post1.id]: {...post1, channel_id: _channelId}, [post2.id]: {...post2, channel_id: _channelId}}, order: [post1.id, post2.id]}));
|
|
|
|
const mockClient = {
|
|
createPost: jest.fn((post: Post) => ({...post, id: 'newid'})),
|
|
pinPost: jest.fn(),
|
|
unpinPost: jest.fn(),
|
|
deletePost: jest.fn(),
|
|
burnPostNow: jest.fn(),
|
|
getChannel: jest.fn((_channelId: string) => ({id: _channelId, name: 'channel1', creatorId: user1.id, total_msg_count: 100})),
|
|
getChannelMember: jest.fn((_channelId: string, userId: string) => ({id: userId + '-' + _channelId, user_id: userId, channel_id: _channelId, roles: '', msg_count: 100, mention_count: 0})),
|
|
getMyChannelMember: jest.fn((_channelId: string) => ({id: user1.id + '-' + _channelId, user_id: user1.id, channel_id: _channelId, roles: '', msg_count: 100, mention_count: 0})),
|
|
markPostAsUnread: jest.fn(),
|
|
patchPost: jest.fn((message: string, postId: string) => ({...post1, id: postId, message})),
|
|
acknowledgePost: jest.fn(() => ({acknowledged_at: acknowledgedTime})),
|
|
unacknowledgePost: jest.fn(),
|
|
revealBoRPost: jest.fn((_postId: string) => ({
|
|
...post1,
|
|
id: _postId,
|
|
message: 'Revealed message content',
|
|
metadata: {
|
|
files: [{id: 'file1', name: 'revealed-file.pdf'}],
|
|
},
|
|
props: {
|
|
revealed: true,
|
|
},
|
|
})),
|
|
getPosts: genericGetPostsMock,
|
|
getPostsBefore: genericGetPostsMock,
|
|
getPostsSince: jest.fn((_channelId: string, since: number) => ({posts: {[post1.id]: {...post1, channel_id: _channelId, create_at: since + 1}, [post2.id]: {...post2, channel_id: _channelId, create_at: since + 2}}, order: [post1.id, post2.id]})),
|
|
getProfilesByIds: jest.fn((ids: string[]) => (ids.map((id) => ({...user1, id})))),
|
|
getProfilesByUsernames: jest.fn((names: string[]) => (names.map((name) => ({...user1, username: name, id: 'id' + name})))),
|
|
getPostThread: jest.fn((_postId: string) => ({posts: {[_postId]: {...post1, id: _postId}, [reply1.id]: {...reply1, root_id: _postId}}, order: [_postId, reply1.id]})),
|
|
getPostsAfter: genericGetPostsMock,
|
|
getPost: jest.fn((_postId: string) => ({...post2, id: _postId})),
|
|
getPostInfo: jest.fn(() => ({
|
|
channel_id: channelId,
|
|
channel_type: 'O' as ChannelType,
|
|
channel_display_name: 'Town Square',
|
|
has_joined_channel: false,
|
|
team_id: teamId,
|
|
team_type: 'O',
|
|
team_display_name: 'Test Team',
|
|
has_joined_team: true,
|
|
})),
|
|
getSavedPosts: genericGetPostsMock,
|
|
getPinnedPosts: genericGetPostsMock,
|
|
};
|
|
|
|
let mockGetIsCRTEnabled: jest.Mock;
|
|
jest.mock('@queries/servers/thread', () => {
|
|
const original = jest.requireActual('@queries/servers/thread');
|
|
mockGetIsCRTEnabled = jest.fn(() => true);
|
|
return {
|
|
...original,
|
|
getIsCRTEnabled: mockGetIsCRTEnabled,
|
|
};
|
|
});
|
|
|
|
let mockAddRecentReaction: jest.Mock;
|
|
jest.mock('@actions/local/reactions', () => {
|
|
const original = jest.requireActual('@actions/local/reactions');
|
|
mockAddRecentReaction = jest.fn(() => [{user_id: 'userid1', emoji_name: 'smile'}]);
|
|
return {
|
|
...original,
|
|
addRecentReaction: mockAddRecentReaction,
|
|
};
|
|
});
|
|
|
|
let mockFetchChannelStats: jest.Mock;
|
|
jest.mock('@actions/remote/channel', () => {
|
|
const original = jest.requireActual('@actions/remote/channel');
|
|
mockFetchChannelStats = jest.fn(() => Promise.resolve({}));
|
|
return {
|
|
...original,
|
|
fetchChannelStats: mockFetchChannelStats,
|
|
};
|
|
});
|
|
|
|
beforeAll(() => {
|
|
// @ts-ignore
|
|
NetworkManager.getClient = () => mockClient;
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await DatabaseManager.init([serverUrl]);
|
|
operator = DatabaseManager.serverDatabases[serverUrl]!.operator;
|
|
mockFetchChannelStats.mockClear();
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await DatabaseManager.destroyServerDatabase(serverUrl);
|
|
});
|
|
|
|
describe('create, update & delete posts', () => {
|
|
it('createPost - handle database not found', async () => {
|
|
const result = await createPost('foo', post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - handle client error', async () => {
|
|
jest.spyOn(NetworkManager, 'getClient').mockImplementationOnce(throwFunc);
|
|
|
|
const result = await createPost(serverUrl, post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - handle existing failed post', async () => {
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [{...post1, props: {failed: false}}],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await createPost(serverUrl, {...post1, pending_post_id: post1.id});
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBe(false);
|
|
});
|
|
|
|
it('createPost - fail create', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(throwFunc));
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await createPost(serverUrl, post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - fail on deleted root post server error', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(() => {
|
|
// eslint-disable-next-line no-throw-literal
|
|
throw {message: 'error', server_error_id: ServerErrors.DELETED_ROOT_POST_ERROR};
|
|
}));
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await createPost(serverUrl, post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - fail on town square read only server error', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(() => {
|
|
// eslint-disable-next-line no-throw-literal
|
|
throw {message: 'error', server_error_id: ServerErrors.TOWN_SQUARE_READ_ONLY_ERROR};
|
|
}));
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await createPost(serverUrl, post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - fail on plugin dismissed post server error', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(() => {
|
|
// eslint-disable-next-line no-throw-literal
|
|
throw {message: 'error', server_error_id: ServerErrors.PLUGIN_DISMISSED_POST_ERROR};
|
|
}));
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await createPost(serverUrl, post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - root', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await createPost(serverUrl, post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - without reactions', async () => {
|
|
mockAddRecentReaction.mockImplementationOnce(() => []);
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await createPost(serverUrl, post1);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBeTruthy();
|
|
});
|
|
|
|
it('createPost - reply', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await createPost(serverUrl, reply1, [fileInfo1]);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.data).toBeTruthy();
|
|
});
|
|
|
|
it('retryFailedPost - handle database not found', async () => {
|
|
const result = await retryFailedPost('foo', mockPostModel());
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('retryFailedPost - handle client error', async () => {
|
|
jest.spyOn(NetworkManager, 'getClient').mockImplementationOnce(throwFunc);
|
|
|
|
const result = await retryFailedPost(serverUrl, mockPostModel({id: post1.id, prepareUpdate: jest.fn(), toApi: async () => post1}));
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('retryFailedPost - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await retryFailedPost(serverUrl, mockPostModel({id: post1.id, prepareUpdate: jest.fn(), toApi: async () => post1}));
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
});
|
|
|
|
it('retryFailedPost - handle error', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(throwFunc));
|
|
const result = await retryFailedPost(serverUrl, mockPostModel({id: post1.id, prepareUpdate: jest.fn(), toApi: async () => post1}));
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('retryFailedPost - fail on deleted root post server error', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(() => {
|
|
// eslint-disable-next-line no-throw-literal
|
|
throw {message: 'error', server_error_id: ServerErrors.DELETED_ROOT_POST_ERROR};
|
|
}));
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await retryFailedPost(serverUrl, mockPostModel({id: post1.id, prepareUpdate: jest.fn(), toApi: async () => post1}));
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeDefined();
|
|
});
|
|
|
|
it('retryFailedPost - fail on town square read only server error', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(() => {
|
|
// eslint-disable-next-line no-throw-literal
|
|
throw {message: 'error', server_error_id: ServerErrors.TOWN_SQUARE_READ_ONLY_ERROR};
|
|
}));
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await retryFailedPost(serverUrl, mockPostModel({id: post1.id, prepareUpdate: jest.fn(), toApi: async () => post1}));
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeDefined();
|
|
});
|
|
|
|
it('retryFailedPost - fail on plugin dismissed post server error', async () => {
|
|
mockClient.createPost.mockImplementationOnce(jest.fn(() => {
|
|
// eslint-disable-next-line no-throw-literal
|
|
throw {message: 'error', server_error_id: ServerErrors.PLUGIN_DISMISSED_POST_ERROR};
|
|
}));
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await retryFailedPost(serverUrl, mockPostModel({id: post1.id, prepareUpdate: jest.fn(), toApi: async () => post1}));
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeDefined();
|
|
});
|
|
|
|
it('togglePinPost - handle database not found', async () => {
|
|
const result = await togglePinPost('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('togglePinPost - base case', async () => {
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await togglePinPost(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
expect(result.post?.isPinned).toBe(true);
|
|
});
|
|
|
|
it('deletePost - handle error', async () => {
|
|
mockClient.deletePost.mockImplementationOnce(jest.fn(throwFunc));
|
|
const result = await deletePost('foo', {} as PostModel);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('deletePost - base case', async () => {
|
|
const postModels = await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await deletePost(serverUrl, postModels[0] as PostModel);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
});
|
|
|
|
it('burnPostNow - handle error', async () => {
|
|
mockClient.burnPostNow.mockImplementationOnce(jest.fn(throwFunc));
|
|
const result = await burnPostNow('foo', {} as PostModel);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('burnPostNow - base case', async () => {
|
|
mockClient.burnPostNow.mockReset();
|
|
const borPost = TestHelper.fakePost({channel_id: channelId, id: 'bor_postid1', user_id: user1.id, type: 'burn_on_read'});
|
|
|
|
const postModels = await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [borPost.id],
|
|
posts: [borPost],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
// verify post exists in database before burn
|
|
const {database} = operator;
|
|
const existingPost = await database.get('Post').find(borPost.id);
|
|
expect(existingPost).toBeDefined();
|
|
|
|
const result = await burnPostNow(serverUrl, postModels[0] as PostModel);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
|
|
// Verify that the post is deleted from the database
|
|
try {
|
|
await database.get('Post').find(borPost.id);
|
|
expect(true).toBe(false); // Should not reach here
|
|
} catch (error) {
|
|
expect(error).toBeDefined();
|
|
expect(getFullErrorMessage(error)).toBe('Record Post#bor_postid1 not found');
|
|
}
|
|
});
|
|
|
|
it('burnPostNow - returns error for non-BoR posts', async () => {
|
|
const result = await burnPostNow('foo', {} as PostModel);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
expect(result.error).toBe('Post is not a Burn-on-Read post');
|
|
});
|
|
|
|
it('deletePost - system post', async () => {
|
|
const postModels = await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [{...post1, id: `user-activity-${post1.id}`, type: Post.POST_TYPES.COMBINED_USER_ACTIVITY as PostType, props: {system_post_ids: [post1.id]}}],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await deletePost(serverUrl, postModels[0] as PostModel);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
});
|
|
|
|
it('markPostAsUnread - handle error', async () => {
|
|
mockClient.deletePost.mockImplementationOnce(jest.fn(throwFunc));
|
|
const result = await markPostAsUnread('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('markPostAsUnread - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await markPostAsUnread(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
});
|
|
|
|
it('markPostAsUnread - no current user', async () => {
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await markPostAsUnread(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
});
|
|
|
|
it('editPost - handle error', async () => {
|
|
mockClient.deletePost.mockImplementationOnce(jest.fn(throwFunc));
|
|
const result = await editPost('foo', '', '', [], []);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('editPost - base case', async () => {
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await editPost(serverUrl, post1.id, 'new message', [], []);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
});
|
|
|
|
it('editPost - delete files', async () => {
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const testFiles: FileInfo[] = [
|
|
TestHelper.fakeFileInfo({
|
|
id: 'file-1',
|
|
post_id: post1.id,
|
|
name: 'test-file-1.jpg',
|
|
extension: 'jpg',
|
|
size: 1024,
|
|
mime_type: 'image/jpeg',
|
|
user_id: user1.id,
|
|
}),
|
|
TestHelper.fakeFileInfo({
|
|
id: 'file-2',
|
|
post_id: post1.id,
|
|
name: 'test-file-2.png',
|
|
extension: 'png',
|
|
size: 2048,
|
|
mime_type: 'image/png',
|
|
user_id: user1.id,
|
|
}),
|
|
TestHelper.fakeFileInfo({
|
|
id: 'file-3',
|
|
post_id: post1.id,
|
|
name: 'test-file-3.pdf',
|
|
extension: 'pdf',
|
|
size: 4096,
|
|
mime_type: 'application/pdf',
|
|
user_id: user1.id,
|
|
}),
|
|
];
|
|
|
|
await operator.handleFiles({
|
|
files: testFiles,
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const {database} = operator;
|
|
const filesBefore = await database.get('File').query().fetch();
|
|
expect(filesBefore).toHaveLength(3);
|
|
|
|
const file1Before = filesBefore.find((f) => f.id === 'file-1');
|
|
const file2Before = filesBefore.find((f) => f.id === 'file-2');
|
|
const file3Before = filesBefore.find((f) => f.id === 'file-3');
|
|
expect(file1Before).toBeDefined();
|
|
expect(file2Before).toBeDefined();
|
|
expect(file3Before).toBeDefined();
|
|
|
|
const result = await editPost(serverUrl, post1.id, 'new message', [], ['file-1', 'file-2']);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
|
|
const filesAfter = await database.get('File').query().fetch();
|
|
expect(filesAfter).toHaveLength(1);
|
|
|
|
const remainingFile = filesAfter[0];
|
|
expect(remainingFile.id).toBe('file-3');
|
|
|
|
const deletedFiles = await database.get('File').query().fetch();
|
|
const file1After = deletedFiles.find((f) => f.id === 'file-1');
|
|
const file2After = deletedFiles.find((f) => f.id === 'file-2');
|
|
expect(file1After).toBeUndefined();
|
|
expect(file2After).toBeUndefined();
|
|
});
|
|
|
|
it('editPost - should call fetchChannelStats when files are removed', async () => {
|
|
// Setup post with files
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const testFiles: FileInfo[] = [
|
|
TestHelper.fakeFileInfo({
|
|
id: 'file-1',
|
|
post_id: post1.id,
|
|
name: 'test-file-1.jpg',
|
|
extension: 'jpg',
|
|
size: 1024,
|
|
mime_type: 'image/jpeg',
|
|
user_id: user1.id,
|
|
}),
|
|
];
|
|
|
|
await operator.handleFiles({
|
|
files: testFiles,
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
mockFetchChannelStats.mockClear();
|
|
|
|
// Edit post to remove files
|
|
const result = await editPost(serverUrl, post1.id, 'new message', [], ['file-1']);
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(mockFetchChannelStats).toHaveBeenCalledWith(serverUrl, post1.channel_id);
|
|
expect(mockFetchChannelStats).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('editPost - should call fetchChannelStats when files are added', async () => {
|
|
// Setup post without files
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
mockFetchChannelStats.mockClear();
|
|
|
|
// Edit post to add files
|
|
const result = await editPost(serverUrl, post1.id, 'new message', ['new-file-1'], []);
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(mockFetchChannelStats).toHaveBeenCalledWith(serverUrl, post1.channel_id);
|
|
expect(mockFetchChannelStats).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('editPost - should NOT call fetchChannelStats when no files change', async () => {
|
|
// Setup post without files
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
mockFetchChannelStats.mockClear();
|
|
|
|
// Edit post without changing files
|
|
const result = await editPost(serverUrl, post1.id, 'new message', [], []);
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(mockFetchChannelStats).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('editPost - should call fetchChannelStats when file IDs change', async () => {
|
|
// Setup post with files
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const testFiles: FileInfo[] = [
|
|
TestHelper.fakeFileInfo({
|
|
id: 'file-1',
|
|
post_id: post1.id,
|
|
name: 'test-file-1.jpg',
|
|
extension: 'jpg',
|
|
size: 1024,
|
|
mime_type: 'image/jpeg',
|
|
user_id: user1.id,
|
|
}),
|
|
];
|
|
|
|
await operator.handleFiles({
|
|
files: testFiles,
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
mockFetchChannelStats.mockClear();
|
|
|
|
// Edit post to replace files (remove one, add another)
|
|
const result = await editPost(serverUrl, post1.id, 'new message', ['new-file-2'], ['file-1']);
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(mockFetchChannelStats).toHaveBeenCalledWith(serverUrl, post1.channel_id);
|
|
expect(mockFetchChannelStats).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('acknowledgePost - handle error', async () => {
|
|
mockClient.deletePost.mockImplementationOnce(jest.fn(throwFunc));
|
|
const result = await acknowledgePost('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('acknowledgePost - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await acknowledgePost(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.model).toBeDefined();
|
|
expect(result.model?.metadata?.acknowledgements?.[0].acknowledged_at).toBe(acknowledgedTime);
|
|
});
|
|
|
|
it('unacknowledgePost - handle error', async () => {
|
|
mockClient.deletePost.mockImplementationOnce(jest.fn(throwFunc));
|
|
const result = await unacknowledgePost('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('unacknowledgePost - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await unacknowledgePost(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.model).toBeDefined();
|
|
expect(result.model?.metadata?.acknowledgements?.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('get posts', () => {
|
|
it('fetchPostsForChannel - handle database not found', async () => {
|
|
const result = await fetchPostsForChannel('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostsForChannel - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [{
|
|
id: channelId,
|
|
team_id: teamId,
|
|
total_msg_count: 0,
|
|
creator_id: user1.id,
|
|
} as Channel],
|
|
myChannels: [{
|
|
id: 'id',
|
|
channel_id: channelId,
|
|
user_id: user1.id,
|
|
msg_count: 0,
|
|
} as ChannelMembership],
|
|
prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostsForChannel(serverUrl, channelId);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsForChannel - base case with since', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [{
|
|
id: channelId,
|
|
team_id: teamId,
|
|
total_msg_count: 0,
|
|
creator_id: user1.id,
|
|
} as Channel],
|
|
myChannels: [{
|
|
id: 'id',
|
|
channel_id: channelId,
|
|
user_id: user1.id,
|
|
msg_count: 0,
|
|
} as ChannelMembership],
|
|
prepareRecordsOnly: false});
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await fetchPostsForChannel(serverUrl, channelId, true);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsForChannel - no posts with since', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [{
|
|
id: channelId,
|
|
team_id: teamId,
|
|
total_msg_count: 0,
|
|
creator_id: user1.id,
|
|
} as Channel],
|
|
myChannels: [{
|
|
id: 'id',
|
|
channel_id: channelId,
|
|
user_id: user1.id,
|
|
msg_count: 0,
|
|
} as ChannelMembership],
|
|
prepareRecordsOnly: false});
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
mockClient.getPostsSince.mockImplementationOnce(jest.fn(() => ({posts: {}, order: []})));
|
|
const result = await fetchPostsForChannel(serverUrl, channelId, true);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(0);
|
|
});
|
|
|
|
it('fetchPostsForChannel - recovers a channel whose newest interval has no posts left (MM-66467)', async () => {
|
|
const database = DatabaseManager.serverDatabases[serverUrl]!.database;
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [channel1], myChannels: [channelMember1], prepareRecordsOnly: false});
|
|
|
|
// History of the channel: interval [1000, 1000].
|
|
const oldPost = TestHelper.fakePost({channel_id: channelId, id: 'oldpostid', user_id: user1.id, create_at: 1000, update_at: 1000});
|
|
await storePostsForChannel(serverUrl, channelId, [oldPost], [oldPost.id], '', ActionType.POSTS.RECEIVED_IN_CHANNEL, []);
|
|
|
|
// A much newer post arrives, creating a second, disjoint interval [9000, 9000].
|
|
const newPost = TestHelper.fakePost({channel_id: channelId, id: 'newpostid', user_id: user2.id, create_at: 9000, update_at: 9000});
|
|
await storePostsForChannel(serverUrl, channelId, [newPost], [newPost.id], '', ActionType.POSTS.RECEIVED_IN_CHANNEL, []);
|
|
expect((await queryPostsInChannel(database, channelId).fetch()).length).toBe(2);
|
|
|
|
// The newer post is deleted on the server, so it is dropped locally and its interval is left
|
|
// with no posts. That interval still has the greatest `latest`, so it is the one the channel
|
|
// post list renders: the channel goes blank even though the old post is still in the database.
|
|
await storePostsForChannel(
|
|
serverUrl, channelId,
|
|
[{...newPost, delete_at: 9500, update_at: 9500}], [newPost.id], '',
|
|
ActionType.POSTS.RECEIVED_IN_CHANNEL, [],
|
|
);
|
|
expect(await getRecentPostsInChannel(database, channelId)).toHaveLength(0);
|
|
|
|
// Nothing new on the server, so a since-fetch cannot repair anything.
|
|
mockClient.getPostsSince.mockImplementationOnce(jest.fn(() => ({posts: {}, order: []})));
|
|
const result = await fetchPostsForChannel(serverUrl, channelId);
|
|
expect(result.error).toBeUndefined();
|
|
|
|
// The post-less interval is gone and the channel renders its history again.
|
|
const intervals = await queryPostsInChannel(database, channelId).fetch();
|
|
expect(intervals.length).toBe(1);
|
|
expect(intervals[0].earliest).toBe(1000);
|
|
const recent = await getRecentPostsInChannel(database, channelId);
|
|
expect(recent.length).toBe(1);
|
|
expect(recent[0].id).toBe(oldPost.id);
|
|
});
|
|
|
|
it('fetchPostsForChannel - falls back to a page fetch when no interval has posts left', async () => {
|
|
const database = DatabaseManager.serverDatabases[serverUrl]!.database;
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [channel1], myChannels: [channelMember1], prepareRecordsOnly: false});
|
|
|
|
const onlyPost = TestHelper.fakePost({channel_id: channelId, id: 'onlypostid', user_id: user1.id, create_at: 1000, update_at: 1000});
|
|
await storePostsForChannel(serverUrl, channelId, [onlyPost], [onlyPost.id], '', ActionType.POSTS.RECEIVED_IN_CHANNEL, []);
|
|
await storePostsForChannel(
|
|
serverUrl, channelId,
|
|
[{...onlyPost, delete_at: 1500, update_at: 1500}], [onlyPost.id], '',
|
|
ActionType.POSTS.RECEIVED_IN_CHANNEL, [],
|
|
);
|
|
|
|
// lastFetchedAt is now 1500 while the channel has no posts at all: a since-fetch would ask
|
|
// for posts newer than 1500 and get nothing, leaving the channel blank forever.
|
|
expect((await getMyChannel(database, channelId))!.lastFetchedAt).toBe(1500);
|
|
|
|
mockClient.getPosts.mockClear();
|
|
mockClient.getPostsSince.mockClear();
|
|
const result = await fetchPostsForChannel(serverUrl, channelId);
|
|
expect(result.error).toBeUndefined();
|
|
expect(mockClient.getPostsSince).not.toHaveBeenCalled();
|
|
expect(mockClient.getPosts).toHaveBeenCalled();
|
|
expect(result.actionType).toBe(ActionType.POSTS.RECEIVED_IN_CHANNEL);
|
|
expect((await getRecentPostsInChannel(database, channelId)).length).toBe(2);
|
|
});
|
|
|
|
describe('refreshPostsForChannel', () => {
|
|
let database: Database;
|
|
const seedChannel = async () => {
|
|
await operator.handleChannel({channels: [channel1], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [channel1], myChannels: [channelMember1], prepareRecordsOnly: false});
|
|
};
|
|
|
|
beforeEach(() => {
|
|
database = DatabaseManager.serverDatabases[serverUrl]!.database;
|
|
mockClient.getPosts.mockClear();
|
|
});
|
|
|
|
it('should just fetch a page when the list is showing posts', async () => {
|
|
await seedChannel();
|
|
const post = TestHelper.fakePost({channel_id: channelId, id: 'shownpostid', create_at: 1000, update_at: 1000});
|
|
await storePostsForChannel(serverUrl, channelId, [post], [post.id], '', ActionType.POSTS.RECEIVED_IN_CHANNEL, []);
|
|
|
|
await refreshPostsForChannel(serverUrl, channelId, false);
|
|
|
|
expect(mockClient.getPosts).toHaveBeenCalledWith(channelId, 0, expect.any(Number), true, true, undefined);
|
|
expect(await getPostById(database, post.id)).toBeDefined();
|
|
});
|
|
|
|
it('should reset the cached posts when the list is blank but the channel has posts', async () => {
|
|
await seedChannel();
|
|
|
|
// A blank channel: its only interval covers a range where no post is left, so the post
|
|
// list renders nothing while the older post is still stored.
|
|
const hiddenPost = TestHelper.fakePost({channel_id: channelId, id: 'hiddenpostid', create_at: 1000, update_at: 1000});
|
|
await storePostsForChannel(serverUrl, channelId, [hiddenPost], [hiddenPost.id], '', ActionType.POSTS.RECEIVED_IN_CHANNEL, []);
|
|
await operator.handleReceivedPostsInChannel([TestHelper.fakePost({channel_id: channelId, create_at: 9000})]);
|
|
|
|
await refreshPostsForChannel(serverUrl, channelId, true);
|
|
|
|
// The stale interval and the hidden post are gone, replaced by a clean fetch.
|
|
expect(await getPostById(database, hiddenPost.id)).toBeUndefined();
|
|
const intervals = await queryPostsInChannel(database, channelId).fetch();
|
|
expect(intervals.length).toBe(1);
|
|
expect((await getRecentPostsInChannel(database, channelId)).length).toBe(2);
|
|
expect((await getMyChannel(database, channelId))!.lastFetchedAt).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should not reset anything when the channel is genuinely empty', async () => {
|
|
await seedChannel();
|
|
|
|
await refreshPostsForChannel(serverUrl, channelId, true);
|
|
|
|
// Nothing to repair, so it behaves like a regular refresh.
|
|
expect(mockClient.getPosts).toHaveBeenCalled();
|
|
expect((await getRecentPostsInChannel(database, channelId)).length).toBe(2);
|
|
});
|
|
});
|
|
|
|
it('fetchPostsForChannel - request error', async () => {
|
|
mockClient.getPosts.mockImplementationOnce(jest.fn(throwFunc));
|
|
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [channel1], myChannels: [channelMember1], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostsForChannel(serverUrl, channelId);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeDefined();
|
|
});
|
|
|
|
it('fetchPostsForUnreadChannels - base case', async () => {
|
|
const spyOnProcessChannelPostsByTeam = jest.spyOn(PostAuxilaryFunctions, 'processChannelPostsByTeam');
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleMyChannel({channels: [channel1], myChannels: [channelMember1], prepareRecordsOnly: false});
|
|
|
|
await fetchPostsForUnreadChannels(serverUrl, [{id: teamId}] as Team[], [channel1, {...channel1, id: 'channelid2', total_msg_count: 10}], [{...channelMember1, msg_count: 5}, {...channelMember1, channel_id: 'channelid2', msg_count: 10}], 'testid');
|
|
|
|
expect(spyOnProcessChannelPostsByTeam).toHaveBeenCalledWith(serverUrl, ['channelid1'], false, undefined, undefined);
|
|
});
|
|
|
|
it('fetchPosts - handle database not found', async () => {
|
|
const result = await fetchPosts('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPosts - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPosts(serverUrl, channelId);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPosts - no CRT', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockGetIsCRTEnabled.mockImplementationOnce(() => false);
|
|
|
|
const result = await fetchPosts(serverUrl, channelId);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPosts - no authors needed', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockClient.getProfilesByIds.mockImplementationOnce(jest.fn(() => []));
|
|
|
|
const result = await fetchPosts(serverUrl, channelId);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsBefore - handle database not found', async () => {
|
|
const result = await fetchPostsBefore('foo', '', '', 50, true) as {error: unknown};
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostsBefore - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostsBefore(serverUrl, channelId, post1.id) as {
|
|
posts: Post[];
|
|
order: string[];
|
|
previousPostId: string | undefined;
|
|
};
|
|
expect(result).toBeDefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsBefore - no CRT', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockGetIsCRTEnabled.mockImplementationOnce(() => false);
|
|
|
|
const result = await fetchPostsBefore(serverUrl, channelId, post1.id) as {
|
|
posts: Post[];
|
|
order: string[];
|
|
previousPostId: string | undefined;
|
|
};
|
|
expect(result).toBeDefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsBefore - no authors needed', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockClient.getProfilesByIds.mockImplementationOnce(jest.fn(() => []));
|
|
|
|
const result = await fetchPostsBefore(serverUrl, channelId, post1.id) as {
|
|
posts: Post[];
|
|
order: string[];
|
|
previousPostId: string | undefined;
|
|
};
|
|
expect(result).toBeDefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsBefore - no posts', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockClient.getPostsBefore.mockImplementationOnce(jest.fn(() => ({posts: {}, order: []})));
|
|
|
|
const result = await fetchPostsBefore(serverUrl, channelId, post1.id) as {
|
|
posts: Post[];
|
|
order: string[];
|
|
previousPostId: string | undefined;
|
|
};
|
|
expect(result).toBeDefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(0);
|
|
});
|
|
|
|
it('fetchPostsSince - handle database not found', async () => {
|
|
const result = await fetchPostsSince('foo', '', 0);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostsSince - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostsSince(serverUrl, channelId, 123);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostAuthors - handle database not found', async () => {
|
|
const result = await fetchPostAuthors('foo', []);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostAuthors - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleUsers({users: [user1], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostAuthors(serverUrl, [{...post1, message: 'hi @user3'}, post2]);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.authors).toBeDefined();
|
|
expect(result.authors?.length).toBe(2); // 1 by id for user2, 1 by username for user3
|
|
});
|
|
|
|
it('fetchPostAuthors - no users to fetch', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
await operator.handleUsers({users: [user1], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostAuthors(serverUrl, [post1]);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.authors).toBeDefined();
|
|
expect(result.authors?.length).toBe(0);
|
|
});
|
|
|
|
it('fetchPostThread - handle database not found', async () => {
|
|
const result = await fetchPostThread('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostThread - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostThread(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
expect(result.posts?.[0].id).toBe(post1.id);
|
|
expect(result.posts?.[1].id).toBe(reply1.id);
|
|
});
|
|
|
|
it('fetchPostThread - no CRT', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockGetIsCRTEnabled.mockImplementationOnce(() => false);
|
|
|
|
const result = await fetchPostThread(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
expect(result.posts?.[0].id).toBe(post1.id);
|
|
expect(result.posts?.[1].id).toBe(reply1.id);
|
|
});
|
|
|
|
it('fetchPostThread - no authors needed', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockClient.getProfilesByIds.mockImplementationOnce(jest.fn(() => []));
|
|
|
|
const result = await fetchPostThread(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostThread - no posts', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockClient.getPostThread.mockImplementationOnce(jest.fn(() => ({posts: {}, order: []})));
|
|
|
|
const result = await fetchPostThread(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(0);
|
|
});
|
|
|
|
it('fetchPostsAround - handle database not found', async () => {
|
|
const result = await fetchPostsAround('foo', '', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostsAround - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostsAround(serverUrl, channelId, post2.id, 100, true);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsAround - no CRT', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockGetIsCRTEnabled.mockImplementationOnce(() => false);
|
|
|
|
const result = await fetchPostsAround(serverUrl, channelId, post2.id, 100, true);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPostsAround - no authors needed', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockClient.getProfilesByIds.mockImplementationOnce(jest.fn(() => []));
|
|
|
|
const result = await fetchPostsAround(serverUrl, channelId, post2.id, 100, true);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchMissingChannelsFromPosts - handle database not found', async () => {
|
|
const result = await fetchMissingChannelsFromPosts('foo', []);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchMissingChannelsFromPosts - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchMissingChannelsFromPosts(serverUrl, [post1]);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.channels).toBeDefined();
|
|
expect(result.channels?.[0].id).toBe(post1.channel_id);
|
|
});
|
|
|
|
it('fetchPostById - handle database not found', async () => {
|
|
const result = await fetchPostById('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPostById - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostById(serverUrl, post2.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
expect(result.post?.id).toBe(post2.id);
|
|
});
|
|
|
|
it('fetchPostById - no CRT', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockGetIsCRTEnabled.mockImplementationOnce(() => false);
|
|
|
|
const result = await fetchPostById(serverUrl, post2.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
expect(result.post?.id).toBe(post2.id);
|
|
});
|
|
|
|
it('fetchPostById - no authors needed', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
mockClient.getProfilesByIds.mockImplementationOnce(jest.fn(() => []));
|
|
|
|
const result = await fetchPostById(serverUrl, post2.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
expect(result.post?.id).toBe(post2.id);
|
|
});
|
|
|
|
it('fetchPostById - fetch only', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPostById(serverUrl, post2.id, true);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
expect(result.post?.id).toBe(post2.id);
|
|
});
|
|
|
|
it('fetchPostInfo - should return post info on success', async () => {
|
|
const result = await fetchPostInfo(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.postInfo).toBeDefined();
|
|
expect(result.postInfo?.channel_id).toBe(channelId);
|
|
expect(result.postInfo?.team_id).toBe(teamId);
|
|
expect(result.postInfo?.has_joined_channel).toBe(false);
|
|
});
|
|
|
|
it('fetchPostInfo - should return error when client throws', async () => {
|
|
mockClient.getPostInfo.mockImplementationOnce(throwFunc);
|
|
const result = await fetchPostInfo(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
expect(result.postInfo).toBeUndefined();
|
|
});
|
|
|
|
it('fetchSavedPosts - handle database not found', async () => {
|
|
const result = await fetchSavedPosts('foo');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchSavedPosts - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchSavedPosts(serverUrl);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
|
|
it('fetchPinnedPosts - handle database not found', async () => {
|
|
const result = await fetchPinnedPosts('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('fetchPinnedPosts - base case', async () => {
|
|
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: user1.id}], prepareRecordsOnly: false});
|
|
|
|
const result = await fetchPinnedPosts(serverUrl, channel1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.posts).toBeTruthy();
|
|
expect(result.posts?.length).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('revealBoRPost', () => {
|
|
it('revealBoRPost - handle database not found', async () => {
|
|
const result = await revealBoRPost('foo', '');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('revealBoRPost - handle client error', async () => {
|
|
jest.spyOn(NetworkManager, 'getClient').mockImplementationOnce(throwFunc);
|
|
|
|
const result = await revealBoRPost(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('revealBoRPost - handle error', async () => {
|
|
mockClient.revealBoRPost.mockImplementationOnce(jest.fn(throwFunc));
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [{...post1, props: {failed: false}}],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await revealBoRPost(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
|
|
it('revealBoRPost - base case', async () => {
|
|
await operator.handlePosts({
|
|
actionType: ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order: [post1.id],
|
|
posts: [post1],
|
|
prepareRecordsOnly: false,
|
|
});
|
|
|
|
const result = await revealBoRPost(serverUrl, post1.id);
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeDefined();
|
|
});
|
|
|
|
it('revealBoRPost - post not found', async () => {
|
|
const result = await revealBoRPost(serverUrl, 'nonexistent-post-id');
|
|
expect(result).toBeDefined();
|
|
expect(result.error).toBeUndefined();
|
|
expect(result.post).toBeUndefined();
|
|
});
|
|
});
|