Skip to content

Commit

Permalink
Test IPC hander.
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang committed Nov 9, 2024
1 parent 286b6e2 commit 77b5dde
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/handlers/appInfoHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { app, ipcMain } from 'electron';
import { IPC_CHANNELS } from '../constants';

/**
* Handles static information about the app in IPC channels.
*/
export class AppInfoHandlers {
constructor() {}

Expand Down
9 changes: 0 additions & 9 deletions src/handlers/ipcHandler.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/handlers/pathHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { app, ipcMain, shell } from 'electron';
import { IPC_CHANNELS } from '../constants';
import { determineResourcesPaths } from '../main';

Check failure on line 3 in src/handlers/pathHandlers.ts

View workflow job for this annotation

GitHub Actions / lint-and-format

Module '"../main"' has no exported member 'determineResourcesPaths'.
import log from 'electron-log/main';
import { getModelConfigPath } from '../config/extra_model_config';

export class PathHandlers {
constructor() {}

registerHandlers() {
// Path-related handlers

ipcMain.on(IPC_CHANNELS.OPEN_LOGS_PATH, () => {
shell.openPath(app.getPath('logs'));
});

ipcMain.handle(IPC_CHANNELS.GET_MODEL_CONFIG_PATH, () => {
return getModelConfigPath();
});

ipcMain.handle(IPC_CHANNELS.GET_BASE_PATH, async () => {
const { basePath } = await determineResourcesPaths();
return basePath;
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ComfyConfigManager } from './config/comfyConfigManager';
import { AppWindow } from './main-process/appWindow';
import { getAppResourcesPath, getBasePath, getPythonInstallPath } from './install/resourcePaths';
import { PathHandlers } from './handlers/pathHandlers';
import { AppInfoHandlers } from './handlers/appInfoHandlers';

dotenv.config();

Expand Down Expand Up @@ -146,6 +147,8 @@ if (!gotTheLock) {

try {
createWindow();
new PathHandlers().registerHandlers();
new AppInfoHandlers().registerHandlers();

ipcMain.handle(IPC_CHANNELS.OPEN_FORUM, () => {
shell.openExternal('https://forum.comfy.org');
Expand All @@ -157,9 +160,6 @@ if (!gotTheLock) {
...options,
});
});
ipcMain.handle(IPC_CHANNELS.GET_MODEL_CONFIG_PATH, () => {
return getModelConfigPath();
});

ipcMain.on(IPC_CHANNELS.OPEN_DEV_TOOLS, () => {
appWindow.openDevTools();
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/handlers/pathHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ipcMain } from 'electron';

import { PathHandlers } from '../../../src/handlers/pathHandlers';
import { IPC_CHANNELS } from '../../../src/constants';

const isChannelRegistered = (channel: string): boolean => {
// @ts-ignore - accessing private property for testing
return ipcMain.listeners(channel).length > 0;
};

describe('IPC Handlers', () => {
beforeEach(() => {
// Clear all existing listeners before each test
ipcMain.removeAllListeners();
});
describe('PathHandlers', () => {
let handler: PathHandlers;
beforeEach(() => {
// Clear all existing listeners before each test
ipcMain.removeAllListeners();
handler = new PathHandlers();
handler.registerHandlers();
});

it('should register all expected channels', () => {
handler.registerHandlers();

const expectedChannels = [
IPC_CHANNELS.GET_MODEL_CONFIG_PATH,
IPC_CHANNELS.GET_BASE_PATH,
IPC_CHANNELS.OPEN_LOGS_PATH,
IPC_CHANNELS.OPEN_PATH,
];

expectedChannels.forEach((channel) => {
expect(isChannelRegistered(channel)).toBe(true);
});
});
});
});

0 comments on commit 77b5dde

Please sign in to comment.