-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-ipc.js
More file actions
121 lines (103 loc) · 3.74 KB
/
test-ipc.js
File metadata and controls
121 lines (103 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Test script to verify IPC handlers work correctly
* This simulates the frontend calls and tests for cloning errors
*/
const { app, ipcMain } = require('electron');
const path = require('path');
const fs = require('fs').promises;
// Test data
const testImagePath = path.join(__dirname, 'test-image.png');
const testVideoPath = path.join(__dirname, 'test-video.mp4');
async function createTestFiles() {
try {
// Create a simple test image (1x1 PNG)
const pngData = Buffer.from([
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xDE, 0x00, 0x00, 0x00,
0x0C, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0xF8, 0x0F, 0x00, 0x00,
0x01, 0x00, 0x01, 0x5C, 0xC2, 0x8A, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x49,
0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
]);
await fs.writeFile(testImagePath, pngData);
console.log('✅ Created test image:', testImagePath);
return testImagePath;
} catch (error) {
console.error('❌ Failed to create test files:', error);
throw error;
}
}
async function testIpcHandlers() {
console.log('🧪 Starting IPC handler tests...');
try {
const testImage = await createTestFiles();
// Test data that should be serializable
const testCases = [
{
name: 'convertToWebp',
handler: 'convertToWebp',
data: { inputPath: testImage, options: { quality: 80 } }
},
{
name: 'decodeWebp',
handler: 'decodeWebp',
data: { inputPath: testImage, options: {} }
},
{
name: 'describeImage',
handler: 'describeImage',
data: { inputPath: testImage, options: {} }
},
{
name: 'add-text-to-image',
handler: 'add-text-to-image',
data: { inputPath: testImage, text: 'Test', options: {} }
}
];
for (const testCase of testCases) {
try {
console.log(`\n🔍 Testing ${testCase.name}...`);
console.log('📤 Sending data:', JSON.stringify(testCase.data, null, 2));
// Simulate the IPC call
const result = await new Promise((resolve, reject) => {
// Find the handler
const handlers = ipcMain._events || {};
const handlerName = `invoke:${testCase.handler}`;
if (handlers[handlerName]) {
const handler = handlers[handlerName][0];
if (handler) {
// Call the handler with mock event
const mockEvent = { sender: { send: () => {} } };
handler(mockEvent, testCase.data)
.then(resolve)
.catch(reject);
} else {
reject(new Error(`Handler not found: ${testCase.handler}`));
}
} else {
reject(new Error(`Handler not registered: ${testCase.handler}`));
}
});
console.log(`✅ ${testCase.name} completed successfully`);
} catch (error) {
console.error(`❌ ${testCase.name} failed:`, error.message);
if (error.message.includes('could not be cloned')) {
console.error('🚨 CLONING ERROR DETECTED - This needs to be fixed!');
}
}
}
} catch (error) {
console.error('❌ Test setup failed:', error);
}
console.log('\n🏁 Test completed');
}
// Run the test if this script is executed directly
if (require.main === module) {
// Load the main.js file to register handlers
require('./electron/main.js');
// Wait a bit for handlers to register
setTimeout(() => {
testIpcHandlers().catch(console.error);
}, 1000);
}
module.exports = { testIpcHandlers, createTestFiles };