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

278 lines
7.1 KiB
TypeScript
Raw Normal View History

// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2024-09-09 22:43:59 +00:00
import type { ElectronApplication, Page } from 'playwright';
import { _electron as electron } from 'playwright';
2024-09-12 23:48:27 +00:00
import { EventEmitter, once } from 'events';
2023-12-01 22:16:19 +00:00
import pTimeout from 'p-timeout';
import type {
IPCRequest as ChallengeRequestType,
IPCResponse as ChallengeResponseType,
} from '../challenge';
import type { ReceiptType } from '../types/Receipt';
import { SECOND } from '../util/durations';
2024-09-12 23:48:27 +00:00
import { drop } from '../util/drop';
export type AppLoadedInfoType = Readonly<{
loadTime: number;
messagesPerSec: number;
}>;
export type MessageSendInfoType = Readonly<{
timestamp: number;
delta: number;
}>;
export type ConversationOpenInfoType = Readonly<{
delta: number;
}>;
export type ReceiptsInfoType = Readonly<{
type: ReceiptType;
timestamps: Array<number>;
}>;
export type StorageServiceInfoType = Readonly<{
manifestVersion: number;
}>;
export type AppOptionsType = Readonly<{
main: string;
args: ReadonlyArray<string>;
config: string;
}>;
const WAIT_FOR_EVENT_TIMEOUT = 30 * SECOND;
2023-03-13 23:41:47 +00:00
export class App extends EventEmitter {
private privApp: ElectronApplication | undefined;
2023-03-13 23:41:47 +00:00
constructor(private readonly options: AppOptionsType) {
super();
}
public async start(): Promise<void> {
2023-12-01 22:16:19 +00:00
try {
// launch the electron processs
this.privApp = await electron.launch({
executablePath: this.options.main,
args: this.options.args.slice(),
env: {
...process.env,
MOCK_TEST: 'true',
2023-12-01 22:16:19 +00:00
SIGNAL_CI_CONFIG: this.options.config,
},
locale: 'en',
2023-12-05 22:33:10 +00:00
timeout: 30 * SECOND,
2023-12-01 22:16:19 +00:00
});
// wait for the first window to load
await pTimeout(
(async () => {
2023-12-05 22:33:10 +00:00
const page = await this.getWindow();
if (process.env.TRACING) {
await page.context().tracing.start({
name: 'tracing',
screenshots: true,
snapshots: true,
});
}
2023-12-01 22:16:19 +00:00
await page?.waitForLoadState('load');
})(),
20 * SECOND
);
} catch (e) {
this.privApp?.process().kill('SIGKILL');
throw e;
}
2023-03-13 23:41:47 +00:00
this.privApp.on('close', () => this.emit('close'));
2024-09-12 23:48:27 +00:00
drop(this.printLoop());
}
public async waitForProvisionURL(): Promise<string> {
return this.waitForEvent('provisioning-url');
}
public async waitForDbInitialized(): Promise<void> {
return this.waitForEvent('db-initialized');
}
public async waitUntilLoaded(): Promise<AppLoadedInfoType> {
return this.waitForEvent('app-loaded');
}
public async waitForBackupImportComplete(): Promise<void> {
return this.waitForEvent('backupImportComplete');
}
public async waitForMessageSend(): Promise<MessageSendInfoType> {
return this.waitForEvent('message:send-complete');
}
public async waitForConversationOpen(): Promise<ConversationOpenInfoType> {
return this.waitForEvent('conversation:open');
}
public async waitForChallenge(): Promise<ChallengeRequestType> {
return this.waitForEvent('challenge');
}
public async waitForReceipts(): Promise<ReceiptsInfoType> {
return this.waitForEvent('receipts');
}
public async waitForStorageService(): Promise<StorageServiceInfoType> {
return this.waitForEvent('storageServiceComplete');
}
public async waitForManifestVersion(version: number): Promise<void> {
// eslint-disable-next-line no-constant-condition
while (true) {
// eslint-disable-next-line no-await-in-loop
const { manifestVersion } = await this.waitForStorageService();
if (manifestVersion >= version) {
break;
}
}
}
public async solveChallenge(response: ChallengeResponseType): Promise<void> {
const window = await this.getWindow();
await window.evaluate(
`window.SignalCI.solveChallenge(${JSON.stringify(response)})`
);
}
public async close(): Promise<void> {
await this.app.close();
}
public async getWindow(): Promise<Page> {
return this.app.firstWindow();
}
2023-11-02 19:42:31 +00:00
public async openSignalRoute(url: URL | string): Promise<void> {
const window = await this.getWindow();
await window.evaluate(
`window.SignalCI.openSignalRoute(${JSON.stringify(url.toString())})`
);
}
2024-03-15 14:20:33 +00:00
public async exportBackupToDisk(path: string): Promise<Uint8Array> {
const window = await this.getWindow();
return window.evaluate(
`window.SignalCI.exportBackupToDisk(${JSON.stringify(path)})`
);
}
2024-09-03 17:18:15 +00:00
public async exportPlaintextBackupToDisk(path: string): Promise<Uint8Array> {
const window = await this.getWindow();
return window.evaluate(
`window.SignalCI.exportPlaintextBackupToDisk(${JSON.stringify(path)})`
);
}
public async unlink(): Promise<void> {
const window = await this.getWindow();
return window.evaluate('window.SignalCI.unlink()');
}
public async waitForUnlink(): Promise<void> {
return this.waitForEvent('unlinkCleanupComplete');
}
public async waitForConversationOpenComplete(): Promise<void> {
return this.waitForEvent('conversationOpenComplete');
}
2023-03-13 23:41:47 +00:00
// EventEmitter types
public override on(type: 'close', callback: () => void): this;
public override on(
type: string | symbol,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listener: (...args: Array<any>) => void
): this {
return super.on(type, listener);
}
public override emit(type: 'close'): boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public override emit(type: string | symbol, ...args: Array<any>): boolean {
return super.emit(type, ...args);
}
public async getPendingEventCount(event: string): Promise<number> {
const window = await this.getWindow();
const result = await window.evaluate(
`window.SignalCI.getPendingEventCount(${JSON.stringify(event)})`
);
return Number(result);
}
2023-03-13 23:41:47 +00:00
//
// Private
//
private async waitForEvent<T>(
event: string,
timeout = WAIT_FOR_EVENT_TIMEOUT
): Promise<T> {
const window = await this.getWindow();
const result = await window.evaluate(
`window.SignalCI.waitForEvent(${JSON.stringify(event)})`,
{ timeout }
);
return result as T;
}
private get app(): ElectronApplication {
if (!this.privApp) {
throw new Error('Call ElectronWrap.start() first');
}
return this.privApp;
}
2024-09-12 23:48:27 +00:00
private async printLoop(): Promise<void> {
const kClosed: unique symbol = Symbol('kClosed');
const onClose = (async (): Promise<typeof kClosed> => {
try {
await once(this, 'close');
} catch {
// Ignore
}
return kClosed;
})();
// eslint-disable-next-line no-constant-condition
while (true) {
try {
// eslint-disable-next-line no-await-in-loop
const value = await Promise.race([
this.waitForEvent<string>('print', 0),
onClose,
]);
if (value === kClosed) {
break;
}
// eslint-disable-next-line no-console
console.error(`CI.print: ${value}`);
} catch {
// Ignore errors
}
}
}
}