signal-desktop/ts/test-mock/bootstrap.ts

679 lines
18 KiB
TypeScript
Raw Normal View History

// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import assert from 'assert';
import fs from 'fs/promises';
2024-03-15 14:20:33 +00:00
import crypto from 'crypto';
import path, { join } from 'path';
import os from 'os';
import createDebug from 'debug';
2023-03-13 23:41:47 +00:00
import pTimeout from 'p-timeout';
import normalizePath from 'normalize-path';
2024-03-15 14:20:33 +00:00
import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import type { Page } from 'playwright';
import type { Device, PrimaryDevice } from '@signalapp/mock-server';
2023-08-16 20:54:39 +00:00
import {
Server,
ServiceIdKind,
loadCertificates,
} from '@signalapp/mock-server';
2022-04-13 00:50:17 +00:00
import { MAX_READ_KEYS as MAX_STORAGE_READ_KEYS } from '../services/storageConstants';
import * as durations from '../util/durations';
2023-03-13 23:41:47 +00:00
import { drop } from '../util/drop';
2024-03-15 14:20:33 +00:00
import type { RendererConfigType } from '../types/RendererConfig';
2022-04-13 00:50:17 +00:00
import { App } from './playwright';
2023-12-13 19:47:51 +00:00
import { CONTACT_COUNT } from './benchmarks/fixtures';
import { strictAssert } from '../util/assert';
2022-04-20 19:35:53 +00:00
export { App };
const debug = createDebug('mock:bootstrap');
const ELECTRON = path.join(
__dirname,
'..',
'..',
'node_modules',
'.bin',
'electron'
);
const CI_SCRIPT = path.join(__dirname, '..', '..', 'ci.js');
const CLOSE_TIMEOUT = 10 * 1000;
const CONTACT_FIRST_NAMES = [
'Alice',
'Bob',
'Charlie',
2023-12-13 19:47:51 +00:00
'Danielle',
'Elaine',
'Frankie',
'Grandma',
'Paul',
'Steve',
'William',
];
const CONTACT_LAST_NAMES = [
'Smith',
'Brown',
'Jones',
'Miller',
'Davis',
'Lopez',
2023-12-13 19:47:51 +00:00
'Gonzales',
'Singh',
'Baker',
'Farmer',
];
const CONTACT_SUFFIXES = [
'Sr.',
'Jr.',
'the 3rd',
'the 4th',
'the 5th',
'the 6th',
'the 7th',
'the 8th',
'the 9th',
'the 10th',
];
const CONTACT_NAMES = new Array<string>();
for (const firstName of CONTACT_FIRST_NAMES) {
for (const lastName of CONTACT_LAST_NAMES) {
CONTACT_NAMES.push(`${firstName} ${lastName}`);
}
}
2023-12-13 19:47:51 +00:00
for (const suffix of CONTACT_SUFFIXES) {
for (const firstName of CONTACT_FIRST_NAMES) {
for (const lastName of CONTACT_LAST_NAMES) {
CONTACT_NAMES.push(`${firstName} ${lastName}, ${suffix}`);
}
}
}
const MAX_CONTACTS = CONTACT_NAMES.length;
export type BootstrapOptions = Readonly<{
benchmark?: boolean;
linkedDevices?: number;
contactCount?: number;
contactsWithoutProfileKey?: number;
unknownContactCount?: number;
2022-03-09 18:22:34 +00:00
contactNames?: ReadonlyArray<string>;
contactPreKeyCount?: number;
}>;
2024-03-15 14:20:33 +00:00
type BootstrapInternalOptions = BootstrapOptions &
Readonly<{
benchmark: boolean;
linkedDevices: number;
contactCount: number;
contactsWithoutProfileKey: number;
unknownContactCount: number;
2022-03-09 18:22:34 +00:00
contactNames: ReadonlyArray<string>;
}>;
2024-03-15 14:20:33 +00:00
function sanitizePathComponent(component: string): string {
return normalizePath(component.replace(/[^a-z]+/gi, '-'));
}
//
// Bootstrap is a class that prepares mock server and desktop for running
// tests/benchmarks.
//
// In general, the usage pattern is:
//
// const bootstrap = new Bootstrap();
// await bootstrap.init();
// const app = await bootstrap.link();
// await bootstrap.teardown();
//
// Once initialized `bootstrap` variable will have following useful properties:
//
// - `server` - a mock server instance
// - `desktop` - a linked device representing currently running desktop instance
// - `phone` - a primary device representing desktop's primary
// - `contacts` - a list of primary devices for contacts that are synced over
// through contact sync
//
// `bootstrap.getTimestamp()` could be used to generate consecutive timestamp
// for sending messages.
//
// All phone numbers and uuids for all contacts and ourselves are random and not
// the same between different test runs.
//
export class Bootstrap {
2022-04-13 00:50:17 +00:00
public readonly server: Server;
public readonly cdn3Path: string;
private readonly options: BootstrapInternalOptions;
private privContacts?: ReadonlyArray<PrimaryDevice>;
private privContactsWithoutProfileKey?: ReadonlyArray<PrimaryDevice>;
private privUnknownContacts?: ReadonlyArray<PrimaryDevice>;
private privPhone?: PrimaryDevice;
private privDesktop?: Device;
private storagePath?: string;
2023-02-02 02:11:33 +00:00
private timestamp: number = Date.now() - durations.WEEK;
2023-03-13 23:41:47 +00:00
private lastApp?: App;
2024-03-15 14:20:33 +00:00
private readonly randomId = crypto.randomBytes(8).toString('hex');
constructor(options: BootstrapOptions = {}) {
2024-09-12 23:48:27 +00:00
this.cdn3Path = path.join(os.tmpdir(), `mock-signal-cdn3-${this.randomId}`);
2022-04-13 00:50:17 +00:00
this.server = new Server({
// Limit number of storage read keys for easier testing
maxStorageReadKeys: MAX_STORAGE_READ_KEYS,
2024-08-02 00:06:52 +00:00
cdn3Path: this.cdn3Path,
2022-04-13 00:50:17 +00:00
});
this.options = {
linkedDevices: 5,
2023-12-13 19:47:51 +00:00
contactCount: CONTACT_COUNT,
contactsWithoutProfileKey: 0,
unknownContactCount: 0,
2022-03-09 18:22:34 +00:00
contactNames: CONTACT_NAMES,
benchmark: false,
...options,
};
2023-12-13 19:47:51 +00:00
const totalContactCount =
this.options.contactCount +
2023-12-13 19:47:51 +00:00
this.options.contactsWithoutProfileKey +
this.options.unknownContactCount;
assert(totalContactCount <= this.options.contactNames.length);
assert(totalContactCount <= MAX_CONTACTS);
}
public async init(): Promise<void> {
debug('initializing');
await this.server.listen(0);
const { port } = this.server.address();
debug('started server on port=%d', port);
2023-12-13 19:47:51 +00:00
const totalContactCount =
this.options.contactCount +
this.options.contactsWithoutProfileKey +
this.options.unknownContactCount;
const allContacts = await Promise.all(
2023-12-13 19:47:51 +00:00
this.options.contactNames
.slice(0, totalContactCount)
.map(async profileName => {
const primary = await this.server.createPrimaryDevice({
profileName,
});
for (let i = 0; i < this.options.linkedDevices; i += 1) {
// eslint-disable-next-line no-await-in-loop
await this.server.createSecondaryDevice(primary);
}
return primary;
})
);
this.privContacts = allContacts.splice(0, this.options.contactCount);
this.privContactsWithoutProfileKey = allContacts.splice(
0,
this.options.contactsWithoutProfileKey
);
this.privUnknownContacts = allContacts.splice(
0,
this.options.unknownContactCount
);
this.privPhone = await this.server.createPrimaryDevice({
2022-07-08 20:46:25 +00:00
profileName: 'Myself',
contacts: this.contacts,
contactsWithoutProfileKey: this.contactsWithoutProfileKey,
});
this.storagePath = await fs.mkdtemp(path.join(os.tmpdir(), 'mock-signal-'));
debug('setting storage path=%j', this.storagePath);
}
2023-03-13 23:41:47 +00:00
public static benchmark(
fn: (bootstrap: Bootstrap) => Promise<void>,
timeout = 5 * durations.MINUTE
): void {
drop(Bootstrap.runBenchmark(fn, timeout));
}
public get logsDir(): string {
assert(
this.storagePath !== undefined,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
return path.join(this.storagePath, 'logs');
}
public get ephemeralConfigPath(): string {
assert(
this.storagePath !== undefined,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
return path.join(this.storagePath, 'ephemeral.json');
}
public eraseStorage(): Promise<void> {
return this.resetAppStorage();
}
private async resetAppStorage(): Promise<void> {
2024-03-15 14:20:33 +00:00
assert(
this.storagePath !== undefined,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
await fs.rm(this.storagePath, { recursive: true });
this.storagePath = await fs.mkdtemp(path.join(os.tmpdir(), 'mock-signal-'));
}
public async teardown(): Promise<void> {
debug('tearing down');
await Promise.race([
2023-03-13 23:41:47 +00:00
Promise.all([
...[this.storagePath, this.cdn3Path].map(tmpPath =>
2024-08-02 00:06:52 +00:00
tmpPath ? fs.rm(tmpPath, { recursive: true }) : Promise.resolve()
),
2023-03-13 23:41:47 +00:00
this.server.close(),
this.lastApp?.close(),
]),
new Promise(resolve => setTimeout(resolve, CLOSE_TIMEOUT).unref()),
]);
}
2024-03-15 14:20:33 +00:00
public async link(extraConfig?: Partial<RendererConfigType>): Promise<App> {
debug('linking');
2024-03-15 14:20:33 +00:00
const app = await this.startApp(extraConfig);
const window = await app.getWindow();
debug('looking for QR code or relink button');
const qrCode = window.locator(
'.module-InstallScreenQrCodeNotScannedStep__qr-code__code'
);
const relinkButton = window.locator('.LeftPaneDialog__icon--relink');
await qrCode.or(relinkButton).waitFor();
if (await relinkButton.isVisible()) {
debug('unlinked, clicking left pane button');
await relinkButton.click();
await qrCode.waitFor();
}
debug('waiting for provision');
const provision = await this.server.waitForProvision();
debug('waiting for provision URL');
const provisionURL = await app.waitForProvisionURL();
debug('completing provision');
this.privDesktop = await provision.complete({
provisionURL,
primaryDevice: this.phone,
});
debug('new desktop device %j', this.desktop.debugId);
const desktopKey = await this.desktop.popSingleUseKey();
await this.phone.addSingleUseKey(this.desktop, desktopKey);
for (const contact of this.allContacts) {
2023-08-16 20:54:39 +00:00
for (const serviceIdKind of [ServiceIdKind.ACI, ServiceIdKind.PNI]) {
2022-07-08 20:46:25 +00:00
// eslint-disable-next-line no-await-in-loop
2023-08-16 20:54:39 +00:00
const contactKey = await this.desktop.popSingleUseKey(serviceIdKind);
2022-07-08 20:46:25 +00:00
// eslint-disable-next-line no-await-in-loop
2023-08-16 20:54:39 +00:00
await contact.addSingleUseKey(this.desktop, contactKey, serviceIdKind);
2022-07-08 20:46:25 +00:00
}
}
await this.phone.waitForSync(this.desktop);
this.phone.resetSyncState(this.desktop);
debug('synced with %j', this.desktop.debugId);
return app;
}
public async linkAndClose(): Promise<void> {
const app = await this.link();
debug('closing the app after link');
await app.close();
}
2024-03-15 14:20:33 +00:00
public async startApp(
extraConfig?: Partial<RendererConfigType>
): Promise<App> {
assert(
this.storagePath !== undefined,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
debug('starting the app');
const { port, family } = this.server.address();
2023-09-18 20:09:28 +00:00
let startAttempts = 0;
2024-03-15 14:20:33 +00:00
const MAX_ATTEMPTS = 4;
2023-09-18 20:09:28 +00:00
let app: App | undefined;
while (!app) {
startAttempts += 1;
if (startAttempts > MAX_ATTEMPTS) {
throw new Error(
`App failed to start after ${MAX_ATTEMPTS} times, giving up`
);
}
// eslint-disable-next-line no-await-in-loop
const config = await this.generateConfig(port, family, extraConfig);
2023-09-18 20:09:28 +00:00
const startedApp = new App({
main: ELECTRON,
args: [CI_SCRIPT],
config,
});
2023-09-18 20:09:28 +00:00
try {
// eslint-disable-next-line no-await-in-loop
2023-12-01 22:16:19 +00:00
await startedApp.start();
2023-09-18 20:09:28 +00:00
} catch (error) {
// eslint-disable-next-line no-console
console.error(
2023-12-01 22:16:19 +00:00
`Failed to start the app, attempt ${startAttempts}, retrying`,
error
);
// eslint-disable-next-line no-await-in-loop
await this.resetAppStorage();
2023-09-18 20:09:28 +00:00
continue;
}
2023-09-18 20:09:28 +00:00
this.lastApp = startedApp;
startedApp.on('close', () => {
if (this.lastApp === startedApp) {
this.lastApp = undefined;
}
});
2023-09-18 20:09:28 +00:00
app = startedApp;
}
2023-03-13 23:41:47 +00:00
return app;
}
public getTimestamp(): number {
const result = this.timestamp;
this.timestamp += 1;
return result;
}
public async maybeSaveLogs(
2024-03-15 14:20:33 +00:00
test?: Mocha.Runnable,
app: App | undefined = this.lastApp
): Promise<void> {
const { FORCE_ARTIFACT_SAVE } = process.env;
if (test?.state !== 'passed' || FORCE_ARTIFACT_SAVE) {
await this.saveLogs(app, test?.fullTitle());
}
}
public async saveLogs(
app: App | undefined = this.lastApp,
2024-03-15 14:20:33 +00:00
testName?: string
): Promise<void> {
2024-03-15 14:20:33 +00:00
const outDir = await this.getArtifactsDir(testName);
if (outDir == null) {
2022-07-08 20:46:25 +00:00
return;
}
// eslint-disable-next-line no-console
console.error(`Saving logs to ${outDir}`);
const { logsDir } = this;
2024-03-15 14:20:33 +00:00
await fs.rename(logsDir, path.join(outDir, 'logs'));
2022-09-01 16:35:44 +00:00
2023-12-05 22:33:10 +00:00
const page = await app?.getWindow();
if (process.env.TRACING) {
await page
?.context()
.tracing.stop({ path: path.join(outDir, 'trace.zip') });
}
2022-09-01 16:35:44 +00:00
if (app) {
const window = await app.getWindow();
const screenshot = await window.screenshot();
await fs.writeFile(path.join(outDir, 'screenshot.png'), screenshot);
}
2022-07-08 20:46:25 +00:00
}
2024-03-15 14:20:33 +00:00
public async createScreenshotComparator(
app: App,
callback: (
page: Page,
snapshot: (name: string) => Promise<void>
) => Promise<void>,
test?: Mocha.Runnable
): Promise<(app: App) => Promise<void>> {
const snapshots = new Array<{ name: string; data: Buffer }>();
const window = await app.getWindow();
await callback(window, async (name: string) => {
debug('creating screenshot');
snapshots.push({
name,
data: await window.screenshot(),
});
2024-03-15 14:20:33 +00:00
});
let index = 0;
return async (anotherApp: App): Promise<void> => {
const anotherWindow = await anotherApp.getWindow();
await callback(anotherWindow, async (name: string) => {
index += 1;
const before = snapshots.shift();
assert(before != null, 'No previous snapshot');
assert.strictEqual(before.name, name, 'Wrong snapshot order');
const after = await anotherWindow.screenshot();
const beforePng = PNG.sync.read(before.data);
const afterPng = PNG.sync.read(after);
const { width, height } = beforePng;
const diffPng = new PNG({ width, height });
const numPixels = pixelmatch(
beforePng.data,
afterPng.data,
diffPng.data,
width,
height,
{}
);
if (numPixels === 0 && !process.env.FORCE_ARTIFACT_SAVE) {
2024-03-15 14:20:33 +00:00
debug('no screenshot difference');
return;
}
debug(
`screenshot difference for ${name}: ${numPixels}/${width * height}`
);
2024-03-15 14:20:33 +00:00
const outDir = await this.getArtifactsDir(test?.fullTitle());
if (outDir != null) {
debug('saving screenshots and diff');
const prefix = `${index}-${sanitizePathComponent(name)}`;
await fs.writeFile(
path.join(outDir, `${prefix}-before.png`),
before.data
);
await fs.writeFile(path.join(outDir, `${prefix}-after.png`), after);
await fs.writeFile(
path.join(outDir, `${prefix}-diff.png`),
PNG.sync.write(diffPng)
);
}
assert.strictEqual(numPixels, 0, 'Expected no pixels to be different');
});
};
}
public getAbsoluteAttachmentPath(relativePath: string): string {
strictAssert(this.storagePath, 'storagePath must exist');
return join(this.storagePath, 'attachments.noindex', relativePath);
}
//
// Getters
//
public get phone(): PrimaryDevice {
assert(
this.privPhone,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
return this.privPhone;
}
public get desktop(): Device {
assert(
this.privDesktop,
'Bootstrap has to be linked first, see: bootstrap.link()'
);
return this.privDesktop;
}
public get contacts(): ReadonlyArray<PrimaryDevice> {
assert(
this.privContacts,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
return this.privContacts;
}
public get contactsWithoutProfileKey(): ReadonlyArray<PrimaryDevice> {
assert(
this.privContactsWithoutProfileKey,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
return this.privContactsWithoutProfileKey;
}
public get unknownContacts(): ReadonlyArray<PrimaryDevice> {
assert(
this.privUnknownContacts,
'Bootstrap has to be initialized first, see: bootstrap.init()'
);
return this.privUnknownContacts;
}
public get allContacts(): ReadonlyArray<PrimaryDevice> {
return [
...this.contacts,
...this.contactsWithoutProfileKey,
...this.unknownContacts,
];
}
//
// Private
//
2024-03-15 14:20:33 +00:00
private async getArtifactsDir(
testName?: string
): Promise<string | undefined> {
const { ARTIFACTS_DIR } = process.env;
if (!ARTIFACTS_DIR) {
// eslint-disable-next-line no-console
console.error(
'Not saving artifacts. Please set ARTIFACTS_DIR env variable'
);
return undefined;
}
const normalizedPath = testName
? `${this.randomId}-${sanitizePathComponent(testName)}`
: this.randomId;
const outDir = path.join(ARTIFACTS_DIR, normalizedPath);
await fs.mkdir(outDir, { recursive: true });
return outDir;
}
2023-03-13 23:41:47 +00:00
private static async runBenchmark(
fn: (bootstrap: Bootstrap) => Promise<void>,
timeout: number
): Promise<void> {
const bootstrap = new Bootstrap({
benchmark: true,
});
await bootstrap.init();
try {
await pTimeout(fn(bootstrap), timeout);
2023-12-01 22:16:19 +00:00
if (process.env.FORCE_ARTIFACT_SAVE) {
await bootstrap.saveLogs();
}
2023-03-13 23:41:47 +00:00
} catch (error) {
await bootstrap.saveLogs();
throw error;
} finally {
await bootstrap.teardown();
}
}
2024-03-15 14:20:33 +00:00
private async generateConfig(
port: number,
family: string,
2024-03-15 14:20:33 +00:00
extraConfig?: Partial<RendererConfigType>
): Promise<string> {
const host = family === 'IPv6' ? '[::1]' : '127.0.0.1';
const url = `https://${host}:${port}`;
return JSON.stringify({
...(await loadCertificates()),
forcePreloadBundle: this.options.benchmark,
ciMode: 'full',
buildExpiration: Date.now() + durations.MONTH,
storagePath: this.storagePath,
storageProfile: 'mock',
serverUrl: url,
storageUrl: url,
2024-06-10 15:23:43 +00:00
sfuUrl: url,
cdn: {
'0': url,
'2': url,
2024-08-02 00:06:52 +00:00
'3': `${url}/cdn3`,
},
updatesEnabled: false,
2022-08-19 00:31:12 +00:00
directoreType: 'cdsi',
directoryCDSIUrl: url,
directoryCDSIMRENCLAVE:
2022-07-08 20:46:25 +00:00
'51133fecb3fa18aaf0c8f64cb763656d3272d9faaacdb26ae7df082e414fb142',
2024-03-15 14:20:33 +00:00
...extraConfig,
});
}
}