opengstack 0.13.7 → 0.13.9

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 (135) hide show
  1. package/bin/opengstack.js +35 -90
  2. package/package.json +2 -3
  3. package/scripts/install-skills.js +47 -58
  4. package/skills/browse/bin/find-browse +21 -0
  5. package/skills/browse/bin/remote-slug +14 -0
  6. package/skills/browse/scripts/build-node-server.sh +48 -0
  7. package/skills/browse/src/activity.ts +208 -0
  8. package/skills/browse/src/browser-manager.ts +959 -0
  9. package/skills/browse/src/buffers.ts +137 -0
  10. package/skills/browse/src/bun-polyfill.cjs +109 -0
  11. package/skills/browse/src/cli.ts +678 -0
  12. package/skills/browse/src/commands.ts +128 -0
  13. package/skills/browse/src/config.ts +150 -0
  14. package/skills/browse/src/cookie-import-browser.ts +625 -0
  15. package/skills/browse/src/cookie-picker-routes.ts +230 -0
  16. package/skills/browse/src/cookie-picker-ui.ts +688 -0
  17. package/skills/browse/src/find-browse.ts +61 -0
  18. package/skills/browse/src/meta-commands.ts +550 -0
  19. package/skills/browse/src/platform.ts +17 -0
  20. package/skills/browse/src/read-commands.ts +358 -0
  21. package/skills/browse/src/server.ts +1192 -0
  22. package/skills/browse/src/sidebar-agent.ts +280 -0
  23. package/skills/browse/src/sidebar-utils.ts +21 -0
  24. package/skills/browse/src/snapshot.ts +407 -0
  25. package/skills/browse/src/url-validation.ts +95 -0
  26. package/skills/browse/src/write-commands.ts +364 -0
  27. package/skills/browse/test/activity.test.ts +120 -0
  28. package/skills/browse/test/adversarial-security.test.ts +32 -0
  29. package/skills/browse/test/browser-manager-unit.test.ts +17 -0
  30. package/skills/browse/test/bun-polyfill.test.ts +72 -0
  31. package/skills/browse/test/commands.test.ts +2075 -0
  32. package/skills/browse/test/compare-board.test.ts +342 -0
  33. package/skills/browse/test/config.test.ts +316 -0
  34. package/skills/browse/test/cookie-import-browser.test.ts +519 -0
  35. package/skills/browse/test/cookie-picker-routes.test.ts +260 -0
  36. package/skills/browse/test/file-drop.test.ts +271 -0
  37. package/skills/browse/test/find-browse.test.ts +50 -0
  38. package/skills/browse/test/findport.test.ts +191 -0
  39. package/skills/browse/test/fixtures/basic.html +33 -0
  40. package/skills/browse/test/fixtures/cursor-interactive.html +22 -0
  41. package/skills/browse/test/fixtures/dialog.html +15 -0
  42. package/skills/browse/test/fixtures/empty.html +2 -0
  43. package/skills/browse/test/fixtures/forms.html +55 -0
  44. package/skills/browse/test/fixtures/iframe.html +30 -0
  45. package/skills/browse/test/fixtures/network-idle.html +30 -0
  46. package/skills/browse/test/fixtures/qa-eval-checkout.html +108 -0
  47. package/skills/browse/test/fixtures/qa-eval-spa.html +98 -0
  48. package/skills/browse/test/fixtures/qa-eval.html +51 -0
  49. package/skills/browse/test/fixtures/responsive.html +49 -0
  50. package/skills/browse/test/fixtures/snapshot.html +55 -0
  51. package/skills/browse/test/fixtures/spa.html +24 -0
  52. package/skills/browse/test/fixtures/states.html +17 -0
  53. package/skills/browse/test/fixtures/upload.html +25 -0
  54. package/skills/browse/test/gstack-config.test.ts +138 -0
  55. package/skills/browse/test/gstack-update-check.test.ts +514 -0
  56. package/skills/browse/test/handoff.test.ts +235 -0
  57. package/skills/browse/test/path-validation.test.ts +91 -0
  58. package/skills/browse/test/platform.test.ts +37 -0
  59. package/skills/browse/test/server-auth.test.ts +65 -0
  60. package/skills/browse/test/sidebar-agent-roundtrip.test.ts +226 -0
  61. package/skills/browse/test/sidebar-agent.test.ts +199 -0
  62. package/skills/browse/test/sidebar-integration.test.ts +320 -0
  63. package/skills/browse/test/sidebar-unit.test.ts +96 -0
  64. package/skills/browse/test/snapshot.test.ts +467 -0
  65. package/skills/browse/test/state-ttl.test.ts +35 -0
  66. package/skills/browse/test/test-server.ts +57 -0
  67. package/skills/browse/test/url-validation.test.ts +72 -0
  68. package/skills/browse/test/watch.test.ts +129 -0
  69. package/skills/careful/bin/check-careful.sh +112 -0
  70. package/skills/cso/ACKNOWLEDGEMENTS.md +14 -0
  71. package/skills/freeze/bin/check-freeze.sh +79 -0
  72. package/skills/qa/references/issue-taxonomy.md +85 -0
  73. package/skills/qa/templates/qa-report-template.md +126 -0
  74. package/skills/review/TODOS-format.md +62 -0
  75. package/skills/review/checklist.md +220 -0
  76. package/skills/review/design-checklist.md +132 -0
  77. package/skills/review/greptile-triage.md +220 -0
  78. /package/{autoplan → skills/autoplan}/SKILL.md +0 -0
  79. /package/{autoplan → skills/autoplan}/SKILL.md.tmpl +0 -0
  80. /package/{benchmark → skills/benchmark}/SKILL.md +0 -0
  81. /package/{benchmark → skills/benchmark}/SKILL.md.tmpl +0 -0
  82. /package/{browse → skills/browse}/SKILL.md +0 -0
  83. /package/{browse → skills/browse}/SKILL.md.tmpl +0 -0
  84. /package/{canary → skills/canary}/SKILL.md +0 -0
  85. /package/{canary → skills/canary}/SKILL.md.tmpl +0 -0
  86. /package/{careful → skills/careful}/SKILL.md +0 -0
  87. /package/{careful → skills/careful}/SKILL.md.tmpl +0 -0
  88. /package/{codex → skills/codex}/SKILL.md +0 -0
  89. /package/{codex → skills/codex}/SKILL.md.tmpl +0 -0
  90. /package/{connect-chrome → skills/connect-chrome}/SKILL.md +0 -0
  91. /package/{connect-chrome → skills/connect-chrome}/SKILL.md.tmpl +0 -0
  92. /package/{cso → skills/cso}/SKILL.md +0 -0
  93. /package/{cso → skills/cso}/SKILL.md.tmpl +0 -0
  94. /package/{design-consultation → skills/design-consultation}/SKILL.md +0 -0
  95. /package/{design-consultation → skills/design-consultation}/SKILL.md.tmpl +0 -0
  96. /package/{design-review → skills/design-review}/SKILL.md +0 -0
  97. /package/{design-review → skills/design-review}/SKILL.md.tmpl +0 -0
  98. /package/{design-shotgun → skills/design-shotgun}/SKILL.md +0 -0
  99. /package/{design-shotgun → skills/design-shotgun}/SKILL.md.tmpl +0 -0
  100. /package/{document-release → skills/document-release}/SKILL.md +0 -0
  101. /package/{document-release → skills/document-release}/SKILL.md.tmpl +0 -0
  102. /package/{freeze → skills/freeze}/SKILL.md +0 -0
  103. /package/{freeze → skills/freeze}/SKILL.md.tmpl +0 -0
  104. /package/{gstack-upgrade → skills/gstack-upgrade}/SKILL.md +0 -0
  105. /package/{gstack-upgrade → skills/gstack-upgrade}/SKILL.md.tmpl +0 -0
  106. /package/{guard → skills/guard}/SKILL.md +0 -0
  107. /package/{guard → skills/guard}/SKILL.md.tmpl +0 -0
  108. /package/{investigate → skills/investigate}/SKILL.md +0 -0
  109. /package/{investigate → skills/investigate}/SKILL.md.tmpl +0 -0
  110. /package/{land-and-deploy → skills/land-and-deploy}/SKILL.md +0 -0
  111. /package/{land-and-deploy → skills/land-and-deploy}/SKILL.md.tmpl +0 -0
  112. /package/{office-hours → skills/office-hours}/SKILL.md +0 -0
  113. /package/{office-hours → skills/office-hours}/SKILL.md.tmpl +0 -0
  114. /package/{plan-ceo-review → skills/plan-ceo-review}/SKILL.md +0 -0
  115. /package/{plan-ceo-review → skills/plan-ceo-review}/SKILL.md.tmpl +0 -0
  116. /package/{plan-design-review → skills/plan-design-review}/SKILL.md +0 -0
  117. /package/{plan-design-review → skills/plan-design-review}/SKILL.md.tmpl +0 -0
  118. /package/{plan-eng-review → skills/plan-eng-review}/SKILL.md +0 -0
  119. /package/{plan-eng-review → skills/plan-eng-review}/SKILL.md.tmpl +0 -0
  120. /package/{qa → skills/qa}/SKILL.md +0 -0
  121. /package/{qa → skills/qa}/SKILL.md.tmpl +0 -0
  122. /package/{qa-only → skills/qa-only}/SKILL.md +0 -0
  123. /package/{qa-only → skills/qa-only}/SKILL.md.tmpl +0 -0
  124. /package/{retro → skills/retro}/SKILL.md +0 -0
  125. /package/{retro → skills/retro}/SKILL.md.tmpl +0 -0
  126. /package/{review → skills/review}/SKILL.md +0 -0
  127. /package/{review → skills/review}/SKILL.md.tmpl +0 -0
  128. /package/{setup-browser-cookies → skills/setup-browser-cookies}/SKILL.md +0 -0
  129. /package/{setup-browser-cookies → skills/setup-browser-cookies}/SKILL.md.tmpl +0 -0
  130. /package/{setup-deploy → skills/setup-deploy}/SKILL.md +0 -0
  131. /package/{setup-deploy → skills/setup-deploy}/SKILL.md.tmpl +0 -0
  132. /package/{ship → skills/ship}/SKILL.md +0 -0
  133. /package/{ship → skills/ship}/SKILL.md.tmpl +0 -0
  134. /package/{unfreeze → skills/unfreeze}/SKILL.md +0 -0
  135. /package/{unfreeze → skills/unfreeze}/SKILL.md.tmpl +0 -0
@@ -0,0 +1,2075 @@
1
+ /**
2
+ * Integration tests for all browse commands
3
+ *
4
+ * Tests run against a local test server serving fixture HTML files.
5
+ * A real browse server is started and commands are sent via the CLI HTTP interface.
6
+ */
7
+
8
+ import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
9
+ import { startTestServer } from './test-server';
10
+ import { BrowserManager } from '../src/browser-manager';
11
+ import { resolveServerScript } from '../src/cli';
12
+ import { handleReadCommand } from '../src/read-commands';
13
+ import { handleWriteCommand } from '../src/write-commands';
14
+ import { handleMetaCommand } from '../src/meta-commands';
15
+ import { consoleBuffer, networkBuffer, dialogBuffer, addConsoleEntry, addNetworkEntry, addDialogEntry, CircularBuffer } from '../src/buffers';
16
+ import * as fs from 'fs';
17
+ import { spawn } from 'child_process';
18
+ import * as path from 'path';
19
+
20
+ let testServer: ReturnType<typeof startTestServer>;
21
+ let bm: BrowserManager;
22
+ let baseUrl: string;
23
+
24
+ beforeAll(async () => {
25
+ testServer = startTestServer(0);
26
+ baseUrl = testServer.url;
27
+
28
+ bm = new BrowserManager();
29
+ await bm.launch();
30
+ });
31
+
32
+ afterAll(() => {
33
+ // Force kill browser instead of graceful close (avoids hang)
34
+ try { testServer.server.stop(); } catch {}
35
+ // bm.close() can hang — just let process exit handle it
36
+ setTimeout(() => process.exit(0), 500);
37
+ });
38
+
39
+ // ─── Navigation ─────────────────────────────────────────────────
40
+
41
+ describe('Navigation', () => {
42
+ test('goto navigates to URL', async () => {
43
+ const result = await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
44
+ expect(result).toContain('Navigated to');
45
+ expect(result).toContain('200');
46
+ });
47
+
48
+ test('url returns current URL', async () => {
49
+ const result = await handleMetaCommand('url', [], bm, async () => {});
50
+ expect(result).toContain('/basic.html');
51
+ });
52
+
53
+ test('back goes back', async () => {
54
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
55
+ const result = await handleWriteCommand('back', [], bm);
56
+ expect(result).toContain('Back');
57
+ });
58
+
59
+ test('forward goes forward', async () => {
60
+ const result = await handleWriteCommand('forward', [], bm);
61
+ expect(result).toContain('Forward');
62
+ });
63
+
64
+ test('reload reloads page', async () => {
65
+ const result = await handleWriteCommand('reload', [], bm);
66
+ expect(result).toContain('Reloaded');
67
+ });
68
+ });
69
+
70
+ // ─── Content Extraction ─────────────────────────────────────────
71
+
72
+ describe('Content extraction', () => {
73
+ beforeAll(async () => {
74
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
75
+ });
76
+
77
+ test('text returns cleaned page text', async () => {
78
+ const result = await handleReadCommand('text', [], bm);
79
+ expect(result).toContain('Hello World');
80
+ expect(result).toContain('Item one');
81
+ expect(result).not.toContain('<h1>');
82
+ });
83
+
84
+ test('html returns full page HTML', async () => {
85
+ const result = await handleReadCommand('html', [], bm);
86
+ expect(result).toContain('<!DOCTYPE html>');
87
+ expect(result).toContain('<h1 id="title">Hello World</h1>');
88
+ });
89
+
90
+ test('html with selector returns element innerHTML', async () => {
91
+ const result = await handleReadCommand('html', ['#content'], bm);
92
+ expect(result).toContain('Some body text here.');
93
+ expect(result).toContain('<li>Item one</li>');
94
+ });
95
+
96
+ test('links returns all links', async () => {
97
+ const result = await handleReadCommand('links', [], bm);
98
+ expect(result).toContain('Page 1');
99
+ expect(result).toContain('Page 2');
100
+ expect(result).toContain('External');
101
+ expect(result).toContain('→');
102
+ });
103
+
104
+ test('forms discovers form fields', async () => {
105
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
106
+ const result = await handleReadCommand('forms', [], bm);
107
+ const forms = JSON.parse(result);
108
+ expect(forms.length).toBe(2);
109
+ expect(forms[0].id).toBe('login-form');
110
+ expect(forms[0].method).toBe('post');
111
+ expect(forms[0].fields.length).toBeGreaterThanOrEqual(2);
112
+ expect(forms[1].id).toBe('profile-form');
113
+
114
+ // Check field discovery
115
+ const emailField = forms[0].fields.find((f: any) => f.name === 'email');
116
+ expect(emailField).toBeDefined();
117
+ expect(emailField.type).toBe('email');
118
+ expect(emailField.required).toBe(true);
119
+ });
120
+
121
+ test('accessibility returns ARIA tree', async () => {
122
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
123
+ const result = await handleReadCommand('accessibility', [], bm);
124
+ expect(result).toContain('Hello World');
125
+ });
126
+ });
127
+
128
+ // ─── JavaScript / CSS / Attrs ───────────────────────────────────
129
+
130
+ describe('Inspection', () => {
131
+ beforeAll(async () => {
132
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
133
+ });
134
+
135
+ test('js evaluates expression', async () => {
136
+ const result = await handleReadCommand('js', ['document.title'], bm);
137
+ expect(result).toBe('Test Page - Basic');
138
+ });
139
+
140
+ test('js returns objects as JSON', async () => {
141
+ const result = await handleReadCommand('js', ['({a: 1, b: 2})'], bm);
142
+ const obj = JSON.parse(result);
143
+ expect(obj.a).toBe(1);
144
+ expect(obj.b).toBe(2);
145
+ });
146
+
147
+ test('js supports await expressions', async () => {
148
+ const result = await handleReadCommand('js', ['await Promise.resolve(42)'], bm);
149
+ expect(result).toBe('42');
150
+ });
151
+
152
+ test('js does not false-positive on await substring', async () => {
153
+ const result = await handleReadCommand('js', ['(() => { const awaitable = 5; return awaitable })()'], bm);
154
+ expect(result).toBe('5');
155
+ });
156
+
157
+ test('eval supports await in single-line file', async () => {
158
+ const tmp = '/tmp/eval-await-test.js';
159
+ fs.writeFileSync(tmp, 'await Promise.resolve("hello from eval")');
160
+ try {
161
+ const result = await handleReadCommand('eval', [tmp], bm);
162
+ expect(result).toBe('hello from eval');
163
+ } finally {
164
+ fs.unlinkSync(tmp);
165
+ }
166
+ });
167
+
168
+ test('eval does not wrap when await is only in a comment', async () => {
169
+ const tmp = '/tmp/eval-comment-test.js';
170
+ fs.writeFileSync(tmp, '// no need to await this\ndocument.title');
171
+ try {
172
+ const result = await handleReadCommand('eval', [tmp], bm);
173
+ expect(result).toBe('Test Page - Basic');
174
+ } finally {
175
+ fs.unlinkSync(tmp);
176
+ }
177
+ });
178
+
179
+ test('eval multi-line with await and explicit return', async () => {
180
+ const tmp = '/tmp/eval-multiline-await.js';
181
+ fs.writeFileSync(tmp, 'const data = await Promise.resolve("multi");\nreturn data;');
182
+ try {
183
+ const result = await handleReadCommand('eval', [tmp], bm);
184
+ expect(result).toBe('multi');
185
+ } finally {
186
+ fs.unlinkSync(tmp);
187
+ }
188
+ });
189
+
190
+ test('eval multi-line with await but no return gives empty string', async () => {
191
+ const tmp = '/tmp/eval-multiline-no-return.js';
192
+ fs.writeFileSync(tmp, 'const data = await Promise.resolve("lost");\ndata;');
193
+ try {
194
+ const result = await handleReadCommand('eval', [tmp], bm);
195
+ expect(result).toBe('');
196
+ } finally {
197
+ fs.unlinkSync(tmp);
198
+ }
199
+ });
200
+
201
+ test('js handles multi-line with await', async () => {
202
+ const code = 'const x = await Promise.resolve(42);\nreturn x;';
203
+ const result = await handleReadCommand('js', [code], bm);
204
+ expect(result).toBe('42');
205
+ });
206
+
207
+ test('js handles await with semicolons', async () => {
208
+ const result = await handleReadCommand('js', ['const x = await Promise.resolve(5); return x + 1;'], bm);
209
+ expect(result).toBe('6');
210
+ });
211
+
212
+ test('js handles await with statement keywords', async () => {
213
+ const result = await handleReadCommand('js', ['const res = await Promise.resolve("ok"); return res;'], bm);
214
+ expect(result).toBe('ok');
215
+ });
216
+
217
+ test('js still works for simple expressions', async () => {
218
+ const result = await handleReadCommand('js', ['1 + 2'], bm);
219
+ expect(result).toBe('3');
220
+ });
221
+
222
+ test('css returns computed property', async () => {
223
+ const result = await handleReadCommand('css', ['h1', 'color'], bm);
224
+ // Navy color
225
+ expect(result).toContain('0, 0, 128');
226
+ });
227
+
228
+ test('css returns font-family', async () => {
229
+ const result = await handleReadCommand('css', ['body', 'font-family'], bm);
230
+ expect(result).toContain('Helvetica');
231
+ });
232
+
233
+ test('attrs returns element attributes', async () => {
234
+ const result = await handleReadCommand('attrs', ['#content'], bm);
235
+ const attrs = JSON.parse(result);
236
+ expect(attrs.id).toBe('content');
237
+ expect(attrs['data-testid']).toBe('main-content');
238
+ expect(attrs['data-version']).toBe('1.0');
239
+ });
240
+ });
241
+
242
+ // ─── Interaction ────────────────────────────────────────────────
243
+
244
+ describe('Interaction', () => {
245
+ test('fill + click works on form', async () => {
246
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
247
+
248
+ let result = await handleWriteCommand('fill', ['#email', 'test@example.com'], bm);
249
+ expect(result).toContain('Filled');
250
+
251
+ result = await handleWriteCommand('fill', ['#password', 'secret123'], bm);
252
+ expect(result).toContain('Filled');
253
+
254
+ // Verify values were set
255
+ const emailVal = await handleReadCommand('js', ['document.querySelector("#email").value'], bm);
256
+ expect(emailVal).toBe('test@example.com');
257
+
258
+ result = await handleWriteCommand('click', ['#login-btn'], bm);
259
+ expect(result).toContain('Clicked');
260
+ });
261
+
262
+ test('select works on dropdown', async () => {
263
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
264
+ const result = await handleWriteCommand('select', ['#role', 'admin'], bm);
265
+ expect(result).toContain('Selected');
266
+
267
+ const val = await handleReadCommand('js', ['document.querySelector("#role").value'], bm);
268
+ expect(val).toBe('admin');
269
+ });
270
+
271
+ test('click on option ref auto-routes to selectOption', async () => {
272
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
273
+ // Reset select to default
274
+ await handleReadCommand('js', ['document.querySelector("#role").value = ""'], bm);
275
+ const snap = await handleMetaCommand('snapshot', [], bm, async () => {});
276
+ // Find an option ref (e.g., "Admin" option)
277
+ const optionLine = snap.split('\n').find((l: string) => l.includes('[option]') && l.includes('"Admin"'));
278
+ expect(optionLine).toBeDefined();
279
+ const refMatch = optionLine!.match(/@(e\d+)/);
280
+ expect(refMatch).toBeDefined();
281
+ const ref = `@${refMatch![1]}`;
282
+ const result = await handleWriteCommand('click', [ref], bm);
283
+ expect(result).toContain('auto-routed');
284
+ expect(result).toContain('Selected');
285
+ // Verify the select value actually changed
286
+ const val = await handleReadCommand('js', ['document.querySelector("#role").value'], bm);
287
+ expect(val).toBe('admin');
288
+ });
289
+
290
+ test('click CSS selector on option gives helpful error', async () => {
291
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
292
+ try {
293
+ await handleWriteCommand('click', ['option[value="admin"]'], bm);
294
+ expect(true).toBe(false); // Should not reach here
295
+ } catch (err: any) {
296
+ expect(err.message).toContain('select');
297
+ expect(err.message).toContain('option');
298
+ }
299
+ }, 15000);
300
+
301
+ test('hover works', async () => {
302
+ const result = await handleWriteCommand('hover', ['h1'], bm);
303
+ expect(result).toContain('Hovered');
304
+ });
305
+
306
+ test('wait finds existing element', async () => {
307
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
308
+ const result = await handleWriteCommand('wait', ['#title'], bm);
309
+ expect(result).toContain('appeared');
310
+ });
311
+
312
+ test('scroll works', async () => {
313
+ const result = await handleWriteCommand('scroll', ['footer'], bm);
314
+ expect(result).toContain('Scrolled');
315
+ });
316
+
317
+ test('viewport changes size', async () => {
318
+ const result = await handleWriteCommand('viewport', ['375x812'], bm);
319
+ expect(result).toContain('Viewport set');
320
+
321
+ const size = await handleReadCommand('js', ['`${window.innerWidth}x${window.innerHeight}`'], bm);
322
+ expect(size).toBe('375x812');
323
+
324
+ // Reset
325
+ await handleWriteCommand('viewport', ['1280x720'], bm);
326
+ });
327
+
328
+ test('type and press work', async () => {
329
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
330
+ await handleWriteCommand('click', ['#name'], bm);
331
+
332
+ const result = await handleWriteCommand('type', ['John Doe'], bm);
333
+ expect(result).toContain('Typed');
334
+
335
+ const val = await handleReadCommand('js', ['document.querySelector("#name").value'], bm);
336
+ expect(val).toBe('John Doe');
337
+ });
338
+ });
339
+
340
+ // ─── SPA / Console / Network ───────────────────────────────────
341
+
342
+ describe('SPA and buffers', () => {
343
+ test('wait handles delayed rendering', async () => {
344
+ await handleWriteCommand('goto', [baseUrl + '/spa.html'], bm);
345
+ const result = await handleWriteCommand('wait', ['.loaded'], bm);
346
+ expect(result).toContain('appeared');
347
+
348
+ const text = await handleReadCommand('text', [], bm);
349
+ expect(text).toContain('SPA Content Loaded');
350
+ });
351
+
352
+ test('console captures messages', async () => {
353
+ const result = await handleReadCommand('console', [], bm);
354
+ expect(result).toContain('[SPA] Starting render');
355
+ expect(result).toContain('[SPA] Render complete');
356
+ });
357
+
358
+ test('console --clear clears buffer', async () => {
359
+ const result = await handleReadCommand('console', ['--clear'], bm);
360
+ expect(result).toContain('cleared');
361
+
362
+ const after = await handleReadCommand('console', [], bm);
363
+ expect(after).toContain('no console messages');
364
+ });
365
+
366
+ test('network captures requests', async () => {
367
+ const result = await handleReadCommand('network', [], bm);
368
+ expect(result).toContain('GET');
369
+ expect(result).toContain('/spa.html');
370
+ });
371
+
372
+ test('network --clear clears buffer', async () => {
373
+ const result = await handleReadCommand('network', ['--clear'], bm);
374
+ expect(result).toContain('cleared');
375
+ });
376
+ });
377
+
378
+ // ─── Cookies / Storage ──────────────────────────────────────────
379
+
380
+ describe('Cookies and storage', () => {
381
+ test('cookies returns array', async () => {
382
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
383
+ const result = await handleReadCommand('cookies', [], bm);
384
+ // Test server doesn't set cookies, so empty array
385
+ expect(result).toBe('[]');
386
+ });
387
+
388
+ test('storage set and get works', async () => {
389
+ await handleReadCommand('storage', ['set', 'testData', 'testValue'], bm);
390
+ const result = await handleReadCommand('storage', [], bm);
391
+ const storage = JSON.parse(result);
392
+ expect(storage.localStorage.testData).toBe('testValue');
393
+ });
394
+
395
+ test('storage read redacts sensitive keys', async () => {
396
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
397
+ await handleReadCommand('storage', ['set', 'auth_token', 'my-secret-token'], bm);
398
+ await handleReadCommand('storage', ['set', 'api_key', 'key-12345'], bm);
399
+ await handleReadCommand('storage', ['set', 'displayName', 'normalValue'], bm);
400
+ const result = await handleReadCommand('storage', [], bm);
401
+ const storage = JSON.parse(result);
402
+ expect(storage.localStorage.auth_token).toMatch(/REDACTED/);
403
+ expect(storage.localStorage.api_key).toMatch(/REDACTED/);
404
+ expect(storage.localStorage.displayName).toBe('normalValue');
405
+ });
406
+
407
+ test('storage read redacts sensitive values by prefix', async () => {
408
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
409
+ // JWT value under innocuous key name
410
+ await handleReadCommand('storage', ['set', 'userData', 'eyJhbGciOiJIUzI1NiJ9.payload.sig'], bm);
411
+ // GitHub PAT under innocuous key name
412
+ await handleReadCommand('storage', ['set', 'repoAccess', 'ghp_abc123def456'], bm);
413
+ const result = await handleReadCommand('storage', [], bm);
414
+ const storage = JSON.parse(result);
415
+ expect(storage.localStorage.userData).toMatch(/REDACTED/);
416
+ expect(storage.localStorage.repoAccess).toMatch(/REDACTED/);
417
+ });
418
+
419
+ test('storage redaction includes value length', async () => {
420
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
421
+ await handleReadCommand('storage', ['set', 'session_token', 'abc123'], bm);
422
+ const result = await handleReadCommand('storage', [], bm);
423
+ const storage = JSON.parse(result);
424
+ expect(storage.localStorage.session_token).toBe('[REDACTED — 6 chars]');
425
+ });
426
+ });
427
+
428
+ // ─── Performance ────────────────────────────────────────────────
429
+
430
+ describe('Performance', () => {
431
+ test('perf returns timing data', async () => {
432
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
433
+ const result = await handleReadCommand('perf', [], bm);
434
+ expect(result).toContain('dns');
435
+ expect(result).toContain('ttfb');
436
+ expect(result).toContain('load');
437
+ expect(result).toContain('ms');
438
+ });
439
+ });
440
+
441
+ // ─── Visual ─────────────────────────────────────────────────────
442
+
443
+ describe('Visual', () => {
444
+ test('screenshot saves file', async () => {
445
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
446
+ const screenshotPath = '/tmp/browse-test-screenshot.png';
447
+ const result = await handleMetaCommand('screenshot', [screenshotPath], bm, async () => {});
448
+ expect(result).toContain('Screenshot saved');
449
+ expect(fs.existsSync(screenshotPath)).toBe(true);
450
+ const stat = fs.statSync(screenshotPath);
451
+ expect(stat.size).toBeGreaterThan(1000);
452
+ fs.unlinkSync(screenshotPath);
453
+ });
454
+
455
+ test('screenshot --viewport saves viewport-only', async () => {
456
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
457
+ const p = '/tmp/browse-test-viewport.png';
458
+ const result = await handleMetaCommand('screenshot', ['--viewport', p], bm, async () => {});
459
+ expect(result).toContain('Screenshot saved (viewport)');
460
+ expect(fs.existsSync(p)).toBe(true);
461
+ expect(fs.statSync(p).size).toBeGreaterThan(1000);
462
+ fs.unlinkSync(p);
463
+ });
464
+
465
+ test('screenshot with CSS selector crops to element', async () => {
466
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
467
+ const p = '/tmp/browse-test-element-css.png';
468
+ const result = await handleMetaCommand('screenshot', ['#title', p], bm, async () => {});
469
+ expect(result).toContain('Screenshot saved (element)');
470
+ expect(fs.existsSync(p)).toBe(true);
471
+ expect(fs.statSync(p).size).toBeGreaterThan(100);
472
+ fs.unlinkSync(p);
473
+ });
474
+
475
+ test('screenshot with @ref crops to element', async () => {
476
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
477
+ await handleMetaCommand('snapshot', [], bm, async () => {});
478
+ const p = '/tmp/browse-test-element-ref.png';
479
+ const result = await handleMetaCommand('screenshot', ['@e1', p], bm, async () => {});
480
+ expect(result).toContain('Screenshot saved (element)');
481
+ expect(fs.existsSync(p)).toBe(true);
482
+ expect(fs.statSync(p).size).toBeGreaterThan(100);
483
+ fs.unlinkSync(p);
484
+ });
485
+
486
+ test('screenshot --clip crops to region', async () => {
487
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
488
+ const p = '/tmp/browse-test-clip.png';
489
+ const result = await handleMetaCommand('screenshot', ['--clip', '0,0,100,100', p], bm, async () => {});
490
+ expect(result).toContain('Screenshot saved (clip 0,0,100,100)');
491
+ expect(fs.existsSync(p)).toBe(true);
492
+ expect(fs.statSync(p).size).toBeGreaterThan(100);
493
+ fs.unlinkSync(p);
494
+ });
495
+
496
+ test('screenshot --clip + selector throws', async () => {
497
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
498
+ try {
499
+ await handleMetaCommand('screenshot', ['--clip', '0,0,100,100', '#title'], bm, async () => {});
500
+ expect(true).toBe(false);
501
+ } catch (err: any) {
502
+ expect(err.message).toContain('Cannot use --clip with a selector/ref');
503
+ }
504
+ });
505
+
506
+ test('screenshot --viewport + --clip throws', async () => {
507
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
508
+ try {
509
+ await handleMetaCommand('screenshot', ['--viewport', '--clip', '0,0,100,100'], bm, async () => {});
510
+ expect(true).toBe(false);
511
+ } catch (err: any) {
512
+ expect(err.message).toContain('Cannot use --viewport with --clip');
513
+ }
514
+ });
515
+
516
+ test('screenshot --clip with invalid coords throws', async () => {
517
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
518
+ try {
519
+ await handleMetaCommand('screenshot', ['--clip', 'abc'], bm, async () => {});
520
+ expect(true).toBe(false);
521
+ } catch (err: any) {
522
+ expect(err.message).toContain('all must be numbers');
523
+ }
524
+ });
525
+
526
+ test('screenshot unknown flag throws', async () => {
527
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
528
+ try {
529
+ await handleMetaCommand('screenshot', ['--bogus', '/tmp/foo.png'], bm, async () => {});
530
+ expect(true).toBe(false);
531
+ } catch (err: any) {
532
+ expect(err.message).toContain('Unknown screenshot flag');
533
+ }
534
+ });
535
+
536
+ test('screenshot --viewport still validates path', async () => {
537
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
538
+ try {
539
+ await handleMetaCommand('screenshot', ['--viewport', '/etc/evil.png'], bm, async () => {});
540
+ expect(true).toBe(false);
541
+ } catch (err: any) {
542
+ expect(err.message).toContain('Path must be within');
543
+ }
544
+ });
545
+
546
+ test('screenshot treats relative dot-slash path as file path, not CSS selector', async () => {
547
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
548
+ // ./path/to/file.png must be treated as output path, not a CSS class selector (#495)
549
+ const relPath = './browse-test-dotpath.png';
550
+ const absPath = path.resolve(relPath);
551
+ const result = await handleMetaCommand('screenshot', [relPath], bm, async () => {});
552
+ expect(result).toContain('Screenshot saved');
553
+ expect(fs.existsSync(absPath)).toBe(true);
554
+ fs.unlinkSync(absPath);
555
+ });
556
+
557
+ test('screenshot with nonexistent selector throws timeout', async () => {
558
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
559
+ try {
560
+ await handleMetaCommand('screenshot', ['.nonexistent-element-xyz'], bm, async () => {});
561
+ expect(true).toBe(false);
562
+ } catch (err: any) {
563
+ expect(err.message).toBeDefined();
564
+ }
565
+ }, 10000);
566
+
567
+ test('responsive saves 3 screenshots', async () => {
568
+ await handleWriteCommand('goto', [baseUrl + '/responsive.html'], bm);
569
+ const prefix = '/tmp/browse-test-resp';
570
+ const result = await handleMetaCommand('responsive', [prefix], bm, async () => {});
571
+ expect(result).toContain('mobile');
572
+ expect(result).toContain('tablet');
573
+ expect(result).toContain('desktop');
574
+
575
+ expect(fs.existsSync(`${prefix}-mobile.png`)).toBe(true);
576
+ expect(fs.existsSync(`${prefix}-tablet.png`)).toBe(true);
577
+ expect(fs.existsSync(`${prefix}-desktop.png`)).toBe(true);
578
+
579
+ // Cleanup
580
+ fs.unlinkSync(`${prefix}-mobile.png`);
581
+ fs.unlinkSync(`${prefix}-tablet.png`);
582
+ fs.unlinkSync(`${prefix}-desktop.png`);
583
+ });
584
+ });
585
+
586
+ // ─── Tabs ───────────────────────────────────────────────────────
587
+
588
+ describe('Tabs', () => {
589
+ test('tabs lists all tabs', async () => {
590
+ const result = await handleMetaCommand('tabs', [], bm, async () => {});
591
+ expect(result).toContain('[');
592
+ expect(result).toContain(']');
593
+ });
594
+
595
+ test('newtab opens new tab', async () => {
596
+ const result = await handleMetaCommand('newtab', [baseUrl + '/forms.html'], bm, async () => {});
597
+ expect(result).toContain('Opened tab');
598
+
599
+ const tabCount = bm.getTabCount();
600
+ expect(tabCount).toBeGreaterThanOrEqual(2);
601
+ });
602
+
603
+ test('tab switches to specific tab', async () => {
604
+ const result = await handleMetaCommand('tab', ['1'], bm, async () => {});
605
+ expect(result).toContain('Switched to tab 1');
606
+ });
607
+
608
+ test('closetab closes a tab', async () => {
609
+ const before = bm.getTabCount();
610
+ // Close the last opened tab
611
+ const tabs = await bm.getTabListWithTitles();
612
+ const lastTab = tabs[tabs.length - 1];
613
+ const result = await handleMetaCommand('closetab', [String(lastTab.id)], bm, async () => {});
614
+ expect(result).toContain('Closed tab');
615
+ expect(bm.getTabCount()).toBe(before - 1);
616
+ });
617
+ });
618
+
619
+ // ─── Diff ───────────────────────────────────────────────────────
620
+
621
+ describe('Diff', () => {
622
+ test('diff shows differences between pages', async () => {
623
+ const result = await handleMetaCommand(
624
+ 'diff',
625
+ [baseUrl + '/basic.html', baseUrl + '/forms.html'],
626
+ bm,
627
+ async () => {}
628
+ );
629
+ expect(result).toContain('---');
630
+ expect(result).toContain('+++');
631
+ // basic.html has "Hello World", forms.html has "Form Test Page"
632
+ expect(result).toContain('Hello World');
633
+ expect(result).toContain('Form Test Page');
634
+ });
635
+ });
636
+
637
+ // ─── Chain ──────────────────────────────────────────────────────
638
+
639
+ describe('Chain', () => {
640
+ test('chain executes sequence of commands', async () => {
641
+ const commands = JSON.stringify([
642
+ ['goto', baseUrl + '/basic.html'],
643
+ ['js', 'document.title'],
644
+ ['css', 'h1', 'color'],
645
+ ]);
646
+ const result = await handleMetaCommand('chain', [commands], bm, async () => {});
647
+ expect(result).toContain('[goto]');
648
+ expect(result).toContain('Test Page - Basic');
649
+ expect(result).toContain('[css]');
650
+ });
651
+
652
+ test('chain reports real error when write command fails', async () => {
653
+ const commands = JSON.stringify([
654
+ ['goto', 'http://localhost:1/unreachable'],
655
+ ]);
656
+ const result = await handleMetaCommand('chain', [commands], bm, async () => {});
657
+ expect(result).toContain('[goto] ERROR:');
658
+ expect(result).not.toContain('Unknown meta command');
659
+ expect(result).not.toContain('Unknown read command');
660
+ });
661
+ });
662
+
663
+ // ─── Status ─────────────────────────────────────────────────────
664
+
665
+ describe('Status', () => {
666
+ test('status reports health', async () => {
667
+ const result = await handleMetaCommand('status', [], bm, async () => {});
668
+ expect(result).toContain('Status: healthy');
669
+ expect(result).toContain('Tabs:');
670
+ });
671
+ });
672
+
673
+ // ─── CLI server script resolution ───────────────────────────────
674
+
675
+ describe('CLI server script resolution', () => {
676
+ test('prefers adjacent browse/src/server.ts for compiled project installs', () => {
677
+ const root = fs.mkdtempSync('/tmp/gstack-cli-');
678
+ const execPath = path.join(root, '.claude/skills/gstack/browse/dist/browse');
679
+ const serverPath = path.join(root, '.claude/skills/gstack/browse/src/server.ts');
680
+
681
+ fs.mkdirSync(path.dirname(execPath), { recursive: true });
682
+ fs.mkdirSync(path.dirname(serverPath), { recursive: true });
683
+ fs.writeFileSync(serverPath, '// test server\n');
684
+
685
+ const resolved = resolveServerScript(
686
+ { HOME: path.join(root, 'empty-home') },
687
+ '$bunfs/root',
688
+ execPath
689
+ );
690
+
691
+ expect(resolved).toBe(serverPath);
692
+
693
+ fs.rmSync(root, { recursive: true, force: true });
694
+ });
695
+ });
696
+
697
+ // ─── CLI lifecycle ──────────────────────────────────────────────
698
+
699
+ describe('CLI lifecycle', () => {
700
+ test('dead state file triggers a clean restart', async () => {
701
+ const stateFile = `/tmp/browse-test-state-${Date.now()}.json`;
702
+ fs.writeFileSync(stateFile, JSON.stringify({
703
+ port: 1,
704
+ token: 'fake',
705
+ pid: 999999,
706
+ }));
707
+
708
+ const cliPath = path.resolve(__dirname, '../src/cli.ts');
709
+ const cliEnv: Record<string, string> = {};
710
+ for (const [k, v] of Object.entries(process.env)) {
711
+ if (v !== undefined) cliEnv[k] = v;
712
+ }
713
+ cliEnv.BROWSE_STATE_FILE = stateFile;
714
+ const result = await new Promise<{ code: number; stdout: string; stderr: string }>((resolve) => {
715
+ const proc = spawn('bun', ['run', cliPath, 'status'], {
716
+ timeout: 15000,
717
+ env: cliEnv,
718
+ });
719
+ let stdout = '';
720
+ let stderr = '';
721
+ proc.stdout.on('data', (d) => stdout += d.toString());
722
+ proc.stderr.on('data', (d) => stderr += d.toString());
723
+ proc.on('close', (code) => resolve({ code: code ?? 1, stdout, stderr }));
724
+ });
725
+
726
+ let restartedPid: number | null = null;
727
+ if (fs.existsSync(stateFile)) {
728
+ restartedPid = JSON.parse(fs.readFileSync(stateFile, 'utf-8')).pid;
729
+ fs.unlinkSync(stateFile);
730
+ }
731
+ if (restartedPid) {
732
+ try { process.kill(restartedPid, 'SIGTERM'); } catch {}
733
+ }
734
+
735
+ expect(result.code).toBe(0);
736
+ expect(result.stdout).toContain('Status: healthy');
737
+ expect(result.stderr).toContain('Starting server');
738
+ }, 20000);
739
+ });
740
+
741
+ // ─── Buffer bounds ──────────────────────────────────────────────
742
+
743
+ describe('Buffer bounds', () => {
744
+ test('console buffer caps at 50000 entries', () => {
745
+ consoleBuffer.clear();
746
+ for (let i = 0; i < 50_010; i++) {
747
+ addConsoleEntry({ timestamp: i, level: 'log', text: `msg-${i}` });
748
+ }
749
+ expect(consoleBuffer.length).toBe(50_000);
750
+ const entries = consoleBuffer.toArray();
751
+ expect(entries[0].text).toBe('msg-10');
752
+ expect(entries[entries.length - 1].text).toBe('msg-50009');
753
+ consoleBuffer.clear();
754
+ });
755
+
756
+ test('network buffer caps at 50000 entries', () => {
757
+ networkBuffer.clear();
758
+ for (let i = 0; i < 50_010; i++) {
759
+ addNetworkEntry({ timestamp: i, method: 'GET', url: `http://x/${i}` });
760
+ }
761
+ expect(networkBuffer.length).toBe(50_000);
762
+ const entries = networkBuffer.toArray();
763
+ expect(entries[0].url).toBe('http://x/10');
764
+ expect(entries[entries.length - 1].url).toBe('http://x/50009');
765
+ networkBuffer.clear();
766
+ });
767
+
768
+ test('totalAdded counters keep incrementing past buffer cap', () => {
769
+ const startConsole = consoleBuffer.totalAdded;
770
+ const startNetwork = networkBuffer.totalAdded;
771
+ for (let i = 0; i < 100; i++) {
772
+ addConsoleEntry({ timestamp: i, level: 'log', text: `t-${i}` });
773
+ addNetworkEntry({ timestamp: i, method: 'GET', url: `http://t/${i}` });
774
+ }
775
+ expect(consoleBuffer.totalAdded).toBe(startConsole + 100);
776
+ expect(networkBuffer.totalAdded).toBe(startNetwork + 100);
777
+ consoleBuffer.clear();
778
+ networkBuffer.clear();
779
+ });
780
+ });
781
+
782
+ // ─── CircularBuffer Unit Tests ─────────────────────────────────
783
+
784
+ describe('CircularBuffer', () => {
785
+ test('push and toArray return items in insertion order', () => {
786
+ const buf = new CircularBuffer<number>(5);
787
+ buf.push(1); buf.push(2); buf.push(3);
788
+ expect(buf.toArray()).toEqual([1, 2, 3]);
789
+ expect(buf.length).toBe(3);
790
+ });
791
+
792
+ test('overwrites oldest when full', () => {
793
+ const buf = new CircularBuffer<number>(3);
794
+ buf.push(1); buf.push(2); buf.push(3); buf.push(4);
795
+ expect(buf.toArray()).toEqual([2, 3, 4]);
796
+ expect(buf.length).toBe(3);
797
+ });
798
+
799
+ test('totalAdded increments past capacity', () => {
800
+ const buf = new CircularBuffer<number>(2);
801
+ buf.push(1); buf.push(2); buf.push(3); buf.push(4); buf.push(5);
802
+ expect(buf.totalAdded).toBe(5);
803
+ expect(buf.length).toBe(2);
804
+ expect(buf.toArray()).toEqual([4, 5]);
805
+ });
806
+
807
+ test('last(n) returns most recent entries', () => {
808
+ const buf = new CircularBuffer<number>(5);
809
+ for (let i = 1; i <= 5; i++) buf.push(i);
810
+ expect(buf.last(3)).toEqual([3, 4, 5]);
811
+ expect(buf.last(10)).toEqual([1, 2, 3, 4, 5]); // clamped
812
+ expect(buf.last(1)).toEqual([5]);
813
+ });
814
+
815
+ test('get and set work by index', () => {
816
+ const buf = new CircularBuffer<string>(3);
817
+ buf.push('a'); buf.push('b'); buf.push('c');
818
+ expect(buf.get(0)).toBe('a');
819
+ expect(buf.get(2)).toBe('c');
820
+ buf.set(1, 'B');
821
+ expect(buf.get(1)).toBe('B');
822
+ expect(buf.get(-1)).toBeUndefined();
823
+ expect(buf.get(5)).toBeUndefined();
824
+ });
825
+
826
+ test('clear resets size but not totalAdded', () => {
827
+ const buf = new CircularBuffer<number>(5);
828
+ buf.push(1); buf.push(2); buf.push(3);
829
+ buf.clear();
830
+ expect(buf.length).toBe(0);
831
+ expect(buf.totalAdded).toBe(3);
832
+ expect(buf.toArray()).toEqual([]);
833
+ });
834
+
835
+ test('works with capacity=1', () => {
836
+ const buf = new CircularBuffer<number>(1);
837
+ buf.push(10);
838
+ expect(buf.toArray()).toEqual([10]);
839
+ buf.push(20);
840
+ expect(buf.toArray()).toEqual([20]);
841
+ expect(buf.totalAdded).toBe(2);
842
+ });
843
+ });
844
+
845
+ // ─── Dialog Handling ─────────────────────────────────────────
846
+
847
+ describe('Dialog handling', () => {
848
+ test('alert does not hang — auto-accepted', async () => {
849
+ await handleWriteCommand('goto', [baseUrl + '/dialog.html'], bm);
850
+ await handleWriteCommand('click', ['#alert-btn'], bm);
851
+ // If we get here, dialog was handled (no hang)
852
+ const result = await handleReadCommand('dialog', [], bm);
853
+ expect(result).toContain('alert');
854
+ expect(result).toContain('Hello from alert');
855
+ expect(result).toContain('accepted');
856
+ });
857
+
858
+ test('confirm is auto-accepted by default', async () => {
859
+ await handleWriteCommand('goto', [baseUrl + '/dialog.html'], bm);
860
+ await handleWriteCommand('click', ['#confirm-btn'], bm);
861
+ // Wait for DOM update
862
+ await new Promise(r => setTimeout(r, 100));
863
+ const result = await handleReadCommand('js', ['document.querySelector("#confirm-result").textContent'], bm);
864
+ expect(result).toBe('confirmed');
865
+ });
866
+
867
+ test('dialog-dismiss changes behavior', async () => {
868
+ const setResult = await handleWriteCommand('dialog-dismiss', [], bm);
869
+ expect(setResult).toContain('dismissed');
870
+
871
+ await handleWriteCommand('goto', [baseUrl + '/dialog.html'], bm);
872
+ await handleWriteCommand('click', ['#confirm-btn'], bm);
873
+ await new Promise(r => setTimeout(r, 100));
874
+ const result = await handleReadCommand('js', ['document.querySelector("#confirm-result").textContent'], bm);
875
+ expect(result).toBe('cancelled');
876
+
877
+ // Reset to accept
878
+ await handleWriteCommand('dialog-accept', [], bm);
879
+ });
880
+
881
+ test('dialog-accept with text provides prompt response', async () => {
882
+ const setResult = await handleWriteCommand('dialog-accept', ['TestUser'], bm);
883
+ expect(setResult).toContain('TestUser');
884
+
885
+ await handleWriteCommand('goto', [baseUrl + '/dialog.html'], bm);
886
+ await handleWriteCommand('click', ['#prompt-btn'], bm);
887
+ await new Promise(r => setTimeout(r, 100));
888
+ const result = await handleReadCommand('js', ['document.querySelector("#prompt-result").textContent'], bm);
889
+ expect(result).toBe('TestUser');
890
+
891
+ // Reset
892
+ await handleWriteCommand('dialog-accept', [], bm);
893
+ });
894
+
895
+ test('dialog --clear clears buffer', async () => {
896
+ const cleared = await handleReadCommand('dialog', ['--clear'], bm);
897
+ expect(cleared).toContain('cleared');
898
+ const after = await handleReadCommand('dialog', [], bm);
899
+ expect(after).toContain('no dialogs');
900
+ });
901
+ });
902
+
903
+ // ─── Element State Checks (is) ─────────────────────────────────
904
+
905
+ describe('Element state checks', () => {
906
+ beforeAll(async () => {
907
+ await handleWriteCommand('goto', [baseUrl + '/states.html'], bm);
908
+ });
909
+
910
+ test('is visible returns true for visible element', async () => {
911
+ const result = await handleReadCommand('is', ['visible', '#visible-div'], bm);
912
+ expect(result).toBe('true');
913
+ });
914
+
915
+ test('is hidden returns true for hidden element', async () => {
916
+ const result = await handleReadCommand('is', ['hidden', '#hidden-div'], bm);
917
+ expect(result).toBe('true');
918
+ });
919
+
920
+ test('is visible returns false for hidden element', async () => {
921
+ const result = await handleReadCommand('is', ['visible', '#hidden-div'], bm);
922
+ expect(result).toBe('false');
923
+ });
924
+
925
+ test('is enabled returns true for enabled input', async () => {
926
+ const result = await handleReadCommand('is', ['enabled', '#enabled-input'], bm);
927
+ expect(result).toBe('true');
928
+ });
929
+
930
+ test('is disabled returns true for disabled input', async () => {
931
+ const result = await handleReadCommand('is', ['disabled', '#disabled-input'], bm);
932
+ expect(result).toBe('true');
933
+ });
934
+
935
+ test('is checked returns true for checked checkbox', async () => {
936
+ const result = await handleReadCommand('is', ['checked', '#checked-box'], bm);
937
+ expect(result).toBe('true');
938
+ });
939
+
940
+ test('is checked returns false for unchecked checkbox', async () => {
941
+ const result = await handleReadCommand('is', ['checked', '#unchecked-box'], bm);
942
+ expect(result).toBe('false');
943
+ });
944
+
945
+ test('is editable returns true for normal input', async () => {
946
+ const result = await handleReadCommand('is', ['editable', '#enabled-input'], bm);
947
+ expect(result).toBe('true');
948
+ });
949
+
950
+ test('is editable returns false for readonly input', async () => {
951
+ const result = await handleReadCommand('is', ['editable', '#readonly-input'], bm);
952
+ expect(result).toBe('false');
953
+ });
954
+
955
+ test('is focused after click', async () => {
956
+ await handleWriteCommand('click', ['#enabled-input'], bm);
957
+ const result = await handleReadCommand('is', ['focused', '#enabled-input'], bm);
958
+ expect(result).toBe('true');
959
+ });
960
+
961
+ test('is with @ref works', async () => {
962
+ await handleMetaCommand('snapshot', ['-i'], bm, async () => {});
963
+ // Find a ref for the enabled input
964
+ const snap = await handleMetaCommand('snapshot', ['-i'], bm, async () => {});
965
+ const textboxLine = snap.split('\n').find(l => l.includes('[textbox]'));
966
+ if (textboxLine) {
967
+ const refMatch = textboxLine.match(/@(e\d+)/);
968
+ if (refMatch) {
969
+ const ref = `@${refMatch[1]}`;
970
+ const result = await handleReadCommand('is', ['visible', ref], bm);
971
+ expect(result).toBe('true');
972
+ }
973
+ }
974
+ });
975
+
976
+ test('is with unknown property throws', async () => {
977
+ try {
978
+ await handleReadCommand('is', ['bogus', '#enabled-input'], bm);
979
+ expect(true).toBe(false);
980
+ } catch (err: any) {
981
+ expect(err.message).toContain('Unknown property');
982
+ }
983
+ });
984
+
985
+ test('is with missing args throws', async () => {
986
+ try {
987
+ await handleReadCommand('is', ['visible'], bm);
988
+ expect(true).toBe(false);
989
+ } catch (err: any) {
990
+ expect(err.message).toContain('Usage');
991
+ }
992
+ });
993
+ });
994
+
995
+ // ─── File Upload ─────────────────────────────────────────────────
996
+
997
+ describe('File upload', () => {
998
+ test('upload single file', async () => {
999
+ await handleWriteCommand('goto', [baseUrl + '/upload.html'], bm);
1000
+ // Create a temp file to upload
1001
+ const tempFile = '/tmp/browse-test-upload.txt';
1002
+ fs.writeFileSync(tempFile, 'test content');
1003
+ const result = await handleWriteCommand('upload', ['#file-input', tempFile], bm);
1004
+ expect(result).toContain('Uploaded');
1005
+ expect(result).toContain('browse-test-upload.txt');
1006
+
1007
+ // Verify upload handler fired
1008
+ await new Promise(r => setTimeout(r, 100));
1009
+ const text = await handleReadCommand('js', ['document.querySelector("#upload-result").textContent'], bm);
1010
+ expect(text).toContain('browse-test-upload.txt');
1011
+ fs.unlinkSync(tempFile);
1012
+ });
1013
+
1014
+ test('upload with @ref works', async () => {
1015
+ await handleWriteCommand('goto', [baseUrl + '/upload.html'], bm);
1016
+ const tempFile = '/tmp/browse-test-upload2.txt';
1017
+ fs.writeFileSync(tempFile, 'ref upload test');
1018
+ const snap = await handleMetaCommand('snapshot', ['-i'], bm, async () => {});
1019
+ // Find the file input ref (it won't appear as "file input" in aria — use CSS selector instead)
1020
+ const result = await handleWriteCommand('upload', ['#file-input', tempFile], bm);
1021
+ expect(result).toContain('Uploaded');
1022
+ fs.unlinkSync(tempFile);
1023
+ });
1024
+
1025
+ test('upload nonexistent file throws', async () => {
1026
+ await handleWriteCommand('goto', [baseUrl + '/upload.html'], bm);
1027
+ try {
1028
+ await handleWriteCommand('upload', ['#file-input', '/tmp/nonexistent-file-12345.txt'], bm);
1029
+ expect(true).toBe(false);
1030
+ } catch (err: any) {
1031
+ expect(err.message).toContain('File not found');
1032
+ }
1033
+ });
1034
+
1035
+ test('upload missing args throws', async () => {
1036
+ try {
1037
+ await handleWriteCommand('upload', ['#file-input'], bm);
1038
+ expect(true).toBe(false);
1039
+ } catch (err: any) {
1040
+ expect(err.message).toContain('Usage');
1041
+ }
1042
+ });
1043
+ });
1044
+
1045
+ // ─── Eval command ───────────────────────────────────────────────
1046
+
1047
+ describe('Eval', () => {
1048
+ test('eval runs JS file', async () => {
1049
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1050
+ const tempFile = '/tmp/browse-test-eval.js';
1051
+ fs.writeFileSync(tempFile, 'document.title + " — evaluated"');
1052
+ const result = await handleReadCommand('eval', [tempFile], bm);
1053
+ expect(result).toBe('Test Page - Basic — evaluated');
1054
+ fs.unlinkSync(tempFile);
1055
+ });
1056
+
1057
+ test('eval returns object as JSON', async () => {
1058
+ const tempFile = '/tmp/browse-test-eval-obj.js';
1059
+ fs.writeFileSync(tempFile, '({title: document.title, keys: Object.keys(document.body.dataset)})');
1060
+ const result = await handleReadCommand('eval', [tempFile], bm);
1061
+ const obj = JSON.parse(result);
1062
+ expect(obj.title).toBe('Test Page - Basic');
1063
+ expect(Array.isArray(obj.keys)).toBe(true);
1064
+ fs.unlinkSync(tempFile);
1065
+ });
1066
+
1067
+ test('eval file not found throws', async () => {
1068
+ try {
1069
+ await handleReadCommand('eval', ['/tmp/nonexistent-eval.js'], bm);
1070
+ expect(true).toBe(false);
1071
+ } catch (err: any) {
1072
+ expect(err.message).toContain('File not found');
1073
+ }
1074
+ });
1075
+
1076
+ test('eval no arg throws', async () => {
1077
+ try {
1078
+ await handleReadCommand('eval', [], bm);
1079
+ expect(true).toBe(false);
1080
+ } catch (err: any) {
1081
+ expect(err.message).toContain('Usage');
1082
+ }
1083
+ });
1084
+ });
1085
+
1086
+ // ─── Press command ──────────────────────────────────────────────
1087
+
1088
+ describe('Press', () => {
1089
+ test('press Tab moves focus', async () => {
1090
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
1091
+ await handleWriteCommand('click', ['#email'], bm);
1092
+ const result = await handleWriteCommand('press', ['Tab'], bm);
1093
+ expect(result).toContain('Pressed Tab');
1094
+ });
1095
+
1096
+ test('press no arg throws', async () => {
1097
+ try {
1098
+ await handleWriteCommand('press', [], bm);
1099
+ expect(true).toBe(false);
1100
+ } catch (err: any) {
1101
+ expect(err.message).toContain('Usage');
1102
+ }
1103
+ });
1104
+ });
1105
+
1106
+ // ─── Cookie command ─────────────────────────────────────────────
1107
+
1108
+ describe('Cookie command', () => {
1109
+ test('cookie sets value', async () => {
1110
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1111
+ const result = await handleWriteCommand('cookie', ['testcookie=testvalue'], bm);
1112
+ expect(result).toContain('Cookie set');
1113
+
1114
+ const cookies = await handleReadCommand('cookies', [], bm);
1115
+ expect(cookies).toContain('testcookie');
1116
+ expect(cookies).toContain('testvalue');
1117
+ });
1118
+
1119
+ test('cookie no arg throws', async () => {
1120
+ try {
1121
+ await handleWriteCommand('cookie', [], bm);
1122
+ expect(true).toBe(false);
1123
+ } catch (err: any) {
1124
+ expect(err.message).toContain('Usage');
1125
+ }
1126
+ });
1127
+
1128
+ test('cookie no = throws', async () => {
1129
+ try {
1130
+ await handleWriteCommand('cookie', ['invalid'], bm);
1131
+ expect(true).toBe(false);
1132
+ } catch (err: any) {
1133
+ expect(err.message).toContain('Usage');
1134
+ }
1135
+ });
1136
+ });
1137
+
1138
+ // ─── Header command ─────────────────────────────────────────────
1139
+
1140
+ describe('Header command', () => {
1141
+ test('header sets value and is sent', async () => {
1142
+ const result = await handleWriteCommand('header', ['X-Test:test-value'], bm);
1143
+ expect(result).toContain('Header set');
1144
+
1145
+ await handleWriteCommand('goto', [baseUrl + '/echo'], bm);
1146
+ const echoText = await handleReadCommand('text', [], bm);
1147
+ expect(echoText).toContain('x-test');
1148
+ expect(echoText).toContain('test-value');
1149
+ });
1150
+
1151
+ test('header no arg throws', async () => {
1152
+ try {
1153
+ await handleWriteCommand('header', [], bm);
1154
+ expect(true).toBe(false);
1155
+ } catch (err: any) {
1156
+ expect(err.message).toContain('Usage');
1157
+ }
1158
+ });
1159
+
1160
+ test('header no colon throws', async () => {
1161
+ try {
1162
+ await handleWriteCommand('header', ['invalid'], bm);
1163
+ expect(true).toBe(false);
1164
+ } catch (err: any) {
1165
+ expect(err.message).toContain('Usage');
1166
+ }
1167
+ });
1168
+ });
1169
+
1170
+ // ─── PDF command ────────────────────────────────────────────────
1171
+
1172
+ describe('PDF', () => {
1173
+ test('pdf saves file with size', async () => {
1174
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1175
+ const pdfPath = '/tmp/browse-test.pdf';
1176
+ const result = await handleMetaCommand('pdf', [pdfPath], bm, async () => {});
1177
+ expect(result).toContain('PDF saved');
1178
+ expect(fs.existsSync(pdfPath)).toBe(true);
1179
+ const stat = fs.statSync(pdfPath);
1180
+ expect(stat.size).toBeGreaterThan(100);
1181
+ fs.unlinkSync(pdfPath);
1182
+ });
1183
+ });
1184
+
1185
+ // ─── Empty page edge cases ──────────────────────────────────────
1186
+
1187
+ describe('Empty page', () => {
1188
+ test('text returns empty on empty page', async () => {
1189
+ await handleWriteCommand('goto', [baseUrl + '/empty.html'], bm);
1190
+ const result = await handleReadCommand('text', [], bm);
1191
+ expect(result).toBe('');
1192
+ });
1193
+
1194
+ test('links returns empty on empty page', async () => {
1195
+ const result = await handleReadCommand('links', [], bm);
1196
+ expect(result).toBe('');
1197
+ });
1198
+
1199
+ test('forms returns empty array on empty page', async () => {
1200
+ const result = await handleReadCommand('forms', [], bm);
1201
+ expect(JSON.parse(result)).toEqual([]);
1202
+ });
1203
+ });
1204
+
1205
+ // ─── Error paths ────────────────────────────────────────────────
1206
+
1207
+ describe('Errors', () => {
1208
+ // Write command errors
1209
+ test('goto with no arg throws', async () => {
1210
+ try {
1211
+ await handleWriteCommand('goto', [], bm);
1212
+ expect(true).toBe(false);
1213
+ } catch (err: any) {
1214
+ expect(err.message).toContain('Usage');
1215
+ }
1216
+ });
1217
+
1218
+ test('click with no arg throws', async () => {
1219
+ try {
1220
+ await handleWriteCommand('click', [], bm);
1221
+ expect(true).toBe(false);
1222
+ } catch (err: any) {
1223
+ expect(err.message).toContain('Usage');
1224
+ }
1225
+ });
1226
+
1227
+ test('fill with no value throws', async () => {
1228
+ try {
1229
+ await handleWriteCommand('fill', ['#input'], bm);
1230
+ expect(true).toBe(false);
1231
+ } catch (err: any) {
1232
+ expect(err.message).toContain('Usage');
1233
+ }
1234
+ });
1235
+
1236
+ test('select with no value throws', async () => {
1237
+ try {
1238
+ await handleWriteCommand('select', ['#sel'], bm);
1239
+ expect(true).toBe(false);
1240
+ } catch (err: any) {
1241
+ expect(err.message).toContain('Usage');
1242
+ }
1243
+ });
1244
+
1245
+ test('hover with no arg throws', async () => {
1246
+ try {
1247
+ await handleWriteCommand('hover', [], bm);
1248
+ expect(true).toBe(false);
1249
+ } catch (err: any) {
1250
+ expect(err.message).toContain('Usage');
1251
+ }
1252
+ });
1253
+
1254
+ test('type with no arg throws', async () => {
1255
+ try {
1256
+ await handleWriteCommand('type', [], bm);
1257
+ expect(true).toBe(false);
1258
+ } catch (err: any) {
1259
+ expect(err.message).toContain('Usage');
1260
+ }
1261
+ });
1262
+
1263
+ test('wait with no arg throws', async () => {
1264
+ try {
1265
+ await handleWriteCommand('wait', [], bm);
1266
+ expect(true).toBe(false);
1267
+ } catch (err: any) {
1268
+ expect(err.message).toContain('Usage');
1269
+ }
1270
+ });
1271
+
1272
+ test('viewport with bad format throws', async () => {
1273
+ try {
1274
+ await handleWriteCommand('viewport', ['badformat'], bm);
1275
+ expect(true).toBe(false);
1276
+ } catch (err: any) {
1277
+ expect(err.message).toContain('Usage');
1278
+ }
1279
+ });
1280
+
1281
+ test('useragent with no arg throws', async () => {
1282
+ try {
1283
+ await handleWriteCommand('useragent', [], bm);
1284
+ expect(true).toBe(false);
1285
+ } catch (err: any) {
1286
+ expect(err.message).toContain('Usage');
1287
+ }
1288
+ });
1289
+
1290
+ // Read command errors
1291
+ test('js with no expression throws', async () => {
1292
+ try {
1293
+ await handleReadCommand('js', [], bm);
1294
+ expect(true).toBe(false);
1295
+ } catch (err: any) {
1296
+ expect(err.message).toContain('Usage');
1297
+ }
1298
+ });
1299
+
1300
+ test('css with missing property throws', async () => {
1301
+ try {
1302
+ await handleReadCommand('css', ['h1'], bm);
1303
+ expect(true).toBe(false);
1304
+ } catch (err: any) {
1305
+ expect(err.message).toContain('Usage');
1306
+ }
1307
+ });
1308
+
1309
+ test('attrs with no selector throws', async () => {
1310
+ try {
1311
+ await handleReadCommand('attrs', [], bm);
1312
+ expect(true).toBe(false);
1313
+ } catch (err: any) {
1314
+ expect(err.message).toContain('Usage');
1315
+ }
1316
+ });
1317
+
1318
+ // Meta command errors
1319
+ test('tab with non-numeric id throws', async () => {
1320
+ try {
1321
+ await handleMetaCommand('tab', ['abc'], bm, async () => {});
1322
+ expect(true).toBe(false);
1323
+ } catch (err: any) {
1324
+ expect(err.message).toContain('Usage');
1325
+ }
1326
+ });
1327
+
1328
+ test('diff with missing urls throws', async () => {
1329
+ try {
1330
+ await handleMetaCommand('diff', [baseUrl + '/basic.html'], bm, async () => {});
1331
+ expect(true).toBe(false);
1332
+ } catch (err: any) {
1333
+ expect(err.message).toContain('Usage');
1334
+ }
1335
+ });
1336
+
1337
+ test('chain with invalid JSON falls back to pipe format', async () => {
1338
+ // Non-JSON input is now treated as pipe-delimited format
1339
+ // 'not json' → [["not", "json"]] → "not" is unknown command → error in result
1340
+ const result = await handleMetaCommand('chain', ['not json'], bm, async () => {});
1341
+ expect(result).toContain('ERROR');
1342
+ expect(result).toContain('Unknown command: not');
1343
+ });
1344
+
1345
+ test('chain with no arg throws', async () => {
1346
+ try {
1347
+ await handleMetaCommand('chain', [], bm, async () => {});
1348
+ expect(true).toBe(false);
1349
+ } catch (err: any) {
1350
+ expect(err.message).toContain('Usage');
1351
+ }
1352
+ });
1353
+
1354
+ test('unknown read command throws', async () => {
1355
+ try {
1356
+ await handleReadCommand('bogus' as any, [], bm);
1357
+ expect(true).toBe(false);
1358
+ } catch (err: any) {
1359
+ expect(err.message).toContain('Unknown');
1360
+ }
1361
+ });
1362
+
1363
+ test('unknown write command throws', async () => {
1364
+ try {
1365
+ await handleWriteCommand('bogus' as any, [], bm);
1366
+ expect(true).toBe(false);
1367
+ } catch (err: any) {
1368
+ expect(err.message).toContain('Unknown');
1369
+ }
1370
+ });
1371
+
1372
+ test('unknown meta command throws', async () => {
1373
+ try {
1374
+ await handleMetaCommand('bogus' as any, [], bm, async () => {});
1375
+ expect(true).toBe(false);
1376
+ } catch (err: any) {
1377
+ expect(err.message).toContain('Unknown');
1378
+ }
1379
+ });
1380
+ });
1381
+
1382
+ // ─── Workflow: Navigation + Snapshot + Interaction ───────────────
1383
+
1384
+ describe('Workflows', () => {
1385
+ test('navigation → snapshot → click @ref → verify URL', async () => {
1386
+ await handleWriteCommand('goto', [baseUrl + '/snapshot.html'], bm);
1387
+ const snap = await handleMetaCommand('snapshot', ['-i'], bm, async () => {});
1388
+ // Find a link ref
1389
+ const linkLine = snap.split('\n').find(l => l.includes('[link]'));
1390
+ expect(linkLine).toBeDefined();
1391
+ const refMatch = linkLine!.match(/@(e\d+)/);
1392
+ expect(refMatch).toBeDefined();
1393
+ // Click the link
1394
+ await handleWriteCommand('click', [`@${refMatch![1]}`], bm);
1395
+ // URL should have changed
1396
+ const url = await handleMetaCommand('url', [], bm, async () => {});
1397
+ expect(url).toBeTruthy();
1398
+ });
1399
+
1400
+ test('form: goto → snapshot → fill @ref → click @ref', async () => {
1401
+ await handleWriteCommand('goto', [baseUrl + '/snapshot.html'], bm);
1402
+ const snap = await handleMetaCommand('snapshot', ['-i'], bm, async () => {});
1403
+ // Find textbox and button
1404
+ const textboxLine = snap.split('\n').find(l => l.includes('[textbox]'));
1405
+ const buttonLine = snap.split('\n').find(l => l.includes('[button]') && l.includes('"Submit"'));
1406
+ if (textboxLine && buttonLine) {
1407
+ const textRef = textboxLine.match(/@(e\d+)/)![1];
1408
+ const btnRef = buttonLine.match(/@(e\d+)/)![1];
1409
+ await handleWriteCommand('fill', [`@${textRef}`, 'testuser'], bm);
1410
+ await handleWriteCommand('click', [`@${btnRef}`], bm);
1411
+ }
1412
+ });
1413
+
1414
+ test('tabs: newtab → goto → switch → verify isolation', async () => {
1415
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1416
+ const tabsBefore = bm.getTabCount();
1417
+ await handleMetaCommand('newtab', [baseUrl + '/forms.html'], bm, async () => {});
1418
+ expect(bm.getTabCount()).toBe(tabsBefore + 1);
1419
+
1420
+ const url = await handleMetaCommand('url', [], bm, async () => {});
1421
+ expect(url).toContain('/forms.html');
1422
+
1423
+ // Switch back to previous tab
1424
+ const tabs = await bm.getTabListWithTitles();
1425
+ const prevTab = tabs.find(t => t.url.includes('/basic.html'));
1426
+ if (prevTab) {
1427
+ bm.switchTab(prevTab.id);
1428
+ const url2 = await handleMetaCommand('url', [], bm, async () => {});
1429
+ expect(url2).toContain('/basic.html');
1430
+ }
1431
+
1432
+ // Clean up extra tab
1433
+ const allTabs = await bm.getTabListWithTitles();
1434
+ const formTab = allTabs.find(t => t.url.includes('/forms.html'));
1435
+ if (formTab) await bm.closeTab(formTab.id);
1436
+ });
1437
+
1438
+ test('cookies: set → read → reload → verify persistence', async () => {
1439
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1440
+ await handleWriteCommand('cookie', ['workflow-test=persisted'], bm);
1441
+ await handleWriteCommand('reload', [], bm);
1442
+ const cookies = await handleReadCommand('cookies', [], bm);
1443
+ expect(cookies).toContain('workflow-test');
1444
+ expect(cookies).toContain('persisted');
1445
+ });
1446
+ });
1447
+
1448
+ // ─── Wait load states ──────────────────────────────────────────
1449
+
1450
+ describe('Wait load states', () => {
1451
+ test('wait --networkidle succeeds after page load', async () => {
1452
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1453
+ const result = await handleWriteCommand('wait', ['--networkidle'], bm);
1454
+ expect(result).toBe('Network idle');
1455
+ });
1456
+
1457
+ test('wait --load succeeds', async () => {
1458
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1459
+ const result = await handleWriteCommand('wait', ['--load'], bm);
1460
+ expect(result).toBe('Page loaded');
1461
+ });
1462
+
1463
+ test('wait --domcontentloaded succeeds', async () => {
1464
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1465
+ const result = await handleWriteCommand('wait', ['--domcontentloaded'], bm);
1466
+ expect(result).toBe('DOM content loaded');
1467
+ });
1468
+
1469
+ test('wait --networkidle with custom timeout', async () => {
1470
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1471
+ const result = await handleWriteCommand('wait', ['--networkidle', '5000'], bm);
1472
+ expect(result).toBe('Network idle');
1473
+ });
1474
+
1475
+ test('wait with selector still works', async () => {
1476
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1477
+ const result = await handleWriteCommand('wait', ['#title'], bm);
1478
+ expect(result).toContain('appeared');
1479
+ });
1480
+ });
1481
+
1482
+ // ─── Console --errors ──────────────────────────────────────────
1483
+
1484
+ describe('Console --errors', () => {
1485
+ test('console --errors filters to error and warning only', async () => {
1486
+ // Clear existing entries
1487
+ await handleReadCommand('console', ['--clear'], bm);
1488
+
1489
+ // Add mixed entries
1490
+ addConsoleEntry({ timestamp: Date.now(), level: 'log', text: 'info message' });
1491
+ addConsoleEntry({ timestamp: Date.now(), level: 'warning', text: 'warn message' });
1492
+ addConsoleEntry({ timestamp: Date.now(), level: 'error', text: 'error message' });
1493
+
1494
+ const result = await handleReadCommand('console', ['--errors'], bm);
1495
+ expect(result).toContain('warn message');
1496
+ expect(result).toContain('error message');
1497
+ expect(result).not.toContain('info message');
1498
+
1499
+ // Cleanup
1500
+ consoleBuffer.clear();
1501
+ });
1502
+
1503
+ test('console --errors returns empty message when no errors', async () => {
1504
+ consoleBuffer.clear();
1505
+ addConsoleEntry({ timestamp: Date.now(), level: 'log', text: 'just a log' });
1506
+
1507
+ const result = await handleReadCommand('console', ['--errors'], bm);
1508
+ expect(result).toBe('(no console errors)');
1509
+
1510
+ consoleBuffer.clear();
1511
+ });
1512
+
1513
+ test('console --errors on empty buffer', async () => {
1514
+ consoleBuffer.clear();
1515
+ const result = await handleReadCommand('console', ['--errors'], bm);
1516
+ expect(result).toBe('(no console errors)');
1517
+ });
1518
+
1519
+ test('console without flag still returns all messages', async () => {
1520
+ consoleBuffer.clear();
1521
+ addConsoleEntry({ timestamp: Date.now(), level: 'log', text: 'all messages test' });
1522
+
1523
+ const result = await handleReadCommand('console', [], bm);
1524
+ expect(result).toContain('all messages test');
1525
+
1526
+ consoleBuffer.clear();
1527
+ });
1528
+ });
1529
+
1530
+ // ─── Cookie Import ─────────────────────────────────────────────
1531
+
1532
+ describe('Cookie import', () => {
1533
+ test('cookie-import loads valid JSON cookies', async () => {
1534
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1535
+ const tempFile = '/tmp/browse-test-cookies.json';
1536
+ const cookies = [
1537
+ { name: 'test-cookie', value: 'test-value' },
1538
+ { name: 'another', value: '123' },
1539
+ ];
1540
+ fs.writeFileSync(tempFile, JSON.stringify(cookies));
1541
+
1542
+ const result = await handleWriteCommand('cookie-import', [tempFile], bm);
1543
+ expect(result).toBe('Loaded 2 cookies from /tmp/browse-test-cookies.json');
1544
+
1545
+ // Verify cookies were set
1546
+ const cookieList = await handleReadCommand('cookies', [], bm);
1547
+ expect(cookieList).toContain('test-cookie');
1548
+ expect(cookieList).toContain('test-value');
1549
+ expect(cookieList).toContain('another');
1550
+
1551
+ fs.unlinkSync(tempFile);
1552
+ });
1553
+
1554
+ test('cookie-import auto-fills domain from page URL', async () => {
1555
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1556
+ const tempFile = '/tmp/browse-test-cookies-nodomain.json';
1557
+ // Cookies without domain — should auto-fill from page URL
1558
+ const cookies = [{ name: 'autofill-test', value: 'works' }];
1559
+ fs.writeFileSync(tempFile, JSON.stringify(cookies));
1560
+
1561
+ const result = await handleWriteCommand('cookie-import', [tempFile], bm);
1562
+ expect(result).toContain('Loaded 1');
1563
+
1564
+ const cookieList = await handleReadCommand('cookies', [], bm);
1565
+ expect(cookieList).toContain('autofill-test');
1566
+
1567
+ fs.unlinkSync(tempFile);
1568
+ });
1569
+
1570
+ test('cookie-import preserves explicit domain', async () => {
1571
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1572
+ const tempFile = '/tmp/browse-test-cookies-domain.json';
1573
+ const cookies = [{ name: 'explicit', value: 'domain', domain: 'example.com', path: '/foo' }];
1574
+ fs.writeFileSync(tempFile, JSON.stringify(cookies));
1575
+
1576
+ const result = await handleWriteCommand('cookie-import', [tempFile], bm);
1577
+ expect(result).toContain('Loaded 1');
1578
+
1579
+ fs.unlinkSync(tempFile);
1580
+ });
1581
+
1582
+ test('cookie-import with empty array succeeds', async () => {
1583
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1584
+ const tempFile = '/tmp/browse-test-cookies-empty.json';
1585
+ fs.writeFileSync(tempFile, '[]');
1586
+
1587
+ const result = await handleWriteCommand('cookie-import', [tempFile], bm);
1588
+ expect(result).toBe('Loaded 0 cookies from /tmp/browse-test-cookies-empty.json');
1589
+
1590
+ fs.unlinkSync(tempFile);
1591
+ });
1592
+
1593
+ test('cookie-import throws on file not found', async () => {
1594
+ try {
1595
+ await handleWriteCommand('cookie-import', ['/tmp/nonexistent-cookies.json'], bm);
1596
+ expect(true).toBe(false);
1597
+ } catch (err: any) {
1598
+ expect(err.message).toContain('File not found');
1599
+ }
1600
+ });
1601
+
1602
+ test('cookie-import throws on invalid JSON', async () => {
1603
+ const tempFile = '/tmp/browse-test-cookies-bad.json';
1604
+ fs.writeFileSync(tempFile, 'not json {{{');
1605
+
1606
+ try {
1607
+ await handleWriteCommand('cookie-import', [tempFile], bm);
1608
+ expect(true).toBe(false);
1609
+ } catch (err: any) {
1610
+ expect(err.message).toContain('Invalid JSON');
1611
+ }
1612
+
1613
+ fs.unlinkSync(tempFile);
1614
+ });
1615
+
1616
+ test('cookie-import throws on non-array JSON', async () => {
1617
+ const tempFile = '/tmp/browse-test-cookies-obj.json';
1618
+ fs.writeFileSync(tempFile, '{"name": "not-an-array"}');
1619
+
1620
+ try {
1621
+ await handleWriteCommand('cookie-import', [tempFile], bm);
1622
+ expect(true).toBe(false);
1623
+ } catch (err: any) {
1624
+ expect(err.message).toContain('JSON array');
1625
+ }
1626
+
1627
+ fs.unlinkSync(tempFile);
1628
+ });
1629
+
1630
+ test('cookie-import throws on cookie missing name', async () => {
1631
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1632
+ const tempFile = '/tmp/browse-test-cookies-noname.json';
1633
+ fs.writeFileSync(tempFile, JSON.stringify([{ value: 'no-name' }]));
1634
+
1635
+ try {
1636
+ await handleWriteCommand('cookie-import', [tempFile], bm);
1637
+ expect(true).toBe(false);
1638
+ } catch (err: any) {
1639
+ expect(err.message).toContain('name');
1640
+ }
1641
+
1642
+ fs.unlinkSync(tempFile);
1643
+ });
1644
+
1645
+ test('cookie-import no arg throws', async () => {
1646
+ try {
1647
+ await handleWriteCommand('cookie-import', [], bm);
1648
+ expect(true).toBe(false);
1649
+ } catch (err: any) {
1650
+ expect(err.message).toContain('Usage');
1651
+ }
1652
+ });
1653
+ });
1654
+
1655
+ // ─── Security: Redact sensitive values (PR #21) ─────────────────
1656
+
1657
+ describe('Sensitive value redaction', () => {
1658
+ test('type command does not echo typed text', async () => {
1659
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1660
+ const result = await handleWriteCommand('type', ['my-secret-password'], bm);
1661
+ expect(result).not.toContain('my-secret-password');
1662
+ expect(result).toContain('18 characters');
1663
+ });
1664
+
1665
+ test('cookie command redacts value', async () => {
1666
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1667
+ const result = await handleWriteCommand('cookie', ['session=secret123'], bm);
1668
+ expect(result).toContain('session');
1669
+ expect(result).toContain('****');
1670
+ expect(result).not.toContain('secret123');
1671
+ });
1672
+
1673
+ test('header command redacts Authorization value', async () => {
1674
+ const result = await handleWriteCommand('header', ['Authorization:Bearer token-xyz'], bm);
1675
+ expect(result).toContain('Authorization');
1676
+ expect(result).toContain('****');
1677
+ expect(result).not.toContain('token-xyz');
1678
+ });
1679
+
1680
+ test('header command shows non-sensitive values', async () => {
1681
+ const result = await handleWriteCommand('header', ['Content-Type:application/json'], bm);
1682
+ expect(result).toContain('Content-Type');
1683
+ expect(result).toContain('application/json');
1684
+ expect(result).not.toContain('****');
1685
+ });
1686
+
1687
+ test('header command redacts X-API-Key', async () => {
1688
+ const result = await handleWriteCommand('header', ['X-API-Key:sk-12345'], bm);
1689
+ expect(result).toContain('X-API-Key');
1690
+ expect(result).toContain('****');
1691
+ expect(result).not.toContain('sk-12345');
1692
+ });
1693
+
1694
+ test('storage set does not echo value', async () => {
1695
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1696
+ const result = await handleReadCommand('storage', ['set', 'apiKey', 'secret-api-key-value'], bm);
1697
+ expect(result).toContain('apiKey');
1698
+ expect(result).not.toContain('secret-api-key-value');
1699
+ });
1700
+
1701
+ test('forms redacts password field values', async () => {
1702
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
1703
+ const formsResult = await handleReadCommand('forms', [], bm);
1704
+ const forms = JSON.parse(formsResult);
1705
+ // Find password fields and verify they're redacted
1706
+ for (const form of forms) {
1707
+ for (const field of form.fields) {
1708
+ if (field.type === 'password') {
1709
+ expect(field.value === undefined || field.value === '[redacted]').toBe(true);
1710
+ }
1711
+ }
1712
+ }
1713
+ });
1714
+ });
1715
+
1716
+ // ─── Security: Path traversal prevention (PR #26) ───────────────
1717
+
1718
+ describe('Path traversal prevention', () => {
1719
+ test('screenshot rejects path outside safe dirs', async () => {
1720
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1721
+ try {
1722
+ await handleMetaCommand('screenshot', ['/etc/evil.png'], bm, () => {});
1723
+ expect(true).toBe(false);
1724
+ } catch (err: any) {
1725
+ expect(err.message).toContain('Path must be within');
1726
+ }
1727
+ });
1728
+
1729
+ test('screenshot allows /tmp path', async () => {
1730
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1731
+ const result = await handleMetaCommand('screenshot', ['/tmp/test-safe.png'], bm, () => {});
1732
+ expect(result).toContain('Screenshot saved');
1733
+ try { fs.unlinkSync('/tmp/test-safe.png'); } catch {}
1734
+ });
1735
+
1736
+ test('pdf rejects path outside safe dirs', async () => {
1737
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1738
+ try {
1739
+ await handleMetaCommand('pdf', ['/home/evil.pdf'], bm, () => {});
1740
+ expect(true).toBe(false);
1741
+ } catch (err: any) {
1742
+ expect(err.message).toContain('Path must be within');
1743
+ }
1744
+ });
1745
+
1746
+ test('responsive rejects path outside safe dirs', async () => {
1747
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1748
+ try {
1749
+ await handleMetaCommand('responsive', ['/var/evil'], bm, () => {});
1750
+ expect(true).toBe(false);
1751
+ } catch (err: any) {
1752
+ expect(err.message).toContain('Path must be within');
1753
+ }
1754
+ });
1755
+
1756
+ test('eval rejects path traversal with ..', async () => {
1757
+ try {
1758
+ await handleReadCommand('eval', ['../../etc/passwd'], bm);
1759
+ expect(true).toBe(false);
1760
+ } catch (err: any) {
1761
+ expect(err.message).toContain('Path must be within');
1762
+ }
1763
+ });
1764
+
1765
+ test('eval rejects absolute path outside safe dirs', async () => {
1766
+ try {
1767
+ await handleReadCommand('eval', ['/etc/passwd'], bm);
1768
+ expect(true).toBe(false);
1769
+ } catch (err: any) {
1770
+ expect(err.message).toContain('Path must be within');
1771
+ }
1772
+ });
1773
+
1774
+ test('eval allows /tmp path', async () => {
1775
+ const tmpFile = '/tmp/test-eval-safe.js';
1776
+ fs.writeFileSync(tmpFile, 'document.title');
1777
+ try {
1778
+ const result = await handleReadCommand('eval', [tmpFile], bm);
1779
+ expect(typeof result).toBe('string');
1780
+ } finally {
1781
+ try { fs.unlinkSync(tmpFile); } catch {}
1782
+ }
1783
+ });
1784
+
1785
+ test('screenshot rejects /tmpevil prefix collision', async () => {
1786
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1787
+ try {
1788
+ await handleMetaCommand('screenshot', ['/tmpevil/steal.png'], bm, () => {});
1789
+ expect(true).toBe(false);
1790
+ } catch (err: any) {
1791
+ expect(err.message).toContain('Path must be within');
1792
+ }
1793
+ });
1794
+
1795
+ test('cookie-import rejects path traversal', async () => {
1796
+ try {
1797
+ await handleWriteCommand('cookie-import', ['../../etc/shadow'], bm);
1798
+ expect(true).toBe(false);
1799
+ } catch (err: any) {
1800
+ expect(err.message).toContain('Path traversal');
1801
+ }
1802
+ });
1803
+
1804
+ test('cookie-import rejects absolute path outside safe dirs', async () => {
1805
+ try {
1806
+ await handleWriteCommand('cookie-import', ['/etc/passwd'], bm);
1807
+ expect(true).toBe(false);
1808
+ } catch (err: any) {
1809
+ expect(err.message).toContain('Path must be within');
1810
+ }
1811
+ });
1812
+
1813
+ test('snapshot -a -o rejects path outside safe dirs', async () => {
1814
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1815
+ // First get a snapshot so refs exist
1816
+ await handleMetaCommand('snapshot', ['-i'], bm, () => {});
1817
+ try {
1818
+ await handleMetaCommand('snapshot', ['-a', '-o', '/etc/evil.png'], bm, () => {});
1819
+ expect(true).toBe(false);
1820
+ } catch (err: any) {
1821
+ expect(err.message).toContain('Path must be within');
1822
+ }
1823
+ });
1824
+ });
1825
+
1826
+ // ─── Chain command: cookie-import in chain ──────────────────────
1827
+
1828
+ describe('Chain with cookie-import', () => {
1829
+ test('cookie-import works inside chain', async () => {
1830
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1831
+ const tmpCookies = '/tmp/test-chain-cookies.json';
1832
+ fs.writeFileSync(tmpCookies, JSON.stringify([
1833
+ { name: 'chain_test', value: 'chain_value', domain: 'localhost', path: '/' }
1834
+ ]));
1835
+ try {
1836
+ const commands = JSON.stringify([
1837
+ ['cookie-import', tmpCookies],
1838
+ ]);
1839
+ const result = await handleMetaCommand('chain', [commands], bm, async () => {});
1840
+ expect(result).toContain('[cookie-import]');
1841
+ expect(result).toContain('Loaded 1 cookie');
1842
+ } finally {
1843
+ try { fs.unlinkSync(tmpCookies); } catch {}
1844
+ }
1845
+ });
1846
+ });
1847
+
1848
+ // ─── Network Idle Detection ─────────────────────────────────────
1849
+
1850
+ describe('Network idle', () => {
1851
+ test('click on fetch button waits for XHR to complete', async () => {
1852
+ await handleWriteCommand('goto', [baseUrl + '/network-idle.html'], bm);
1853
+ // Click the button that triggers a fetch → networkidle waits for it
1854
+ await handleWriteCommand('click', ['#fetch-btn'], bm);
1855
+ // The DOM should be updated by the time click returns
1856
+ const result = await handleReadCommand('js', ['document.getElementById("result").textContent'], bm);
1857
+ expect(result).toContain('Data loaded');
1858
+ });
1859
+
1860
+ test('click on static button has no latency penalty', async () => {
1861
+ await handleWriteCommand('goto', [baseUrl + '/network-idle.html'], bm);
1862
+ const start = Date.now();
1863
+ await handleWriteCommand('click', ['#static-btn'], bm);
1864
+ const elapsed = Date.now() - start;
1865
+ // Static click should complete well under 2s (the networkidle timeout)
1866
+ // networkidle resolves immediately when no requests are in flight
1867
+ expect(elapsed).toBeLessThan(1500);
1868
+ const result = await handleReadCommand('js', ['document.getElementById("static-result").textContent'], bm);
1869
+ expect(result).toBe('Static action done');
1870
+ });
1871
+
1872
+ test('fill triggers networkidle wait', async () => {
1873
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
1874
+ // fill should complete without error (networkidle resolves immediately on static page)
1875
+ const result = await handleWriteCommand('fill', ['#email', 'idle@test.com'], bm);
1876
+ expect(result).toContain('Filled');
1877
+ });
1878
+ });
1879
+
1880
+ // ─── Chain Pipe Format ──────────────────────────────────────────
1881
+
1882
+ describe('Chain pipe format', () => {
1883
+ test('pipe-delimited commands work', async () => {
1884
+ const result = await handleMetaCommand(
1885
+ 'chain',
1886
+ [`goto ${baseUrl}/basic.html | js document.title`],
1887
+ bm,
1888
+ async () => {}
1889
+ );
1890
+ expect(result).toContain('[goto]');
1891
+ expect(result).toContain('[js]');
1892
+ expect(result).toContain('Test Page - Basic');
1893
+ });
1894
+
1895
+ test('pipe format with quoted args', async () => {
1896
+ const result = await handleMetaCommand(
1897
+ 'chain',
1898
+ [`goto ${baseUrl}/forms.html | fill #email "pipe@test.com"`],
1899
+ bm,
1900
+ async () => {}
1901
+ );
1902
+ expect(result).toContain('[fill]');
1903
+ expect(result).toContain('Filled');
1904
+ // Verify the fill actually worked
1905
+ const val = await handleReadCommand('js', ['document.querySelector("#email").value'], bm);
1906
+ expect(val).toBe('pipe@test.com');
1907
+ });
1908
+
1909
+ test('JSON format still works', async () => {
1910
+ const commands = JSON.stringify([
1911
+ ['goto', baseUrl + '/basic.html'],
1912
+ ['js', 'document.title'],
1913
+ ]);
1914
+ const result = await handleMetaCommand('chain', [commands], bm, async () => {});
1915
+ expect(result).toContain('[goto]');
1916
+ expect(result).toContain('Test Page - Basic');
1917
+ });
1918
+
1919
+ test('pipe format with unknown command includes error', async () => {
1920
+ const result = await handleMetaCommand(
1921
+ 'chain',
1922
+ ['bogus command'],
1923
+ bm,
1924
+ async () => {}
1925
+ );
1926
+ expect(result).toContain('ERROR');
1927
+ expect(result).toContain('Unknown command: bogus');
1928
+ });
1929
+ });
1930
+
1931
+ // ─── State Persistence ──────────────────────────────────────────
1932
+
1933
+ describe('State persistence', () => {
1934
+ test('state save and load round-trip', async () => {
1935
+ await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm);
1936
+ // Set a cookie so we can verify it persists
1937
+ await handleWriteCommand('cookie', ['state_test=hello'], bm);
1938
+
1939
+ // Save state
1940
+ const saveResult = await handleMetaCommand('state', ['save', 'test-roundtrip'], bm, async () => {});
1941
+ expect(saveResult).toContain('State saved');
1942
+ expect(saveResult).toContain('Cookies stored in plaintext');
1943
+
1944
+ // Navigate away
1945
+ await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
1946
+
1947
+ // Load state — should restore to basic.html with cookie
1948
+ const loadResult = await handleMetaCommand('state', ['load', 'test-roundtrip'], bm, async () => {});
1949
+ expect(loadResult).toContain('State loaded');
1950
+
1951
+ // Verify we're back on basic.html
1952
+ const url = await handleReadCommand('js', ['location.pathname'], bm);
1953
+ expect(url).toContain('basic.html');
1954
+
1955
+ // Clean up
1956
+ try {
1957
+ const { resolveConfig } = await import('../src/config');
1958
+ const config = resolveConfig();
1959
+ fs.unlinkSync(`${config.stateDir}/browse-states/test-roundtrip.json`);
1960
+ } catch {}
1961
+ });
1962
+
1963
+ test('state save rejects invalid names', async () => {
1964
+ try {
1965
+ await handleMetaCommand('state', ['save', '../../evil'], bm, async () => {});
1966
+ expect(true).toBe(false);
1967
+ } catch (err: any) {
1968
+ expect(err.message).toContain('alphanumeric');
1969
+ }
1970
+ });
1971
+
1972
+ test('state save accepts valid names', async () => {
1973
+ const result = await handleMetaCommand('state', ['save', 'my-state_1'], bm, async () => {});
1974
+ expect(result).toContain('State saved');
1975
+ // Clean up
1976
+ try {
1977
+ const { resolveConfig } = await import('../src/config');
1978
+ const config = resolveConfig();
1979
+ fs.unlinkSync(`${config.stateDir}/browse-states/my-state_1.json`);
1980
+ } catch {}
1981
+ });
1982
+
1983
+ test('state load rejects missing state', async () => {
1984
+ try {
1985
+ await handleMetaCommand('state', ['load', 'nonexistent-state-xyz'], bm, async () => {});
1986
+ expect(true).toBe(false);
1987
+ } catch (err: any) {
1988
+ expect(err.message).toContain('State not found');
1989
+ }
1990
+ });
1991
+
1992
+ test('state requires action and name', async () => {
1993
+ try {
1994
+ await handleMetaCommand('state', [], bm, async () => {});
1995
+ expect(true).toBe(false);
1996
+ } catch (err: any) {
1997
+ expect(err.message).toContain('Usage');
1998
+ }
1999
+ });
2000
+ });
2001
+
2002
+ // ─── Frame (Iframe Support) ─────────────────────────────────────
2003
+
2004
+ describe('Frame', () => {
2005
+ test('frame switch to iframe and back', async () => {
2006
+ await handleWriteCommand('goto', [baseUrl + '/iframe.html'], bm);
2007
+
2008
+ // Verify we're on the main page
2009
+ const mainTitle = await handleReadCommand('js', ['document.getElementById("main-title").textContent'], bm);
2010
+ expect(mainTitle).toBe('Main Page');
2011
+
2012
+ // Switch to iframe by CSS selector
2013
+ const switchResult = await handleMetaCommand('frame', ['#test-frame'], bm, async () => {});
2014
+ expect(switchResult).toContain('Switched to frame');
2015
+
2016
+ // Verify we can read iframe content
2017
+ const frameTitle = await handleReadCommand('js', ['document.getElementById("frame-title").textContent'], bm);
2018
+ expect(frameTitle).toBe('Inside Frame');
2019
+
2020
+ // Switch back to main
2021
+ const mainResult = await handleMetaCommand('frame', ['main'], bm, async () => {});
2022
+ expect(mainResult).toBe('Switched to main frame');
2023
+
2024
+ // Verify we're back on the main page
2025
+ const mainTitleAgain = await handleReadCommand('js', ['document.getElementById("main-title").textContent'], bm);
2026
+ expect(mainTitleAgain).toBe('Main Page');
2027
+ });
2028
+
2029
+ test('snapshot shows frame context header', async () => {
2030
+ await handleWriteCommand('goto', [baseUrl + '/iframe.html'], bm);
2031
+ await handleMetaCommand('frame', ['#test-frame'], bm, async () => {});
2032
+
2033
+ const snap = await handleMetaCommand('snapshot', ['-i'], bm, async () => {});
2034
+ expect(snap).toContain('[Context: iframe');
2035
+
2036
+ // Clean up — return to main
2037
+ await handleMetaCommand('frame', ['main'], bm, async () => {});
2038
+ });
2039
+
2040
+ test('goto throws error when in frame context', async () => {
2041
+ await handleWriteCommand('goto', [baseUrl + '/iframe.html'], bm);
2042
+ await handleMetaCommand('frame', ['#test-frame'], bm, async () => {});
2043
+
2044
+ try {
2045
+ await handleWriteCommand('goto', ['https://example.com'], bm);
2046
+ expect(true).toBe(false);
2047
+ } catch (err: any) {
2048
+ expect(err.message).toContain('Cannot use goto inside a frame');
2049
+ }
2050
+
2051
+ await handleMetaCommand('frame', ['main'], bm, async () => {});
2052
+ });
2053
+
2054
+ test('frame requires argument', async () => {
2055
+ try {
2056
+ await handleMetaCommand('frame', [], bm, async () => {});
2057
+ expect(true).toBe(false);
2058
+ } catch (err: any) {
2059
+ expect(err.message).toContain('Usage');
2060
+ }
2061
+ });
2062
+
2063
+ test('fill works inside iframe', async () => {
2064
+ await handleWriteCommand('goto', [baseUrl + '/iframe.html'], bm);
2065
+ await handleMetaCommand('frame', ['#test-frame'], bm, async () => {});
2066
+
2067
+ const result = await handleWriteCommand('fill', ['#frame-input', 'hello from frame'], bm);
2068
+ expect(result).toContain('Filled');
2069
+
2070
+ const value = await handleReadCommand('js', ['document.getElementById("frame-input").value'], bm);
2071
+ expect(value).toBe('hello from frame');
2072
+
2073
+ await handleMetaCommand('frame', ['main'], bm, async () => {});
2074
+ });
2075
+ });