mattermost-mobile-upstream/app/actions/websocket/index.test.ts
Guillermo Vayá 4b12bcec7b fix(drafts): Phase 5 review — capability re-read, disable epoch, commit ordering, overflow, reliable reconnect
Addresses 5 validated review findings on the Phase 5 trigger wiring:

- #3 Reconnect honored a stale cached capability: doReconnect now awaits
  handleCapabilityChange (re-reading the freshly-committed sync_drafts
  preference) before requestReconcile, so a persisted sync_drafts=false is
  respected on a fresh login.
- #4 Disable did not fence in-flight work: handleCapabilityChange's disable
  branch now bumps the lifecycle epoch (like invalidate) and resets the
  reconcile single-flight, so a GET/POST/DELETE crossing the disabled
  interval cannot apply results or set a baseline after re-enable.
- #5 Team reconcile ran before the switch committed: moved requestReconcile
  in handleTeamChange to after the new team is persisted (both the tablet
  fast-path and the normal path), so a concurrent DB-reading trigger can't
  coalesce the baseline back onto the previous team.
- #6a Reliable reconnect never reconciled: WebsocketManager.onReliableReconnect
  now calls DraftSyncManager.onReliableReconnect, which GET-reconciles the
  current team so silent (event-less) server-side deletions are caught.
- #7 Overflow could permanently stall event-triggered reconcile: enqueue now
  fires the reconcile trigger even on buffer overflow (dropping only the
  push), so a full buffer left by a transient current-team lookup failure
  cannot wedge reconciliation after the database recovers.

Pushed back (not changed): #1 (fence absent keys — would resurrect deleted
drafts; belongs to Phase 6 deleted_fingerprint) and #2 (drain during pending
GET is local-wins-safe, absence is 2-observation guarded). Deferred: #6b
suspend/resume, #8a Global-Drafts stale-on-open, #8b reconciliation deadline.

tsc + eslint clean; new manager tests for #4/#6a/#7 and a reconnect
capability/ordering assertion for #3. All affected suites green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-18 14:36:43 +02:00

236 lines
10 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {DeviceEventEmitter} from 'react-native';
import {markChannelAsViewed} from '@actions/local/channel';
import {dataRetentionCleanup, expiredBoRPostCleanup} from '@actions/local/systems';
import {markChannelAsRead} from '@actions/remote/channel';
import {entry, handleEntryAfterLoadNavigation} from '@actions/remote/entry/common';
import {deferredAppEntryActions} from '@actions/remote/entry/deferred';
import {fetchPostsForChannel, fetchPostThread} from '@actions/remote/post';
import {openAllUnreadChannels} from '@actions/remote/preference';
import {loadConfigAndCalls} from '@calls/actions/calls';
import {isSupportedServerCalls} from '@calls/utils';
import DatabaseManager from '@database/manager';
import AppsManager from '@managers/apps_manager';
import DraftSyncManager from '@managers/draft_sync_manager';
import {handlePlaybookReconnect} from '@playbooks/actions/websocket/reconnect';
import {getActiveServerUrl} from '@queries/app/servers';
import {getLastPostInThread} from '@queries/servers/post';
import {getConfig, getCurrentChannelId, getCurrentTeamId, setLastFullSync} from '@queries/servers/system';
import {getIsCRTEnabled} from '@queries/servers/thread';
import {getCurrentUser} from '@queries/servers/user';
import EphemeralStore from '@store/ephemeral_store';
import {NavigationStore} from '@store/navigation_store';
import TestHelper from '@test/test_helper';
import {handleFirstConnect, handleReconnect} from './index';
jest.mock('@actions/local/channel');
jest.mock('@actions/local/systems');
jest.mock('@actions/remote/channel');
jest.mock('@actions/remote/entry/common');
jest.mock('@actions/remote/entry/deferred');
jest.mock('@actions/remote/post');
jest.mock('@actions/remote/scheduled_post');
jest.mock('@actions/remote/preference');
jest.mock('@actions/remote/user');
jest.mock('@calls/actions/calls');
jest.mock('@calls/utils');
jest.mock('@database/manager');
jest.mock('@managers/apps_manager');
jest.mock('@queries/app/servers');
jest.mock('@queries/servers/post');
jest.mock('@queries/servers/system');
jest.mock('@queries/servers/thread');
jest.mock('@queries/servers/user');
jest.mock('@store/ephemeral_store');
jest.mock('@store/navigation_store');
jest.mock('@store/team_load_store');
jest.mock('@utils/helpers', () => ({
isTablet: jest.fn().mockReturnValue(false),
}));
jest.mock('@playbooks/actions/websocket/reconnect');
jest.mock('@managers/draft_sync_manager', () => ({
__esModule: true,
default: {requestReconcile: jest.fn(), handleCapabilityChange: jest.fn()},
}));
describe('WebSocket Index Actions', () => {
const serverUrl = 'baseHandler.test.com';
const currentUserId = 'current-user-id';
const currentTeamId = 'current-team-id';
const currentChannelId = 'current-channel-id';
const groupLabel = 'DeepLink';
beforeEach(async () => {
jest.clearAllMocks();
jest.spyOn(DeviceEventEmitter, 'emit');
await DatabaseManager.init([serverUrl]);
DatabaseManager.serverDatabases[serverUrl] = {
operator: {
database: {},
batchRecords: jest.fn(),
},
} as any;
DatabaseManager.getServerDatabaseAndOperator = jest.fn().mockReturnValue({
database: {},
operator: {
batchRecords: jest.fn(),
},
});
});
describe('handleFirstConnect', () => {
it('should handle first connection successfully', async () => {
const mockEntryData = {
models: [],
initialTeamId: currentTeamId,
initialChannelId: currentChannelId,
prefData: {preferences: []},
teamData: {memberships: [], teams: []},
chData: {memberships: [], channels: []},
gmConverted: false,
};
jest.mocked(entry).mockResolvedValue(mockEntryData);
jest.mocked(getCurrentUser).mockResolvedValue(TestHelper.fakeUserModel({
id: currentUserId,
locale: 'en',
}));
jest.mocked(getConfig).mockResolvedValue({Version: '9.0.0'} as ClientConfig);
jest.mocked(isSupportedServerCalls).mockReturnValue(true);
const error = await handleFirstConnect(serverUrl, groupLabel);
expect(error).toBeUndefined();
expect(entry).toHaveBeenCalled();
expect(handleEntryAfterLoadNavigation).toHaveBeenCalled();
expect(setLastFullSync).toHaveBeenCalled();
expect(loadConfigAndCalls).toHaveBeenCalled();
expect(deferredAppEntryActions).toHaveBeenCalled();
expect(handlePlaybookReconnect).toHaveBeenCalledWith(serverUrl);
expect(DraftSyncManager.requestReconcile).toHaveBeenCalledWith(serverUrl, currentTeamId, 'reconnect');
// Capability is re-read from the committed entry batch BEFORE reconciling, so a persisted
// sync_drafts=false is honored on a fresh login (fix #3).
expect(DraftSyncManager.handleCapabilityChange).toHaveBeenCalledWith(serverUrl);
expect(jest.mocked(DraftSyncManager.handleCapabilityChange).mock.invocationCallOrder[0]).
toBeLessThan(jest.mocked(DraftSyncManager.requestReconcile).mock.invocationCallOrder[0]);
});
it('should handle error when server database not found', async () => {
DatabaseManager.serverDatabases = {};
const error = await handleFirstConnect(serverUrl);
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toBe('cannot find server database');
});
});
describe('handleReconnect', () => {
beforeEach(() => {
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue([]);
jest.mocked(getActiveServerUrl).mockResolvedValue(serverUrl);
jest.mocked(getCurrentChannelId).mockResolvedValue(currentChannelId);
jest.mocked(getCurrentTeamId).mockResolvedValue(currentTeamId);
jest.mocked(getIsCRTEnabled).mockResolvedValue(false);
jest.mocked(EphemeralStore.getCurrentThreadId).mockReturnValue('');
jest.mocked(EphemeralStore.wasNotificationTapped).mockReturnValue(false);
});
it('should handle reconnection successfully', async () => {
const mockEntryData = {
models: [],
initialTeamId: currentTeamId,
initialChannelId: currentChannelId,
prefData: {preferences: []},
teamData: {memberships: [], teams: []},
chData: {memberships: [], channels: []},
gmConverted: false,
};
jest.mocked(entry).mockResolvedValue(mockEntryData);
jest.mocked(getCurrentUser).mockResolvedValue(TestHelper.fakeUserModel({
id: currentUserId,
locale: 'en',
}));
jest.mocked(getConfig).mockResolvedValue({Version: '9.0.0'} as ClientConfig);
jest.mocked(isSupportedServerCalls).mockReturnValue(true);
jest.mocked(getActiveServerUrl).mockResolvedValue(serverUrl);
const error = await handleReconnect(serverUrl);
expect(error).toBeUndefined();
expect(entry).toHaveBeenCalled();
expect(handleEntryAfterLoadNavigation).toHaveBeenCalled();
expect(setLastFullSync).toHaveBeenCalled();
expect(loadConfigAndCalls).toHaveBeenCalled();
expect(deferredAppEntryActions).toHaveBeenCalled();
expect(openAllUnreadChannels).toHaveBeenCalled();
expect(dataRetentionCleanup).toHaveBeenCalled();
expect(expiredBoRPostCleanup).toHaveBeenCalled();
expect(AppsManager.refreshAppBindings).toHaveBeenCalled();
expect(handlePlaybookReconnect).toHaveBeenCalledWith(serverUrl);
});
it('should fetch posts for channel screen', async () => {
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue(['channel']);
await handleReconnect(serverUrl);
expect(fetchPostsForChannel).toHaveBeenCalledWith(serverUrl, currentChannelId, false, false, 'WebSocket Reconnect');
expect(markChannelAsRead).toHaveBeenCalledWith(serverUrl, currentChannelId, false, 'WebSocket Reconnect');
expect(markChannelAsViewed).toHaveBeenCalledWith(serverUrl, currentChannelId, true);
});
it('should fetch thread posts when CRT enabled', async () => {
const threadId = 'thread-id';
const lastPost = TestHelper.fakePostModel({id: 'post-id', createAt: 123});
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue(['thread']);
jest.mocked(getIsCRTEnabled).mockResolvedValue(true);
jest.mocked(EphemeralStore.getCurrentThreadId).mockReturnValue(threadId);
jest.mocked(getLastPostInThread).mockResolvedValue(lastPost);
await handleReconnect(serverUrl);
expect(fetchPostThread).toHaveBeenCalledWith(
serverUrl,
threadId,
{
fromCreateAt: lastPost.createAt,
fromPost: lastPost.id,
direction: 'down',
},
false,
'WebSocket Reconnect',
);
});
it('should handle notification tapped state', async () => {
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue(['channel']);
jest.mocked(EphemeralStore.wasNotificationTapped).mockReturnValue(true);
await handleReconnect(serverUrl);
expect(markChannelAsViewed).not.toHaveBeenCalled();
expect(EphemeralStore.setNotificationTapped).toHaveBeenCalledWith(false);
});
it('should handle error in entry data', async () => {
jest.mocked(entry).mockResolvedValue({error: new Error('entry error')});
const result = await handleReconnect(serverUrl);
expect(result).toBeInstanceOf(Error);
expect((result as Error).message).toBe('entry error');
});
});
});