mattermost-mobile-upstream/app/actions/remote/restore_server.test.ts
Carlos Garcia a5ff8a0d95
Mm 68767 zero persistence (#9803)
* implements auto cache cleanup when zero persistence mode is enabled (0 value)

* uses memory flag with sqlite instead of loki when running in zero persistence mode

* renames ephemeral mode manager and improves readability

* zero persistence refactors and restore values during transitions

* fixes ephemeral mode tests

* use initializeClient instead of just initialize to wait for doReconnect to finish

that way teams loading is awaited while WS reconnects during ZPM transitions

* adds inital sync as a mount hook to prevent undesired load error states

during app cold start in ZPM mode, we need a way to load team and channels
without showing loading error during the transition. The nre is initial sync
hook allows for it

* reset started sync subject on wipe

* removes extra log lines

* resets tracked server during ephemeral mode transitions

* improvements to apply persistence mode changes when something fails

* address coderabbit PR suggestions

* add batching time logging

* throws ephemeral mode change error as is

* make deleteServerDatabaseFiles private

* inline derive persistence flag function

* return current user id when refetching current user

* support zero persistence mode server for push notifications

* adds support for zero persistence mode when sharing to mattermost

* adds clarifying comment

* catch and log error deleting files

* reorder translations to correct position

* adds some new tests to improve coverage

* ios push notification native fix

* logout when fetchMe fails with 401 status code during reconnection

* properly surface errors

* return proper false value when checking for zero persistence flag

* change comments and properly await on some operations

* prevent not set persistence mode to report as an error

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* include servers with no persistence flag set in share list

* disable button for accessibility

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* improves when to store and remove signing key from global area

* review push notification reception running in zero persistence mode

* add server to ephemeral mode manager right after login

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-16 13:55:45 +02:00

139 lines
6.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import NetInfo, {type NetInfoState} from '@react-native-community/netinfo';
import {router} from 'expo-router';
import {DeviceEventEmitter} from 'react-native';
import {wipeServerDatabaseWithRetry} from '@actions/local/ephemeral_mode/wipe';
import {loginEntry} from '@actions/remote/entry';
import {restoreServerAfterDatabaseWipe} from '@actions/remote/restore_server';
import {fetchMe} from '@actions/remote/user';
import {Events, Launch} from '@constants';
import DatabaseManager from '@database/manager';
import {getServerCredentials} from '@init/credentials';
import {determineRouteFromLaunchProps} from '@init/launch';
import {setCurrentUserId} from '@queries/servers/system';
jest.mock('@react-native-community/netinfo', () => ({
...jest.requireActual('@react-native-community/netinfo'),
__esModule: true,
default: {fetch: jest.fn()},
}));
jest.mock('expo-router', () => ({
router: {replace: jest.fn()},
}));
jest.mock('@actions/local/ephemeral_mode/wipe', () => ({
wipeServerDatabaseWithRetry: jest.fn(),
}));
jest.mock('@actions/remote/entry', () => ({
loginEntry: jest.fn(),
}));
jest.mock('@actions/remote/user', () => ({
fetchMe: jest.fn(),
}));
jest.mock('@init/credentials', () => ({
getServerCredentials: jest.fn(),
}));
jest.mock('@init/launch', () => ({
determineRouteFromLaunchProps: jest.fn(),
}));
jest.mock('@queries/servers/system', () => ({
setCurrentUserId: jest.fn().mockResolvedValue({}),
}));
jest.mock('@utils/log');
describe('restoreServerAfterDatabaseWipe', () => {
const serverUrl = 'https://server.test';
let updatePersistenceFlagSpy: jest.SpyInstance;
let setActiveServerDatabaseSpy: jest.SpyInstance;
const fakeUser = {id: 'u1', email: 'u@example.com'};
const mockOperator = {handleUsers: jest.fn().mockResolvedValue(undefined)};
beforeEach(() => {
jest.clearAllMocks();
updatePersistenceFlagSpy = jest.spyOn(DatabaseManager, 'updatePersistenceFlag').mockResolvedValue(undefined);
setActiveServerDatabaseSpy = jest.spyOn(DatabaseManager, 'setActiveServerDatabase').mockResolvedValue(undefined);
jest.spyOn(DatabaseManager, 'getServerDatabaseAndOperator').mockReturnValue({
operator: mockOperator as never,
database: {} as never,
});
jest.mocked(fetchMe).mockResolvedValue({user: fakeUser} as never);
jest.mocked(wipeServerDatabaseWithRetry).mockResolvedValue({success: true});
jest.mocked(NetInfo.fetch).mockResolvedValue({isConnected: true} as NetInfoState);
jest.mocked(getServerCredentials).mockResolvedValue({serverUrl, userId: 'u1', token: 'tok-abc', preauthSecret: 'preauth'});
jest.mocked(loginEntry).mockResolvedValue({});
jest.mocked(determineRouteFromLaunchProps).mockResolvedValue({
route: '/(authenticated)/(home)',
params: {launchType: Launch.Normal, serverUrl, coldStart: true},
} as Awaited<ReturnType<typeof determineRouteFromLaunchProps>>);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('on no device connectivity: returns no_connection without touching the database', async () => {
jest.mocked(NetInfo.fetch).mockResolvedValue({isConnected: false} as NetInfoState);
const result = await restoreServerAfterDatabaseWipe(serverUrl);
expect(fetchMe).not.toHaveBeenCalled();
expect(updatePersistenceFlagSpy).not.toHaveBeenCalled();
expect(router.replace).not.toHaveBeenCalled();
expect(result).toEqual({error: 'no_connection'});
});
it('on missing credentials: returns no_credentials without fetching the user', async () => {
jest.mocked(getServerCredentials).mockResolvedValue(null);
const result = await restoreServerAfterDatabaseWipe(serverUrl);
expect(fetchMe).not.toHaveBeenCalled();
expect(result).toEqual({error: 'no_credentials'});
});
it('on success: opens WebSocket via loginEntry and navigates home', async () => {
const result = await restoreServerAfterDatabaseWipe(serverUrl);
expect(fetchMe).toHaveBeenCalledWith(serverUrl);
expect(setCurrentUserId).toHaveBeenCalledWith(mockOperator, 'u1');
expect(loginEntry).toHaveBeenCalledWith({serverUrl});
expect(setActiveServerDatabaseSpy).toHaveBeenCalledWith(serverUrl);
expect(updatePersistenceFlagSpy).toHaveBeenCalledWith(serverUrl, '');
expect(determineRouteFromLaunchProps).toHaveBeenCalledWith({launchType: Launch.Normal, serverUrl, coldStart: true});
expect(router.replace).toHaveBeenCalledWith({pathname: '/(authenticated)/(home)', params: {launchType: Launch.Normal, serverUrl, coldStart: true}});
expect(wipeServerDatabaseWithRetry).not.toHaveBeenCalled();
expect(result).toEqual({});
});
it('on fetchMe failure: returns the error and skips loginEntry, wipe, and navigation', async () => {
const fetchError = new Error('User fetch failed');
jest.mocked(fetchMe).mockResolvedValue({error: fetchError} as never);
const emitSpy = jest.spyOn(DeviceEventEmitter, 'emit');
const result = await restoreServerAfterDatabaseWipe(serverUrl);
expect(loginEntry).not.toHaveBeenCalled();
expect(wipeServerDatabaseWithRetry).not.toHaveBeenCalled();
expect(updatePersistenceFlagSpy).not.toHaveBeenCalled();
expect(router.replace).not.toHaveBeenCalled();
expect(emitSpy).not.toHaveBeenCalledWith(Events.SERVER_LOGOUT, expect.anything());
expect(result).toEqual({error: fetchError});
});
it('on error from loginEntry: re-wipes the partial DB and returns the error', async () => {
const wsError = new Error('WebSocket failed');
jest.mocked(loginEntry).mockResolvedValue({error: wsError});
const result = await restoreServerAfterDatabaseWipe(serverUrl);
expect(wipeServerDatabaseWithRetry).toHaveBeenCalledWith(serverUrl);
expect(updatePersistenceFlagSpy).not.toHaveBeenCalled();
expect(router.replace).not.toHaveBeenCalled();
expect(result).toEqual({error: wsError});
});
});