2022-02-11 22:32:51 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
import type { ElectronApplication, Locator, Page } from 'playwright';
|
2022-02-11 22:32:51 +00:00
|
|
|
import { _electron as electron } from 'playwright';
|
2023-03-13 23:41:47 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-09-19 22:08:55 +00:00
|
|
|
import type {
|
|
|
|
IPCRequest as ChallengeRequestType,
|
|
|
|
IPCResponse as ChallengeResponseType,
|
|
|
|
} from '../challenge';
|
2023-02-06 17:24:34 +00:00
|
|
|
import type { ReceiptType } from '../types/Receipt';
|
2023-09-19 17:02:37 +00:00
|
|
|
import { SECOND } from '../util/durations';
|
2022-09-19 22:08:55 +00:00
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
export type AppLoadedInfoType = Readonly<{
|
|
|
|
loadTime: number;
|
|
|
|
messagesPerSec: number;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type MessageSendInfoType = Readonly<{
|
|
|
|
timestamp: number;
|
|
|
|
delta: number;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type ConversationOpenInfoType = Readonly<{
|
|
|
|
delta: number;
|
|
|
|
}>;
|
|
|
|
|
2023-02-06 17:24:34 +00:00
|
|
|
export type ReceiptsInfoType = Readonly<{
|
|
|
|
type: ReceiptType;
|
|
|
|
timestamps: Array<number>;
|
|
|
|
}>;
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
export type StorageServiceInfoType = Readonly<{
|
|
|
|
manifestVersion: number;
|
|
|
|
}>;
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
export type AppOptionsType = Readonly<{
|
|
|
|
main: string;
|
|
|
|
args: ReadonlyArray<string>;
|
|
|
|
config: string;
|
|
|
|
}>;
|
|
|
|
|
2023-09-19 17:02:37 +00:00
|
|
|
const WAIT_FOR_EVENT_TIMEOUT = 30 * SECOND;
|
|
|
|
|
2023-03-13 23:41:47 +00:00
|
|
|
export class App extends EventEmitter {
|
2022-02-11 22:32:51 +00:00
|
|
|
private privApp: ElectronApplication | undefined;
|
|
|
|
|
2023-03-13 23:41:47 +00:00
|
|
|
constructor(private readonly options: AppOptionsType) {
|
|
|
|
super();
|
|
|
|
}
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
public async start(): Promise<void> {
|
|
|
|
this.privApp = await electron.launch({
|
|
|
|
executablePath: this.options.main,
|
|
|
|
args: this.options.args.slice(),
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
SIGNAL_CI_CONFIG: this.options.config,
|
|
|
|
},
|
|
|
|
locale: 'en',
|
|
|
|
});
|
2023-03-13 23:41:47 +00:00
|
|
|
|
|
|
|
this.privApp.on('close', () => this.emit('close'));
|
2022-02-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 20:50:29 +00:00
|
|
|
public async waitForEnabledComposer(): Promise<Locator> {
|
2023-04-14 18:16:28 +00:00
|
|
|
const window = await this.getWindow();
|
|
|
|
const composeArea = window.locator(
|
2023-07-26 22:23:32 +00:00
|
|
|
'.composition-area-wrapper, .Inbox__conversation .ConversationView'
|
2023-04-14 18:16:28 +00:00
|
|
|
);
|
|
|
|
const composeContainer = composeArea.locator(
|
|
|
|
'[data-testid=CompositionInput][data-enabled=true]'
|
|
|
|
);
|
|
|
|
await composeContainer.waitFor();
|
|
|
|
|
|
|
|
return composeContainer.locator('.ql-editor');
|
|
|
|
}
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
public async waitForProvisionURL(): Promise<string> {
|
|
|
|
return this.waitForEvent('provisioning-url');
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:27:29 +00:00
|
|
|
public async waitForDbInitialized(): Promise<void> {
|
|
|
|
return this.waitForEvent('db-initialized');
|
|
|
|
}
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
public async waitUntilLoaded(): Promise<AppLoadedInfoType> {
|
|
|
|
return this.waitForEvent('app-loaded');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async waitForMessageSend(): Promise<MessageSendInfoType> {
|
|
|
|
return this.waitForEvent('message:send-complete');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async waitForConversationOpen(): Promise<ConversationOpenInfoType> {
|
|
|
|
return this.waitForEvent('conversation:open');
|
|
|
|
}
|
|
|
|
|
2022-09-19 22:08:55 +00:00
|
|
|
public async waitForChallenge(): Promise<ChallengeRequestType> {
|
|
|
|
return this.waitForEvent('challenge');
|
|
|
|
}
|
|
|
|
|
2023-02-06 17:24:34 +00:00
|
|
|
public async waitForReceipts(): Promise<ReceiptsInfoType> {
|
|
|
|
return this.waitForEvent('receipts');
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 22:08:55 +00:00
|
|
|
public async solveChallenge(response: ChallengeResponseType): Promise<void> {
|
|
|
|
const window = await this.getWindow();
|
|
|
|
|
|
|
|
await window.evaluate(
|
2023-01-19 00:02:03 +00:00
|
|
|
`window.SignalCI.solveChallenge(${JSON.stringify(response)})`
|
2022-09-19 22:08:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
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())})`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Private
|
|
|
|
//
|
|
|
|
|
2023-09-19 17:02:37 +00:00
|
|
|
private async waitForEvent<T>(
|
|
|
|
event: string,
|
|
|
|
timeout = WAIT_FOR_EVENT_TIMEOUT
|
|
|
|
): Promise<T> {
|
2022-02-11 22:32:51 +00:00
|
|
|
const window = await this.getWindow();
|
|
|
|
|
|
|
|
const result = await window.evaluate(
|
2023-09-19 17:02:37 +00:00
|
|
|
`window.SignalCI.waitForEvent(${JSON.stringify(event)})`,
|
|
|
|
{ timeout }
|
2022-02-11 22:32:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return result as T;
|
|
|
|
}
|
|
|
|
|
|
|
|
private get app(): ElectronApplication {
|
|
|
|
if (!this.privApp) {
|
|
|
|
throw new Error('Call ElectronWrap.start() first');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.privApp;
|
|
|
|
}
|
|
|
|
}
|