Files
gridpilot.gg/tests/smoke/helpers/ipc-verifier.ts
2025-11-26 17:03:29 +01:00

159 lines
4.1 KiB
TypeScript

import { ElectronApplication } from '@playwright/test';
export interface IPCTestResult {
channel: string;
success: boolean;
error?: string;
duration: number;
}
/**
* IPCVerifier - Tests IPC channel contracts
*
* Purpose: Verify main <-> renderer communication works
* Scope: Core IPC channels required for app functionality
*/
export class IPCVerifier {
constructor(private app: ElectronApplication) {}
/**
* Test checkAuth IPC channel
*/
async testCheckAuth(): Promise<IPCTestResult> {
const start = Date.now();
const channel = 'checkAuth';
try {
const result = await this.app.evaluate(async ({ ipcMain }) => {
return new Promise((resolve) => {
// Simulate IPC call
const mockEvent = { reply: (ch: string, data: any) => resolve(data) } as any;
const handler = (ipcMain as any).listeners('checkAuth')[0];
if (!handler) {
resolve({ error: 'Handler not registered' });
} else {
handler(mockEvent);
}
});
});
return {
channel,
success: !result.error,
error: result.error,
duration: Date.now() - start,
};
} catch (error) {
return {
channel,
success: false,
error: error instanceof Error ? error.message : String(error),
duration: Date.now() - start,
};
}
}
/**
* Test getBrowserMode IPC channel
*/
async testGetBrowserMode(): Promise<IPCTestResult> {
const start = Date.now();
const channel = 'getBrowserMode';
try {
const result = await this.app.evaluate(async ({ ipcMain }) => {
return new Promise((resolve) => {
const mockEvent = { reply: (ch: string, data: any) => resolve(data) } as any;
const handler = (ipcMain as any).listeners('getBrowserMode')[0];
if (!handler) {
resolve({ error: 'Handler not registered' });
} else {
handler(mockEvent);
}
});
});
return {
channel,
success: typeof result === 'boolean' || !result.error,
error: result.error,
duration: Date.now() - start,
};
} catch (error) {
return {
channel,
success: false,
error: error instanceof Error ? error.message : String(error),
duration: Date.now() - start,
};
}
}
/**
* Test startAutomationSession IPC channel contract
*/
async testStartAutomationSession(): Promise<IPCTestResult> {
const start = Date.now();
const channel = 'startAutomationSession';
try {
const result = await this.app.evaluate(async ({ ipcMain }) => {
return new Promise((resolve) => {
const mockEvent = { reply: (ch: string, data: any) => resolve(data) } as any;
const handler = (ipcMain as any).listeners('startAutomationSession')[0];
if (!handler) {
resolve({ error: 'Handler not registered' });
} else {
// Test with mock data
handler(mockEvent, { mode: 'test' });
}
});
});
return {
channel,
success: !result.error,
error: result.error,
duration: Date.now() - start,
};
} catch (error) {
return {
channel,
success: false,
error: error instanceof Error ? error.message : String(error),
duration: Date.now() - start,
};
}
}
/**
* Run all IPC tests and return results
*/
async verifyAllChannels(): Promise<IPCTestResult[]> {
return Promise.all([
this.testCheckAuth(),
this.testGetBrowserMode(),
this.testStartAutomationSession(),
]);
}
/**
* Format IPC test results for output
*/
static formatResults(results: IPCTestResult[]): string {
const lines = ['IPC Channel Verification:', ''];
results.forEach(result => {
const status = result.success ? '✓' : '✗';
lines.push(`${status} ${result.channel} (${result.duration}ms)`);
if (result.error) {
lines.push(` Error: ${result.error}`);
}
});
return lines.join('\n');
}
}