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

View file

@ -15,9 +15,12 @@ import {
// In production mode, NODE_ENV cannot be customized by the user // In production mode, NODE_ENV cannot be customized by the user
if (app.isPackaged) { if (app.isPackaged) {
setEnvironment(Environment.Production); setEnvironment(Environment.Production, false);
} else { } 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 // 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 !isTestEnvironment(getEnvironment()) && ciMode
? Environment.Production ? Environment.Production
: getEnvironment(), : getEnvironment(),
isMockTestEnvironment: Boolean(process.env.MOCK_TEST),
ciMode, ciMode,
// Should be already computed and cached at this point // Should be already computed and cached at this point
dnsFallback: await getDNSFallback(), dnsFallback: await getDNSFallback(),

View file

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

View file

@ -142,21 +142,20 @@ export function FromSomeoneToGroup(): JSX.Element {
} }
export function LongSearchResult(): 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({ const props1 = useProps({
from: someone, from: someone,
to: me, 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({ const props2 = useProps({
from: someone, from: someone,
to: me, 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 ( return (

View file

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

View file

@ -12,6 +12,7 @@ export enum Environment {
} }
let environment: undefined | Environment; let environment: undefined | Environment;
let isMockTestEnvironment: undefined | boolean;
export function getEnvironment(): Environment { export function getEnvironment(): Environment {
if (environment === undefined) { 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 * Sets the current environment. Should be called early in a process's life, and can only
* be called once. * 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) { if (environment !== undefined) {
throw new Error('Environment has already been set'); throw new Error('Environment has already been set');
} }
environment = env; environment = env;
isMockTestEnvironment = isMockTestEnv;
} }
export const parseEnvironment = makeEnumParser( export const parseEnvironment = makeEnumParser(
@ -41,3 +46,10 @@ export const parseEnvironment = makeEnumParser(
export const isTestEnvironment = (env: Environment): boolean => export const isTestEnvironment = (env: Environment): boolean =>
env === Environment.Test; 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, dnsFallback: DNSFallbackSchema,
ciBackupPath: configOptionalStringSchema, ciBackupPath: configOptionalStringSchema,
environment: environmentSchema, environment: environmentSchema,
isMockTestEnvironment: z.boolean(),
homePath: configRequiredStringSchema, homePath: configRequiredStringSchema,
hostname: configRequiredStringSchema, hostname: configRequiredStringSchema,
installPath: configRequiredStringSchema, installPath: configRequiredStringSchema,

View file

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

View file

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

View file

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