signal-desktop/ts/windows/main/preload_test.ts

149 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-console */
2024-06-28 00:22:06 +00:00
import { inspect, parseArgs } from 'node:util';
import { ipcRenderer as ipc } from 'electron';
import { sync } from 'fast-glob';
// eslint-disable-next-line import/no-extraneous-dependencies
2024-07-22 19:27:09 +00:00
import chai, { assert, config as chaiConfig } from 'chai';
// eslint-disable-next-line import/no-extraneous-dependencies
import chaiAsPromised from 'chai-as-promised';
// eslint-disable-next-line import/no-extraneous-dependencies
2024-06-28 00:22:06 +00:00
import { reporters, type MochaOptions } from 'mocha';
import { getSignalProtocolStore } from '../../SignalProtocolStore';
import { initMessageCleanup } from '../../services/messageStateCleanup';
2023-04-11 03:54:43 +00:00
import { initializeMessageCounter } from '../../util/incrementMessageCounter';
import { initializeRedux } from '../../state/initializeRedux';
import * as Stickers from '../../types/Stickers';
import { ThemeType } from '../../types/Util';
2024-07-22 19:27:09 +00:00
chai.use(chaiAsPromised);
// Show actual objects instead of abbreviated errors
chaiConfig.truncateThreshold = 0;
function patchDeepEqual(method: 'deepEqual' | 'deepStrictEqual'): void {
const originalFn = assert[method];
assert[method] = (...args) => {
try {
return originalFn(...args);
} catch (error) {
reporters.base.useColors = false;
error.message = reporters.base.generateDiff(
inspect(error.actual, { depth: Infinity, sorted: true }),
inspect(error.expected, { depth: Infinity, sorted: true })
);
throw error;
}
};
}
patchDeepEqual('deepEqual');
patchDeepEqual('deepStrictEqual');
window.assert = assert;
// This is a hack to let us run TypeScript tests in the renderer process. See the
// code in `test/test.js`.
2024-06-28 00:22:06 +00:00
const setup: MochaOptions = {};
2024-07-22 19:27:09 +00:00
let worker = 0;
let workerCount = 1;
2024-06-28 00:22:06 +00:00
{
const { values } = parseArgs({
args: ipc.sendSync('ci:test-electron:getArgv'),
options: {
grep: {
type: 'string',
},
2024-07-22 19:27:09 +00:00
worker: {
type: 'string',
},
'worker-count': {
type: 'string',
},
2024-06-28 00:22:06 +00:00
},
strict: false,
});
if (typeof values.grep === 'string') {
setup.grep = values.grep;
}
2024-07-22 19:27:09 +00:00
if (typeof values.worker === 'string') {
worker = parseInt(values.worker, 10);
}
if (typeof values['worker-count'] === 'string') {
workerCount = parseInt(values['worker-count'], 10);
}
2024-06-28 00:22:06 +00:00
}
window.testUtilities = {
2024-06-28 00:22:06 +00:00
setup,
2024-06-28 00:22:06 +00:00
onTestEvent(event: unknown) {
return ipc.invoke('ci:test-electron:event', event);
},
2024-07-02 19:56:40 +00:00
debug(info) {
return ipc.invoke('ci:test-electron:debug', info);
},
async initialize() {
initMessageCleanup();
await initializeMessageCounter();
await Stickers.load();
initializeRedux({
badgesState: { byId: {} },
2024-04-01 19:19:35 +00:00
callLinks: [],
callHistory: [],
callHistoryUnreadCount: 0,
mainWindowStats: {
isFullScreen: false,
isMaximized: false,
},
menuOptions: {
development: false,
devTools: false,
includeSetup: false,
isProduction: false,
platform: 'test',
},
recentEmoji: {
recents: [],
},
stories: [],
storyDistributionLists: [],
stickers: {
installedPack: null,
packs: {},
recentStickers: [],
blessedPacks: {},
},
theme: ThemeType.dark,
});
},
prepareTests() {
console.log('Preparing tests...');
2024-07-22 19:27:09 +00:00
const files = sync('../../test-{both,electron}/**/*_test.js', {
absolute: true,
cwd: __dirname,
2024-07-22 19:27:09 +00:00
});
for (let i = 0; i < files.length; i += 1) {
if (i % workerCount === worker) {
// eslint-disable-next-line import/no-dynamic-require, global-require
require(files[i]);
}
}
},
};
window.getSignalProtocolStore = getSignalProtocolStore;