@wingman-ai/gateway 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/README.md +8 -0
  2. package/dist/agent/config/agentConfig.cjs +12 -0
  3. package/dist/agent/config/agentConfig.d.ts +22 -0
  4. package/dist/agent/config/agentConfig.js +10 -1
  5. package/dist/agent/config/agentLoader.cjs +9 -0
  6. package/dist/agent/config/agentLoader.js +9 -0
  7. package/dist/agent/config/toolRegistry.cjs +17 -0
  8. package/dist/agent/config/toolRegistry.d.ts +15 -0
  9. package/dist/agent/config/toolRegistry.js +17 -0
  10. package/dist/agent/tests/agentConfig.test.cjs +6 -1
  11. package/dist/agent/tests/agentConfig.test.js +6 -1
  12. package/dist/agent/tests/browserControlHelpers.test.cjs +35 -0
  13. package/dist/agent/tests/browserControlHelpers.test.d.ts +1 -0
  14. package/dist/agent/tests/browserControlHelpers.test.js +29 -0
  15. package/dist/agent/tests/browserControlTool.test.cjs +2117 -0
  16. package/dist/agent/tests/browserControlTool.test.d.ts +1 -0
  17. package/dist/agent/tests/browserControlTool.test.js +2111 -0
  18. package/dist/agent/tests/toolRegistry.test.cjs +6 -0
  19. package/dist/agent/tests/toolRegistry.test.js +6 -0
  20. package/dist/agent/tools/browser_control.cjs +1282 -0
  21. package/dist/agent/tools/browser_control.d.ts +478 -0
  22. package/dist/agent/tools/browser_control.js +1242 -0
  23. package/dist/cli/commands/agent.cjs +16 -2
  24. package/dist/cli/commands/agent.js +16 -2
  25. package/dist/cli/commands/browser.cjs +603 -0
  26. package/dist/cli/commands/browser.d.ts +13 -0
  27. package/dist/cli/commands/browser.js +566 -0
  28. package/dist/cli/commands/gateway.cjs +18 -7
  29. package/dist/cli/commands/gateway.d.ts +5 -1
  30. package/dist/cli/commands/gateway.js +18 -7
  31. package/dist/cli/commands/init.cjs +134 -45
  32. package/dist/cli/commands/init.js +134 -45
  33. package/dist/cli/commands/skill.cjs +3 -2
  34. package/dist/cli/commands/skill.js +3 -2
  35. package/dist/cli/config/loader.cjs +15 -0
  36. package/dist/cli/config/loader.js +15 -0
  37. package/dist/cli/config/schema.cjs +51 -2
  38. package/dist/cli/config/schema.d.ts +49 -0
  39. package/dist/cli/config/schema.js +44 -1
  40. package/dist/cli/core/workspace.cjs +89 -0
  41. package/dist/cli/core/workspace.d.ts +1 -0
  42. package/dist/cli/core/workspace.js +55 -0
  43. package/dist/cli/index.cjs +53 -5
  44. package/dist/cli/index.js +53 -5
  45. package/dist/cli/types/browser.cjs +18 -0
  46. package/dist/cli/types/browser.d.ts +9 -0
  47. package/dist/cli/types/browser.js +0 -0
  48. package/dist/gateway/browserRelayServer.cjs +338 -0
  49. package/dist/gateway/browserRelayServer.d.ts +38 -0
  50. package/dist/gateway/browserRelayServer.js +301 -0
  51. package/dist/gateway/http/agents.cjs +22 -0
  52. package/dist/gateway/http/agents.js +22 -0
  53. package/dist/gateway/http/fs.cjs +57 -0
  54. package/dist/gateway/http/fs.js +58 -1
  55. package/dist/gateway/server.cjs +43 -6
  56. package/dist/gateway/server.d.ts +4 -1
  57. package/dist/gateway/server.js +36 -5
  58. package/dist/gateway/transport/websocket.cjs +45 -10
  59. package/dist/gateway/transport/websocket.d.ts +1 -0
  60. package/dist/gateway/transport/websocket.js +41 -9
  61. package/dist/gateway/types.d.ts +4 -0
  62. package/dist/tests/agents-api.test.cjs +52 -0
  63. package/dist/tests/agents-api.test.js +53 -1
  64. package/dist/tests/browser-command.test.cjs +264 -0
  65. package/dist/tests/browser-command.test.d.ts +1 -0
  66. package/dist/tests/browser-command.test.js +258 -0
  67. package/dist/tests/browser-relay-server.test.cjs +20 -0
  68. package/dist/tests/browser-relay-server.test.d.ts +1 -0
  69. package/dist/tests/browser-relay-server.test.js +14 -0
  70. package/dist/tests/cli-config-loader.test.cjs +43 -0
  71. package/dist/tests/cli-config-loader.test.js +43 -0
  72. package/dist/tests/cli-init.test.cjs +25 -2
  73. package/dist/tests/cli-init.test.js +25 -2
  74. package/dist/tests/cli-workspace-root.test.cjs +114 -0
  75. package/dist/tests/cli-workspace-root.test.d.ts +1 -0
  76. package/dist/tests/cli-workspace-root.test.js +108 -0
  77. package/dist/tests/fs-api.test.cjs +138 -0
  78. package/dist/tests/fs-api.test.d.ts +1 -0
  79. package/dist/tests/fs-api.test.js +132 -0
  80. package/dist/tests/gateway-command-workspace.test.cjs +150 -0
  81. package/dist/tests/gateway-command-workspace.test.d.ts +1 -0
  82. package/dist/tests/gateway-command-workspace.test.js +144 -0
  83. package/dist/tests/gateway-request-execution-overrides.test.cjs +42 -0
  84. package/dist/tests/gateway-request-execution-overrides.test.d.ts +1 -0
  85. package/dist/tests/gateway-request-execution-overrides.test.js +36 -0
  86. package/dist/tests/gateway.test.cjs +31 -0
  87. package/dist/tests/gateway.test.js +31 -0
  88. package/dist/tests/websocket-transport.test.cjs +31 -0
  89. package/dist/tests/websocket-transport.test.d.ts +1 -0
  90. package/dist/tests/websocket-transport.test.js +25 -0
  91. package/dist/webui/assets/index-BW9nM0J2.css +11 -0
  92. package/dist/webui/assets/{index-0nUBsUUq.js → index-C8-oboEC.js} +107 -107
  93. package/dist/webui/index.html +2 -2
  94. package/extensions/wingman-browser-extension/README.md +27 -0
  95. package/extensions/wingman-browser-extension/background.js +416 -0
  96. package/extensions/wingman-browser-extension/manifest.json +19 -0
  97. package/extensions/wingman-browser-extension/options.html +156 -0
  98. package/extensions/wingman-browser-extension/options.js +106 -0
  99. package/package.json +8 -6
  100. package/{.wingman → templates}/agents/README.md +2 -1
  101. package/{.wingman → templates}/agents/coding/agent.md +0 -1
  102. package/{.wingman → templates}/agents/coding-v2/agent.md +0 -1
  103. package/{.wingman → templates}/agents/game-dev/agent.md +8 -1
  104. package/{.wingman → templates}/agents/game-dev/art-generation.md +1 -0
  105. package/{.wingman → templates}/agents/main/agent.md +5 -0
  106. package/{.wingman → templates}/agents/researcher/agent.md +9 -0
  107. package/{.wingman → templates}/agents/stock-trader/agent.md +1 -0
  108. package/dist/webui/assets/index-kk7OrD-G.css +0 -11
  109. /package/{.wingman → templates}/agents/coding-v2/implementor.md +0 -0
  110. /package/{.wingman → templates}/agents/game-dev/asset-refinement.md +0 -0
  111. /package/{.wingman → templates}/agents/game-dev/planning-idea.md +0 -0
  112. /package/{.wingman → templates}/agents/game-dev/ui-specialist.md +0 -0
  113. /package/{.wingman → templates}/agents/stock-trader/chain-curator.md +0 -0
  114. /package/{.wingman → templates}/agents/stock-trader/goal-translator.md +0 -0
  115. /package/{.wingman → templates}/agents/stock-trader/guardrails-veto.md +0 -0
  116. /package/{.wingman → templates}/agents/stock-trader/path-planner.md +0 -0
  117. /package/{.wingman → templates}/agents/stock-trader/regime-analyst.md +0 -0
  118. /package/{.wingman → templates}/agents/stock-trader/risk.md +0 -0
  119. /package/{.wingman → templates}/agents/stock-trader/selection.md +0 -0
  120. /package/{.wingman → templates}/agents/stock-trader/strategy-composer.md +0 -0
@@ -0,0 +1,2111 @@
1
+ import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
+ import { spawn } from "node:child_process";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { afterEach, describe, expect, it } from "vitest";
6
+ import { createBrowserControlTool } from "../tools/browser_control.js";
7
+ describe("browser_control tool", ()=>{
8
+ const workspaces = [];
9
+ afterEach(()=>{
10
+ for (const workspace of workspaces)rmSync(workspace, {
11
+ recursive: true,
12
+ force: true
13
+ });
14
+ workspaces.length = 0;
15
+ });
16
+ it("runs browser actions through injected CDP/playwright dependencies", async ()=>{
17
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
18
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
19
+ workspaces.push(workspace, tempDir);
20
+ let currentUrl = "about:blank";
21
+ let browserClosed = false;
22
+ let chromeClosed = false;
23
+ const actionCalls = [];
24
+ const page = {
25
+ goto: async (url)=>{
26
+ currentUrl = url;
27
+ actionCalls.push(`goto:${url}`);
28
+ },
29
+ click: async (selector)=>{
30
+ actionCalls.push(`click:${selector}`);
31
+ },
32
+ fill: async (selector, text)=>{
33
+ actionCalls.push(`fill:${selector}:${text}`);
34
+ },
35
+ keyboard: {
36
+ press: async (key)=>{
37
+ actionCalls.push(`press:${key}`);
38
+ }
39
+ },
40
+ waitForTimeout: async (ms)=>{
41
+ actionCalls.push(`wait:${ms}`);
42
+ },
43
+ textContent: async (selector)=>{
44
+ actionCalls.push(`text:${selector}`);
45
+ return "body" === selector ? "Example page body content" : "Selector content";
46
+ },
47
+ evaluate: async (expression)=>{
48
+ actionCalls.push(`eval:${expression}`);
49
+ return {
50
+ ok: true
51
+ };
52
+ },
53
+ screenshot: async ({ path })=>{
54
+ actionCalls.push(`screenshot:${path}`);
55
+ writeFileSync(path, "test image data");
56
+ },
57
+ title: async ()=>"Example Title",
58
+ url: ()=>currentUrl
59
+ };
60
+ const context = {
61
+ pages: ()=>[
62
+ page
63
+ ],
64
+ newPage: async ()=>page
65
+ };
66
+ const browser = {
67
+ contexts: ()=>[
68
+ context
69
+ ],
70
+ close: async ()=>{
71
+ browserClosed = true;
72
+ }
73
+ };
74
+ const testDeps = {
75
+ importPlaywright: async ()=>({
76
+ chromium: {
77
+ connectOverCDP: async ()=>browser
78
+ }
79
+ }),
80
+ startChrome: async ()=>({
81
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
82
+ close: async ()=>{
83
+ chromeClosed = true;
84
+ }
85
+ }),
86
+ mkTempDir: ()=>tempDir,
87
+ removeDir: ()=>{},
88
+ now: ()=>1700000000000
89
+ };
90
+ const tool = createBrowserControlTool({
91
+ workspace
92
+ }, testDeps);
93
+ const result = await tool.invoke({
94
+ url: "https://example.com",
95
+ actions: [
96
+ {
97
+ type: "click",
98
+ selector: "#cta"
99
+ },
100
+ {
101
+ type: "type",
102
+ selector: "#query",
103
+ text: "wingman",
104
+ submit: true
105
+ },
106
+ {
107
+ type: "extract_text",
108
+ selector: "body",
109
+ maxChars: 10
110
+ },
111
+ {
112
+ type: "evaluate",
113
+ expression: "window.location.href"
114
+ },
115
+ {
116
+ type: "screenshot",
117
+ path: "artifacts/shot.png",
118
+ fullPage: false
119
+ }
120
+ ]
121
+ });
122
+ const parsed = JSON.parse(String(result));
123
+ expect(parsed.browser).toBe("chrome-cdp");
124
+ expect(parsed.finalUrl).toBe("https://example.com");
125
+ expect(parsed.title).toBe("Example Title");
126
+ expect(parsed.actionResults).toHaveLength(5);
127
+ expect(parsed.actionResults[2].text).toBe("Example pa");
128
+ expect(parsed.actionResults[4].path).toBe("artifacts/shot.png");
129
+ expect(actionCalls).toContain("click:#cta");
130
+ expect(actionCalls).toContain("press:Enter");
131
+ expect(browserClosed).toBe(true);
132
+ expect(chromeClosed).toBe(true);
133
+ });
134
+ it("targets the most recently opened tab in the CDP context", async ()=>{
135
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
136
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
137
+ workspaces.push(workspace, tempDir);
138
+ const stalePageCalls = [];
139
+ const activePageCalls = [];
140
+ let currentUrl = "about:blank";
141
+ const stalePage = {
142
+ goto: async (url)=>{
143
+ stalePageCalls.push(`goto:${url}`);
144
+ },
145
+ click: async ()=>{},
146
+ fill: async ()=>{},
147
+ keyboard: {
148
+ press: async ()=>{}
149
+ },
150
+ waitForTimeout: async ()=>{},
151
+ textContent: async ()=>"",
152
+ evaluate: async ()=>({}),
153
+ screenshot: async ()=>{},
154
+ title: async ()=>"Stale",
155
+ url: ()=>"about:blank"
156
+ };
157
+ const activePage = {
158
+ goto: async (url)=>{
159
+ currentUrl = url;
160
+ activePageCalls.push(`goto:${url}`);
161
+ },
162
+ bringToFront: async ()=>{
163
+ activePageCalls.push("bringToFront");
164
+ },
165
+ click: async ()=>{},
166
+ fill: async ()=>{},
167
+ keyboard: {
168
+ press: async ()=>{}
169
+ },
170
+ waitForTimeout: async ()=>{},
171
+ textContent: async ()=>"",
172
+ evaluate: async ()=>({}),
173
+ screenshot: async ()=>{},
174
+ title: async ()=>"Active",
175
+ url: ()=>currentUrl
176
+ };
177
+ const tool = createBrowserControlTool({
178
+ workspace
179
+ }, {
180
+ importPlaywright: async ()=>({
181
+ chromium: {
182
+ connectOverCDP: async ()=>({
183
+ contexts: ()=>[
184
+ {
185
+ pages: ()=>[
186
+ stalePage,
187
+ activePage
188
+ ],
189
+ newPage: async ()=>activePage
190
+ }
191
+ ],
192
+ close: async ()=>{}
193
+ })
194
+ }
195
+ }),
196
+ startChrome: async ()=>({
197
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
198
+ close: async ()=>{}
199
+ }),
200
+ mkTempDir: ()=>tempDir,
201
+ removeDir: ()=>{},
202
+ now: ()=>1700000000000
203
+ });
204
+ const result = await tool.invoke({
205
+ url: "https://example.com"
206
+ });
207
+ const parsed = JSON.parse(String(result));
208
+ expect(parsed.finalUrl).toBe("https://example.com");
209
+ expect(stalePageCalls).toHaveLength(0);
210
+ expect(activePageCalls).toContain("bringToFront");
211
+ expect(activePageCalls).toContain("goto:https://example.com");
212
+ });
213
+ it("uses a context with pages when the first CDP context is empty", async ()=>{
214
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
215
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
216
+ workspaces.push(workspace, tempDir);
217
+ const hiddenContextCalls = [];
218
+ const visibleContextCalls = [];
219
+ let currentUrl = "about:blank";
220
+ const hiddenContext = {
221
+ pages: ()=>[],
222
+ newPage: async ()=>{
223
+ hiddenContextCalls.push("newPage");
224
+ return {
225
+ goto: async (url)=>hiddenContextCalls.push(`goto:${url}`),
226
+ click: async ()=>{},
227
+ fill: async ()=>{},
228
+ keyboard: {
229
+ press: async ()=>{}
230
+ },
231
+ waitForTimeout: async ()=>{},
232
+ textContent: async ()=>"",
233
+ evaluate: async ()=>({}),
234
+ screenshot: async ()=>{},
235
+ title: async ()=>"Hidden",
236
+ url: ()=>"about:blank"
237
+ };
238
+ }
239
+ };
240
+ const visiblePage = {
241
+ goto: async (url)=>{
242
+ currentUrl = url;
243
+ visibleContextCalls.push(`goto:${url}`);
244
+ },
245
+ bringToFront: async ()=>{
246
+ visibleContextCalls.push("bringToFront");
247
+ },
248
+ click: async ()=>{},
249
+ fill: async ()=>{},
250
+ keyboard: {
251
+ press: async ()=>{}
252
+ },
253
+ waitForTimeout: async ()=>{},
254
+ textContent: async ()=>"",
255
+ evaluate: async ()=>({}),
256
+ screenshot: async ()=>{},
257
+ title: async ()=>"Visible",
258
+ url: ()=>currentUrl
259
+ };
260
+ const visibleContext = {
261
+ pages: ()=>[
262
+ visiblePage
263
+ ],
264
+ newPage: async ()=>visiblePage
265
+ };
266
+ const tool = createBrowserControlTool({
267
+ workspace
268
+ }, {
269
+ importPlaywright: async ()=>({
270
+ chromium: {
271
+ connectOverCDP: async ()=>({
272
+ contexts: ()=>[
273
+ hiddenContext,
274
+ visibleContext
275
+ ],
276
+ close: async ()=>{}
277
+ })
278
+ }
279
+ }),
280
+ startChrome: async ()=>({
281
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
282
+ close: async ()=>{}
283
+ }),
284
+ mkTempDir: ()=>tempDir,
285
+ removeDir: ()=>{},
286
+ now: ()=>1700000000000
287
+ });
288
+ const result = await tool.invoke({
289
+ url: "https://robinhood.com/login",
290
+ actions: [
291
+ {
292
+ type: "wait",
293
+ ms: 50
294
+ }
295
+ ]
296
+ });
297
+ const parsed = JSON.parse(String(result));
298
+ expect(parsed.finalUrl).toBe("https://robinhood.com/login");
299
+ expect(hiddenContextCalls).toHaveLength(0);
300
+ expect(visibleContextCalls).toContain("bringToFront");
301
+ expect(visibleContextCalls).toContain("goto:https://robinhood.com/login");
302
+ });
303
+ it("accepts larger top-level timeoutMs values for long browser runs", async ()=>{
304
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
305
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
306
+ workspaces.push(workspace, tempDir);
307
+ let capturedLaunchTimeoutMs = 0;
308
+ let capturedCdpTimeoutMs = 0;
309
+ const page = {
310
+ goto: async ()=>{},
311
+ click: async ()=>{},
312
+ fill: async ()=>{},
313
+ keyboard: {
314
+ press: async ()=>{}
315
+ },
316
+ waitForTimeout: async ()=>{},
317
+ textContent: async ()=>"",
318
+ evaluate: async ()=>"ok",
319
+ screenshot: async ()=>{},
320
+ title: async ()=>"Timeout Test",
321
+ url: ()=>"about:blank"
322
+ };
323
+ const tool = createBrowserControlTool({
324
+ workspace,
325
+ launchTimeoutMs: 15000
326
+ }, {
327
+ importPlaywright: async ()=>({
328
+ chromium: {
329
+ connectOverCDP: async (_wsEndpoint, options)=>{
330
+ capturedCdpTimeoutMs = options?.timeout ?? 0;
331
+ return {
332
+ contexts: ()=>[
333
+ {
334
+ pages: ()=>[
335
+ page
336
+ ],
337
+ newPage: async ()=>page
338
+ }
339
+ ],
340
+ close: async ()=>{}
341
+ };
342
+ }
343
+ }
344
+ }),
345
+ startChrome: async (input)=>{
346
+ capturedLaunchTimeoutMs = input.launchTimeoutMs;
347
+ return {
348
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
349
+ close: async ()=>{}
350
+ };
351
+ },
352
+ mkTempDir: ()=>tempDir,
353
+ removeDir: ()=>{},
354
+ now: ()=>1700000000000
355
+ });
356
+ await tool.invoke({
357
+ actions: [
358
+ {
359
+ type: "evaluate",
360
+ expression: "document.title"
361
+ }
362
+ ],
363
+ timeoutMs: 180000
364
+ });
365
+ expect(capturedLaunchTimeoutMs).toBe(180000);
366
+ expect(capturedCdpTimeoutMs).toBe(180000);
367
+ });
368
+ it("accepts large extract_text maxChars values for long page content", async ()=>{
369
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
370
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
371
+ workspaces.push(workspace, tempDir);
372
+ const largeBody = "A".repeat(1020000);
373
+ const page = {
374
+ goto: async ()=>{},
375
+ click: async ()=>{},
376
+ fill: async ()=>{},
377
+ keyboard: {
378
+ press: async ()=>{}
379
+ },
380
+ waitForTimeout: async ()=>{},
381
+ textContent: async ()=>largeBody,
382
+ evaluate: async ()=>"ok",
383
+ screenshot: async ()=>{},
384
+ title: async ()=>"Large Extract",
385
+ url: ()=>"https://robinhood.com"
386
+ };
387
+ const tool = createBrowserControlTool({
388
+ workspace
389
+ }, {
390
+ importPlaywright: async ()=>({
391
+ chromium: {
392
+ connectOverCDP: async ()=>({
393
+ contexts: ()=>[
394
+ {
395
+ pages: ()=>[
396
+ page
397
+ ],
398
+ newPage: async ()=>page
399
+ }
400
+ ],
401
+ close: async ()=>{}
402
+ })
403
+ }
404
+ }),
405
+ startChrome: async ()=>({
406
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
407
+ close: async ()=>{}
408
+ }),
409
+ mkTempDir: ()=>tempDir,
410
+ removeDir: ()=>{},
411
+ now: ()=>1700000000000
412
+ });
413
+ const result = await tool.invoke({
414
+ actions: [
415
+ {
416
+ type: "extract_text",
417
+ selector: "body",
418
+ maxChars: 1000000
419
+ }
420
+ ],
421
+ timeoutMs: 60000
422
+ });
423
+ const parsed = JSON.parse(String(result));
424
+ expect(parsed.actionResults[0].type).toBe("extract_text");
425
+ expect(parsed.actionResults[0].selector).toBe("body");
426
+ expect(parsed.actionResults[0].text.length).toBe(1000000);
427
+ expect(parsed.actionResults[0].truncated).toBe(true);
428
+ });
429
+ it("uses persistent Playwright launch when preferred", async ()=>{
430
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
431
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
432
+ workspaces.push(workspace, tempDir);
433
+ const fakeChromePath = join(tempDir, "chrome-bin");
434
+ writeFileSync(fakeChromePath, "fake chrome binary");
435
+ let launchCalled = false;
436
+ let closeCalled = false;
437
+ let currentUrl = "about:blank";
438
+ const page = {
439
+ goto: async (url)=>{
440
+ currentUrl = url;
441
+ },
442
+ click: async ()=>{},
443
+ fill: async ()=>{},
444
+ keyboard: {
445
+ press: async ()=>{}
446
+ },
447
+ waitForTimeout: async ()=>{},
448
+ textContent: async ()=>"",
449
+ evaluate: async ()=>"ok",
450
+ screenshot: async ()=>{},
451
+ title: async ()=>"Persistent",
452
+ url: ()=>currentUrl
453
+ };
454
+ const result = await createBrowserControlTool({
455
+ workspace,
456
+ preferPersistentLaunch: true,
457
+ defaultExecutablePath: fakeChromePath
458
+ }, {
459
+ importPlaywright: async ()=>({
460
+ chromium: {
461
+ connectOverCDP: async ()=>{
462
+ throw new Error("connectOverCDP should not be called");
463
+ },
464
+ launchPersistentContext: async ()=>{
465
+ launchCalled = true;
466
+ return {
467
+ pages: ()=>[
468
+ page
469
+ ],
470
+ newPage: async ()=>page,
471
+ close: async ()=>{
472
+ closeCalled = true;
473
+ }
474
+ };
475
+ }
476
+ }
477
+ }),
478
+ mkTempDir: ()=>tempDir,
479
+ removeDir: ()=>{},
480
+ now: ()=>1700000000000
481
+ }).invoke({
482
+ url: "https://example.com",
483
+ actions: [
484
+ {
485
+ type: "evaluate",
486
+ expression: "document.title"
487
+ }
488
+ ]
489
+ });
490
+ const parsed = JSON.parse(String(result));
491
+ expect(launchCalled).toBe(true);
492
+ expect(closeCalled).toBe(true);
493
+ expect(parsed.browser).toBe("chrome-playwright");
494
+ expect(parsed.transport).toBe("persistent-context");
495
+ expect(parsed.finalUrl).toBe("https://example.com");
496
+ });
497
+ it("falls back to persistent launch when CDP connection fails", async ()=>{
498
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
499
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
500
+ workspaces.push(workspace, tempDir);
501
+ const fakeChromePath = join(tempDir, "chrome-bin");
502
+ writeFileSync(fakeChromePath, "fake chrome binary");
503
+ let startChromeCalled = false;
504
+ let cdpCalled = false;
505
+ let cdpChromeClosed = false;
506
+ let persistentClosed = false;
507
+ let currentUrl = "about:blank";
508
+ const page = {
509
+ goto: async (url)=>{
510
+ currentUrl = url;
511
+ },
512
+ click: async ()=>{},
513
+ fill: async ()=>{},
514
+ keyboard: {
515
+ press: async ()=>{}
516
+ },
517
+ waitForTimeout: async ()=>{},
518
+ textContent: async ()=>"",
519
+ evaluate: async ()=>"ok",
520
+ screenshot: async ()=>{},
521
+ title: async ()=>"Fallback",
522
+ url: ()=>currentUrl
523
+ };
524
+ const tool = createBrowserControlTool({
525
+ workspace,
526
+ preferPersistentLaunch: false,
527
+ defaultExecutablePath: fakeChromePath
528
+ }, {
529
+ importPlaywright: async ()=>({
530
+ chromium: {
531
+ connectOverCDP: async ()=>{
532
+ cdpCalled = true;
533
+ throw new Error("overCDP: Timeout 30000ms exceeded.");
534
+ },
535
+ launchPersistentContext: async ()=>({
536
+ pages: ()=>[
537
+ page
538
+ ],
539
+ newPage: async ()=>page,
540
+ close: async ()=>{
541
+ persistentClosed = true;
542
+ }
543
+ })
544
+ }
545
+ }),
546
+ startChrome: async ()=>{
547
+ startChromeCalled = true;
548
+ return {
549
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
550
+ close: async ()=>{
551
+ cdpChromeClosed = true;
552
+ }
553
+ };
554
+ },
555
+ mkTempDir: ()=>tempDir,
556
+ removeDir: ()=>{},
557
+ now: ()=>1700000000000
558
+ });
559
+ const result = await tool.invoke({
560
+ url: "https://robinhood.com/login",
561
+ actions: [
562
+ {
563
+ type: "evaluate",
564
+ expression: "document.title"
565
+ }
566
+ ],
567
+ timeoutMs: 120000
568
+ });
569
+ const parsed = JSON.parse(String(result));
570
+ expect(startChromeCalled).toBe(true);
571
+ expect(cdpCalled).toBe(true);
572
+ expect(cdpChromeClosed).toBe(true);
573
+ expect(persistentClosed).toBe(true);
574
+ expect(parsed.browser).toBe("chrome-playwright");
575
+ expect(parsed.transport).toBe("persistent-context");
576
+ expect(parsed.finalUrl).toBe("https://robinhood.com/login");
577
+ });
578
+ it("uses relay transport when explicitly requested", async ()=>{
579
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
580
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
581
+ workspaces.push(workspace, tempDir);
582
+ let capturedRelayTimeout = 0;
583
+ let capturedRelayConfig = null;
584
+ let capturedWsEndpoint = "";
585
+ let startChromeCalled = false;
586
+ let currentUrl = "about:blank";
587
+ const page = {
588
+ goto: async (url)=>{
589
+ currentUrl = url;
590
+ },
591
+ click: async ()=>{},
592
+ fill: async ()=>{},
593
+ keyboard: {
594
+ press: async ()=>{}
595
+ },
596
+ waitForTimeout: async ()=>{},
597
+ textContent: async ()=>"",
598
+ evaluate: async ()=>"ok",
599
+ screenshot: async ()=>{},
600
+ title: async ()=>"Relay Mode",
601
+ url: ()=>currentUrl
602
+ };
603
+ const tool = createBrowserControlTool({
604
+ workspace,
605
+ browserTransport: "relay",
606
+ relayConfig: {
607
+ enabled: true,
608
+ host: "127.0.0.1",
609
+ port: 18792,
610
+ requireAuth: true,
611
+ authToken: "test-relay-token-123456"
612
+ }
613
+ }, {
614
+ importPlaywright: async ()=>({
615
+ chromium: {
616
+ connectOverCDP: async (wsEndpoint, options)=>{
617
+ capturedWsEndpoint = wsEndpoint;
618
+ capturedRelayTimeout = options?.timeout ?? 0;
619
+ return {
620
+ contexts: ()=>[
621
+ {
622
+ pages: ()=>[
623
+ page
624
+ ],
625
+ newPage: async ()=>page
626
+ }
627
+ ],
628
+ close: async ()=>{}
629
+ };
630
+ }
631
+ }
632
+ }),
633
+ startChrome: async ()=>{
634
+ startChromeCalled = true;
635
+ return {
636
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
637
+ close: async ()=>{}
638
+ };
639
+ },
640
+ resolveRelayWsEndpoint: async (config, timeoutMs)=>{
641
+ capturedRelayConfig = {
642
+ host: config.host,
643
+ port: config.port
644
+ };
645
+ capturedRelayTimeout = timeoutMs;
646
+ return "ws://127.0.0.1:18792/cdp?token=test-relay-token-123456";
647
+ },
648
+ mkTempDir: ()=>tempDir,
649
+ removeDir: ()=>{},
650
+ now: ()=>1700000000000
651
+ });
652
+ const result = await tool.invoke({
653
+ url: "https://example.com",
654
+ actions: [
655
+ {
656
+ type: "evaluate",
657
+ expression: "document.title"
658
+ }
659
+ ],
660
+ timeoutMs: 45000
661
+ });
662
+ const parsed = JSON.parse(String(result));
663
+ expect(startChromeCalled).toBe(false);
664
+ expect(parsed.transportRequested).toBe("relay");
665
+ expect(parsed.transport).toBe("relay-cdp");
666
+ expect(parsed.browser).toBe("chrome-relay");
667
+ expect(parsed.finalUrl).toBe("https://example.com");
668
+ expect(parsed.fallbackReason).toBeNull();
669
+ expect(capturedRelayConfig?.host).toBe("127.0.0.1");
670
+ expect(capturedRelayConfig?.port).toBe(18792);
671
+ expect(capturedRelayTimeout).toBe(45000);
672
+ expect(capturedWsEndpoint).toContain("/cdp");
673
+ });
674
+ it("falls back to relay in auto mode when playwright startup fails", async ()=>{
675
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
676
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
677
+ workspaces.push(workspace, tempDir);
678
+ let startChromeCalled = false;
679
+ let relayResolveCalled = false;
680
+ let currentUrl = "about:blank";
681
+ const page = {
682
+ goto: async (url)=>{
683
+ currentUrl = url;
684
+ },
685
+ click: async ()=>{},
686
+ fill: async ()=>{},
687
+ keyboard: {
688
+ press: async ()=>{}
689
+ },
690
+ waitForTimeout: async ()=>{},
691
+ textContent: async ()=>"",
692
+ evaluate: async ()=>"ok",
693
+ screenshot: async ()=>{},
694
+ title: async ()=>"Auto Relay Fallback",
695
+ url: ()=>currentUrl
696
+ };
697
+ const tool = createBrowserControlTool({
698
+ workspace,
699
+ browserTransport: "auto",
700
+ relayConfig: {
701
+ enabled: true,
702
+ host: "127.0.0.1",
703
+ port: 18792,
704
+ requireAuth: false
705
+ }
706
+ }, {
707
+ importPlaywright: async ()=>({
708
+ chromium: {
709
+ connectOverCDP: async (wsEndpoint)=>{
710
+ if (wsEndpoint.includes("18792")) return {
711
+ contexts: ()=>[
712
+ {
713
+ pages: ()=>[
714
+ page
715
+ ],
716
+ newPage: async ()=>page
717
+ }
718
+ ],
719
+ close: async ()=>{}
720
+ };
721
+ throw new Error("playwright cdp endpoint refused");
722
+ }
723
+ }
724
+ }),
725
+ startChrome: async ()=>{
726
+ startChromeCalled = true;
727
+ throw new Error("Failed to launch Chrome for CDP connection");
728
+ },
729
+ resolveRelayWsEndpoint: async ()=>{
730
+ relayResolveCalled = true;
731
+ return "ws://127.0.0.1:18792/cdp";
732
+ },
733
+ mkTempDir: ()=>tempDir,
734
+ removeDir: ()=>{},
735
+ now: ()=>1700000000000
736
+ });
737
+ const result = await tool.invoke({
738
+ url: "https://example.com/auto",
739
+ actions: [
740
+ {
741
+ type: "evaluate",
742
+ expression: "document.title"
743
+ }
744
+ ],
745
+ timeoutMs: 90000
746
+ });
747
+ const parsed = JSON.parse(String(result));
748
+ expect(startChromeCalled).toBe(true);
749
+ expect(relayResolveCalled).toBe(true);
750
+ expect(parsed.transportRequested).toBe("auto");
751
+ expect(parsed.transport).toBe("relay-cdp");
752
+ expect(parsed.browser).toBe("chrome-relay");
753
+ expect(parsed.fallbackReason).toContain("playwright initialization failed");
754
+ expect(parsed.finalUrl).toBe("https://example.com/auto");
755
+ });
756
+ it("does not fall back to relay when transport is explicitly playwright", async ()=>{
757
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
758
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
759
+ workspaces.push(workspace, tempDir);
760
+ let relayResolveCalled = false;
761
+ const tool = createBrowserControlTool({
762
+ workspace,
763
+ browserTransport: "playwright",
764
+ relayConfig: {
765
+ enabled: true,
766
+ host: "127.0.0.1",
767
+ port: 18792,
768
+ requireAuth: false
769
+ }
770
+ }, {
771
+ importPlaywright: async ()=>({
772
+ chromium: {
773
+ connectOverCDP: async ()=>{
774
+ throw new Error("playwright cdp endpoint refused");
775
+ }
776
+ }
777
+ }),
778
+ startChrome: async ()=>{
779
+ throw new Error("CDP startup failed");
780
+ },
781
+ resolveRelayWsEndpoint: async ()=>{
782
+ relayResolveCalled = true;
783
+ return "ws://127.0.0.1:18792/cdp";
784
+ },
785
+ mkTempDir: ()=>tempDir,
786
+ removeDir: ()=>{},
787
+ now: ()=>1700000000000
788
+ });
789
+ const result = await tool.invoke({
790
+ url: "https://example.com/playwright-only"
791
+ });
792
+ expect(relayResolveCalled).toBe(false);
793
+ expect(String(result)).toContain("Error running browser_control");
794
+ expect(String(result)).toContain("CDP startup failed");
795
+ });
796
+ it("rejects screenshot paths that escape the workspace", async ()=>{
797
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
798
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
799
+ workspaces.push(workspace, tempDir);
800
+ const page = {
801
+ goto: async ()=>{},
802
+ click: async ()=>{},
803
+ fill: async ()=>{},
804
+ keyboard: {
805
+ press: async ()=>{}
806
+ },
807
+ waitForTimeout: async ()=>{},
808
+ textContent: async ()=>"",
809
+ evaluate: async ()=>({}),
810
+ screenshot: async ()=>{},
811
+ title: async ()=>"Example",
812
+ url: ()=>"about:blank"
813
+ };
814
+ const tool = createBrowserControlTool({
815
+ workspace
816
+ }, {
817
+ importPlaywright: async ()=>({
818
+ chromium: {
819
+ connectOverCDP: async ()=>({
820
+ contexts: ()=>[
821
+ {
822
+ pages: ()=>[
823
+ page
824
+ ],
825
+ newPage: async ()=>page
826
+ }
827
+ ],
828
+ close: async ()=>{}
829
+ })
830
+ }
831
+ }),
832
+ startChrome: async ()=>({
833
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
834
+ close: async ()=>{}
835
+ }),
836
+ mkTempDir: ()=>tempDir,
837
+ removeDir: ()=>{},
838
+ now: ()=>1700000000000
839
+ });
840
+ const result = await tool.invoke({
841
+ actions: [
842
+ {
843
+ type: "screenshot",
844
+ path: "../escape.png"
845
+ }
846
+ ]
847
+ });
848
+ expect(String(result)).toContain("Error running browser_control");
849
+ expect(String(result)).toContain("inside the workspace");
850
+ });
851
+ it("supports alias action types used by some agent prompts", async ()=>{
852
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
853
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
854
+ workspaces.push(workspace, tempDir);
855
+ let currentUrl = "about:blank";
856
+ const actionCalls = [];
857
+ const page = {
858
+ goto: async (url)=>{
859
+ currentUrl = url;
860
+ actionCalls.push(`goto:${url}`);
861
+ },
862
+ click: async ()=>{},
863
+ fill: async ()=>{},
864
+ keyboard: {
865
+ press: async ()=>{}
866
+ },
867
+ waitForTimeout: async (ms)=>{
868
+ actionCalls.push(`wait:${ms}`);
869
+ },
870
+ textContent: async (selector)=>{
871
+ actionCalls.push(`text:${selector}`);
872
+ return "Robinhood";
873
+ },
874
+ evaluate: async (expression)=>{
875
+ actionCalls.push(`eval:${expression}`);
876
+ return "Robinhood - Investing";
877
+ },
878
+ screenshot: async ({ path })=>{
879
+ actionCalls.push(`screenshot:${path}`);
880
+ writeFileSync(path, "test image data");
881
+ },
882
+ title: async ()=>"Robinhood - Investing",
883
+ url: ()=>currentUrl
884
+ };
885
+ const tool = createBrowserControlTool({
886
+ workspace
887
+ }, {
888
+ importPlaywright: async ()=>({
889
+ chromium: {
890
+ connectOverCDP: async ()=>({
891
+ contexts: ()=>[
892
+ {
893
+ pages: ()=>[
894
+ page
895
+ ],
896
+ newPage: async ()=>page
897
+ }
898
+ ],
899
+ close: async ()=>{}
900
+ })
901
+ }
902
+ }),
903
+ startChrome: async ()=>({
904
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
905
+ close: async ()=>{}
906
+ }),
907
+ mkTempDir: ()=>tempDir,
908
+ removeDir: ()=>{},
909
+ now: ()=>1700000000000
910
+ });
911
+ const result = await tool.invoke({
912
+ url: "https://robinhood.com/?classic=1",
913
+ actions: [
914
+ {
915
+ type: "url",
916
+ url: "https://robinhood.com/?classic=1"
917
+ },
918
+ {
919
+ type: "ms",
920
+ ms: 4000
921
+ },
922
+ {
923
+ type: "selector",
924
+ selector: "body",
925
+ maxChars: 4000
926
+ },
927
+ {
928
+ type: "expression",
929
+ expression: "document.title"
930
+ },
931
+ {
932
+ type: "path",
933
+ path: "robinhood.png",
934
+ fullPage: true
935
+ }
936
+ ],
937
+ headless: true,
938
+ timeoutMs: 60000
939
+ });
940
+ const parsed = JSON.parse(String(result));
941
+ expect(parsed.finalUrl).toBe("https://robinhood.com/?classic=1");
942
+ expect(parsed.actionResults[0].type).toBe("navigate");
943
+ expect(parsed.actionResults[1].type).toBe("wait");
944
+ expect(parsed.actionResults[2].type).toBe("extract_text");
945
+ expect(parsed.actionResults[3].type).toBe("evaluate");
946
+ expect(parsed.actionResults[4].type).toBe("screenshot");
947
+ expect(parsed.actionResults[4].path).toBe("robinhood.png");
948
+ expect(actionCalls).toContain("goto:https://robinhood.com/?classic=1");
949
+ expect(actionCalls).toContain("wait:4000");
950
+ expect(actionCalls).toContain("text:body");
951
+ expect(actionCalls).toContain("eval:document.title");
952
+ });
953
+ it("supports snapshot alias for screenshot actions", async ()=>{
954
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
955
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
956
+ workspaces.push(workspace, tempDir);
957
+ let currentUrl = "about:blank";
958
+ const actionCalls = [];
959
+ const page = {
960
+ goto: async (url)=>{
961
+ currentUrl = url;
962
+ actionCalls.push(`goto:${url}`);
963
+ },
964
+ click: async ()=>{},
965
+ fill: async ()=>{},
966
+ keyboard: {
967
+ press: async ()=>{}
968
+ },
969
+ waitForTimeout: async ()=>{},
970
+ textContent: async ()=>"",
971
+ evaluate: async ()=>"ok",
972
+ screenshot: async ({ path })=>{
973
+ actionCalls.push(`screenshot:${path}`);
974
+ writeFileSync(path, "test image data");
975
+ },
976
+ title: async ()=>"Robinhood",
977
+ url: ()=>currentUrl
978
+ };
979
+ const tool = createBrowserControlTool({
980
+ workspace
981
+ }, {
982
+ importPlaywright: async ()=>({
983
+ chromium: {
984
+ connectOverCDP: async ()=>({
985
+ contexts: ()=>[
986
+ {
987
+ pages: ()=>[
988
+ page
989
+ ],
990
+ newPage: async ()=>page
991
+ }
992
+ ],
993
+ close: async ()=>{}
994
+ })
995
+ }
996
+ }),
997
+ startChrome: async ()=>({
998
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
999
+ close: async ()=>{}
1000
+ }),
1001
+ mkTempDir: ()=>tempDir,
1002
+ removeDir: ()=>{},
1003
+ now: ()=>1700000000000
1004
+ });
1005
+ const result = await tool.invoke({
1006
+ url: "https://robinhood.com/?classic=1",
1007
+ actions: [
1008
+ {
1009
+ type: "navigate",
1010
+ url: "https://robinhood.com/?classic=1"
1011
+ },
1012
+ {
1013
+ type: "wait",
1014
+ ms: 10
1015
+ },
1016
+ {
1017
+ type: "snapshot",
1018
+ path: "robinhood_classic.png",
1019
+ fullPage: true
1020
+ }
1021
+ ],
1022
+ headless: true,
1023
+ timeoutMs: 60000
1024
+ });
1025
+ const parsed = JSON.parse(String(result));
1026
+ expect(parsed.finalUrl).toBe("https://robinhood.com/?classic=1");
1027
+ expect(parsed.actionResults[2].type).toBe("screenshot");
1028
+ expect(parsed.actionResults[2].path).toBe("robinhood_classic.png");
1029
+ expect(actionCalls).toContain("goto:https://robinhood.com/?classic=1");
1030
+ });
1031
+ it("supports additional action aliases for robust prompting", async ()=>{
1032
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1033
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1034
+ workspaces.push(workspace, tempDir);
1035
+ let currentUrl = "about:blank";
1036
+ const actionCalls = [];
1037
+ const page = {
1038
+ goto: async (url)=>{
1039
+ currentUrl = url;
1040
+ actionCalls.push(`goto:${url}`);
1041
+ },
1042
+ click: async ()=>{},
1043
+ fill: async ()=>{},
1044
+ keyboard: {
1045
+ press: async ()=>{}
1046
+ },
1047
+ waitForTimeout: async (ms)=>{
1048
+ actionCalls.push(`wait:${ms}`);
1049
+ },
1050
+ textContent: async ()=>"",
1051
+ evaluate: async (expression)=>{
1052
+ actionCalls.push(`eval:${expression}`);
1053
+ return "ok";
1054
+ },
1055
+ screenshot: async ({ path })=>{
1056
+ actionCalls.push(`screenshot:${path}`);
1057
+ writeFileSync(path, "test image data");
1058
+ },
1059
+ title: async ()=>"Alias Test",
1060
+ url: ()=>currentUrl
1061
+ };
1062
+ const tool = createBrowserControlTool({
1063
+ workspace
1064
+ }, {
1065
+ importPlaywright: async ()=>({
1066
+ chromium: {
1067
+ connectOverCDP: async ()=>({
1068
+ contexts: ()=>[
1069
+ {
1070
+ pages: ()=>[
1071
+ page
1072
+ ],
1073
+ newPage: async ()=>page
1074
+ }
1075
+ ],
1076
+ close: async ()=>{}
1077
+ })
1078
+ }
1079
+ }),
1080
+ startChrome: async ()=>({
1081
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1082
+ close: async ()=>{}
1083
+ }),
1084
+ mkTempDir: ()=>tempDir,
1085
+ removeDir: ()=>{},
1086
+ now: ()=>1700000000000
1087
+ });
1088
+ const result = await tool.invoke({
1089
+ url: "https://example.com",
1090
+ actions: [
1091
+ {
1092
+ type: "open",
1093
+ url: "https://example.com/docs"
1094
+ },
1095
+ {
1096
+ type: "sleep",
1097
+ ms: 25
1098
+ },
1099
+ {
1100
+ type: "js",
1101
+ expression: "document.title"
1102
+ },
1103
+ {
1104
+ type: "capture",
1105
+ path: "alias-capture.png",
1106
+ fullPage: true
1107
+ }
1108
+ ],
1109
+ headless: true
1110
+ });
1111
+ const parsed = JSON.parse(String(result));
1112
+ expect(parsed.finalUrl).toBe("https://example.com/docs");
1113
+ expect(parsed.actionResults[0].type).toBe("navigate");
1114
+ expect(parsed.actionResults[1].type).toBe("wait");
1115
+ expect(parsed.actionResults[2].type).toBe("evaluate");
1116
+ expect(parsed.actionResults[3].type).toBe("screenshot");
1117
+ expect(actionCalls).toContain("goto:https://example.com/docs");
1118
+ expect(actionCalls).toContain("wait:25");
1119
+ expect(actionCalls).toContain("eval:document.title");
1120
+ });
1121
+ it("supports extract alias for text extraction", async ()=>{
1122
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1123
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1124
+ workspaces.push(workspace, tempDir);
1125
+ let currentUrl = "about:blank";
1126
+ const actionCalls = [];
1127
+ const page = {
1128
+ goto: async (url)=>{
1129
+ currentUrl = url;
1130
+ actionCalls.push(`goto:${url}`);
1131
+ },
1132
+ click: async ()=>{},
1133
+ fill: async ()=>{},
1134
+ keyboard: {
1135
+ press: async ()=>{}
1136
+ },
1137
+ waitForTimeout: async ()=>{},
1138
+ waitForLoadState: async (state)=>{
1139
+ actionCalls.push(`load:${state}`);
1140
+ },
1141
+ textContent: async (selector)=>{
1142
+ actionCalls.push(`text:${selector}`);
1143
+ return "Robinhood support content";
1144
+ },
1145
+ evaluate: async ()=>"ok",
1146
+ screenshot: async ()=>{},
1147
+ title: async ()=>"Support",
1148
+ url: ()=>currentUrl
1149
+ };
1150
+ const tool = createBrowserControlTool({
1151
+ workspace
1152
+ }, {
1153
+ importPlaywright: async ()=>({
1154
+ chromium: {
1155
+ connectOverCDP: async ()=>({
1156
+ contexts: ()=>[
1157
+ {
1158
+ pages: ()=>[
1159
+ page
1160
+ ],
1161
+ newPage: async ()=>page
1162
+ }
1163
+ ],
1164
+ close: async ()=>{}
1165
+ })
1166
+ }
1167
+ }),
1168
+ startChrome: async ()=>({
1169
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1170
+ close: async ()=>{}
1171
+ }),
1172
+ mkTempDir: ()=>tempDir,
1173
+ removeDir: ()=>{},
1174
+ now: ()=>1700000000000
1175
+ });
1176
+ const result = await tool.invoke({
1177
+ url: "https://robinhood.com",
1178
+ actions: [
1179
+ {
1180
+ type: "goto",
1181
+ url: "https://robinhood.com/"
1182
+ },
1183
+ {
1184
+ type: "wait",
1185
+ load: "networkidle",
1186
+ timeoutMs: 30000
1187
+ },
1188
+ {
1189
+ type: "extract",
1190
+ selector: "body",
1191
+ maxChars: 5000
1192
+ }
1193
+ ],
1194
+ headless: true,
1195
+ timeoutMs: 60000
1196
+ });
1197
+ const parsed = JSON.parse(String(result));
1198
+ expect(parsed.finalUrl).toBe("https://robinhood.com/");
1199
+ expect(parsed.actionResults[2].type).toBe("extract_text");
1200
+ expect(parsed.actionResults[2].selector).toBe("body");
1201
+ expect(actionCalls).toContain("goto:https://robinhood.com/");
1202
+ expect(actionCalls).toContain("load:networkidle");
1203
+ expect(actionCalls).toContain("text:body");
1204
+ });
1205
+ it("supports getContent alias for text extraction", async ()=>{
1206
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1207
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1208
+ workspaces.push(workspace, tempDir);
1209
+ let currentUrl = "about:blank";
1210
+ const actionCalls = [];
1211
+ const page = {
1212
+ goto: async (url)=>{
1213
+ currentUrl = url;
1214
+ actionCalls.push(`goto:${url}`);
1215
+ },
1216
+ click: async ()=>{},
1217
+ fill: async ()=>{},
1218
+ keyboard: {
1219
+ press: async ()=>{}
1220
+ },
1221
+ waitForTimeout: async ()=>{},
1222
+ waitForLoadState: async (state)=>{
1223
+ actionCalls.push(`load:${state}`);
1224
+ },
1225
+ textContent: async (selector)=>{
1226
+ actionCalls.push(`text:${selector}`);
1227
+ return "Robinhood homepage content";
1228
+ },
1229
+ evaluate: async ()=>"ok",
1230
+ screenshot: async ()=>{},
1231
+ title: async ()=>"Robinhood",
1232
+ url: ()=>currentUrl
1233
+ };
1234
+ const tool = createBrowserControlTool({
1235
+ workspace
1236
+ }, {
1237
+ importPlaywright: async ()=>({
1238
+ chromium: {
1239
+ connectOverCDP: async ()=>({
1240
+ contexts: ()=>[
1241
+ {
1242
+ pages: ()=>[
1243
+ page
1244
+ ],
1245
+ newPage: async ()=>page
1246
+ }
1247
+ ],
1248
+ close: async ()=>{}
1249
+ })
1250
+ }
1251
+ }),
1252
+ startChrome: async ()=>({
1253
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1254
+ close: async ()=>{}
1255
+ }),
1256
+ mkTempDir: ()=>tempDir,
1257
+ removeDir: ()=>{},
1258
+ now: ()=>1700000000000
1259
+ });
1260
+ const result = await tool.invoke({
1261
+ url: "https://robinhood.com/",
1262
+ actions: [
1263
+ {
1264
+ type: "goto",
1265
+ url: "https://robinhood.com/"
1266
+ },
1267
+ {
1268
+ type: "wait",
1269
+ load: "networkidle",
1270
+ timeoutMs: 30000
1271
+ },
1272
+ {
1273
+ type: "getContent",
1274
+ selector: "body",
1275
+ maxChars: 5000
1276
+ }
1277
+ ],
1278
+ timeoutMs: 60000
1279
+ });
1280
+ const parsed = JSON.parse(String(result));
1281
+ expect(parsed.finalUrl).toBe("https://robinhood.com/");
1282
+ expect(parsed.actionResults[2].type).toBe("extract_text");
1283
+ expect(parsed.actionResults[2].selector).toBe("body");
1284
+ expect(actionCalls).toContain("goto:https://robinhood.com/");
1285
+ expect(actionCalls).toContain("load:networkidle");
1286
+ expect(actionCalls).toContain("text:body");
1287
+ });
1288
+ it("supports querySelector alias for text extraction", async ()=>{
1289
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1290
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1291
+ workspaces.push(workspace, tempDir);
1292
+ let currentUrl = "about:blank";
1293
+ const actionCalls = [];
1294
+ const page = {
1295
+ goto: async (url)=>{
1296
+ currentUrl = url;
1297
+ actionCalls.push(`goto:${url}`);
1298
+ },
1299
+ click: async ()=>{},
1300
+ fill: async ()=>{},
1301
+ keyboard: {
1302
+ press: async ()=>{}
1303
+ },
1304
+ waitForTimeout: async ()=>{},
1305
+ waitForLoadState: async (state)=>{
1306
+ actionCalls.push(`load:${state}`);
1307
+ },
1308
+ textContent: async (selector)=>{
1309
+ actionCalls.push(`text:${selector}`);
1310
+ return "Robinhood homepage content";
1311
+ },
1312
+ evaluate: async ()=>"ok",
1313
+ screenshot: async ()=>{},
1314
+ title: async ()=>"Robinhood",
1315
+ url: ()=>currentUrl
1316
+ };
1317
+ const tool = createBrowserControlTool({
1318
+ workspace
1319
+ }, {
1320
+ importPlaywright: async ()=>({
1321
+ chromium: {
1322
+ connectOverCDP: async ()=>({
1323
+ contexts: ()=>[
1324
+ {
1325
+ pages: ()=>[
1326
+ page
1327
+ ],
1328
+ newPage: async ()=>page
1329
+ }
1330
+ ],
1331
+ close: async ()=>{}
1332
+ })
1333
+ }
1334
+ }),
1335
+ startChrome: async ()=>({
1336
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1337
+ close: async ()=>{}
1338
+ }),
1339
+ mkTempDir: ()=>tempDir,
1340
+ removeDir: ()=>{},
1341
+ now: ()=>1700000000000
1342
+ });
1343
+ const result = await tool.invoke({
1344
+ url: "https://robinhood.com",
1345
+ actions: [
1346
+ {
1347
+ type: "navigate",
1348
+ url: "https://robinhood.com"
1349
+ },
1350
+ {
1351
+ type: "wait",
1352
+ load: "networkidle",
1353
+ timeoutMs: 30000
1354
+ },
1355
+ {
1356
+ type: "querySelector",
1357
+ selector: "body",
1358
+ maxChars: 4000
1359
+ }
1360
+ ],
1361
+ timeoutMs: 30000
1362
+ });
1363
+ const parsed = JSON.parse(String(result));
1364
+ expect(parsed.finalUrl).toBe("https://robinhood.com");
1365
+ expect(parsed.actionResults[2].type).toBe("extract_text");
1366
+ expect(parsed.actionResults[2].selector).toBe("body");
1367
+ expect(actionCalls).toContain("goto:https://robinhood.com");
1368
+ expect(actionCalls).toContain("load:networkidle");
1369
+ expect(actionCalls).toContain("text:body");
1370
+ });
1371
+ it("supports wait_for conditions", async ()=>{
1372
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1373
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1374
+ workspaces.push(workspace, tempDir);
1375
+ const waitCalls = [];
1376
+ const page = {
1377
+ goto: async ()=>{},
1378
+ click: async ()=>{},
1379
+ fill: async ()=>{},
1380
+ keyboard: {
1381
+ press: async ()=>{}
1382
+ },
1383
+ waitForTimeout: async ()=>{},
1384
+ waitForSelector: async (selector)=>{
1385
+ waitCalls.push(`selector:${selector}`);
1386
+ },
1387
+ waitForURL: async ()=>{
1388
+ waitCalls.push("url");
1389
+ },
1390
+ waitForLoadState: async (state)=>{
1391
+ waitCalls.push(`load:${state}`);
1392
+ },
1393
+ waitForFunction: async (expression)=>{
1394
+ waitCalls.push(`fn:${expression}`);
1395
+ },
1396
+ textContent: async ()=>"",
1397
+ evaluate: async ()=>"ok",
1398
+ screenshot: async ()=>{},
1399
+ title: async ()=>"Wait For Test",
1400
+ url: ()=>"https://example.com/dashboard"
1401
+ };
1402
+ const tool = createBrowserControlTool({
1403
+ workspace
1404
+ }, {
1405
+ importPlaywright: async ()=>({
1406
+ chromium: {
1407
+ connectOverCDP: async ()=>({
1408
+ contexts: ()=>[
1409
+ {
1410
+ pages: ()=>[
1411
+ page
1412
+ ],
1413
+ newPage: async ()=>page
1414
+ }
1415
+ ],
1416
+ close: async ()=>{}
1417
+ })
1418
+ }
1419
+ }),
1420
+ startChrome: async ()=>({
1421
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1422
+ close: async ()=>{}
1423
+ }),
1424
+ mkTempDir: ()=>tempDir,
1425
+ removeDir: ()=>{},
1426
+ now: ()=>1700000000000
1427
+ });
1428
+ const result = await tool.invoke({
1429
+ actions: [
1430
+ {
1431
+ type: "wait_for",
1432
+ selector: "#portfolio-root",
1433
+ url: "https://example.com/**",
1434
+ load: "domcontentloaded",
1435
+ fn: "document.readyState === 'complete'",
1436
+ timeoutMs: 25000
1437
+ }
1438
+ ],
1439
+ headless: true,
1440
+ timeoutMs: 60000
1441
+ });
1442
+ const parsed = JSON.parse(String(result));
1443
+ expect(parsed.actionResults[0].type).toBe("wait_for");
1444
+ expect(parsed.actionResults[0].timeoutMs).toBe(25000);
1445
+ expect(waitCalls).toContain("selector:#portfolio-root");
1446
+ expect(waitCalls).toContain("url");
1447
+ expect(waitCalls).toContain("load:domcontentloaded");
1448
+ expect(waitCalls).toContain("fn:document.readyState === 'complete'");
1449
+ });
1450
+ it("supports wait with load/timeoutMs alias style", async ()=>{
1451
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1452
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1453
+ workspaces.push(workspace, tempDir);
1454
+ const waitCalls = [];
1455
+ const page = {
1456
+ goto: async ()=>{},
1457
+ click: async ()=>{},
1458
+ fill: async ()=>{},
1459
+ keyboard: {
1460
+ press: async ()=>{}
1461
+ },
1462
+ waitForTimeout: async ()=>{},
1463
+ waitForLoadState: async (state)=>{
1464
+ waitCalls.push(`load:${state}`);
1465
+ },
1466
+ textContent: async ()=>"",
1467
+ evaluate: async ()=>"ok",
1468
+ screenshot: async ()=>{},
1469
+ title: async ()=>"Wait Alias Test",
1470
+ url: ()=>"https://support.robinhood.com"
1471
+ };
1472
+ const tool = createBrowserControlTool({
1473
+ workspace
1474
+ }, {
1475
+ importPlaywright: async ()=>({
1476
+ chromium: {
1477
+ connectOverCDP: async ()=>({
1478
+ contexts: ()=>[
1479
+ {
1480
+ pages: ()=>[
1481
+ page
1482
+ ],
1483
+ newPage: async ()=>page
1484
+ }
1485
+ ],
1486
+ close: async ()=>{}
1487
+ })
1488
+ }
1489
+ }),
1490
+ startChrome: async ()=>({
1491
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1492
+ close: async ()=>{}
1493
+ }),
1494
+ mkTempDir: ()=>tempDir,
1495
+ removeDir: ()=>{},
1496
+ now: ()=>1700000000000
1497
+ });
1498
+ const result = await tool.invoke({
1499
+ actions: [
1500
+ {
1501
+ type: "wait",
1502
+ load: "networkidle",
1503
+ timeoutMs: 180000
1504
+ }
1505
+ ],
1506
+ headless: true
1507
+ });
1508
+ const parsed = JSON.parse(String(result));
1509
+ expect(parsed.actionResults[0].type).toBe("wait_for");
1510
+ expect(parsed.actionResults[0].load).toBe("networkidle");
1511
+ expect(parsed.actionResults[0].timeoutMs).toBe(180000);
1512
+ expect(waitCalls).toContain("load:networkidle");
1513
+ });
1514
+ it("auto-provisions bundled wingman extension when configured path is missing", async ()=>{
1515
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1516
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1517
+ workspaces.push(workspace, tempDir);
1518
+ let capturedChromeArgs = [];
1519
+ const page = {
1520
+ goto: async ()=>{},
1521
+ click: async ()=>{},
1522
+ fill: async ()=>{},
1523
+ keyboard: {
1524
+ press: async ()=>{}
1525
+ },
1526
+ waitForTimeout: async ()=>{},
1527
+ textContent: async ()=>"",
1528
+ evaluate: async ()=>"ok",
1529
+ screenshot: async ()=>{},
1530
+ title: async ()=>"Bundled Extension",
1531
+ url: ()=>"about:blank"
1532
+ };
1533
+ const tool = createBrowserControlTool({
1534
+ workspace,
1535
+ defaultExtensions: [
1536
+ "wingman"
1537
+ ]
1538
+ }, {
1539
+ importPlaywright: async ()=>({
1540
+ chromium: {
1541
+ connectOverCDP: async ()=>({
1542
+ contexts: ()=>[
1543
+ {
1544
+ pages: ()=>[
1545
+ page
1546
+ ],
1547
+ newPage: async ()=>page
1548
+ }
1549
+ ],
1550
+ close: async ()=>{}
1551
+ })
1552
+ }
1553
+ }),
1554
+ startChrome: async (input)=>{
1555
+ capturedChromeArgs = input.chromeArgs || [];
1556
+ return {
1557
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1558
+ close: async ()=>{}
1559
+ };
1560
+ },
1561
+ mkTempDir: ()=>tempDir,
1562
+ removeDir: ()=>{},
1563
+ now: ()=>1700000000000
1564
+ });
1565
+ const result = await tool.invoke({
1566
+ actions: [
1567
+ {
1568
+ type: "evaluate",
1569
+ expression: "document.title"
1570
+ }
1571
+ ]
1572
+ });
1573
+ const parsed = JSON.parse(String(result));
1574
+ expect(parsed.extensions).toContain("wingman");
1575
+ expect(existsSync(join(workspace, ".wingman", "browser-extensions", "wingman", "manifest.json"))).toBe(true);
1576
+ expect(capturedChromeArgs.some((arg)=>arg.startsWith("--load-extension="))).toBe(true);
1577
+ });
1578
+ it("resolves configured extension paths relative to config workspace", async ()=>{
1579
+ const executionWorkspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1580
+ const configWorkspace = mkdtempSync(join(tmpdir(), "wingman-browser-config-workspace-"));
1581
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1582
+ workspaces.push(executionWorkspace, configWorkspace, tempDir);
1583
+ const extensionDir = join(configWorkspace, ".wingman", "browser-extensions", "relay");
1584
+ mkdirSync(extensionDir, {
1585
+ recursive: true
1586
+ });
1587
+ writeFileSync(join(extensionDir, "manifest.json"), JSON.stringify({
1588
+ manifest_version: 3,
1589
+ name: "Relay Test Extension",
1590
+ version: "0.0.1"
1591
+ }));
1592
+ let capturedChromeArgs = [];
1593
+ const page = {
1594
+ goto: async ()=>{},
1595
+ click: async ()=>{},
1596
+ fill: async ()=>{},
1597
+ keyboard: {
1598
+ press: async ()=>{}
1599
+ },
1600
+ waitForTimeout: async ()=>{},
1601
+ textContent: async ()=>"",
1602
+ evaluate: async ()=>"ok",
1603
+ screenshot: async ()=>{},
1604
+ title: async ()=>"Config Workspace Extension",
1605
+ url: ()=>"about:blank"
1606
+ };
1607
+ const tool = createBrowserControlTool({
1608
+ workspace: executionWorkspace,
1609
+ configWorkspace,
1610
+ defaultExtensions: [
1611
+ "relay"
1612
+ ]
1613
+ }, {
1614
+ importPlaywright: async ()=>({
1615
+ chromium: {
1616
+ connectOverCDP: async ()=>({
1617
+ contexts: ()=>[
1618
+ {
1619
+ pages: ()=>[
1620
+ page
1621
+ ],
1622
+ newPage: async ()=>page
1623
+ }
1624
+ ],
1625
+ close: async ()=>{}
1626
+ })
1627
+ }
1628
+ }),
1629
+ startChrome: async (input)=>{
1630
+ capturedChromeArgs = input.chromeArgs || [];
1631
+ return {
1632
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1633
+ close: async ()=>{}
1634
+ };
1635
+ },
1636
+ mkTempDir: ()=>tempDir,
1637
+ removeDir: ()=>{},
1638
+ now: ()=>1700000000000
1639
+ });
1640
+ const result = await tool.invoke({
1641
+ actions: [
1642
+ {
1643
+ type: "evaluate",
1644
+ expression: "document.title"
1645
+ }
1646
+ ]
1647
+ });
1648
+ const parsed = JSON.parse(String(result));
1649
+ expect(parsed.extensions).toContain("relay");
1650
+ expect(capturedChromeArgs.some((arg)=>arg.includes(extensionDir))).toBe(true);
1651
+ });
1652
+ it("uses persistent named browser profile when configured", async ()=>{
1653
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1654
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1655
+ workspaces.push(workspace, tempDir);
1656
+ let capturedUserDataDir = "";
1657
+ let capturedHeadless = null;
1658
+ let capturedIgnoreDefaultArgs = [];
1659
+ let removedTempDir = false;
1660
+ const page = {
1661
+ goto: async ()=>{},
1662
+ click: async ()=>{},
1663
+ fill: async ()=>{},
1664
+ keyboard: {
1665
+ press: async ()=>{}
1666
+ },
1667
+ waitForTimeout: async ()=>{},
1668
+ textContent: async ()=>"",
1669
+ evaluate: async ()=>"ok",
1670
+ screenshot: async ()=>{},
1671
+ title: async ()=>"Profile Session",
1672
+ url: ()=>"about:blank"
1673
+ };
1674
+ const tool = createBrowserControlTool({
1675
+ workspace,
1676
+ browserProfile: "trading"
1677
+ }, {
1678
+ importPlaywright: async ()=>({
1679
+ chromium: {
1680
+ connectOverCDP: async ()=>{
1681
+ throw new Error("connectOverCDP should not be called");
1682
+ },
1683
+ launchPersistentContext: async (userDataDir, launchOptions)=>{
1684
+ capturedUserDataDir = userDataDir;
1685
+ capturedHeadless = launchOptions?.headless ?? null;
1686
+ capturedIgnoreDefaultArgs = launchOptions?.ignoreDefaultArgs || [];
1687
+ return {
1688
+ pages: ()=>[
1689
+ page
1690
+ ],
1691
+ newPage: async ()=>page,
1692
+ close: async ()=>{}
1693
+ };
1694
+ }
1695
+ }
1696
+ }),
1697
+ mkTempDir: ()=>tempDir,
1698
+ removeDir: ()=>{
1699
+ removedTempDir = true;
1700
+ },
1701
+ now: ()=>1700000000000
1702
+ });
1703
+ const result = await tool.invoke({
1704
+ actions: [
1705
+ {
1706
+ type: "evaluate",
1707
+ expression: "document.title"
1708
+ }
1709
+ ]
1710
+ });
1711
+ const parsed = JSON.parse(String(result));
1712
+ expect(parsed.persistentProfile).toBe(true);
1713
+ expect(parsed.profileId).toBe("trading");
1714
+ expect(parsed.profilePath).toBe(capturedUserDataDir);
1715
+ expect(parsed.transport).toBe("persistent-context");
1716
+ expect(parsed.mode).toBe("headed");
1717
+ expect(capturedHeadless).toBe(false);
1718
+ expect(capturedIgnoreDefaultArgs).toContain("--password-store=basic");
1719
+ expect(capturedIgnoreDefaultArgs).toContain("--use-mock-keychain");
1720
+ expect(capturedUserDataDir).toContain(".wingman/browser-profiles/trading");
1721
+ expect(removedTempDir).toBe(false);
1722
+ });
1723
+ it("honors headless requests for persistent browser profiles", async ()=>{
1724
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1725
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1726
+ workspaces.push(workspace, tempDir);
1727
+ let capturedHeadless = null;
1728
+ const page = {
1729
+ goto: async ()=>{},
1730
+ click: async ()=>{},
1731
+ fill: async ()=>{},
1732
+ keyboard: {
1733
+ press: async ()=>{}
1734
+ },
1735
+ waitForTimeout: async ()=>{},
1736
+ textContent: async ()=>"",
1737
+ evaluate: async ()=>"ok",
1738
+ screenshot: async ()=>{},
1739
+ title: async ()=>"Profile Session",
1740
+ url: ()=>"about:blank"
1741
+ };
1742
+ const tool = createBrowserControlTool({
1743
+ workspace,
1744
+ browserProfile: "trading"
1745
+ }, {
1746
+ importPlaywright: async ()=>({
1747
+ chromium: {
1748
+ connectOverCDP: async ()=>{
1749
+ throw new Error("connectOverCDP should not be called");
1750
+ },
1751
+ launchPersistentContext: async (_userDataDir, launchOptions)=>{
1752
+ capturedHeadless = launchOptions?.headless ?? null;
1753
+ return {
1754
+ pages: ()=>[
1755
+ page
1756
+ ],
1757
+ newPage: async ()=>page,
1758
+ close: async ()=>{}
1759
+ };
1760
+ }
1761
+ }
1762
+ }),
1763
+ mkTempDir: ()=>tempDir,
1764
+ removeDir: ()=>{},
1765
+ now: ()=>1700000000000
1766
+ });
1767
+ const result = await tool.invoke({
1768
+ headless: true,
1769
+ actions: [
1770
+ {
1771
+ type: "evaluate",
1772
+ expression: "document.title"
1773
+ }
1774
+ ]
1775
+ });
1776
+ const parsed = JSON.parse(String(result));
1777
+ expect(parsed.mode).toBe("headless");
1778
+ expect(capturedHeadless).toBe(true);
1779
+ });
1780
+ it("retries CDP once for persistent profiles without switching transport", async ()=>{
1781
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1782
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1783
+ workspaces.push(workspace, tempDir);
1784
+ let startChromeCalls = 0;
1785
+ let connectCalls = 0;
1786
+ let persistentLaunchCalled = false;
1787
+ const page = {
1788
+ goto: async ()=>{},
1789
+ click: async ()=>{},
1790
+ fill: async ()=>{},
1791
+ keyboard: {
1792
+ press: async ()=>{}
1793
+ },
1794
+ waitForTimeout: async ()=>{},
1795
+ textContent: async ()=>"",
1796
+ evaluate: async ()=>"ok",
1797
+ screenshot: async ()=>{},
1798
+ title: async ()=>"Profile Retry",
1799
+ url: ()=>"about:blank"
1800
+ };
1801
+ const tool = createBrowserControlTool({
1802
+ workspace,
1803
+ browserProfile: "trading",
1804
+ preferPersistentLaunch: false
1805
+ }, {
1806
+ importPlaywright: async ()=>({
1807
+ chromium: {
1808
+ connectOverCDP: async ()=>{
1809
+ connectCalls += 1;
1810
+ if (1 === connectCalls) throw new Error("overCDP: WebSocket error: ECONNREFUSED");
1811
+ return {
1812
+ contexts: ()=>[
1813
+ {
1814
+ pages: ()=>[
1815
+ page
1816
+ ],
1817
+ newPage: async ()=>page
1818
+ }
1819
+ ],
1820
+ close: async ()=>{}
1821
+ };
1822
+ },
1823
+ launchPersistentContext: async ()=>{
1824
+ persistentLaunchCalled = true;
1825
+ return {
1826
+ pages: ()=>[
1827
+ page
1828
+ ],
1829
+ newPage: async ()=>page
1830
+ };
1831
+ }
1832
+ }
1833
+ }),
1834
+ startChrome: async ()=>{
1835
+ startChromeCalls += 1;
1836
+ return {
1837
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1838
+ close: async ()=>{}
1839
+ };
1840
+ },
1841
+ mkTempDir: ()=>tempDir,
1842
+ removeDir: ()=>{},
1843
+ now: ()=>1700000000000
1844
+ });
1845
+ const result = await tool.invoke({
1846
+ actions: [
1847
+ {
1848
+ type: "evaluate",
1849
+ expression: "document.title"
1850
+ }
1851
+ ]
1852
+ });
1853
+ const parsed = JSON.parse(String(result));
1854
+ expect(parsed.persistentProfile).toBe(true);
1855
+ expect(parsed.transport).toBe("cdp");
1856
+ expect(connectCalls).toBe(2);
1857
+ expect(startChromeCalls).toBe(2);
1858
+ expect(persistentLaunchCalled).toBe(false);
1859
+ });
1860
+ it("falls back to persistent launch when persistent-profile CDP retry fails", async ()=>{
1861
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1862
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1863
+ workspaces.push(workspace, tempDir);
1864
+ let startChromeCalls = 0;
1865
+ let connectCalls = 0;
1866
+ let persistentLaunchCalled = false;
1867
+ const page = {
1868
+ goto: async ()=>{},
1869
+ click: async ()=>{},
1870
+ fill: async ()=>{},
1871
+ keyboard: {
1872
+ press: async ()=>{}
1873
+ },
1874
+ waitForTimeout: async ()=>{},
1875
+ textContent: async ()=>"",
1876
+ evaluate: async ()=>"ok",
1877
+ screenshot: async ()=>{},
1878
+ title: async ()=>"Profile Fallback",
1879
+ url: ()=>"about:blank"
1880
+ };
1881
+ const tool = createBrowserControlTool({
1882
+ workspace,
1883
+ browserProfile: "trading",
1884
+ preferPersistentLaunch: false
1885
+ }, {
1886
+ importPlaywright: async ()=>({
1887
+ chromium: {
1888
+ connectOverCDP: async ()=>{
1889
+ connectCalls += 1;
1890
+ throw new Error("overCDP: WebSocket error: ECONNREFUSED");
1891
+ },
1892
+ launchPersistentContext: async ()=>{
1893
+ persistentLaunchCalled = true;
1894
+ return {
1895
+ pages: ()=>[
1896
+ page
1897
+ ],
1898
+ newPage: async ()=>page
1899
+ };
1900
+ }
1901
+ }
1902
+ }),
1903
+ startChrome: async ()=>{
1904
+ startChromeCalls += 1;
1905
+ return {
1906
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
1907
+ close: async ()=>{}
1908
+ };
1909
+ },
1910
+ mkTempDir: ()=>tempDir,
1911
+ removeDir: ()=>{},
1912
+ now: ()=>1700000000000
1913
+ });
1914
+ const result = await tool.invoke({
1915
+ actions: [
1916
+ {
1917
+ type: "evaluate",
1918
+ expression: "document.title"
1919
+ }
1920
+ ]
1921
+ });
1922
+ const parsed = JSON.parse(String(result));
1923
+ expect(parsed.persistentProfile).toBe(true);
1924
+ expect(parsed.transport).toBe("persistent-context");
1925
+ expect(connectCalls).toBe(2);
1926
+ expect(startChromeCalls).toBe(2);
1927
+ expect(persistentLaunchCalled).toBe(true);
1928
+ });
1929
+ it("resolves persistent profile paths relative to config workspace", async ()=>{
1930
+ const executionWorkspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1931
+ const configWorkspace = mkdtempSync(join(tmpdir(), "wingman-browser-config-workspace-"));
1932
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1933
+ workspaces.push(executionWorkspace, configWorkspace, tempDir);
1934
+ let capturedUserDataDir = "";
1935
+ const page = {
1936
+ goto: async ()=>{},
1937
+ click: async ()=>{},
1938
+ fill: async ()=>{},
1939
+ keyboard: {
1940
+ press: async ()=>{}
1941
+ },
1942
+ waitForTimeout: async ()=>{},
1943
+ textContent: async ()=>"",
1944
+ evaluate: async ()=>"ok",
1945
+ screenshot: async ()=>{},
1946
+ title: async ()=>"Config Workspace Profile",
1947
+ url: ()=>"about:blank"
1948
+ };
1949
+ const tool = createBrowserControlTool({
1950
+ workspace: executionWorkspace,
1951
+ configWorkspace,
1952
+ browserProfile: "trading"
1953
+ }, {
1954
+ importPlaywright: async ()=>({
1955
+ chromium: {
1956
+ connectOverCDP: async ()=>{
1957
+ throw new Error("connectOverCDP should not be called");
1958
+ },
1959
+ launchPersistentContext: async (userDataDir)=>{
1960
+ capturedUserDataDir = userDataDir;
1961
+ return {
1962
+ pages: ()=>[
1963
+ page
1964
+ ],
1965
+ newPage: async ()=>page,
1966
+ close: async ()=>{}
1967
+ };
1968
+ }
1969
+ }
1970
+ }),
1971
+ mkTempDir: ()=>tempDir,
1972
+ removeDir: ()=>{},
1973
+ now: ()=>1700000000000
1974
+ });
1975
+ const result = await tool.invoke({
1976
+ actions: [
1977
+ {
1978
+ type: "evaluate",
1979
+ expression: "document.title"
1980
+ }
1981
+ ]
1982
+ });
1983
+ const parsed = JSON.parse(String(result));
1984
+ expect(parsed.persistentProfile).toBe(true);
1985
+ expect(parsed.configWorkspace).toBe(configWorkspace);
1986
+ expect(parsed.executionWorkspace).toBe(executionWorkspace);
1987
+ expect(parsed.profilePath).toBe(capturedUserDataDir);
1988
+ expect(capturedUserDataDir).toBe(join(configWorkspace, ".wingman", "browser-profiles", "trading"));
1989
+ expect(existsSync(capturedUserDataDir)).toBe(true);
1990
+ expect(existsSync(join(executionWorkspace, ".wingman", "browser-profiles", "trading"))).toBe(false);
1991
+ });
1992
+ it("rejects concurrent runs when profile lock belongs to an active process", async ()=>{
1993
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
1994
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
1995
+ workspaces.push(workspace, tempDir);
1996
+ const profileDir = join(workspace, ".wingman", "browser-profiles", "trading");
1997
+ mkdirSync(profileDir, {
1998
+ recursive: true
1999
+ });
2000
+ const blocker = spawn("sleep", [
2001
+ "30"
2002
+ ], {
2003
+ stdio: "ignore"
2004
+ });
2005
+ try {
2006
+ writeFileSync(join(profileDir, ".wingman-browser.lock"), JSON.stringify({
2007
+ pid: blocker.pid
2008
+ }));
2009
+ let startChromeCalled = false;
2010
+ const tool = createBrowserControlTool({
2011
+ workspace,
2012
+ browserProfile: "trading"
2013
+ }, {
2014
+ importPlaywright: async ()=>({
2015
+ chromium: {
2016
+ connectOverCDP: async ()=>({
2017
+ contexts: ()=>[],
2018
+ close: async ()=>{}
2019
+ })
2020
+ }
2021
+ }),
2022
+ startChrome: async ()=>{
2023
+ startChromeCalled = true;
2024
+ return {
2025
+ wsEndpoint: "ws://127.0.0.1:1234/devtools/browser/test",
2026
+ close: async ()=>{}
2027
+ };
2028
+ },
2029
+ mkTempDir: ()=>tempDir,
2030
+ removeDir: ()=>{},
2031
+ now: ()=>1700000000000
2032
+ });
2033
+ const result = await tool.invoke({
2034
+ actions: [
2035
+ {
2036
+ type: "wait",
2037
+ ms: 10
2038
+ }
2039
+ ]
2040
+ });
2041
+ expect(startChromeCalled).toBe(false);
2042
+ expect(String(result)).toContain("already in use");
2043
+ } finally{
2044
+ try {
2045
+ blocker.kill("SIGKILL");
2046
+ } catch {}
2047
+ }
2048
+ });
2049
+ it("recovers from stale profile lock when lock PID is no longer alive", async ()=>{
2050
+ const workspace = mkdtempSync(join(tmpdir(), "wingman-browser-workspace-"));
2051
+ const tempDir = mkdtempSync(join(tmpdir(), "wingman-browser-temp-"));
2052
+ workspaces.push(workspace, tempDir);
2053
+ const profileDir = join(workspace, ".wingman", "browser-profiles", "trading");
2054
+ mkdirSync(profileDir, {
2055
+ recursive: true
2056
+ });
2057
+ writeFileSync(join(profileDir, ".wingman-browser.lock"), JSON.stringify({
2058
+ pid: 999999,
2059
+ createdAt: "2026-01-01T00:00:00.000Z"
2060
+ }));
2061
+ let startChromeCalled = false;
2062
+ const page = {
2063
+ goto: async ()=>{},
2064
+ click: async ()=>{},
2065
+ fill: async ()=>{},
2066
+ keyboard: {
2067
+ press: async ()=>{}
2068
+ },
2069
+ waitForTimeout: async ()=>{},
2070
+ textContent: async ()=>"",
2071
+ evaluate: async ()=>"ok",
2072
+ screenshot: async ()=>{},
2073
+ title: async ()=>"Recovered",
2074
+ url: ()=>"about:blank"
2075
+ };
2076
+ const tool = createBrowserControlTool({
2077
+ workspace,
2078
+ browserProfile: "trading"
2079
+ }, {
2080
+ importPlaywright: async ()=>({
2081
+ chromium: {
2082
+ connectOverCDP: async ()=>{
2083
+ throw new Error("connectOverCDP should not be called");
2084
+ },
2085
+ launchPersistentContext: async ()=>({
2086
+ pages: ()=>[
2087
+ page
2088
+ ],
2089
+ newPage: async ()=>page,
2090
+ close: async ()=>{}
2091
+ })
2092
+ }
2093
+ }),
2094
+ mkTempDir: ()=>tempDir,
2095
+ removeDir: ()=>{},
2096
+ now: ()=>1700000000000
2097
+ });
2098
+ const result = await tool.invoke({
2099
+ actions: [
2100
+ {
2101
+ type: "evaluate",
2102
+ expression: "document.title"
2103
+ }
2104
+ ]
2105
+ });
2106
+ const parsed = JSON.parse(String(result));
2107
+ expect(startChromeCalled).toBe(false);
2108
+ expect(parsed.persistentProfile).toBe(true);
2109
+ expect(String(result)).not.toContain("already in use");
2110
+ });
2111
+ });