mattermost-mobile-upstream/app/queries/servers/system.test.ts
2026-07-09 09:42:30 +08:00

1139 lines
48 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable max-lines */
import {Platform} from 'react-native';
import {firstValueFrom} from 'rxjs';
import {License, Preferences} from '@constants';
import {SYSTEM_IDENTIFIERS} from '@constants/database';
import DatabaseManager from '@database/manager';
import {
getCurrentChannelId, getCurrentTeamId, getCurrentUserId, getPushVerificationStatus,
getCommonSystemValues, getConfig, getConfigValue, getConfigBooleanValue, getDisconnectedSince, getLastGlobalDataRetentionRun,
getLastBoRPostCleanupRun, getGlobalDataRetentionPolicy, getGranularDataRetentionPolicies,
getIsDataRetentionEnabled, getLicense, getRecentCustomStatuses, getExpandedLinks,
getRecentReactions, getLastFullSync, setLastFullSync, resetLastFullSync,
getTeamHistory, patchTeamHistory, prepareCommonSystemValues, setCurrentUserId,
setCurrentChannelId, setCurrentTeamId, setCurrentTeamAndChannelId,
getLastUnreadChannelId, getExpiredSession,
getLastInitialLoad, setLastInitialLoad, getLastTeamLoad, setLastTeamLoad, prepareDeleteTeamLoadCursor,
decrementTeamBlob, adjustThreadInBlob, incrementTeamBlob,
observeCurrentChannelId, observeCurrentTeamId, observeCurrentUserId, observeGlobalThreadsTab,
observePushVerificationStatus, observeConfig, observeConfigValue, observeMaxFileCount,
observeIsCustomStatusExpirySupported, observeConfigBooleanValue, observeConfigIntValue,
canViewArchivedChannels, observeCanViewArchivedChannels,
observeLicense, observeAllowedThemesKeys, observeOnlyUnreads,
observeIsFreeEdition, observeIsMinimumLicenseTier, observeReportAProblemMetadata,
} from './system';
import type ServerDataOperator from '@database/operator/server_data_operator';
import type {Database} from '@nozbe/watermelondb';
import type SystemModel from '@typings/database/models/servers/system';
jest.mock('expo-application', () => {
return {
nativeApplicationVersion: '1.2.3',
nativeBuildVersion: '456',
};
});
const makeSeedTeamBadgeBlob = (operator: ServerDataOperator) => async (blob: TeamBadgeCounts) => {
await operator.handleSystem({
systems: [{id: SYSTEM_IDENTIFIERS.TEAM_BADGE_COUNTS, value: JSON.stringify(blob)}],
prepareRecordsOnly: false,
});
};
const makeReadTeamBadgeBlob = (database: Database) => async (): Promise<TeamBadgeCounts | undefined> => {
try {
const record = await database.get<SystemModel>('System').find(SYSTEM_IDENTIFIERS.TEAM_BADGE_COUNTS);
return typeof record.value === 'string' ? JSON.parse(record.value) : record.value;
} catch {
return undefined;
}
};
describe('observeReportAProblemMetadata', () => {
const serverUrl = 'baseHandler.test.com';
let database: Database;
let operator: ServerDataOperator;
let originalPlatform: typeof Platform.OS;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
const serverDatabaseAndOperator = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
database = serverDatabaseAndOperator.database;
operator = serverDatabaseAndOperator.operator;
originalPlatform = Platform.OS;
// @ts-expect-error Platform.OS is mocked
Platform.OS = 'somePlatform';
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
Platform.OS = originalPlatform;
});
it('should return correct metadata', (done) => {
operator.handleConfigs({
configs: [
{id: 'Version', value: '7.8.0'},
{id: 'BuildNumber', value: '123'},
],
prepareRecordsOnly: false,
configsToDelete: [],
}).then(() => {
operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', Compliance: 'true'}},
{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: 'user1'},
{id: SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID, value: 'team1'},
],
prepareRecordsOnly: false,
}).then(() => {
observeReportAProblemMetadata(database).subscribe((data) => {
expect(data).toEqual({
currentUserId: 'user1',
currentTeamId: 'team1',
serverVersion: '7.8.0 (Build 123)',
appVersion: '1.2.3 (Build 456)',
appPlatform: 'somePlatform',
deviceModel: 'Unknown',
});
done();
});
});
});
});
it('should handle empty or undefined values', (done) => {
observeReportAProblemMetadata(database).subscribe((data) => {
expect(data).toEqual({
currentUserId: '',
currentTeamId: '',
serverVersion: 'Unknown (Build Unknown)',
appVersion: '1.2.3 (Build 456)',
appPlatform: 'somePlatform',
deviceModel: 'Unknown',
});
done();
});
});
});
describe('observeIsFreeEdition', () => {
const serverUrl = 'baseHandler.test.com';
let database: Database;
let operator: ServerDataOperator;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should return true when no license is present', (done) => {
observeIsFreeEdition(database).subscribe((value) => {
expect(value).toBe(true);
done();
});
});
it('should return true when IsLicensed is false', (done) => {
operator.handleSystem({
systems: [{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'false'}}],
prepareRecordsOnly: false,
}).then(() => {
observeIsFreeEdition(database).subscribe((value) => {
expect(value).toBe(true);
done();
});
});
});
it('should return true when licensed with Entry SKU', (done) => {
operator.handleSystem({
systems: [{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', SkuShortName: License.SKU_SHORT_NAME.Entry}}],
prepareRecordsOnly: false,
}).then(() => {
observeIsFreeEdition(database).subscribe((value) => {
expect(value).toBe(true);
done();
});
});
});
it('should return false when licensed with a paid SKU', (done) => {
operator.handleSystem({
systems: [{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', SkuShortName: License.SKU_SHORT_NAME.Professional}}],
prepareRecordsOnly: false,
}).then(() => {
observeIsFreeEdition(database).subscribe((value) => {
expect(value).toBe(false);
done();
});
});
});
});
describe('getDisconnectedSince', () => {
const serverUrl = 'baseHandler.test.com';
let database: Database;
let operator: ServerDataOperator;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
const serverDatabaseAndOperator = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
database = serverDatabaseAndOperator.database;
operator = serverDatabaseAndOperator.operator;
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should return undefined when no record exists', async () => {
const result = await getDisconnectedSince(database);
expect(result).toBeUndefined();
});
it('should return the stored timestamp when a record exists', async () => {
const timestamp = 1719400000000;
await operator.handleSystem({
systems: [{id: SYSTEM_IDENTIFIERS.DISCONNECTED_SINCE, value: timestamp}],
prepareRecordsOnly: false,
});
const result = await getDisconnectedSince(database);
expect(result).toBe(timestamp);
});
});
describe('getLastBoRPostCleanupRun', () => {
const serverUrl = 'baseHandler.test.com';
let database: Database;
let operator: ServerDataOperator;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
const serverDatabaseAndOperator = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
database = serverDatabaseAndOperator.database;
operator = serverDatabaseAndOperator.operator;
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should return 0 when no BoR post cleanup run record exists', async () => {
const result = await getLastBoRPostCleanupRun(database);
expect(result).toBe(0);
});
it('should return the stored timestamp when BoR post cleanup run record exists', async () => {
const timestamp = 1640995200000; // Example timestamp
await operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LAST_BOR_POST_CLEANUP_RUN, value: timestamp},
],
prepareRecordsOnly: false,
});
const result = await getLastBoRPostCleanupRun(database);
expect(result).toBe(timestamp);
});
it('should return 0 when stored value is falsy', async () => {
await operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LAST_BOR_POST_CLEANUP_RUN, value: 0},
],
prepareRecordsOnly: false,
});
const result = await getLastBoRPostCleanupRun(database);
expect(result).toBe(0);
});
it('should return 0 when stored value is null or undefined', async () => {
await operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LAST_BOR_POST_CLEANUP_RUN, value: null},
],
prepareRecordsOnly: false,
});
const result = await getLastBoRPostCleanupRun(database);
expect(result).toBe(0);
});
});
describe('observeIsMinimumLicenseTier', () => {
const serverUrl = 'baseHandler.test.com';
let database: Database;
let operator: ServerDataOperator;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
const serverDatabaseAndOperator = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
database = serverDatabaseAndOperator.database;
operator = serverDatabaseAndOperator.operator;
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should return false if no license is present', (done) => {
operator.handleConfigs({configs: [
{id: 'BuildEnterpriseReady', value: 'true'},
],
prepareRecordsOnly: false,
configsToDelete: []}).then(() => {
observeIsMinimumLicenseTier(database, License.SKU_SHORT_NAME.Professional).subscribe((isMinimumTier) => {
expect(isMinimumTier).toBe(false);
done();
});
});
});
it('should return false if license tier is below the required tier', (done) => {
operator.handleConfigs({configs: [
{id: 'BuildEnterpriseReady', value: 'true'},
],
prepareRecordsOnly: false,
configsToDelete: []}).then(() => {
operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', SkuShortName: License.SKU_SHORT_NAME.Starter}},
],
prepareRecordsOnly: false,
}).then(() => {
observeIsMinimumLicenseTier(database, License.SKU_SHORT_NAME.Professional).subscribe((isMinimumTier) => {
expect(isMinimumTier).toBe(false);
done();
});
});
});
});
it('should return true if license tier matches the required tier', (done) => {
operator.handleConfigs({configs: [
{id: 'BuildEnterpriseReady', value: 'true'},
],
prepareRecordsOnly: false,
configsToDelete: []}).then(() => {
operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', SkuShortName: License.SKU_SHORT_NAME.Professional}},
],
prepareRecordsOnly: false,
}).then(() => {
observeIsMinimumLicenseTier(database, License.SKU_SHORT_NAME.Professional).subscribe((isMinimumTier) => {
expect(isMinimumTier).toBe(true);
done();
});
});
});
});
it('should return true if license tier is above the required tier', (done) => {
operator.handleConfigs({configs: [
{id: 'BuildEnterpriseReady', value: 'true'},
],
prepareRecordsOnly: false,
configsToDelete: []}).then(() => {
operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', SkuShortName: License.SKU_SHORT_NAME.Enterprise}},
],
prepareRecordsOnly: false,
}).then(() => {
observeIsMinimumLicenseTier(database, License.SKU_SHORT_NAME.Professional).subscribe((isMinimumTier) => {
expect(isMinimumTier).toBe(true);
done();
});
});
});
});
it('should return false if BuildEnterpriseReady is false', (done) => {
operator.handleConfigs({
configs: [
{id: 'BuildEnterpriseReady', value: 'false'},
],
prepareRecordsOnly: false,
configsToDelete: [],
}).then(() => {
operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', SkuShortName: 'professional'}},
],
prepareRecordsOnly: false,
}).then(() => {
observeIsMinimumLicenseTier(database, 'professional').subscribe((isMinimumTier) => {
expect(isMinimumTier).toBe(false);
done();
});
});
});
});
});
describe('system query functions', () => {
const serverUrl = 'system.query.test.com';
let database: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['database'];
let operator: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['operator'];
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('getCurrentChannelId returns empty string when not set', async () => {
expect(await getCurrentChannelId(database)).toBe('');
});
it('getCurrentChannelId returns value when set', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_CHANNEL_ID, value: 'ch1'}], prepareRecordsOnly: false});
expect(await getCurrentChannelId(database)).toBe('ch1');
});
it('getCurrentTeamId returns empty string when not set', async () => {
expect(await getCurrentTeamId(database)).toBe('');
});
it('getCurrentTeamId returns value when set', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID, value: 'team1'}], prepareRecordsOnly: false});
expect(await getCurrentTeamId(database)).toBe('team1');
});
it('getCurrentUserId returns empty string when not set', async () => {
expect(await getCurrentUserId(database)).toBe('');
});
it('getCurrentUserId returns value when set', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: 'user1'}], prepareRecordsOnly: false});
expect(await getCurrentUserId(database)).toBe('user1');
});
it('getPushVerificationStatus returns empty string when not set', async () => {
expect(await getPushVerificationStatus(database)).toBe('');
});
it('getPushVerificationStatus returns value when set', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.PUSH_VERIFICATION_STATUS, value: 'verified'}], prepareRecordsOnly: false});
expect(await getPushVerificationStatus(database)).toBe('verified');
});
it('getCommonSystemValues returns defaults when nothing set', async () => {
const result = await getCommonSystemValues(database);
expect(result.currentChannelId).toBe('');
expect(result.currentTeamId).toBe('');
expect(result.currentUserId).toBe('');
expect(result.lastUnreadChannelId).toBe('');
});
it('getCommonSystemValues returns stored values', async () => {
const license = {IsLicensed: 'true'};
await operator.handleSystem({
systems: [
{id: SYSTEM_IDENTIFIERS.CURRENT_CHANNEL_ID, value: 'ch1'},
{id: SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID, value: 'team1'},
{id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID, value: 'user1'},
{id: SYSTEM_IDENTIFIERS.LICENSE, value: license},
{id: SYSTEM_IDENTIFIERS.LAST_UNREAD_CHANNEL_ID, value: 'unread1'},
],
prepareRecordsOnly: false,
});
const result = await getCommonSystemValues(database);
expect(result.currentChannelId).toBe('ch1');
expect(result.currentTeamId).toBe('team1');
expect(result.currentUserId).toBe('user1');
expect(result.lastUnreadChannelId).toBe('unread1');
});
it('getConfig returns all config as object', async () => {
await operator.handleConfigs({
configs: [{id: 'SiteName', value: 'MyMattermost'}, {id: 'Version', value: '7.0.0'}],
configsToDelete: [],
prepareRecordsOnly: false,
});
const config = await getConfig(database);
expect(config.SiteName).toBe('MyMattermost');
expect(config.Version).toBe('7.0.0');
});
it('getConfigValue returns undefined when key missing', async () => {
expect(await getConfigValue(database, 'SiteName')).toBeUndefined();
});
it('getConfigValue returns value when key present', async () => {
await operator.handleConfigs({configs: [{id: 'SiteName', value: 'Test'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await getConfigValue(database, 'SiteName')).toBe('Test');
});
it("getConfigBooleanValue returns true only for the string 'true'", async () => {
await operator.handleConfigs({configs: [{id: 'EnableCustomEmoji', value: 'true'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await getConfigBooleanValue(database, 'EnableCustomEmoji')).toBe(true);
});
it("getConfigBooleanValue returns false for a non-'true' string value", async () => {
await operator.handleConfigs({configs: [{id: 'EnableCustomEmoji', value: 'false'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await getConfigBooleanValue(database, 'EnableCustomEmoji')).toBe(false);
});
it('getConfigBooleanValue returns the default value when the key is missing', async () => {
expect(await getConfigBooleanValue(database, 'EnableCustomEmoji')).toBe(false);
expect(await getConfigBooleanValue(database, 'EnableCustomEmoji', true)).toBe(true);
});
it('getLastGlobalDataRetentionRun returns undefined when not set', async () => {
expect(await getLastGlobalDataRetentionRun(database)).toBeUndefined();
});
it('getLastGlobalDataRetentionRun returns stored value', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.LAST_DATA_RETENTION_RUN, value: 12345}], prepareRecordsOnly: false});
expect(await getLastGlobalDataRetentionRun(database)).toBe(12345);
});
it('getGlobalDataRetentionPolicy returns undefined when not set', async () => {
expect(await getGlobalDataRetentionPolicy(database)).toBeUndefined();
});
it('getGranularDataRetentionPolicies returns undefined when not set', async () => {
expect(await getGranularDataRetentionPolicies(database)).toBeUndefined();
});
it('getIsDataRetentionEnabled returns null when no license', async () => {
expect(await getIsDataRetentionEnabled(database)).toBeNull();
});
it('getIsDataRetentionEnabled returns false when DataRetention not in license', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', DataRetention: 'false'}}], prepareRecordsOnly: false});
await operator.handleConfigs({configs: [{id: 'DataRetentionEnableMessageDeletion', value: 'true'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await getIsDataRetentionEnabled(database)).toBe(false);
});
it('getIsDataRetentionEnabled returns true when all conditions met', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.LICENSE, value: {IsLicensed: 'true', DataRetention: 'true'}}], prepareRecordsOnly: false});
await operator.handleConfigs({configs: [{id: 'DataRetentionEnableMessageDeletion', value: 'true'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await getIsDataRetentionEnabled(database)).toBe(true);
});
it('getLicense returns undefined when not set', async () => {
expect(await getLicense(database)).toBeUndefined();
});
it('getLicense returns stored license', async () => {
const license = {IsLicensed: 'true', SkuShortName: 'enterprise'};
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.LICENSE, value: license}], prepareRecordsOnly: false});
expect(await getLicense(database)).toEqual(license);
});
it('getRecentCustomStatuses returns empty array when not set', async () => {
expect(await getRecentCustomStatuses(database)).toEqual([]);
});
it('getExpandedLinks returns empty object when not set', async () => {
expect(await getExpandedLinks(database)).toEqual({});
});
it('getExpandedLinks returns stored value', async () => {
const links = {'https://short.url': 'https://expanded.url'};
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.EXPANDED_LINKS, value: links}], prepareRecordsOnly: false});
expect(await getExpandedLinks(database)).toEqual(links);
});
it('getRecentReactions returns empty array when not set', async () => {
expect(await getRecentReactions(database)).toEqual([]);
});
it('getLastFullSync returns 0 when not set', async () => {
expect(await getLastFullSync(database)).toBe(0);
});
it('setLastFullSync stores value and getLastFullSync retrieves it', async () => {
await setLastFullSync(operator, 99999);
expect(await getLastFullSync(database)).toBe(99999);
});
it('resetLastFullSync sets value to 0 when previously set', async () => {
await setLastFullSync(operator, 12345);
await resetLastFullSync(operator);
expect(await getLastFullSync(database)).toBe(0);
});
it('resetLastFullSync returns empty array when already 0', async () => {
const result = await resetLastFullSync(operator);
expect(result).toEqual([]);
});
it('getTeamHistory returns empty array when not set', async () => {
expect(await getTeamHistory(database)).toEqual([]);
});
it('patchTeamHistory stores team history', async () => {
await patchTeamHistory(operator, ['team1', 'team2']);
const history = await getTeamHistory(database);
expect(history).toEqual(['team1', 'team2']);
});
it('prepareCommonSystemValues returns system models', async () => {
const models = await prepareCommonSystemValues(operator, {currentUserId: 'u1', currentTeamId: 't1', currentChannelId: 'c1'});
expect(models.length).toBeGreaterThan(0);
});
it('setCurrentUserId stores and returns userId', async () => {
const result = await setCurrentUserId(operator, 'user123');
expect(result).toEqual({currentUserId: 'user123'});
expect(await getCurrentUserId(database)).toBe('user123');
});
it('setCurrentChannelId stores and returns channelId', async () => {
const result = await setCurrentChannelId(operator, 'ch123');
expect(result).toEqual({currentChannelId: 'ch123'});
expect(await getCurrentChannelId(database)).toBe('ch123');
});
it('setCurrentTeamId stores and returns teamId', async () => {
const result = await setCurrentTeamId(operator, 'team123');
expect(result).toEqual({currentTeamId: 'team123'});
expect(await getCurrentTeamId(database)).toBe('team123');
});
it('setCurrentTeamAndChannelId stores both values', async () => {
const result = await setCurrentTeamAndChannelId(operator, 'team1', 'ch1');
expect(result).toEqual({currentTeamId: 'team1', currentChannelId: 'ch1'});
});
it('getLastUnreadChannelId returns empty string when not set', async () => {
expect(await getLastUnreadChannelId(database)).toBe('');
});
it('getLastUnreadChannelId returns stored value', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.LAST_UNREAD_CHANNEL_ID, value: 'ch_unread'}], prepareRecordsOnly: false});
expect(await getLastUnreadChannelId(database)).toBe('ch_unread');
});
it('getExpiredSession returns undefined when not set', async () => {
expect(await getExpiredSession(database)).toBeUndefined();
});
});
describe('system observe functions', () => {
const serverUrl = 'system.observe.test.com';
let database: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['database'];
let operator: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['operator'];
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('observeCurrentChannelId emits empty string when not set', async () => {
expect(await firstValueFrom(observeCurrentChannelId(database))).toBe('');
});
it('observeCurrentChannelId emits stored value', async () => {
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.CURRENT_CHANNEL_ID, value: 'ch1'}], prepareRecordsOnly: false});
expect(await firstValueFrom(observeCurrentChannelId(database))).toBe('ch1');
});
it('observeCurrentTeamId emits empty string when not set', async () => {
expect(await firstValueFrom(observeCurrentTeamId(database))).toBe('');
});
it('observeCurrentUserId emits empty string when not set', async () => {
expect(await firstValueFrom(observeCurrentUserId(database))).toBe('');
});
it('observeGlobalThreadsTab emits "all" when not set', async () => {
expect(await firstValueFrom(observeGlobalThreadsTab(database))).toBe('all');
});
it('observePushVerificationStatus emits unknown when not set', async () => {
const val = await firstValueFrom(observePushVerificationStatus(database));
expect(typeof val).toBe('string');
});
it('observeConfig emits config object', async () => {
await operator.handleConfigs({configs: [{id: 'SiteName', value: 'Test'}], configsToDelete: [], prepareRecordsOnly: false});
const config = await firstValueFrom(observeConfig(database));
expect(config?.SiteName).toBe('Test');
});
it('observeConfigValue emits value for key', async () => {
await operator.handleConfigs({configs: [{id: 'Version', value: '8.0.0'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeConfigValue(database, 'Version'))).toBe('8.0.0');
});
it('observeConfigValue emits undefined when key missing', async () => {
expect(await firstValueFrom(observeConfigValue(database, 'SiteName'))).toBeUndefined();
});
it('observeMaxFileCount emits 5 for old server version', async () => {
await operator.handleConfigs({configs: [{id: 'Version', value: '5.0.0'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeMaxFileCount(database))).toBe(5);
});
it('observeMaxFileCount emits 10 for server version >= 6.0', async () => {
await operator.handleConfigs({configs: [{id: 'Version', value: '6.0.0'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeMaxFileCount(database))).toBe(10);
});
it('observeIsCustomStatusExpirySupported emits true for server version >= 5.37', async () => {
await operator.handleConfigs({configs: [{id: 'Version', value: '5.37.0'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeIsCustomStatusExpirySupported(database))).toBe(true);
});
it('observeConfigBooleanValue emits false by default', async () => {
expect(await firstValueFrom(observeConfigBooleanValue(database, 'EnableCustomEmoji'))).toBe(false);
});
it('observeConfigBooleanValue emits true when config is "true"', async () => {
await operator.handleConfigs({configs: [{id: 'EnableCustomEmoji', value: 'true'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeConfigBooleanValue(database, 'EnableCustomEmoji'))).toBe(true);
});
it('canViewArchivedChannels resolves true when ExperimentalViewArchivedChannels is absent', async () => {
expect(await canViewArchivedChannels(database)).toBe(true);
});
it('canViewArchivedChannels resolves true when ExperimentalViewArchivedChannels is "true"', async () => {
await operator.handleConfigs({configs: [{id: 'ExperimentalViewArchivedChannels', value: 'true'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await canViewArchivedChannels(database)).toBe(true);
});
it('canViewArchivedChannels resolves false when ExperimentalViewArchivedChannels is "false"', async () => {
await operator.handleConfigs({configs: [{id: 'ExperimentalViewArchivedChannels', value: 'false'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await canViewArchivedChannels(database)).toBe(false);
});
it('observeCanViewArchivedChannels emits true when ExperimentalViewArchivedChannels is absent', async () => {
expect(await firstValueFrom(observeCanViewArchivedChannels(database))).toBe(true);
});
it('observeCanViewArchivedChannels emits true when ExperimentalViewArchivedChannels is "true"', async () => {
await operator.handleConfigs({configs: [{id: 'ExperimentalViewArchivedChannels', value: 'true'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeCanViewArchivedChannels(database))).toBe(true);
});
it('observeCanViewArchivedChannels emits false when ExperimentalViewArchivedChannels is "false"', async () => {
await operator.handleConfigs({configs: [{id: 'ExperimentalViewArchivedChannels', value: 'false'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeCanViewArchivedChannels(database))).toBe(false);
});
it('observeConfigIntValue emits defaultValue when not set', async () => {
expect(await firstValueFrom(observeConfigIntValue(database, 'MaxPostSize', 50))).toBe(50);
});
it('observeConfigIntValue emits parsed integer', async () => {
await operator.handleConfigs({configs: [{id: 'MaxPostSize', value: '200'}], configsToDelete: [], prepareRecordsOnly: false});
expect(await firstValueFrom(observeConfigIntValue(database, 'MaxPostSize'))).toBe(200);
});
it('observeLicense emits undefined when not set', async () => {
expect(await firstValueFrom(observeLicense(database))).toBeUndefined();
});
it('observeLicense emits stored license', async () => {
const license = {IsLicensed: 'true'};
await operator.handleSystem({systems: [{id: SYSTEM_IDENTIFIERS.LICENSE, value: license}], prepareRecordsOnly: false});
expect(await firstValueFrom(observeLicense(database))).toEqual(license);
});
it('observeAllowedThemesKeys emits all default theme keys when AllowedThemes not set', async () => {
const keys = await firstValueFrom(observeAllowedThemesKeys(database));
expect(keys).toEqual(Object.keys(Preferences.THEMES));
});
it('observeAllowedThemesKeys filters when AllowedThemes is set', async () => {
await operator.handleConfigs({configs: [{id: 'AllowedThemes', value: 'denim'}], configsToDelete: [], prepareRecordsOnly: false});
const keys = await firstValueFrom(observeAllowedThemesKeys(database));
expect(keys).toEqual(['denim']);
});
it('observeOnlyUnreads emits false when not set', async () => {
expect(await firstValueFrom(observeOnlyUnreads(database))).toBe(false);
});
});
describe('getLastInitialLoad / setLastInitialLoad', () => {
const serverUrl = 'baseHandler.test.com';
let database: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['database'];
let operator: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['operator'];
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should return 0 when no record exists', async () => {
const result = await getLastInitialLoad(database);
expect(result).toBe(0);
});
it('should return the stored timestamp after setLastInitialLoad', async () => {
const timestamp = 1706000000000;
await setLastInitialLoad(operator, timestamp);
const result = await getLastInitialLoad(database);
expect(result).toBe(timestamp);
});
});
describe('getLastTeamLoad / setLastTeamLoad / prepareDeleteTeamLoadCursor', () => {
const serverUrl = 'baseHandler.test.com';
const teamId = 'team1';
let database: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['database'];
let operator: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['operator'];
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should return 0 when no record exists', async () => {
const result = await getLastTeamLoad(database, teamId);
expect(result).toBe(0);
});
it('should return the stored timestamp after setLastTeamLoad', async () => {
const timestamp = 1706000000000;
await setLastTeamLoad(operator, teamId, timestamp);
const result = await getLastTeamLoad(database, teamId);
expect(result).toBe(timestamp);
});
it('prepareDeleteTeamLoadCursor - returns undefined when record does not exist', async () => {
const model = await prepareDeleteTeamLoadCursor(database, teamId);
expect(model).toBeUndefined();
});
it('prepareDeleteTeamLoadCursor - returns a prepared destroy model when record exists', async () => {
await setLastTeamLoad(operator, teamId, 1706000000000);
const model = await prepareDeleteTeamLoadCursor(database, teamId);
expect(model).toBeDefined();
await database.write(async () => {
await model!.destroyPermanently();
});
const result = await getLastTeamLoad(database, teamId);
expect(result).toBe(0);
});
});
describe('decrementTeamBlob', () => {
const serverUrl = 'baseHandler.test.com';
const teamId = 'team1';
let database: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['database'];
let operator: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['operator'];
let seedBlob: ReturnType<typeof makeSeedTeamBadgeBlob>;
let readBlob: ReturnType<typeof makeReadTeamBadgeBlob>;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
seedBlob = makeSeedTeamBadgeBlob(operator);
readBlob = makeReadTeamBadgeBlob(database);
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should subtract clearedMentions from the team slot', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 5, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await decrementTeamBlob(operator, teamId, 3);
const blob = await readBlob();
expect(blob?.teams[teamId].mentionCount).toBe(2);
expect(blob?.teams[teamId].hasUnreads).toBe(true);
});
it('should leave hasUnreads true on a team slot even when mentionCount drops to 0 (conservative)', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 3, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await decrementTeamBlob(operator, teamId, 3);
const blob = await readBlob();
expect(blob?.teams[teamId].mentionCount).toBe(0);
// Conservative — other channels might still contribute plain unreads.
expect(blob?.teams[teamId].hasUnreads).toBe(true);
});
it('should flip direct.hasUnreads to false when direct mentionCount reaches 0', async () => {
await seedBlob({
teams: {},
direct: {mentionCount: 2, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false},
});
await decrementTeamBlob(operator, '', 2);
const blob = await readBlob();
expect(blob?.direct.mentionCount).toBe(0);
// DM/GM: every message is a mention, so hasUnreads cleanly flips off.
expect(blob?.direct.hasUnreads).toBe(false);
});
it('should clamp the decrement at 0 instead of going negative', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 1, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await decrementTeamBlob(operator, teamId, 999);
const blob = await readBlob();
expect(blob?.teams[teamId].mentionCount).toBe(0);
});
it('should be a no-op when there is no blob in the system table', async () => {
// No seed.
await expect(decrementTeamBlob(operator, teamId, 5)).resolves.not.toThrow();
});
it('should be a no-op when the team slot does not exist in the blob', async () => {
await seedBlob({
teams: {other_team: {mentionCount: 2, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await decrementTeamBlob(operator, teamId, 5);
const blob = await readBlob();
expect(blob?.teams.other_team.mentionCount).toBe(2);
expect(blob?.teams[teamId]).toBeUndefined();
});
});
describe('adjustThreadInBlob', () => {
const serverUrl = 'baseHandler.test.com';
const teamId = 'team1';
let database: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['database'];
let operator: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['operator'];
let seedBlob: ReturnType<typeof makeSeedTeamBadgeBlob>;
let readBlob: ReturnType<typeof makeReadTeamBadgeBlob>;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
seedBlob = makeSeedTeamBadgeBlob(operator);
readBlob = makeReadTeamBadgeBlob(database);
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should subtract a positive mentionDelta from the team slot threadMentionCount', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 0, hasUnreads: false, threadMentionCount: 4, threadHasUnreads: true}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
// previous=3, after=1 -> delta=2 mentions cleared, thread still unread
await adjustThreadInBlob(operator, teamId, 2, true);
const blob = await readBlob();
expect(blob?.teams[teamId].threadMentionCount).toBe(2);
expect(blob?.teams[teamId].threadHasUnreads).toBe(true);
});
it('should add a negative mentionDelta back to the team slot (mark-unread)', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 0, hasUnreads: false, threadMentionCount: 1, threadHasUnreads: true}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
// previous=0, after=2 -> delta=-2 (re-introduce)
await adjustThreadInBlob(operator, teamId, -2, true);
const blob = await readBlob();
expect(blob?.teams[teamId].threadMentionCount).toBe(3);
expect(blob?.teams[teamId].threadHasUnreads).toBe(true);
});
it('should leave threadHasUnreads true on a team slot even when threadMentionCount drops to 0 (conservative)', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 0, hasUnreads: false, threadMentionCount: 2, threadHasUnreads: true}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await adjustThreadInBlob(operator, teamId, 2, false);
const blob = await readBlob();
expect(blob?.teams[teamId].threadMentionCount).toBe(0);
// Conservative — other threads in the team may still be unread.
expect(blob?.teams[teamId].threadHasUnreads).toBe(true);
});
it('should flip direct.threadHasUnreads to false when hasUnreadsAfter is false', async () => {
await seedBlob({
teams: {},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 1, threadHasUnreads: true},
});
await adjustThreadInBlob(operator, '', 1, false);
const blob = await readBlob();
expect(blob?.direct.threadMentionCount).toBe(0);
expect(blob?.direct.threadHasUnreads).toBe(false);
});
it('should clamp the decrement at 0 instead of going negative', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 0, hasUnreads: false, threadMentionCount: 1, threadHasUnreads: true}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await adjustThreadInBlob(operator, teamId, 999, false);
const blob = await readBlob();
expect(blob?.teams[teamId].threadMentionCount).toBe(0);
});
it('should be a no-op when there is no blob in the system table', async () => {
// No seed.
await expect(adjustThreadInBlob(operator, teamId, 2, false)).resolves.not.toThrow();
});
it('should be a no-op when the team slot does not exist in the blob', async () => {
await seedBlob({
teams: {other_team: {mentionCount: 0, hasUnreads: false, threadMentionCount: 5, threadHasUnreads: true}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await adjustThreadInBlob(operator, teamId, 2, true);
const blob = await readBlob();
expect(blob?.teams.other_team.threadMentionCount).toBe(5);
expect(blob?.teams[teamId]).toBeUndefined();
});
});
describe('incrementTeamBlob', () => {
const serverUrl = 'baseHandler.test.com';
const teamId = 'team1';
let database: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['database'];
let operator: ReturnType<typeof DatabaseManager.getServerDatabaseAndOperator>['operator'];
let seedBlob: ReturnType<typeof makeSeedTeamBadgeBlob>;
let readBlob: ReturnType<typeof makeReadTeamBadgeBlob>;
beforeEach(async () => {
await DatabaseManager.init([serverUrl]);
({database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl));
seedBlob = makeSeedTeamBadgeBlob(operator);
readBlob = makeReadTeamBadgeBlob(database);
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase(serverUrl);
});
it('should increment mentionCount by 1 when mentionDelta is 1', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 2, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await incrementTeamBlob(operator, teamId, 1);
const blob = await readBlob();
expect(blob?.teams[teamId].mentionCount).toBe(3);
expect(blob?.teams[teamId].hasUnreads).toBe(true);
});
it('should increment mentionCount by N when mentionDelta is N (e.g. unmute restores historical mentions)', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 1, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await incrementTeamBlob(operator, teamId, 5);
const blob = await readBlob();
expect(blob?.teams[teamId].mentionCount).toBe(6);
expect(blob?.teams[teamId].hasUnreads).toBe(true);
});
it('should leave mentionCount unchanged when mentionDelta is 0 but flip hasUnreads', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await incrementTeamBlob(operator, teamId, 0);
const blob = await readBlob();
expect(blob?.teams[teamId].mentionCount).toBe(0);
expect(blob?.teams[teamId].hasUnreads).toBe(true);
});
it('should leave hasUnreads true when already true and mentionDelta is 0', async () => {
await seedBlob({
teams: {[teamId]: {mentionCount: 0, hasUnreads: true, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await incrementTeamBlob(operator, teamId, 0);
const blob = await readBlob();
expect(blob?.teams[teamId].mentionCount).toBe(0);
expect(blob?.teams[teamId].hasUnreads).toBe(true);
});
it('should be a no-op when there is no blob in the system table', async () => {
// No seed.
await expect(incrementTeamBlob(operator, teamId, 1)).resolves.not.toThrow();
});
it('should be a no-op when the team slot does not exist in the blob', async () => {
await seedBlob({
teams: {other_team: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false}},
direct: {mentionCount: 0, hasUnreads: false, threadMentionCount: 0, threadHasUnreads: false},
});
await incrementTeamBlob(operator, teamId, 1);
const blob = await readBlob();
expect(blob?.teams.other_team.mentionCount).toBe(0);
expect(blob?.teams[teamId]).toBeUndefined();
});
});