forked from openagents-org/openagents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e-launcher-test.js
More file actions
425 lines (374 loc) · 12.8 KB
/
e2e-launcher-test.js
File metadata and controls
425 lines (374 loc) · 12.8 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* E2E test for OpenAgents Launcher — drives the Electron app via CDP.
*
* Steps:
* 1. Connect to Electron via CDP
* 2. Install tab → Install OpenClaw (if needed)
* 3. Dashboard → Create Agent
* 4. Configure LLM API key
* 5. Connect to workspace
* 6. Start agent
* 7. Send message via workspace
* 8. Verify agent responds
*/
const http = require('http');
const https = require('https');
const WebSocket = require('ws');
const CDP_PORT = process.env.CDP_PORT || 9333;
const LLM_API_KEY = process.env.LLM_API_KEY;
const LLM_BASE_URL = process.env.LLM_BASE_URL;
const LLM_MODEL = process.env.LLM_MODEL;
const WS_TOKEN = process.env.WS_TOKEN;
const WS_SLUG = 'c5be7aa2';
let ws;
let msgId = 1;
const pending = {};
// ── CDP helpers ──────────────────────────────────────────────────────────────
async function getCdpTarget() {
return new Promise((resolve, reject) => {
http.get(`http://127.0.0.1:${CDP_PORT}/json`, (res) => {
let data = '';
res.on('data', (d) => (data += d));
res.on('end', () => {
const targets = JSON.parse(data);
const page = targets.find((t) => t.type === 'page');
resolve(page.webSocketDebuggerUrl);
});
}).on('error', reject);
});
}
async function connectCdp() {
const wsUrl = await getCdpTarget();
return new Promise((resolve, reject) => {
ws = new WebSocket(wsUrl, { maxPayload: 50 * 1024 * 1024 });
ws.on('open', () => resolve());
ws.on('error', reject);
ws.on('message', (data) => {
const msg = JSON.parse(data);
if (msg.id && pending[msg.id]) {
pending[msg.id](msg);
delete pending[msg.id];
}
});
});
}
function cdpSend(method, params = {}) {
return new Promise((resolve, reject) => {
const id = msgId++;
const timeout = setTimeout(() => {
delete pending[id];
reject(new Error(`CDP timeout: ${method}`));
}, 30000);
pending[id] = (msg) => {
clearTimeout(timeout);
if (msg.error) reject(new Error(msg.error.message));
else resolve(msg.result);
};
ws.send(JSON.stringify({ id, method, params }));
});
}
async function evaluate(expression) {
const result = await cdpSend('Runtime.evaluate', {
expression,
awaitPromise: true,
returnByValue: true,
});
if (result.exceptionDetails) {
throw new Error(result.exceptionDetails.text || result.exceptionDetails.exception?.description || 'Eval error');
}
return result.result?.value;
}
async function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function screenshot(name) {
const { data } = await cdpSend('Page.captureScreenshot', { format: 'png' });
const fs = require('fs');
const path = require('path');
const dir = process.env.GITHUB_WORKSPACE
? path.join(process.env.GITHUB_WORKSPACE, 'e2e-screenshots')
: 'e2e-screenshots';
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, `${name}.png`), Buffer.from(data, 'base64'));
console.log(` 📸 ${name}.png`);
}
// ── Test steps ───────────────────────────────────────────────────────────────
async function step(name, fn) {
process.stdout.write(` ${name}... `);
try {
await fn();
console.log('✅');
} catch (e) {
console.log(`❌ ${e.message}`);
try { await screenshot(`FAIL-${name.replace(/\s+/g, '-')}`); } catch {}
throw e;
}
}
async function clickTab(tabName) {
await evaluate(`document.querySelector('[data-tab="${tabName}"]').click()`);
await sleep(2000);
}
async function clickButtonByText(text) {
await evaluate(`
(function() {
var btns = document.querySelectorAll('button');
for (var i = 0; i < btns.length; i++) {
if (btns[i].textContent.trim() === '${text}') {
btns[i].click();
return 'clicked';
}
}
throw new Error('Button "${text}" not found');
})()
`);
await sleep(2000);
}
async function getBodyText() {
return evaluate('document.body.innerText');
}
async function waitForText(text, timeoutMs = 120000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const body = await getBodyText();
if (body.includes(text)) return body;
await sleep(3000);
}
throw new Error(`Timeout waiting for "${text}"`);
}
// ── Main ─────────────────────────────────────────────────────────────────────
async function main() {
console.log('\n🚀 OpenAgents Launcher E2E Test\n');
// Connect to Electron via CDP
await step('Connect to Electron via CDP', async () => {
await connectCdp();
const title = await evaluate('document.title');
if (!title.includes('OpenAgents')) throw new Error(`Unexpected title: ${title}`);
});
await screenshot('01-launch');
// Step 1: Verify app loaded
await step('App loaded', async () => {
const text = await getBodyText();
if (!text.includes('Dashboard')) throw new Error('Dashboard not found');
});
// Step 2: Go to Install tab, install OpenClaw if needed
await step('Install OpenClaw via UI', async () => {
await clickTab('install');
await sleep(2000);
const text = await getBodyText();
if (text.includes('OpenClaw') && text.includes('INSTALLED')) {
console.log('(already installed) ');
return;
}
// Find and click Install on OpenClaw row
await evaluate(`
(function() {
var rows = document.querySelectorAll('.catalog-row');
for (var i = 0; i < rows.length; i++) {
if (rows[i].textContent.includes('OpenClaw')) {
rows[i].querySelector('button').click();
return 'clicked';
}
}
throw new Error('OpenClaw not found in catalog');
})()
`);
await sleep(2000);
// Click Install in confirm dialog
var confirmBtn = await evaluate(`
(function() {
var btn = document.getElementById('confirm-install-yes');
if (btn) { btn.click(); return 'confirmed'; }
// Try any Install button in overlay
var btns = document.querySelectorAll('.confirm-overlay button, .modal button');
for (var i = 0; i < btns.length; i++) {
if (btns[i].textContent.trim() === 'Install') { btns[i].click(); return 'confirmed'; }
}
throw new Error('Confirm button not found');
})()
`);
// Wait for install to complete (up to 3 minutes)
await waitForText('Back to Install', 180000);
await screenshot('02-openclaw-installed');
// Click Back to Install
await clickButtonByText('Back to Install');
});
// Step 3: Go to Dashboard, create agent
await step('Create agent', async () => {
await clickTab('dashboard');
await sleep(2000);
const text = await getBodyText();
if (text.includes('openclaw') && (text.includes('stopped') || text.includes('running'))) {
console.log('(agent exists) ');
return;
}
// Click New Agent or Add Agent
try {
await clickButtonByText('New Agent');
} catch {
await clickButtonByText('Add Agent');
}
await sleep(2000);
// Click Create (default type is openclaw)
await clickButtonByText('Create');
await sleep(3000);
});
await screenshot('03-agent-created');
// Step 4: Configure LLM API key
await step('Configure LLM', async () => {
if (!LLM_API_KEY) throw new Error('LLM_API_KEY not set');
// Use the API directly to save env vars
await evaluate(`
window.api.saveAgentEnv('openclaw', {
LLM_API_KEY: '${LLM_API_KEY}',
LLM_BASE_URL: '${LLM_BASE_URL || 'https://api.openai.com/v1'}',
LLM_MODEL: '${LLM_MODEL || 'gpt-4o'}'
})
`);
await sleep(1000);
});
// Step 5: Connect to workspace via UI
await step('Connect to workspace', async () => {
await clickTab('dashboard');
await sleep(2000);
// Click Actions on the agent card
await evaluate(`
(function() {
var btn = document.querySelector('[data-action="actions"]');
if (btn) { btn.click(); return 'clicked'; }
throw new Error('Actions button not found');
})()
`);
await sleep(2000);
// Click Connect Workspace
const connected = await evaluate(`
(function() {
var btns = document.querySelectorAll('.modal-action-btn, button');
for (var i = 0; i < btns.length; i++) {
if (btns[i].textContent.includes('Connect') && btns[i].dataset.action === 'connect') {
btns[i].click();
return 'clicked';
}
}
// If no connect button, agent might already be connected
return 'no-connect-btn';
})()
`);
if (connected === 'clicked') {
await sleep(2000);
// Enter workspace token
await evaluate(`
(function() {
var input = document.querySelector('#workspace-token-input, input[placeholder*="token"], input[type="text"]');
if (input) {
input.value = '${WS_TOKEN}';
input.dispatchEvent(new Event('input', {bubbles: true}));
return 'filled';
}
throw new Error('Token input not found');
})()
`);
await sleep(1000);
// Click Connect/Join
try { await clickButtonByText('Connect'); } catch {
try { await clickButtonByText('Join'); } catch {
await clickButtonByText('Save');
}
}
await sleep(3000);
} else {
// Use API fallback
const agentName = await evaluate(`
(function() {
var cards = document.querySelectorAll('.agent-card');
if (cards.length) return cards[0].querySelector('.agent-name')?.textContent?.trim();
return null;
})()
`);
if (agentName) {
await evaluate(`window.api.connectWorkspace('${agentName}', '${WS_TOKEN}')`);
}
}
// Close any modal
try {
await evaluate(`
var overlay = document.querySelector('.modal-overlay, .confirm-overlay');
if (overlay) overlay.click();
`);
} catch {}
await sleep(2000);
});
await screenshot('04-connected');
// Step 6: Start agent
await step('Start agent', async () => {
await clickTab('dashboard');
await sleep(2000);
await clickButtonByText('Start All');
await sleep(15000); // Wait for daemon + adapter
const text = await getBodyText();
if (text.includes('running')) {
return; // Good
}
// Wait more
await sleep(15000);
});
await screenshot('05-agent-started');
// Step 7: Send message via workspace
await step('Send message via workspace', async () => {
// Use the workspace API to send a message targeted at the agent
const agentName = await evaluate(`
(function() {
var cards = document.querySelectorAll('.agent-card');
if (cards.length) return cards[0].querySelector('.agent-name')?.textContent?.trim();
return 'e2e-agent';
})()
`);
const result = await evaluate(`
fetch('https://workspace-endpoint.openagents.org/v1/events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Workspace-Token': '${WS_TOKEN}'
},
body: JSON.stringify({
type: 'workspace.message.posted',
source: 'human:e2e-tester',
target: 'openagents:' + '${agentName || "e2e-agent"}',
network: '${WS_SLUG}',
payload: { content: 'What is 9 plus 3? Reply with just the number.', message_type: 'chat' },
metadata: {}
})
}).then(r => r.json()).then(d => JSON.stringify(d))
`);
const parsed = JSON.parse(result);
if (parsed.code !== 0) throw new Error(`Workspace API error: ${result}`);
console.log(`(sent to ${agentName}) `);
});
// Step 8: Wait for response
await step('Wait for agent response (up to 120s)', async () => {
// Poll the daemon log or status for a response
const start = Date.now();
while (Date.now() - start < 120000) {
try {
const log = await evaluate(`
window.api.getLogs && window.api.getLogs().then(r => r.lines ? r.lines.join('\\n') : '')
`);
if (log && log.includes('responded')) {
console.log(`(found response in ${Math.round((Date.now() - start) / 1000)}s) `);
return;
}
} catch {}
await sleep(5000);
}
throw new Error('No response within 120s');
});
await screenshot('06-response');
// Done
console.log('\n✅ All E2E tests passed!\n');
ws.close();
process.exit(0);
}
main().catch((e) => {
console.error(`\n❌ E2E test failed: ${e.message}\n`);
if (ws) ws.close();
process.exit(1);
});