Fix mock test env check outside of browser

This commit is contained in:
Jamie Kyle 2024-04-16 13:13:02 -07:00 committed by GitHub
parent dbff1ab4d1
commit 3f074a7737
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 47 additions and 22 deletions

View file

@ -21,6 +21,9 @@ import {
ScrollerLockContext,
createScrollerLock,
} from '../ts/hooks/useScrollLock';
import { Environment, setEnvironment } from '../ts/environment.ts';
setEnvironment(Environment.Development, true);
const i18n = setupI18n('en', messages);
@ -94,7 +97,6 @@ window.SignalContext = {
unregisterForChange: noop,
},
isTestOrMockEnvironment: () => false,
nativeThemeListener: {
getSystemTheme: () => 'light',
subscribe: noop,

View file

@ -15,9 +15,12 @@ import {
// In production mode, NODE_ENV cannot be customized by the user
if (app.isPackaged) {
setEnvironment(Environment.Production);
setEnvironment(Environment.Production, false);
} else {
setEnvironment(parseEnvironment(process.env.NODE_ENV || 'development'));
setEnvironment(
parseEnvironment(process.env.NODE_ENV || 'development'),
Boolean(process.env.MOCK_TEST)
);
}
// Set environment vars to configure node-config before requiring it

View file

@ -2450,6 +2450,7 @@ ipc.on('get-config', async event => {
!isTestEnvironment(getEnvironment()) && ciMode
? Environment.Production
: getEnvironment(),
isMockTestEnvironment: Boolean(process.env.MOCK_TEST),
ciMode,
// Should be already computed and cached at this point
dnsFallback: await getDNSFallback(),

View file

@ -12,7 +12,7 @@ const { HourCyclePreference } = require('../ts/types/I18N');
chai.use(chaiAsPromised);
setEnvironment(Environment.Test);
setEnvironment(Environment.Test, true);
const storageMap = new Map();
@ -32,7 +32,6 @@ global.window = {
getHourCyclePreference: () => HourCyclePreference.UnknownPreference,
getPreferredSystemLocales: () => ['en'],
getLocaleOverride: () => null,
isTestOrMockEnvironment: () => true,
},
i18n: key => `i18n(${key})`,
storage: {

View file

@ -142,21 +142,20 @@ export function FromSomeoneToGroup(): JSX.Element {
}
export function LongSearchResult(): JSX.Element {
const snippets = [
'This is a really <<left>>detail<<right>>ed long line which will wrap and only be cut off after it gets to three lines. So maybe this will make it in as well?',
"Okay, here are the <<left>>detail<<right>>s:\n\n1355 Ridge Way\nCode: 234\n\nI'm excited!",
];
const props1 = useProps({
from: someone,
to: me,
snippet: snippets[0],
snippet:
'This is a really <<left>>detail<<right>>ed long line which will wrap and only be cut off after it gets to three lines. So maybe this will make it in as well?',
body: 'This is a really detailed long line which will wrap and only be cut off after it gets to three lines. So maybe this will make it in as well?',
});
const props2 = useProps({
from: someone,
to: me,
snippet: snippets[1],
snippet:
"Okay, here are the <<left>>detail<<right>>s:\n\n1355 Ridge Way\nCode: 234\n\nI'm excited!",
body: "Okay, here are the details:\n\n1355 Ridge Way\nCode: 234\n\nI'm excited!",
});
return (

View file

@ -4,16 +4,15 @@
import { config } from './config';
import {
getEnvironment,
isTestEnvironment,
parseEnvironment,
setEnvironment,
} from '../environment';
setEnvironment(parseEnvironment(config.environment));
setEnvironment(
parseEnvironment(config.environment),
config.isMockTestEnvironment
);
const environment = getEnvironment();
const isTestOrMockEnvironment =
isTestEnvironment(environment) || Boolean(process.env.MOCK_TEST);
export { environment, isTestOrMockEnvironment };
export { environment };

View file

@ -12,6 +12,7 @@ export enum Environment {
}
let environment: undefined | Environment;
let isMockTestEnvironment: undefined | boolean;
export function getEnvironment(): Environment {
if (environment === undefined) {
@ -26,12 +27,16 @@ export function getEnvironment(): Environment {
/**
* Sets the current environment. Should be called early in a process's life, and can only
* be called once.
*
* isMockTestEnv is used when running tests that require a non-"test" environment but
* need to mock certain behaviors.
*/
export function setEnvironment(env: Environment): void {
export function setEnvironment(env: Environment, isMockTestEnv: boolean): void {
if (environment !== undefined) {
throw new Error('Environment has already been set');
}
environment = env;
isMockTestEnvironment = isMockTestEnv;
}
export const parseEnvironment = makeEnumParser(
@ -41,3 +46,10 @@ export const parseEnvironment = makeEnumParser(
export const isTestEnvironment = (env: Environment): boolean =>
env === Environment.Test;
export const isTestOrMockEnvironment = (): boolean => {
if (isMockTestEnvironment == null) {
throw new Error('Mock test environment not set');
}
return isTestEnvironment(getEnvironment()) || isMockTestEnvironment;
};

View file

@ -45,6 +45,7 @@ export const rendererConfigSchema = z.object({
dnsFallback: DNSFallbackSchema,
ciBackupPath: configOptionalStringSchema,
environment: environmentSchema,
isMockTestEnvironment: z.boolean(),
homePath: configRequiredStringSchema,
hostname: configRequiredStringSchema,
installPath: configRequiredStringSchema,

View file

@ -1,6 +1,8 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { isTestOrMockEnvironment } from '../environment';
/**
* Left-to-Right Isolate
* Sets direction to LTR and isolates the embedded content from the surrounding text
@ -208,7 +210,7 @@ export function _bidiIsolate(text: string): string {
* ```
*/
export function bidiIsolate(text: string): string {
if (window.SignalContext.isTestOrMockEnvironment()) {
if (isTestOrMockEnvironment()) {
// Turn this off in tests to make it easier to compare strings
return text;
}

View file

@ -12,13 +12,14 @@ import { activeWindowService } from '../context/activeWindowService';
import { config } from '../context/config';
import { createNativeThemeListener } from '../context/createNativeThemeListener';
import { createSetting } from '../util/preload';
import { environment, isTestOrMockEnvironment } from '../context/environment';
import { environment } from '../context/environment';
import {
localeDisplayNames,
countryDisplayNames,
localeMessages,
} from '../context/localeMessages';
import { waitForSettingsChange } from '../context/waitForSettingsChange';
import { isTestOrMockEnvironment } from '../environment';
const emojiListCache = new Map<string, LocaleEmojiListType>();
@ -71,7 +72,7 @@ export const MinimalSignalContext: MinimalSignalContextType = {
getHourCyclePreference: () => config.hourCyclePreference,
getPreferredSystemLocales: () => config.preferredSystemLocales,
getLocaleOverride: () => config.localeOverride,
isTestOrMockEnvironment: () => isTestOrMockEnvironment,
isTestOrMockEnvironment,
nativeThemeListener: createNativeThemeListener(ipcRenderer, window),
restartApp: () => ipcRenderer.send('restart'),
OS: {

View file

@ -9,6 +9,7 @@ import { i18n } from '../sandboxedInit';
import { Preferences } from '../../components/Preferences';
import { startInteractionMode } from '../../services/InteractionMode';
import { strictAssert } from '../../util/assert';
import { parseEnvironment, setEnvironment } from '../../environment';
const { SettingsWindowProps } = window.Signal;
@ -16,6 +17,11 @@ strictAssert(SettingsWindowProps, 'window values not provided');
startInteractionMode();
setEnvironment(
parseEnvironment(window.SignalContext.getEnvironment()),
window.SignalContext.isTestOrMockEnvironment()
);
SettingsWindowProps.onRender(
({
addCustomColor,