@jackwener/opencli 0.7.0 → 0.7.2

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.
@@ -0,0 +1,521 @@
1
+ /**
2
+ * Tests for snapshotFormatter.ts: Playwright MCP snapshot tree filtering.
3
+ *
4
+ * Uses sanitized excerpts from real websites (GitHub, Bilibili, Twitter)
5
+ * to validate noise filtering, annotation stripping, and output quality.
6
+ */
7
+ import { describe, it, expect } from 'vitest';
8
+ import { formatSnapshot } from './snapshotFormatter.js';
9
+ // ---------------------------------------------------------------------------
10
+ // Fixtures: sanitized excerpts from real Playwright MCP snapshots
11
+ // ---------------------------------------------------------------------------
12
+ /** GitHub dashboard navigation bar (generic-heavy, refs, /url: lines) */
13
+ const GITHUB_NAV = `\
14
+ - generic [ref=e2]:
15
+ - region
16
+ - generic [ref=e3]:
17
+ - link "Skip to content" [ref=e4] [cursor=pointer]:
18
+ - /url: "#start-of-content"
19
+ - banner "Global Navigation Menu" [ref=e8]:
20
+ - generic [ref=e9]:
21
+ - generic [ref=e10]:
22
+ - button "Open menu" [ref=e12] [cursor=pointer]:
23
+ - img [ref=e13]
24
+ - link "Homepage" [ref=e15] [cursor=pointer]:
25
+ - /url: /
26
+ - img [ref=e16]
27
+ - generic [ref=e18]:
28
+ - navigation "Breadcrumbs" [ref=e19]:
29
+ - list [ref=e20]:
30
+ - listitem [ref=e21]:
31
+ - link "Dashboard" [ref=e22] [cursor=pointer]:
32
+ - /url: https://github.com/
33
+ - generic [ref=e23]: Dashboard
34
+ - button "Search or jump to…" [ref=e26] [cursor=pointer]:
35
+ - generic [ref=e27]:
36
+ - generic:
37
+ - img
38
+ - generic [ref=e28]:
39
+ - generic:
40
+ - text: Type
41
+ - generic: /
42
+ - text: to search`;
43
+ /** GitHub repo list sidebar (repetitive structure) */
44
+ const GITHUB_REPOS = `\
45
+ - navigation "Repositories" [ref=e79]:
46
+ - generic [ref=e80]:
47
+ - generic [ref=e81]:
48
+ - heading "Top repositories" [level=2] [ref=e82]
49
+ - link "New" [ref=e83] [cursor=pointer]:
50
+ - /url: /new
51
+ - generic [ref=e84]:
52
+ - generic:
53
+ - img
54
+ - generic [ref=e85]: New
55
+ - search "Top repositories" [ref=e86]:
56
+ - textbox "Find a repository…" [ref=e87]
57
+ - list [ref=e88]:
58
+ - listitem [ref=e89]:
59
+ - generic [ref=e90]:
60
+ - link "Repository" [ref=e91] [cursor=pointer]:
61
+ - /url: /jackwener/twitter-cli
62
+ - img "Repository" [ref=e92]
63
+ - link "jackwener/twitter-cli" [ref=e94] [cursor=pointer]:
64
+ - /url: /jackwener/twitter-cli
65
+ - listitem [ref=e95]:
66
+ - generic [ref=e96]:
67
+ - link "Repository" [ref=e97] [cursor=pointer]:
68
+ - /url: /jackwener/opencli
69
+ - img "Repository" [ref=e98]
70
+ - link "jackwener/opencli" [ref=e100] [cursor=pointer]:
71
+ - /url: /jackwener/opencli`;
72
+ /** Bilibili nav bar (Chinese text, multiple link categories) */
73
+ const BILIBILI_NAV = `\
74
+ - generic [ref=e3]:
75
+ - generic [ref=e4]:
76
+ - generic [ref=e5]:
77
+ - list [ref=e6]:
78
+ - listitem [ref=e7]:
79
+ - link "首页" [ref=e8] [cursor=pointer]:
80
+ - /url: //www.bilibili.com
81
+ - img [ref=e9]
82
+ - generic [ref=e11]: 首页
83
+ - listitem [ref=e12]:
84
+ - link "番剧" [ref=e13] [cursor=pointer]:
85
+ - /url: //www.bilibili.com/anime/
86
+ - listitem [ref=e14]:
87
+ - link "直播" [ref=e15] [cursor=pointer]:
88
+ - /url: //live.bilibili.com
89
+ - generic [ref=e32]:
90
+ - textbox "冷知识 金廷26年胜率100%" [ref=e34]
91
+ - img [ref=e36] [cursor=pointer]`;
92
+ /** Bilibili video card (deeply nested generic wrappers, view counts) */
93
+ const BILIBILI_VIDEO = `\
94
+ - generic [ref=e363]:
95
+ - link "超酷时刻 即将到来 3.3万 40 16:24" [ref=e364] [cursor=pointer]:
96
+ - /url: https://www.bilibili.com/video/BV1zVw5zoEFt
97
+ - generic [ref=e365]:
98
+ - img "超酷时刻 即将到来" [ref=e368]
99
+ - generic:
100
+ - generic:
101
+ - generic:
102
+ - generic:
103
+ - img
104
+ - generic: 3.3万
105
+ - generic:
106
+ - img
107
+ - generic: "40"
108
+ - generic: 16:24
109
+ - generic [ref=e370]:
110
+ - heading "超酷时刻 即将到来" [level=3] [ref=e371]:
111
+ - link "超酷时刻 即将到来" [ref=e372] [cursor=pointer]:
112
+ - /url: https://www.bilibili.com/video/BV1zVw5zoEFt
113
+ - link "Tesla特斯拉中国 · 13小时前" [ref=e374] [cursor=pointer]:
114
+ - /url: //space.bilibili.com/491190876
115
+ - img [ref=e375]
116
+ - generic "Tesla特斯拉中国" [ref=e379]
117
+ - generic [ref=e380]: · 13小时前`;
118
+ /** Empty paragraph blocks (Bilibili bottom section) */
119
+ const BILIBILI_EMPTY = `\
120
+ - generic [ref=e576]:
121
+ - generic:
122
+ - generic:
123
+ - generic:
124
+ - paragraph
125
+ - paragraph
126
+ - paragraph
127
+ - generic [ref=e577]:
128
+ - generic:
129
+ - generic:
130
+ - generic:
131
+ - paragraph
132
+ - paragraph
133
+ - paragraph`;
134
+ /** Twitter-style feed item (simulated based on common patterns) */
135
+ const TWITTER_TWEET = `\
136
+ - main [ref=e100]:
137
+ - region "Timeline" [ref=e101]:
138
+ - article [ref=e200]:
139
+ - generic [ref=e201]:
140
+ - generic [ref=e202]:
141
+ - link "@elonmusk" [ref=e203] [cursor=pointer]:
142
+ - /url: /elonmusk
143
+ - img "@elonmusk" [ref=e204]
144
+ - generic [ref=e205]:
145
+ - generic [ref=e206]: Elon Musk
146
+ - generic [ref=e207]: @elonmusk
147
+ - generic [ref=e208]:
148
+ - generic [ref=e209]: This is a very long tweet that goes on and on about various things including technology, space, and other random topics that make this text exceed any reasonable length limit we might want to set for display purposes in a CLI interface.
149
+ - generic [ref=e210]:
150
+ - button "Reply" [ref=e211] [cursor=pointer]:
151
+ - img [ref=e212]
152
+ - generic [ref=e213]: "42"
153
+ - button "Retweet" [ref=e214] [cursor=pointer]:
154
+ - img [ref=e215]
155
+ - generic [ref=e216]: "1.2K"
156
+ - button "Like" [ref=e217] [cursor=pointer]:
157
+ - img [ref=e218]
158
+ - generic [ref=e219]: "5.3K"
159
+ - separator [ref=e300]`;
160
+ // ---------------------------------------------------------------------------
161
+ // Tests
162
+ // ---------------------------------------------------------------------------
163
+ describe('formatSnapshot', () => {
164
+ describe('basic behavior', () => {
165
+ it('returns empty string for empty/null input', () => {
166
+ expect(formatSnapshot('')).toBe('');
167
+ expect(formatSnapshot(null)).toBe('');
168
+ expect(formatSnapshot(undefined)).toBe('');
169
+ });
170
+ it('strips [ref=...] and [cursor=...] annotations', () => {
171
+ const input = '- button "Click me" [ref=e42] [cursor=pointer]';
172
+ const result = formatSnapshot(input);
173
+ expect(result).not.toContain('[ref=');
174
+ expect(result).not.toContain('[cursor=');
175
+ expect(result).toContain('button "Click me"');
176
+ });
177
+ it('removes /url: metadata lines', () => {
178
+ const input = `\
179
+ - link "Home" [ref=e1] [cursor=pointer]:
180
+ - /url: https://example.com
181
+ - generic [ref=e2]: Home`;
182
+ const result = formatSnapshot(input);
183
+ expect(result).not.toContain('/url:');
184
+ expect(result).not.toContain('https://example.com');
185
+ });
186
+ it('assigns sequential [@N] refs to interactive elements', () => {
187
+ const input = `\
188
+ - button "Save" [ref=e1]
189
+ - link "Cancel" [ref=e2]
190
+ - textbox "Name" [ref=e3]`;
191
+ const result = formatSnapshot(input);
192
+ expect(result).toContain('[@1] button "Save"');
193
+ expect(result).toContain('[@2] link "Cancel"');
194
+ expect(result).toContain('[@3] textbox "Name"');
195
+ });
196
+ });
197
+ describe('noise filtering', () => {
198
+ it('removes generic nodes without text', () => {
199
+ const input = `\
200
+ - generic [ref=e1]:
201
+ - generic [ref=e2]:
202
+ - button "Click" [ref=e3]`;
203
+ const result = formatSnapshot(input);
204
+ expect(result).not.toMatch(/^generic/m);
205
+ expect(result).toContain('button "Click"');
206
+ });
207
+ it('keeps generic nodes WITH text content', () => {
208
+ const input = '- generic [ref=e23]: Dashboard';
209
+ const result = formatSnapshot(input);
210
+ expect(result).toContain('generic: Dashboard');
211
+ });
212
+ it('removes img nodes without alt text', () => {
213
+ const input = `\
214
+ - img [ref=e13]
215
+ - img "Profile photo" [ref=e14]`;
216
+ const result = formatSnapshot(input);
217
+ expect(result).not.toContain('img\n');
218
+ expect(result).toContain('img "Profile photo"');
219
+ });
220
+ it('removes separator nodes', () => {
221
+ const input = '- separator [ref=e304]';
222
+ const result = formatSnapshot(input);
223
+ expect(result).toBe('');
224
+ });
225
+ it('removes presentation/none roles', () => {
226
+ const input = `\
227
+ - presentation [ref=e1]
228
+ - none [ref=e2]
229
+ - button "OK" [ref=e3]`;
230
+ const result = formatSnapshot(input);
231
+ expect(result).not.toContain('presentation');
232
+ expect(result).not.toContain('none');
233
+ expect(result).toContain('button "OK"');
234
+ });
235
+ });
236
+ describe('empty container pruning', () => {
237
+ it('prunes containers with no visible children', () => {
238
+ const input = `\
239
+ - list [ref=e88]:
240
+ - listitem [ref=e89]:
241
+ - generic [ref=e90]:
242
+ - img [ref=e91]`;
243
+ // After filtering: generic (no text) → removed, img (no alt) → removed
244
+ // listitem becomes empty → pruned, list becomes empty → pruned
245
+ const result = formatSnapshot(input);
246
+ expect(result).toBe('');
247
+ });
248
+ it('keeps containers with visible children', () => {
249
+ const input = `\
250
+ - list [ref=e1]:
251
+ - listitem [ref=e2]:
252
+ - link "Home" [ref=e3]`;
253
+ const result = formatSnapshot(input);
254
+ expect(result).toContain('list');
255
+ expect(result).toContain('listitem');
256
+ expect(result).toContain('link "Home"');
257
+ });
258
+ });
259
+ describe('maxDepth option', () => {
260
+ it('limits output to specified depth', () => {
261
+ const input = `\
262
+ - main [ref=e1]:
263
+ - heading "Dashboard" [ref=e2]
264
+ - navigation [ref=e3]:
265
+ - list [ref=e4]:
266
+ - link "Deep link" [ref=e5]`;
267
+ const result = formatSnapshot(input, { maxDepth: 2 });
268
+ expect(result).toContain('main');
269
+ expect(result).toContain('heading "Dashboard"');
270
+ // navigation is pruned: its only child list is empty after link is excluded by maxDepth
271
+ expect(result).not.toContain('navigation');
272
+ expect(result).not.toContain('Deep link');
273
+ });
274
+ it('handles maxDepth=0 correctly (was a bug)', () => {
275
+ const input = `\
276
+ - heading "Title" [ref=e1]
277
+ - link "Sub" [ref=e2]`;
278
+ const result = formatSnapshot(input, { maxDepth: 0 });
279
+ expect(result).toContain('heading "Title"');
280
+ expect(result).not.toContain('Sub');
281
+ });
282
+ });
283
+ describe('interactive mode', () => {
284
+ it('keeps interactive elements and landmarks', () => {
285
+ const result = formatSnapshot(GITHUB_NAV, { interactive: true });
286
+ // Interactive elements should be present
287
+ expect(result).toContain('button');
288
+ expect(result).toContain('link');
289
+ // Landmarks preserved
290
+ expect(result).toContain('banner');
291
+ expect(result).toContain('navigation');
292
+ });
293
+ it('filters non-interactive, non-landmark, textless nodes', () => {
294
+ const input = `\
295
+ - main [ref=e1]:
296
+ - generic [ref=e2]:
297
+ - generic [ref=e3]:
298
+ - button "Save" [ref=e4]
299
+ - generic [ref=e5]: some text content`;
300
+ const result = formatSnapshot(input, { interactive: true });
301
+ expect(result).toContain('main');
302
+ expect(result).toContain('button "Save"');
303
+ // generic with text is kept
304
+ expect(result).toContain('generic: some text content');
305
+ });
306
+ });
307
+ describe('compact mode', () => {
308
+ it('strips bracket annotations and collapses whitespace', () => {
309
+ const input = '- button "Save" [ref=e1] [cursor=pointer] [level=2]';
310
+ const result = formatSnapshot(input, { compact: true });
311
+ // ref/cursor already stripped, but [level=...] should also go in compact
312
+ expect(result).not.toContain('[level=');
313
+ expect(result).toContain('button');
314
+ });
315
+ });
316
+ describe('maxTextLength option', () => {
317
+ it('truncates long content lines', () => {
318
+ const input = '- heading "This is a very long heading that should be truncated at some point" [ref=e1]';
319
+ const result = formatSnapshot(input, { maxTextLength: 30 });
320
+ expect(result.length).toBeLessThanOrEqual(35); // some tolerance for ellipsis
321
+ expect(result).toContain('…');
322
+ });
323
+ });
324
+ // ---------------------------------------------------------------------------
325
+ // Real-world snapshot integration tests
326
+ // ---------------------------------------------------------------------------
327
+ describe('GitHub snapshot', () => {
328
+ it('drastically reduces nav bar output', () => {
329
+ const raw = GITHUB_NAV;
330
+ const rawLineCount = raw.split('\n').length;
331
+ const result = formatSnapshot(raw);
332
+ const resultLineCount = result.split('\n').length;
333
+ // Should significantly reduce line count
334
+ expect(resultLineCount).toBeLessThan(rawLineCount);
335
+ // Key content preserved
336
+ expect(result).toContain('link "Skip to content"');
337
+ expect(result).toContain('banner "Global Navigation Menu"');
338
+ expect(result).toContain('link "Dashboard"');
339
+ expect(result).toContain('button "Search or jump to…"');
340
+ // Noise removed
341
+ expect(result).not.toContain('[ref=');
342
+ expect(result).not.toContain('/url:');
343
+ });
344
+ it('preserves repo list structure', () => {
345
+ const result = formatSnapshot(GITHUB_REPOS);
346
+ expect(result).toContain('navigation "Repositories"');
347
+ expect(result).toContain('heading "Top repositories"');
348
+ expect(result).toContain('textbox "Find a repository…"');
349
+ expect(result).toContain('link "jackwener/twitter-cli"');
350
+ expect(result).toContain('link "jackwener/opencli"');
351
+ expect(result).toContain('img "Repository"');
352
+ // No refs or urls
353
+ expect(result).not.toContain('[ref=');
354
+ expect(result).not.toContain('/url:');
355
+ });
356
+ });
357
+ describe('Bilibili snapshot', () => {
358
+ it('cleans nav bar with Chinese text', () => {
359
+ const result = formatSnapshot(BILIBILI_NAV);
360
+ expect(result).toContain('link "首页"');
361
+ expect(result).toContain('link "番剧"');
362
+ expect(result).toContain('link "直播"');
363
+ expect(result).toContain('textbox "冷知识 金廷26年胜率100%"');
364
+ expect(result).not.toContain('[ref=');
365
+ });
366
+ it('handles video card with deeply nested wrappers', () => {
367
+ const result = formatSnapshot(BILIBILI_VIDEO);
368
+ expect(result).toContain('link "超酷时刻 即将到来 3.3万 40 16:24"');
369
+ expect(result).toContain('heading "超酷时刻 即将到来"');
370
+ expect(result).toContain('generic "Tesla特斯拉中国"');
371
+ // Deeply nested view count generics with text are kept
372
+ expect(result).toContain('3.3万');
373
+ });
374
+ it('prunes empty paragraph blocks', () => {
375
+ const result = formatSnapshot(BILIBILI_EMPTY);
376
+ // All content is generic (no text) and empty paragraphs
377
+ // After noise filtering, everything should be pruned
378
+ expect(result.trim()).toBe('');
379
+ });
380
+ });
381
+ describe('Twitter snapshot', () => {
382
+ it('preserves tweet structure', () => {
383
+ const result = formatSnapshot(TWITTER_TWEET);
384
+ expect(result).toContain('main');
385
+ expect(result).toContain('region "Timeline"');
386
+ expect(result).toContain('link "@elonmusk"');
387
+ expect(result).toContain('button "Reply"');
388
+ expect(result).toContain('button "Like"');
389
+ expect(result).not.toContain('separator');
390
+ });
391
+ it('truncates long tweet text with maxTextLength', () => {
392
+ const result = formatSnapshot(TWITTER_TWEET, { maxTextLength: 60 });
393
+ // The long tweet text should be truncated
394
+ expect(result).toContain('…');
395
+ // But short elements are unaffected
396
+ expect(result).toContain('button "Reply"');
397
+ });
398
+ it('interactive mode keeps only buttons and links', () => {
399
+ const result = formatSnapshot(TWITTER_TWEET, { interactive: true });
400
+ expect(result).toContain('link "@elonmusk"');
401
+ expect(result).toContain('button "Reply"');
402
+ expect(result).toContain('button "Retweet"');
403
+ expect(result).toContain('button "Like"');
404
+ // Structural landmarks kept
405
+ expect(result).toContain('main');
406
+ expect(result).toContain('region "Timeline"');
407
+ expect(result).toContain('article');
408
+ });
409
+ it('combined options: interactive + maxDepth', () => {
410
+ // With maxDepth: 2 and interactive, depth > 2 is filtered.
411
+ // article at depth 2 has only generic children (noise-filtered),
412
+ // so article gets pruned by container pruning, which cascades up.
413
+ const result = formatSnapshot(TWITTER_TWEET, { interactive: true, maxDepth: 2 });
414
+ expect(result).toContain('main');
415
+ expect(result).not.toContain('button "Reply"');
416
+ expect(result).not.toContain('link "@elonmusk"');
417
+ });
418
+ });
419
+ describe('reduction ratios on real data', () => {
420
+ it('achieves significant reduction on GitHub nav', () => {
421
+ const rawLines = GITHUB_NAV.split('\n').length;
422
+ const formatted = formatSnapshot(GITHUB_NAV);
423
+ const formattedLines = formatted.split('\n').filter(l => l.trim()).length;
424
+ // Expect at least 40% reduction
425
+ expect(formattedLines).toBeLessThan(rawLines * 0.6);
426
+ });
427
+ it('achieves significant reduction on Bilibili video card', () => {
428
+ const rawLines = BILIBILI_VIDEO.split('\n').length;
429
+ const formatted = formatSnapshot(BILIBILI_VIDEO);
430
+ const formattedLines = formatted.split('\n').filter(l => l.trim()).length;
431
+ // Expect at least 30% reduction
432
+ expect(formattedLines).toBeLessThan(rawLines * 0.7);
433
+ });
434
+ });
435
+ // ---------------------------------------------------------------------------
436
+ // Full-page snapshot fixture tests (loaded from __fixtures__/)
437
+ // ---------------------------------------------------------------------------
438
+ describe('full-page snapshots from fixtures', () => {
439
+ const fs = require('node:fs');
440
+ const path = require('node:path');
441
+ const fixturesDir = path.join(__dirname, '__fixtures__');
442
+ function loadFixture(name) {
443
+ const p = path.join(fixturesDir, name);
444
+ if (!fs.existsSync(p))
445
+ return null;
446
+ return fs.readFileSync(p, 'utf-8');
447
+ }
448
+ it('GitHub: significant reduction and clean output', () => {
449
+ const raw = loadFixture('snapshot_github.txt');
450
+ if (!raw)
451
+ return;
452
+ const rawLines = raw.split('\n').length;
453
+ const result = formatSnapshot(raw);
454
+ const resultLines = result.split('\n').filter((l) => l.trim()).length;
455
+ // Should achieve > 50% reduction on GitHub dashboard (heavy generic noise)
456
+ expect(resultLines).toBeLessThan(rawLines * 0.5);
457
+ // No annotations remain
458
+ expect(result).not.toContain('[ref=');
459
+ expect(result).not.toContain('[cursor=');
460
+ expect(result).not.toContain('/url:');
461
+ // Key content preserved
462
+ expect(result).toContain('link "Skip to content"');
463
+ expect(result).toContain('banner "Global Navigation Menu"');
464
+ expect(result).toContain('heading "Dashboard"');
465
+ });
466
+ it('Bilibili: significant reduction and Chinese text preserved', () => {
467
+ const raw = loadFixture('snapshot_bilibili.txt');
468
+ if (!raw)
469
+ return;
470
+ const rawLines = raw.split('\n').length;
471
+ const result = formatSnapshot(raw);
472
+ const resultLines = result.split('\n').filter((l) => l.trim()).length;
473
+ // Should achieve > 40% reduction on Bilibili (lots of imgs and generics)
474
+ expect(resultLines).toBeLessThan(rawLines * 0.6);
475
+ // No annotations remain
476
+ expect(result).not.toContain('[ref=');
477
+ expect(result).not.toContain('[cursor=');
478
+ // Chinese text preserved
479
+ expect(result).toContain('link "首页"');
480
+ expect(result).toContain('link "番剧"');
481
+ });
482
+ it('Twitter/X: significant reduction and tweet structure preserved', () => {
483
+ const raw = loadFixture('snapshot_twitter.txt');
484
+ if (!raw)
485
+ return;
486
+ const rawLines = raw.split('\n').length;
487
+ const result = formatSnapshot(raw);
488
+ const resultLines = result.split('\n').filter((l) => l.trim()).length;
489
+ // Should achieve > 40% reduction on Twitter/X
490
+ expect(resultLines).toBeLessThan(rawLines * 0.6);
491
+ // No annotations remain
492
+ expect(result).not.toContain('[ref=');
493
+ expect(result).not.toContain('[cursor=');
494
+ expect(result).not.toContain('/url:');
495
+ // Key structure preserved
496
+ expect(result).toContain('main');
497
+ });
498
+ it('GitHub interactive mode: drastic reduction', () => {
499
+ const raw = loadFixture('snapshot_github.txt');
500
+ if (!raw)
501
+ return;
502
+ const result = formatSnapshot(raw, { interactive: true });
503
+ const resultLines = result.split('\n').filter((l) => l.trim()).length;
504
+ // Interactive mode should be much more aggressive
505
+ expect(resultLines).toBeLessThan(200);
506
+ // Interactive elements still present
507
+ expect(result).toContain('button');
508
+ expect(result).toContain('link');
509
+ expect(result).toContain('textbox');
510
+ });
511
+ it('Bilibili maxDepth=3: shallow view', () => {
512
+ const raw = loadFixture('snapshot_bilibili.txt');
513
+ if (!raw)
514
+ return;
515
+ const result = formatSnapshot(raw, { maxDepth: 3 });
516
+ const resultLines = result.split('\n').filter((l) => l.trim()).length;
517
+ // Depth-limited should be very compact
518
+ expect(resultLines).toBeLessThan(50);
519
+ });
520
+ });
521
+ });
@@ -1,2 +1,14 @@
1
- export declare function validateClisWithTarget(dirs: string[], target?: string): any;
2
- export declare function renderValidationReport(report: any): string;
1
+ export interface FileValidationResult {
2
+ path: string;
3
+ errors: string[];
4
+ warnings: string[];
5
+ }
6
+ export interface ValidationReport {
7
+ ok: boolean;
8
+ results: FileValidationResult[];
9
+ errors: number;
10
+ warnings: number;
11
+ files: number;
12
+ }
13
+ export declare function validateClisWithTarget(dirs: string[], target?: string): ValidationReport;
14
+ export declare function renderValidationReport(report: ValidationReport): string;
package/dist/verify.d.ts CHANGED
@@ -5,5 +5,17 @@
5
5
  * smoke testing requires a running browser session and is better suited
6
6
  * to the `opencli test` command or CI pipelines.
7
7
  */
8
- export declare function verifyClis(opts: any): Promise<any>;
9
- export declare function renderVerifyReport(report: any): string;
8
+ import { type ValidationReport } from './validate.js';
9
+ export interface VerifyOptions {
10
+ builtinClis: string;
11
+ userClis: string;
12
+ target?: string;
13
+ smoke?: boolean;
14
+ }
15
+ export interface VerifyReport {
16
+ ok: boolean;
17
+ validation: ValidationReport;
18
+ smoke: null;
19
+ }
20
+ export declare function verifyClis(opts: VerifyOptions): Promise<VerifyReport>;
21
+ export declare function renderVerifyReport(report: VerifyReport): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jackwener/opencli",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -34,7 +34,7 @@
34
34
  "playwright"
35
35
  ],
36
36
  "author": "jackwener",
37
- "license": "BSD-3-Clause",
37
+ "license": "Apache-2.0",
38
38
  "repository": {
39
39
  "type": "git",
40
40
  "url": "git+https://github.com/jackwener/opencli.git"
package/src/browser.ts CHANGED
@@ -13,9 +13,7 @@ import { formatSnapshot } from './snapshotFormatter.js';
13
13
  import { PKG_VERSION } from './version.js';
14
14
  import { normalizeEvaluateSource } from './pipeline/template.js';
15
15
  import { generateInterceptorJs, generateReadInterceptedJs } from './interceptor.js';
16
- import { withTimeoutMs } from './runtime.js';
17
-
18
- const CONNECT_TIMEOUT = parseInt(process.env.OPENCLI_BROWSER_CONNECT_TIMEOUT ?? '30', 10);
16
+ import { withTimeoutMs, DEFAULT_BROWSER_CONNECT_TIMEOUT } from './runtime.js';
19
17
  const STDERR_BUFFER_LIMIT = 16 * 1024;
20
18
  const INITIAL_TABS_TIMEOUT_MS = 1500;
21
19
  const TAB_CLEANUP_TIMEOUT_MS = 2000;
@@ -344,7 +342,7 @@ export class PlaywrightMCP {
344
342
  PlaywrightMCP._registerGlobalCleanup();
345
343
  PlaywrightMCP._activeInsts.add(this);
346
344
  this._state = 'connecting';
347
- const timeout = opts.timeout ?? CONNECT_TIMEOUT;
345
+ const timeout = opts.timeout ?? DEFAULT_BROWSER_CONNECT_TIMEOUT;
348
346
 
349
347
  return new Promise<Page>((resolve, reject) => {
350
348
  const isDebug = process.env.DEBUG?.includes('opencli:mcp');
package/src/engine.ts CHANGED
@@ -101,7 +101,10 @@ async function discoverClisFromFs(dir: string): Promise<void> {
101
101
  const filePath = path.join(siteDir, file);
102
102
  if (file.endsWith('.yaml') || file.endsWith('.yml')) {
103
103
  registerYamlCli(filePath, site);
104
- } else if (file.endsWith('.js') && !file.endsWith('.d.js')) {
104
+ } else if (
105
+ (file.endsWith('.js') && !file.endsWith('.d.js')) ||
106
+ (file.endsWith('.ts') && !file.endsWith('.d.ts') && !file.endsWith('.test.ts'))
107
+ ) {
105
108
  promises.push(
106
109
  import(`file://${filePath}`).catch((err: any) => {
107
110
  process.stderr.write(`Warning: failed to load module ${filePath}: ${err.message}\n`);
package/src/main.ts CHANGED
@@ -62,10 +62,18 @@ program.command('list').description('List all available CLI commands').option('-
62
62
  });
63
63
 
64
64
  program.command('validate').description('Validate CLI definitions').argument('[target]', 'site or site/name')
65
- .action(async (target) => { const { validateClisWithTarget, renderValidationReport } = await import('./validate.js'); console.log(renderValidationReport(validateClisWithTarget([BUILTIN_CLIS, USER_CLIS], target))); });
65
+ .action(async (target) => {
66
+ const { validateClisWithTarget, renderValidationReport } = await import('./validate.js');
67
+ console.log(renderValidationReport(validateClisWithTarget([BUILTIN_CLIS, USER_CLIS], target)));
68
+ });
66
69
 
67
70
  program.command('verify').description('Validate + smoke test').argument('[target]').option('--smoke', 'Run smoke tests', false)
68
- .action(async (target, opts) => { const { verifyClis, renderVerifyReport } = await import('./verify.js'); const r = await verifyClis({ builtinClis: BUILTIN_CLIS, userClis: USER_CLIS, target, smoke: opts.smoke }); console.log(renderVerifyReport(r)); process.exitCode = r.ok ? 0 : 1; });
71
+ .action(async (target, opts) => {
72
+ const { verifyClis, renderVerifyReport } = await import('./verify.js');
73
+ const r = await verifyClis({ builtinClis: BUILTIN_CLIS, userClis: USER_CLIS, target, smoke: opts.smoke });
74
+ console.log(renderVerifyReport(r));
75
+ process.exitCode = r.ok ? 0 : 1;
76
+ });
69
77
 
70
78
  program.command('explore').alias('probe').description('Explore a website: discover APIs, stores, and recommend strategies').argument('<url>').option('--site <name>').option('--goal <text>').option('--wait <s>', '', '3').option('--auto', 'Enable interactive fuzzing (simulate clicks to trigger lazy APIs)').option('--click <labels>', 'Comma-separated labels to click before fuzzing (e.g. "字幕,CC,评论")')
71
79
  .action(async (url, opts) => { const { exploreUrl, renderExploreSummary } = await import('./explore.js'); const clickLabels = opts.click ? opts.click.split(',').map((s: string) => s.trim()) : undefined; console.log(renderExploreSummary(await exploreUrl(url, { BrowserFactory: PlaywrightMCP, site: opts.site, goal: opts.goal, waitSeconds: parseFloat(opts.wait), auto: opts.auto, clickLabels }))); });
package/src/output.ts CHANGED
@@ -82,7 +82,8 @@ function renderCsv(data: any, opts: RenderOptions): void {
82
82
  for (const row of rows) {
83
83
  console.log(columns.map(c => {
84
84
  const v = String(row[c] ?? '');
85
- return v.includes(',') || v.includes('"') ? `"${v.replace(/"/g, '""')}"` : v;
85
+ return v.includes(',') || v.includes('"') || v.includes('\n')
86
+ ? `"${v.replace(/"/g, '""')}"` : v;
86
87
  }).join(','));
87
88
  }
88
89
  }
package/src/registry.ts CHANGED
@@ -42,18 +42,11 @@ export interface InternalCliCommand extends CliCommand {
42
42
  _lazy?: boolean;
43
43
  _modulePath?: string;
44
44
  }
45
- export interface CliOptions {
45
+ export interface CliOptions extends Partial<Omit<CliCommand, 'args' | 'description'>> {
46
46
  site: string;
47
47
  name: string;
48
48
  description?: string;
49
- domain?: string;
50
- strategy?: Strategy;
51
- browser?: boolean;
52
49
  args?: Arg[];
53
- columns?: string[];
54
- func?: (page: IPage, kwargs: Record<string, any>, debug?: boolean) => Promise<any>;
55
- pipeline?: any[];
56
- timeoutSeconds?: number;
57
50
  }
58
51
  const _registry = new Map<string, CliCommand>();
59
52