mattermost-mobile/app/client/rest/threads.test.ts
Elias Nahum d1d3fda83a
Request metrics improvements (#8420)
* Network metrics

* update network-client ref

* fix unit tests

* missing catch error parsing

* Replace API's to fetch all teams and channels for a user
Update groupLabel to use a type definition
Include effective Latency and Average Speed in the metrics
Include parallel and sequential request counts

* update network library and fix tests

* feat(MM-61865): send network request telemetry data to the server (#8436)

* feat(MM-61865): send network info telemetry data

* unit testing

* fix latency vs effectiveLatency

* cleanup test

* fix failing tests

* fix spelling error

* fix failing test

* more cleanup

* fix: improve parallel requests calculation (#8439)

* fix: improve parallel requests calculation

* fix test issue

* multiple parallelGroups testing

* calculateAverageSpeedWithCategories test

* categorizedRequests in it's own describe

* clean up duplicate tests

* check for when groupLabel is non-existent

* a case when groupLabel is non-existent

* more testing with effective latency.

* resolveAndFlattenModels with a capital F

* add try..catch within resolveAndFlattenModels

* update groupLabel to fix failing lint

* fix linting issue due to unknown group label

* forgot to push changes

* fix the indentation problem again

* add env var option for COLLECT_NETWORK_METRICS

---------

Co-authored-by: Rahim Rahman <rahim.rahman@mattermost.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
2025-01-16 13:55:29 -07:00

129 lines
4.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import TestHelper from '@test/test_helper';
import {buildQueryString} from '@utils/helpers';
import {PER_PAGE_DEFAULT} from './constants';
import type ClientBase from './base';
import type {ClientThreadsMix} from './threads';
const userId = 'user_id';
const teamId = 'team_id';
const threadId = 'thread_id';
describe('ClientThreads', () => {
let client: ClientThreadsMix & ClientBase;
beforeAll(() => {
client = TestHelper.createClient();
client.doFetch = jest.fn();
});
test('getThreads', async () => {
const before = 'before';
const after = 'after';
const pageSize = 10;
const deleted = true;
const unread = true;
const since = 123456;
const totalsOnly = true;
const serverVersion = '6.0.0';
const excludeDirect = true;
const groupLabel = 'Cold Start';
const queryStringObj = {
extended: 'true',
before,
after,
deleted,
unread,
since,
totalsOnly,
excludeDirect,
per_page: pageSize,
};
const expectedUrl = `${client.getThreadsRoute(userId, teamId)}${buildQueryString(queryStringObj)}`;
const expectedOptions = {method: 'get', groupLabel};
await client.getThreads(userId, teamId, before, after, pageSize, deleted, unread, since, totalsOnly, serverVersion, excludeDirect, groupLabel);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
// Test with default values
const defaultQueryStringObj = {
extended: 'true',
before: '',
after: '',
deleted: false,
unread: false,
since: 0,
totalsOnly: false,
excludeDirect: false,
pageSize: PER_PAGE_DEFAULT,
};
const expectedUrlDefault = `${client.getThreadsRoute(userId, teamId)}${buildQueryString(defaultQueryStringObj)}`;
await client.getThreads(userId, teamId);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrlDefault, {method: 'get', groupLabel: undefined});
});
test('getThread', async () => {
const extended = false;
const expectedUrl = `${client.getThreadRoute(userId, teamId, threadId)}${buildQueryString({extended})}`;
const expectedOptions = {method: 'get'};
await client.getThread(userId, teamId, threadId, extended);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
// Test with default values
const expectedUrlDefault = `${client.getThreadRoute(userId, teamId, threadId)}${buildQueryString({extended: true})}`;
await client.getThread(userId, teamId, threadId);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrlDefault, expectedOptions);
});
test('markThreadAsRead', async () => {
const timestamp = 123456;
const expectedUrl = `${client.getThreadRoute(userId, teamId, threadId)}/read/${timestamp}`;
const expectedOptions = {method: 'put', body: {}};
await client.markThreadAsRead(userId, teamId, threadId, timestamp);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('markThreadAsUnread', async () => {
const postId = 'post_id';
const expectedUrl = `${client.getThreadRoute(userId, teamId, threadId)}/set_unread/${postId}`;
const expectedOptions = {method: 'post'};
await client.markThreadAsUnread(userId, teamId, threadId, postId);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('updateTeamThreadsAsRead', async () => {
const expectedUrl = `${client.getThreadsRoute(userId, teamId)}/read`;
const expectedOptions = {method: 'put', body: {}};
await client.updateTeamThreadsAsRead(userId, teamId);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});
test('updateThreadFollow', async () => {
const state = true;
const expectedUrl = `${client.getThreadRoute(userId, teamId, threadId)}/following`;
const expectedOptions = {method: 'put', body: {}};
await client.updateThreadFollow(userId, teamId, threadId, state);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
// Test with state = false
const expectedOptionsDelete = {method: 'delete', body: {}};
await client.updateThreadFollow(userId, teamId, threadId, false);
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptionsDelete);
});
});