mattermost-mobile-upstream/app/client/rest/base.test.ts
yasserfaraazkhan 7f75e5c88a fix(network): retry a lost pooled connection once, and use the options helper in mentions
Two independent fixes.

app/client/rest/base.ts — cloud load balancers close idle keep-alive sockets (~40s), so the
next request down a pooled connection dies with NSURLError -1005 "network connection was
lost" before reaching the server (response_status=-1, 0 response bytes). Reproduced against
the PR cloud servers: the custom-status PUT failed 2/2, and retrying once — which opens a
fresh connection — made it pass 4/4.

Deliberately scoped:
- ONE extra attempt, not four. The native client already attaches a RuntimeRetrier that
  delegates to `request.request?.retryPolicy ?? session.retryPolicy`, and the session policy
  from NetworkManager (EXPONENTIAL_RETRY, retryLimit 3) inherits Alamofire's
  defaultRetryableURLErrorCodes, which includes .networkConnectionLost. Why that does not
  fire is unresolved, so this is a mitigation, not the root-cause fix — and it must not stack
  multiple JS attempts on top of a native retryLimit of 3.
- Match the message text only, never the raw codes. "-1005" and "-1001" occur as substrings
  in our own hostnames (mobile-pr-10050…, mobile-pr-10010…), so a code match retried
  permanent 4xx/5xx errors. There is a regression test for exactly that.
- Idempotent methods only; callers that must not retry already pass `noRetry`.

detox recent_mentions MM-T4909_3 — the reply and delete steps hand-rolled a longPress on
post_header.date_time with no scroll, no retry and no wait for the sheet, and post_options
never opened, so replyPostOption was not there. Every other site in this file uses
openPostOptionsFor, which scrolls the row in and longPressWithRetry's until
post_options.screen appears — and it already opens options for this same post earlier in the
same test, so it clears the @mention handler the date_time target was chosen to avoid.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 15:49:56 +05:30

100 lines
4.0 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import TestHelper from '@test/test_helper';
import type ClientBase from './base';
import type {ClientChannelsMix} from './channels';
describe('ClientBase route methods', () => {
let client: ClientChannelsMix & ClientBase;
beforeAll(() => {
client = TestHelper.createClient();
});
test('getAPIRoute joins base route and api version', () => {
expect(client.getAPIRoute()).toBe(`${client.getBaseRoute()}${client.urlVersion}`);
});
describe('shared channels and remote cluster routes', () => {
test('getSharedChannelsRoute returns sharedchannels path', () => {
expect(client.getSharedChannelsRoute()).toBe(`${client.urlVersion}/sharedchannels`);
});
test('getRemoteClustersRoute returns remotecluster path', () => {
expect(client.getRemoteClustersRoute()).toBe(`${client.urlVersion}/remotecluster`);
});
test('getChannelRemotesRoute returns channel remotes path', () => {
const channelId = 'channel-id-1';
expect(client.getChannelRemotesRoute(channelId)).toBe(
`${client.urlVersion}/sharedchannels/${channelId}/remotes`,
);
});
test('getRemoteClusterChannelRoute returns remote cluster channel path', () => {
const remoteId = 'remote-id-1';
const channelId = 'channel-id-1';
expect(client.getRemoteClusterChannelRoute(remoteId, channelId)).toBe(
`${client.urlVersion}/remotecluster/${remoteId}/channels/${channelId}`,
);
});
});
});
describe('doFetch transient transport retry', () => {
let client: ClientChannelsMix & ClientBase;
let doFetchWithTracking: jest.SpyInstance;
const lostConnection = () => new Error('URLSessionTask failed with error: The network connection was lost.');
beforeEach(() => {
client = TestHelper.createClient();
doFetchWithTracking = jest.spyOn(client, 'doFetchWithTracking');
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should retry a lost connection once and return the second result', async () => {
doFetchWithTracking.mockRejectedValueOnce(lostConnection()).mockResolvedValueOnce('ok');
await expect(client.doFetch('/url', {method: 'put'})).resolves.toBe('ok');
expect(doFetchWithTracking).toHaveBeenCalledTimes(2);
});
it('should not retry more than once', async () => {
doFetchWithTracking.mockRejectedValue(lostConnection());
await expect(client.doFetch('/url', {method: 'put'})).rejects.toThrow('network connection was lost');
expect(doFetchWithTracking).toHaveBeenCalledTimes(2);
});
it('should not retry when the caller passes noRetry', async () => {
doFetchWithTracking.mockRejectedValue(lostConnection());
await expect(client.doFetch('/url', {method: 'put', noRetry: true})).rejects.toThrow();
expect(doFetchWithTracking).toHaveBeenCalledTimes(1);
});
it('should not retry a non-idempotent post', async () => {
doFetchWithTracking.mockRejectedValue(lostConnection());
await expect(client.doFetch('/url', {method: 'post'})).rejects.toThrow();
expect(doFetchWithTracking).toHaveBeenCalledTimes(1);
});
// Our own test hosts are named mobile-pr-10050 / mobile-pr-10010, which contain "-1005"
// and "-1001". Matching on raw error codes would retry permanent failures.
it('should not retry a permanent error whose message contains the server hostname', async () => {
doFetchWithTracking.mockRejectedValue(
new Error('You do not have permission: https://mobile-pr-10050-ios-site-1.test.mattermost.cloud/api/v4/users/me'),
);
await expect(client.doFetch('/url', {method: 'put'})).rejects.toThrow('do not have permission');
expect(doFetchWithTracking).toHaveBeenCalledTimes(1);
});
});