mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
test(electron): add unit tests for updater (#5439)
This commit is contained in:
34
packages/frontend/electron/test/main/mocks/app-adapter.ts
Normal file
34
packages/frontend/electron/test/main/mocks/app-adapter.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { AppAdapter } from 'electron-updater/out/AppAdapter';
|
||||
|
||||
/**
|
||||
* For testing and same as:
|
||||
* https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/ElectronAppAdapter.ts
|
||||
*/
|
||||
export class MockedAppAdapter implements AppAdapter {
|
||||
version: string;
|
||||
name = 'AFFiNE-testing';
|
||||
isPackaged = true;
|
||||
appUpdateConfigPath = '';
|
||||
userDataPath = '';
|
||||
baseCachePath = '';
|
||||
|
||||
constructor(version: string) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
whenReady() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
relaunch() {
|
||||
return;
|
||||
}
|
||||
|
||||
quit() {
|
||||
return;
|
||||
}
|
||||
|
||||
onQuit(_handler: (exitCode: number) => void) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
26
packages/frontend/electron/test/main/mocks/http-executor.ts
Normal file
26
packages/frontend/electron/test/main/mocks/http-executor.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import http from 'node:https';
|
||||
|
||||
import { HttpExecutor } from 'builder-util-runtime';
|
||||
import type { ClientRequest } from 'electron';
|
||||
|
||||
/**
|
||||
* For testing and same as:
|
||||
* https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/electronHttpExecutor.ts
|
||||
*/
|
||||
export class MockedHttpExecutor extends HttpExecutor<ClientRequest> {
|
||||
createRequest(
|
||||
options: any,
|
||||
callback: (response: any) => void
|
||||
): ClientRequest {
|
||||
if (options.headers && options.headers.Host) {
|
||||
// set host value from headers.Host
|
||||
options.host = options.headers.Host;
|
||||
// remove header property 'Host', if not removed causes net::ERR_INVALID_ARGUMENT exception
|
||||
delete options.headers.Host;
|
||||
}
|
||||
|
||||
const request = http.request(options);
|
||||
request.on('response', callback);
|
||||
return request as unknown as ClientRequest;
|
||||
}
|
||||
}
|
||||
3
packages/frontend/electron/test/main/mocks/index.ts
Normal file
3
packages/frontend/electron/test/main/mocks/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './app-adapter';
|
||||
export * from './http-executor';
|
||||
export * from './updater';
|
||||
37
packages/frontend/electron/test/main/mocks/updater.ts
Normal file
37
packages/frontend/electron/test/main/mocks/updater.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'electron-updater'; // Prevent BaseUpdater is undefined.
|
||||
|
||||
import { type AllPublishOptions, UUID } from 'builder-util-runtime';
|
||||
import { randomBytes } from 'crypto';
|
||||
import type { AppAdapter } from 'electron-updater/out/AppAdapter';
|
||||
import type { DownloadUpdateOptions } from 'electron-updater/out/AppUpdater';
|
||||
import type { InstallOptions } from 'electron-updater/out/BaseUpdater';
|
||||
import { BaseUpdater } from 'electron-updater/out/BaseUpdater';
|
||||
|
||||
import { MockedHttpExecutor } from './http-executor';
|
||||
|
||||
/**
|
||||
* For testing, like:
|
||||
* https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/MacUpdater.ts
|
||||
*/
|
||||
export class MockedUpdater extends BaseUpdater {
|
||||
httpExecutor: MockedHttpExecutor;
|
||||
|
||||
constructor(options?: AllPublishOptions | null, app?: AppAdapter) {
|
||||
super(options, app);
|
||||
|
||||
this.httpExecutor = new MockedHttpExecutor();
|
||||
Object.assign(this, {
|
||||
getOrCreateStagingUserId: () => {
|
||||
const id = UUID.v5(randomBytes(4096), UUID.OID);
|
||||
return id;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
doInstall(_options: InstallOptions) {
|
||||
return true;
|
||||
}
|
||||
doDownloadUpdate(_options: DownloadUpdateOptions): Promise<string[]> {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user