@jackwener/opencli 1.7.17 → 1.7.19

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 (118) hide show
  1. package/README.md +10 -8
  2. package/README.zh-CN.md +9 -8
  3. package/cli-manifest.json +585 -9
  4. package/clis/ctrip/ctrip.test.js +486 -1
  5. package/clis/ctrip/flight.js +136 -0
  6. package/clis/ctrip/hotel-search.js +132 -0
  7. package/clis/ctrip/utils.js +298 -0
  8. package/clis/doubao/utils.js +17 -0
  9. package/clis/doubao/utils.test.js +61 -0
  10. package/clis/google/search.js +16 -6
  11. package/clis/google-scholar/search.js +20 -5
  12. package/clis/google-scholar/search.test.js +35 -2
  13. package/clis/reddit/home.js +117 -0
  14. package/clis/reddit/home.test.js +127 -0
  15. package/clis/reddit/read.js +400 -54
  16. package/clis/reddit/read.test.js +315 -12
  17. package/clis/reddit/reply.js +182 -0
  18. package/clis/reddit/reply.test.js +89 -0
  19. package/clis/reddit/subreddit-info.js +117 -0
  20. package/clis/reddit/subreddit-info.test.js +163 -0
  21. package/clis/reddit/whoami.js +84 -0
  22. package/clis/reddit/whoami.test.js +105 -0
  23. package/clis/rednote/comments.js +76 -0
  24. package/clis/rednote/download.js +59 -0
  25. package/clis/rednote/feed.js +95 -0
  26. package/clis/rednote/navigation.test.js +26 -0
  27. package/clis/rednote/note.js +68 -0
  28. package/clis/rednote/notifications.js +139 -0
  29. package/clis/rednote/rednote.test.js +157 -0
  30. package/clis/rednote/search.js +101 -0
  31. package/clis/rednote/user.js +55 -0
  32. package/clis/twitter/bookmark-folder.js +3 -1
  33. package/clis/twitter/bookmarks.js +3 -1
  34. package/clis/twitter/followers.js +20 -5
  35. package/clis/twitter/followers.test.js +44 -0
  36. package/clis/twitter/following.js +36 -20
  37. package/clis/twitter/following.test.js +60 -8
  38. package/clis/twitter/likes.js +28 -13
  39. package/clis/twitter/likes.test.js +111 -1
  40. package/clis/twitter/list-add.js +128 -204
  41. package/clis/twitter/list-add.test.js +97 -1
  42. package/clis/twitter/list-tweets.js +13 -4
  43. package/clis/twitter/list-tweets.test.js +48 -0
  44. package/clis/twitter/lists.js +5 -2
  45. package/clis/twitter/post.js +23 -4
  46. package/clis/twitter/post.test.js +30 -0
  47. package/clis/twitter/profile.js +16 -8
  48. package/clis/twitter/profile.test.js +39 -0
  49. package/clis/twitter/reply.js +133 -10
  50. package/clis/twitter/reply.test.js +55 -0
  51. package/clis/twitter/search.js +188 -170
  52. package/clis/twitter/search.test.js +96 -258
  53. package/clis/twitter/shared.js +167 -16
  54. package/clis/twitter/shared.test.js +102 -1
  55. package/clis/twitter/timeline.js +3 -1
  56. package/clis/twitter/tweets.js +147 -51
  57. package/clis/twitter/tweets.test.js +238 -1
  58. package/clis/xiaohongshu/comments.js +57 -26
  59. package/clis/xiaohongshu/comments.test.js +63 -1
  60. package/clis/xiaohongshu/download.js +32 -23
  61. package/clis/xiaohongshu/feed.js +23 -15
  62. package/clis/xiaohongshu/note-helpers.js +16 -6
  63. package/clis/xiaohongshu/note.js +26 -20
  64. package/clis/xiaohongshu/notifications.js +26 -19
  65. package/clis/xiaohongshu/search.js +201 -37
  66. package/clis/xiaohongshu/search.test.js +82 -8
  67. package/clis/xiaohongshu/user-helpers.js +13 -4
  68. package/clis/xiaohongshu/user-helpers.test.js +20 -0
  69. package/clis/xiaohongshu/user.js +9 -4
  70. package/clis/xueqiu/earnings-date.js +2 -2
  71. package/clis/xueqiu/kline.js +2 -2
  72. package/clis/xueqiu/utils.js +19 -0
  73. package/clis/xueqiu/utils.test.js +26 -0
  74. package/clis/youtube/transcript.js +28 -3
  75. package/clis/youtube/transcript.test.js +90 -1
  76. package/clis/zhihu/answer-detail.js +233 -0
  77. package/clis/zhihu/answer-detail.test.js +330 -0
  78. package/clis/zhihu/question.js +44 -10
  79. package/clis/zhihu/question.test.js +78 -1
  80. package/clis/zhihu/recommend.js +103 -0
  81. package/clis/zhihu/recommend.test.js +143 -0
  82. package/dist/src/browser/base-page.d.ts +3 -2
  83. package/dist/src/browser/base-page.test.js +2 -2
  84. package/dist/src/browser/cdp.js +3 -3
  85. package/dist/src/browser/page.d.ts +3 -2
  86. package/dist/src/browser/page.js +4 -4
  87. package/dist/src/browser/page.test.js +31 -0
  88. package/dist/src/browser/utils.d.ts +10 -0
  89. package/dist/src/browser/utils.js +37 -0
  90. package/dist/src/browser/utils.test.d.ts +1 -0
  91. package/dist/src/browser/utils.test.js +29 -0
  92. package/dist/src/cli-argv-preprocess.d.ts +37 -0
  93. package/dist/src/cli-argv-preprocess.js +131 -0
  94. package/dist/src/cli-argv-preprocess.test.d.ts +1 -0
  95. package/dist/src/cli-argv-preprocess.test.js +130 -0
  96. package/dist/src/cli.js +123 -86
  97. package/dist/src/cli.test.js +32 -22
  98. package/dist/src/commands/daemon.js +6 -7
  99. package/dist/src/doctor.js +21 -17
  100. package/dist/src/doctor.test.js +2 -0
  101. package/dist/src/download/progress.js +15 -11
  102. package/dist/src/download/progress.test.d.ts +1 -0
  103. package/dist/src/download/progress.test.js +25 -0
  104. package/dist/src/execution.js +1 -3
  105. package/dist/src/execution.test.js +4 -16
  106. package/dist/src/help.d.ts +11 -0
  107. package/dist/src/help.js +46 -5
  108. package/dist/src/logger.js +8 -9
  109. package/dist/src/main.js +16 -0
  110. package/dist/src/output.js +4 -5
  111. package/dist/src/runtime-detect.d.ts +1 -1
  112. package/dist/src/runtime-detect.js +1 -1
  113. package/dist/src/runtime-detect.test.js +3 -2
  114. package/dist/src/tui.d.ts +0 -1
  115. package/dist/src/tui.js +9 -22
  116. package/dist/src/types.d.ts +3 -1
  117. package/dist/src/update-check.js +4 -5
  118. package/package.json +5 -4
@@ -1,8 +1,39 @@
1
1
  import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { JSDOM } from 'jsdom';
2
3
  import { getRegistry } from '@jackwener/opencli/registry';
3
4
  import './search.js';
4
5
  import './hotel-suggest.js';
5
- import { buildUrl, mapSuggestRow, parseLimit, pickCoords } from './utils.js';
6
+ import './hotel-search.js';
7
+ import './flight.js';
8
+ import { __test__ as hotelSearchTest } from './hotel-search.js';
9
+ import {
10
+ buildFlightExtractJs,
11
+ buildScrollUntilJs,
12
+ buildUrl,
13
+ mapHotelRow,
14
+ mapSuggestRow,
15
+ parseCityId,
16
+ parseIataCode,
17
+ parseIsoDate,
18
+ parseLimit,
19
+ pickCoords,
20
+ pickHotelMapCoords,
21
+ } from './utils.js';
22
+
23
+ function createPageMock(evaluateResults) {
24
+ const evaluate = vi.fn();
25
+ for (const result of evaluateResults) {
26
+ evaluate.mockResolvedValueOnce(result);
27
+ }
28
+ return {
29
+ goto: vi.fn().mockResolvedValue(undefined),
30
+ evaluate,
31
+ wait: vi.fn().mockResolvedValue(undefined),
32
+ scroll: vi.fn().mockResolvedValue(undefined),
33
+ autoScroll: vi.fn().mockResolvedValue(undefined),
34
+ getCookies: vi.fn().mockResolvedValue([]),
35
+ };
36
+ }
6
37
 
7
38
  function ok(payload) {
8
39
  return new Response(JSON.stringify(payload), { status: 200 });
@@ -232,3 +263,457 @@ describe('ctrip hotel-suggest command (registry-level)', () => {
232
263
  await expect(cmd.func({ query: 'zzz', limit: 5 })).rejects.toThrow('ctrip hotel-suggest returned no data');
233
264
  });
234
265
  });
266
+
267
+ describe('ctrip parseIsoDate', () => {
268
+ it('accepts well-formed dates', () => {
269
+ expect(parseIsoDate('checkin', '2026-06-15')).toBe('2026-06-15');
270
+ expect(parseIsoDate('date', '2030-12-31')).toBe('2030-12-31');
271
+ });
272
+ it('rejects missing/blank with required-arg message', () => {
273
+ expect(() => parseIsoDate('checkin', '')).toThrow(/--checkin is required/);
274
+ expect(() => parseIsoDate('date', undefined)).toThrow(/--date is required/);
275
+ });
276
+ it('rejects malformed strings', () => {
277
+ expect(() => parseIsoDate('checkin', '2026/06/15')).toThrow(/must be YYYY-MM-DD/);
278
+ expect(() => parseIsoDate('checkin', 'tomorrow')).toThrow(/must be YYYY-MM-DD/);
279
+ });
280
+ it('rejects out-of-range month/day before Date math', () => {
281
+ expect(() => parseIsoDate('checkin', '2026-13-01')).toThrow(/invalid month\/day/);
282
+ expect(() => parseIsoDate('checkin', '2026-06-32')).toThrow(/invalid month\/day/);
283
+ });
284
+ it('rejects impossible calendar dates (Feb 30) via UTC cross-check', () => {
285
+ expect(() => parseIsoDate('checkin', '2026-02-30')).toThrow(/not a real calendar date/);
286
+ expect(() => parseIsoDate('checkin', '2025-02-29')).toThrow(/not a real calendar date/); // 2025 not leap
287
+ });
288
+ });
289
+
290
+ describe('ctrip parseIataCode', () => {
291
+ it('uppercases and accepts 3-letter codes', () => {
292
+ expect(parseIataCode('from', 'pek')).toBe('PEK');
293
+ expect(parseIataCode('from', 'BJS')).toBe('BJS');
294
+ expect(parseIataCode('to', ' sha ')).toBe('SHA');
295
+ });
296
+ it('rejects non-3-letter / mixed inputs', () => {
297
+ expect(() => parseIataCode('from', 'PE')).toThrow(/3-letter IATA/);
298
+ expect(() => parseIataCode('from', 'PEKK')).toThrow(/3-letter IATA/);
299
+ expect(() => parseIataCode('from', '123')).toThrow(/3-letter IATA/);
300
+ expect(() => parseIataCode('from', '')).toThrow(/required/);
301
+ });
302
+ });
303
+
304
+ describe('ctrip parseCityId', () => {
305
+ it('accepts positive integer city IDs (numeric and string)', () => {
306
+ expect(parseCityId(2)).toBe(2);
307
+ expect(parseCityId('1')).toBe(1);
308
+ expect(parseCityId('12345')).toBe(12345);
309
+ });
310
+ it('rejects zero / negative / non-integer / empty', () => {
311
+ expect(() => parseCityId(0)).toThrow(/positive integer/);
312
+ expect(() => parseCityId(-1)).toThrow(/positive integer/);
313
+ expect(() => parseCityId(2.5)).toThrow(/positive integer/);
314
+ expect(() => parseCityId('shanghai')).toThrow(/positive integer/);
315
+ expect(() => parseCityId('')).toThrow(/--city is required/);
316
+ });
317
+ });
318
+
319
+ describe('ctrip pickHotelMapCoords', () => {
320
+ it('prefers WGS84 (coordinateType=1) when multiple available', () => {
321
+ const coords = [
322
+ { coordinateType: 3, latitude: '31.25', longitude: '121.51' },
323
+ { coordinateType: 1, latitude: '31.23', longitude: '121.47' },
324
+ { coordinateType: 2, latitude: '31.24', longitude: '121.49' },
325
+ ];
326
+ expect(pickHotelMapCoords(coords)).toEqual({ lat: 31.23, lon: 121.47 });
327
+ });
328
+ it('falls through to GCJ02 then BD09 if WGS84 missing', () => {
329
+ const onlyBD09 = [{ coordinateType: 3, latitude: '31.25', longitude: '121.51' }];
330
+ expect(pickHotelMapCoords(onlyBD09)).toEqual({ lat: 31.25, lon: 121.51 });
331
+ });
332
+ it('returns null/null on empty / non-array / all-zero coords', () => {
333
+ expect(pickHotelMapCoords([])).toEqual({ lat: null, lon: null });
334
+ expect(pickHotelMapCoords(null)).toEqual({ lat: null, lon: null });
335
+ expect(pickHotelMapCoords([{ coordinateType: 1, latitude: '0', longitude: '0' }])).toEqual({ lat: null, lon: null });
336
+ });
337
+ });
338
+
339
+ describe('ctrip mapHotelRow', () => {
340
+ const HOTEL_FIXTURE = {
341
+ hotelInfo: {
342
+ summary: { hotelId: '106876528' },
343
+ nameInfo: { name: '上海外滩滨江珍宝酒店', enName: 'Shanghai Bund Riverside Treasury Hotel' },
344
+ hotelStar: { star: 4 },
345
+ commentInfo: { commentScore: '4.7', commentDescription: '超棒', commenterNumber: '13,966条点评' },
346
+ positionInfo: {
347
+ cityName: '上海',
348
+ positionDesc: '北外滩地区 · 近北外滩来福士',
349
+ address: '东大名路988号',
350
+ mapCoordinate: [{ coordinateType: 3, latitude: '31.25693033446487', longitude: '121.51336547497098' }],
351
+ },
352
+ },
353
+ roomInfo: [{ priceInfo: { price: 548, currency: 'RMB', displayPrice: '¥548' } }],
354
+ };
355
+
356
+ it('projects every declared column key (no silent drop)', () => {
357
+ const row = mapHotelRow(HOTEL_FIXTURE, 0);
358
+ expect(row).toEqual({
359
+ rank: 1,
360
+ hotelId: '106876528',
361
+ name: '上海外滩滨江珍宝酒店',
362
+ enName: 'Shanghai Bund Riverside Treasury Hotel',
363
+ star: 4,
364
+ score: 4.7,
365
+ scoreLabel: '超棒',
366
+ reviewCount: 13966,
367
+ cityName: '上海',
368
+ district: '北外滩地区 · 近北外滩来福士',
369
+ address: '东大名路988号',
370
+ lat: 31.25693033446487,
371
+ lon: 121.51336547497098,
372
+ price: 548,
373
+ currency: 'RMB',
374
+ url: 'https://hotels.ctrip.com/hotels/detail/?hotelid=106876528',
375
+ });
376
+ });
377
+
378
+ it('returns null (not 0 / "") for missing optional fields', () => {
379
+ const sparse = { hotelInfo: { summary: { hotelId: '999' }, nameInfo: { name: 'X' } }, roomInfo: [] };
380
+ const row = mapHotelRow(sparse, 4);
381
+ expect(row.rank).toBe(5);
382
+ expect(row.star).toBeNull();
383
+ expect(row.score).toBeNull();
384
+ expect(row.reviewCount).toBeNull();
385
+ expect(row.price).toBeNull();
386
+ expect(row.currency).toBeNull();
387
+ expect(row.lat).toBeNull();
388
+ expect(row.lon).toBeNull();
389
+ expect(row.address).toBeNull();
390
+ });
391
+
392
+ it('parses reviewCount from "13,966条点评" / "999 reviews" by stripping non-digits', () => {
393
+ const a = mapHotelRow({ hotelInfo: { summary: { hotelId: '1' }, nameInfo: { name: 'A' }, commentInfo: { commenterNumber: '13,966条点评' } }, roomInfo: [] }, 0);
394
+ expect(a.reviewCount).toBe(13966);
395
+ const b = mapHotelRow({ hotelInfo: { summary: { hotelId: '2' }, nameInfo: { name: 'B' }, commentInfo: { commenterNumber: '999 reviews' } }, roomInfo: [] }, 0);
396
+ expect(b.reviewCount).toBe(999);
397
+ });
398
+ });
399
+
400
+ describe('ctrip hotel-search command (registry-level)', () => {
401
+ const cmd = getRegistry().get('ctrip/hotel-search');
402
+
403
+ const SHANGHAI_HOTEL = {
404
+ hotelInfo: {
405
+ summary: { hotelId: '106876528' },
406
+ nameInfo: { name: '上海外滩滨江珍宝酒店' },
407
+ hotelStar: { star: 4 },
408
+ commentInfo: { commentScore: '4.7', commentDescription: '超棒', commenterNumber: '13,966条点评' },
409
+ positionInfo: { cityName: '上海', address: '东大名路988号', mapCoordinate: [{ coordinateType: 1, latitude: '31.25', longitude: '121.51' }] },
410
+ },
411
+ roomInfo: [{ priceInfo: { price: 548, currency: 'RMB' } }],
412
+ };
413
+
414
+ it('declares Strategy.COOKIE + browser:true + navigateBefore:false + access:read', () => {
415
+ expect(cmd.access).toBe('read');
416
+ expect(cmd.browser).toBe(true);
417
+ expect(String(cmd.strategy)).toContain('cookie');
418
+ expect(cmd.navigateBefore).toBe(false);
419
+ expect(cmd.domain).toBe('hotels.ctrip.com');
420
+ });
421
+
422
+ it('rejects invalid city / date / limit before browser navigation', async () => {
423
+ const page = createPageMock([]);
424
+ await expect(cmd.func(page, { city: 'shanghai', checkin: '2026-06-15', checkout: '2026-06-17', limit: 5 }))
425
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('--city') });
426
+ await expect(cmd.func(page, { city: 2, checkin: 'tomorrow', checkout: '2026-06-17', limit: 5 }))
427
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('--checkin') });
428
+ await expect(cmd.func(page, { city: 2, checkin: '2026-06-15', checkout: '2026-06-17', limit: 0 }))
429
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('--limit') });
430
+ expect(page.goto).not.toHaveBeenCalled();
431
+ });
432
+
433
+ it('rejects checkin >= checkout before navigation', async () => {
434
+ const page = createPageMock([]);
435
+ await expect(cmd.func(page, { city: 2, checkin: '2026-06-17', checkout: '2026-06-15', limit: 5 }))
436
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('--checkin must be earlier') });
437
+ await expect(cmd.func(page, { city: 2, checkin: '2026-06-15', checkout: '2026-06-15', limit: 5 }))
438
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('--checkin must be earlier') });
439
+ expect(page.goto).not.toHaveBeenCalled();
440
+ });
441
+
442
+ it('throws AuthRequired when captcha gate is detected', async () => {
443
+ const page = createPageMock(['captcha']);
444
+ await expect(cmd.func(page, { city: 2, checkin: '2026-06-15', checkout: '2026-06-17', limit: 5 }))
445
+ .rejects.toThrow('Ctrip is asking for a captcha');
446
+ // No extract call when captcha caught early
447
+ expect(page.evaluate).toHaveBeenCalledTimes(1);
448
+ });
449
+
450
+ it('throws EmptyResultError when SSR hotelList is empty', async () => {
451
+ const page = createPageMock(['content', []]);
452
+ await expect(cmd.func(page, { city: 9999, checkin: '2026-06-15', checkout: '2026-06-17', limit: 5 }))
453
+ .rejects.toMatchObject({ code: 'EMPTY_RESULT' });
454
+ });
455
+
456
+ it('waits for an empty SSR hotelList so empty results do not become timeout failures', async () => {
457
+ const dom = new JSDOM('<!doctype html><html><body></body></html>', {
458
+ url: 'https://hotels.ctrip.com/hotels/list?city=9999',
459
+ runScripts: 'outside-only',
460
+ });
461
+ dom.window.__NEXT_DATA__ = {
462
+ props: { pageProps: { initListData: { hotelList: [] } } },
463
+ };
464
+ await expect(dom.window.Function(`return (${hotelSearchTest.WAIT_FOR_SSR_JS})`)())
465
+ .resolves.toBe('content');
466
+ });
467
+
468
+ it('throws CommandExecutionError when SSR state times out or is malformed', async () => {
469
+ await expect(cmd.func(createPageMock(['timeout']), { city: 2, checkin: '2026-06-15', checkout: '2026-06-17', limit: 5 }))
470
+ .rejects.toMatchObject({ code: 'COMMAND_EXEC', message: expect.stringContaining('did not expose SSR hotel list') });
471
+ await expect(cmd.func(createPageMock(['content', { hotelList: [] }]), { city: 2, checkin: '2026-06-15', checkout: '2026-06-17', limit: 5 }))
472
+ .rejects.toMatchObject({ code: 'COMMAND_EXEC', message: expect.stringContaining('malformed SSR hotel list') });
473
+ });
474
+
475
+ it('maps SSR rows and respects --limit', async () => {
476
+ const page = createPageMock([
477
+ 'content',
478
+ [SHANGHAI_HOTEL, { ...SHANGHAI_HOTEL, hotelInfo: { ...SHANGHAI_HOTEL.hotelInfo, summary: { hotelId: '2' } } }],
479
+ ]);
480
+ const rows = await cmd.func(page, { city: 2, checkin: '2026-06-15', checkout: '2026-06-17', limit: 1 });
481
+ expect(rows).toHaveLength(1);
482
+ expect(rows[0]).toMatchObject({ rank: 1, hotelId: '106876528', name: '上海外滩滨江珍宝酒店', star: 4, price: 548 });
483
+ // Every declared column appears on every row
484
+ for (const row of rows) {
485
+ for (const col of cmd.columns) expect(row).toHaveProperty(col);
486
+ }
487
+ // Single goto, single URL
488
+ expect(page.goto).toHaveBeenCalledTimes(1);
489
+ expect(page.goto.mock.calls[0][0]).toContain('city=2');
490
+ expect(page.goto.mock.calls[0][0]).toContain('checkin=2026-06-15');
491
+ expect(page.goto.mock.calls[0][0]).toContain('checkout=2026-06-17');
492
+ });
493
+
494
+ it('filters out SSR rows missing hotelId or name (no silent partial rows)', async () => {
495
+ const incomplete = { hotelInfo: { summary: {}, nameInfo: { name: 'No-id' } }, roomInfo: [] };
496
+ const page = createPageMock(['content', [incomplete, SHANGHAI_HOTEL]]);
497
+ const rows = await cmd.func(page, { city: 2, checkin: '2026-06-15', checkout: '2026-06-17', limit: 5 });
498
+ expect(rows).toHaveLength(1);
499
+ expect(rows[0].hotelId).toBe('106876528');
500
+ });
501
+
502
+ it('throws CommandExecutionError when all SSR rows miss required anchors', async () => {
503
+ const incomplete = { hotelInfo: { summary: {}, nameInfo: { name: 'No-id' } }, roomInfo: [] };
504
+ const page = createPageMock(['content', [incomplete]]);
505
+ await expect(cmd.func(page, { city: 2, checkin: '2026-06-15', checkout: '2026-06-17', limit: 5 }))
506
+ .rejects.toMatchObject({ code: 'COMMAND_EXEC', message: expect.stringContaining('required hotelId/name anchors') });
507
+ });
508
+ });
509
+
510
+ describe('ctrip flight command (registry-level)', () => {
511
+ const cmd = getRegistry().get('ctrip/flight');
512
+
513
+ const FLIGHT_RAW = {
514
+ airline: '厦门航空',
515
+ flightNo: 'MF8561',
516
+ aircraft: '空客321(中)',
517
+ departureTime: '07:50',
518
+ departureAirport: '大兴国际机场',
519
+ arrivalTime: '09:45',
520
+ arrivalAirport: '浦东国际机场',
521
+ terminal: 'T2',
522
+ price: 487,
523
+ currency: '¥',
524
+ cabin: '经济舱',
525
+ };
526
+
527
+ it('declares Strategy.COOKIE + browser:true + navigateBefore:false + access:read', () => {
528
+ expect(cmd.access).toBe('read');
529
+ expect(cmd.browser).toBe(true);
530
+ expect(String(cmd.strategy)).toContain('cookie');
531
+ expect(cmd.navigateBefore).toBe(false);
532
+ expect(cmd.domain).toBe('flights.ctrip.com');
533
+ });
534
+
535
+ it('rejects invalid IATA / date / from==to / limit before browser navigation', async () => {
536
+ const page = createPageMock([]);
537
+ await expect(cmd.func(page, { from: 'PE', to: 'SHA', date: '2026-06-15', limit: 5 }))
538
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('IATA') });
539
+ await expect(cmd.func(page, { from: 'PEK', to: 'PEK', date: '2026-06-15', limit: 5 }))
540
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('must differ') });
541
+ await expect(cmd.func(page, { from: 'PEK', to: 'SHA', date: '06/15', limit: 5 }))
542
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('--date') });
543
+ await expect(cmd.func(page, { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 0 }))
544
+ .rejects.toMatchObject({ code: 'ARGUMENT', message: expect.stringContaining('--limit') });
545
+ expect(page.goto).not.toHaveBeenCalled();
546
+ });
547
+
548
+ it('throws AuthRequired when captcha gate is detected', async () => {
549
+ const page = createPageMock(['captcha']);
550
+ await expect(cmd.func(page, { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 5 }))
551
+ .rejects.toThrow('Ctrip is asking for a captcha');
552
+ expect(page.evaluate).toHaveBeenCalledTimes(1);
553
+ });
554
+
555
+ it('throws EmptyResultError when DOM extraction returns no flights', async () => {
556
+ const page = createPageMock(['content', 0, []]);
557
+ await expect(cmd.func(page, { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 5 }))
558
+ .rejects.toMatchObject({ code: 'EMPTY_RESULT' });
559
+ });
560
+
561
+ it('throws CommandExecutionError when visible cards render but parser finds no flight anchors', async () => {
562
+ const page = createPageMock(['content', 2, []]);
563
+ await expect(cmd.func(page, { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 5 }))
564
+ .rejects.toMatchObject({
565
+ code: 'COMMAND_EXEC',
566
+ message: expect.stringContaining('parser did not find required flight anchors'),
567
+ });
568
+ });
569
+
570
+ it('throws CommandExecutionError when flight render waits timeout or extraction is malformed', async () => {
571
+ await expect(cmd.func(createPageMock(['timeout']), { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 5 }))
572
+ .rejects.toMatchObject({ code: 'COMMAND_EXEC', message: expect.stringContaining('did not render flight cards') });
573
+ await expect(cmd.func(createPageMock(['content', 1, { rows: [] }]), { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 5 }))
574
+ .rejects.toMatchObject({ code: 'COMMAND_EXEC', message: expect.stringContaining('malformed rows') });
575
+ });
576
+
577
+ it('builds URL with lowercase IATA codes and Y_S_C_F cabin', async () => {
578
+ const page = createPageMock(['content', 1, [FLIGHT_RAW]]);
579
+ await cmd.func(page, { from: 'pek', to: 'sha', date: '2026-06-15', limit: 1 });
580
+ const url = page.goto.mock.calls[0][0];
581
+ expect(url).toContain('oneway-pek-sha');
582
+ expect(url).toContain('depdate=2026-06-15');
583
+ expect(url).toContain('cabin=Y_S_C_F');
584
+ expect(url).toContain('adult=1');
585
+ });
586
+
587
+ it('maps DOM-extracted rows and respects --limit', async () => {
588
+ const page = createPageMock([
589
+ 'content',
590
+ 2,
591
+ [FLIGHT_RAW, { ...FLIGHT_RAW, flightNo: 'CA1234', airline: '国航' }],
592
+ ]);
593
+ const rows = await cmd.func(page, { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 1 });
594
+ expect(rows).toHaveLength(1);
595
+ expect(rows[0]).toMatchObject({
596
+ rank: 1,
597
+ airline: '厦门航空',
598
+ flightNo: 'MF8561',
599
+ departureTime: '07:50',
600
+ arrivalTime: '09:45',
601
+ price: 487,
602
+ currency: '¥',
603
+ cabin: '经济舱',
604
+ });
605
+ for (const row of rows) {
606
+ for (const col of cmd.columns) expect(row).toHaveProperty(col);
607
+ }
608
+ });
609
+
610
+ it('filters out flight rows missing core anchors (no silent partial rows)', async () => {
611
+ const page = createPageMock(['content', 2, [{ ...FLIGHT_RAW, departureTime: '' }, FLIGHT_RAW]]);
612
+ const rows = await cmd.func(page, { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 5 });
613
+ expect(rows).toHaveLength(1);
614
+ expect(rows[0].departureTime).toBe('07:50');
615
+ });
616
+
617
+ it('throws CommandExecutionError when every flight row misses core anchors', async () => {
618
+ const page = createPageMock(['content', 2, [{ ...FLIGHT_RAW, departureAirport: '' }, { ...FLIGHT_RAW, flightNo: null }]]);
619
+ await expect(cmd.func(page, { from: 'PEK', to: 'SHA', date: '2026-06-15', limit: 5 }))
620
+ .rejects.toMatchObject({ code: 'COMMAND_EXEC', message: expect.stringContaining('required airline/flight/time/airport anchors') });
621
+ });
622
+ });
623
+
624
+ describe('ctrip buildScrollUntilJs', () => {
625
+ it('inlines the row selector + target count + default maxScrolls', () => {
626
+ const js = buildScrollUntilJs('.flight-list > span > div', 20);
627
+ expect(js).toContain('"\.flight-list > span > div"'.replace('\\.', '.')); // selector literal
628
+ expect(js).toContain('countItems() >= 20');
629
+ expect(js).toContain('i < 8');
630
+ expect(js).toContain('plateauRounds');
631
+ expect(js).toContain('getBoundingClientRect');
632
+ expect(js).toContain('getComputedStyle');
633
+ });
634
+ it('respects a custom maxScrolls override', () => {
635
+ const js = buildScrollUntilJs('.hotel-card', 50, 3);
636
+ expect(js).toContain('countItems() >= 50');
637
+ expect(js).toContain('i < 3');
638
+ });
639
+ it('rejects unsafe target / maxScrolls values before interpolation', () => {
640
+ expect(() => buildScrollUntilJs('.hotel-card', 0)).toThrow('targetCount');
641
+ expect(() => buildScrollUntilJs('.hotel-card', 101)).toThrow('targetCount');
642
+ expect(() => buildScrollUntilJs('.hotel-card', 10, 0)).toThrow('maxScrolls');
643
+ expect(() => buildScrollUntilJs('.hotel-card', 10, 31)).toThrow('maxScrolls');
644
+ });
645
+ });
646
+
647
+ describe('ctrip buildFlightExtractJs (JSDOM)', () => {
648
+ function runExtract(html) {
649
+ const dom = new JSDOM(`<!doctype html><html><body>${html}</body></html>`,
650
+ { url: 'https://flights.ctrip.com/' });
651
+ const js = buildFlightExtractJs();
652
+ return Function('document', `return (${js})`)(dom.window.document);
653
+ }
654
+
655
+ it('extracts a single ordered card via position-anchored chunks', () => {
656
+ const html = `
657
+ <div class="flight-list"><span>
658
+ <div>
659
+ <span>厦门航空</span><span>MF8561</span><span>空客321(中)</span>
660
+ <span>当日低价</span>
661
+ <span>07:50</span><span>大兴国际机场</span>
662
+ <span>09:45</span><span>浦东国际机场</span><span>T2</span>
663
+ <span>已减¥3</span><span>惊喜低价</span>
664
+ <span>¥</span><span>487</span><span>起</span>
665
+ <span>经济舱</span><span>订票</span>
666
+ </div>
667
+ </span></div>
668
+ `;
669
+ const rows = runExtract(html);
670
+ expect(rows).toEqual([{
671
+ airline: '厦门航空',
672
+ flightNo: 'MF8561',
673
+ aircraft: '空客321(中)',
674
+ departureTime: '07:50',
675
+ departureAirport: '大兴国际机场',
676
+ arrivalTime: '09:45',
677
+ arrivalAirport: '浦东国际机场',
678
+ terminal: 'T2',
679
+ price: 487,
680
+ currency: '¥',
681
+ cabin: '经济舱',
682
+ }]);
683
+ });
684
+
685
+ it('omits terminal when not present after arrAirport', () => {
686
+ const html = `
687
+ <div class="flight-list"><span>
688
+ <div>
689
+ <span>国航</span><span>CA1234</span><span>波音737</span>
690
+ <span>08:00</span><span>首都国际机场</span>
691
+ <span>10:00</span><span>虹桥国际机场</span>
692
+ <span>¥</span><span>520</span><span>起</span><span>经济舱</span>
693
+ </div>
694
+ </span></div>
695
+ `;
696
+ const rows = runExtract(html);
697
+ expect(rows).toHaveLength(1);
698
+ expect(rows[0].terminal).toBeNull();
699
+ expect(rows[0].arrivalAirport).toBe('虹桥国际机场');
700
+ });
701
+
702
+ it('returns empty array when there are no flight cards (not a sentinel row)', () => {
703
+ const rows = runExtract('<div class="flight-list"></div>');
704
+ expect(rows).toEqual([]);
705
+ });
706
+
707
+ it('does not fabricate rows from non-flight cards with two times', () => {
708
+ const html = `
709
+ <div class="flight-list"><span>
710
+ <div>
711
+ <span>筛选</span><span>价格排序</span><span>推荐</span>
712
+ <span>08:00</span><span>出发</span><span>10:00</span><span>到达</span>
713
+ <span>¥</span><span>520</span><span>经济舱</span>
714
+ </div>
715
+ </span></div>
716
+ `;
717
+ expect(runExtract(html)).toEqual([]);
718
+ });
719
+ });
@@ -0,0 +1,136 @@
1
+ /**
2
+ * 携程机票 oneway search — domestic + international flight search by route + date.
3
+ *
4
+ * Unlike `hotel-search`, the flight rows are NOT in `__NEXT_DATA__` — they
5
+ * arrive via a post-load XHR that the daemon network buffer currently can't
6
+ * capture (see MEMORY `daemon_capture_pipeline_bug_2026_05_07`). We instead
7
+ * extract from the rendered `.flight-list > span > div` cards using a
8
+ * position-anchored innerText parser (see `buildFlightExtractJs` in utils).
9
+ *
10
+ * Round-trip + advanced filters (airline whitelist, cabin selection beyond
11
+ * 全舱位) are out of scope for v1 — track in #1481 follow-up if requested.
12
+ */
13
+ import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
14
+ import { cli, Strategy } from '@jackwener/opencli/registry';
15
+ import { buildFlightExtractJs, buildScrollUntilJs, parseIataCode, parseIsoDate } from './utils.js';
16
+
17
+ const MIN_LIMIT = 1;
18
+ const MAX_LIMIT = 50;
19
+ const DEFAULT_LIMIT = 20;
20
+
21
+ function parseFlightLimit(raw) {
22
+ if (raw === undefined || raw === null || raw === '') return DEFAULT_LIMIT;
23
+ const parsed = Number(raw);
24
+ if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {
25
+ throw new ArgumentError(`--limit must be an integer between ${MIN_LIMIT} and ${MAX_LIMIT}, got ${JSON.stringify(raw)}`);
26
+ }
27
+ if (parsed < MIN_LIMIT || parsed > MAX_LIMIT) {
28
+ throw new ArgumentError(`--limit must be between ${MIN_LIMIT} and ${MAX_LIMIT}, got ${parsed}`);
29
+ }
30
+ return parsed;
31
+ }
32
+
33
+ /**
34
+ * Wait for `.flight-list > span > div` to render (the post-load XHR settles
35
+ * 1-3s after navigation), or detect a captcha/login redirect.
36
+ */
37
+ const WAIT_FOR_FLIGHTS_JS = `
38
+ new Promise((resolve) => {
39
+ const detect = () => {
40
+ if (location.pathname.includes('captcha') || /验证码|verify the human/i.test(document.body?.innerText || '')) return 'captcha';
41
+ if (document.querySelector('.flight-list > span > div')) return 'content';
42
+ return null;
43
+ };
44
+ const found = detect();
45
+ if (found) return resolve(found);
46
+ const observer = new MutationObserver(() => {
47
+ const result = detect();
48
+ if (result) { observer.disconnect(); resolve(result); }
49
+ });
50
+ observer.observe(document.documentElement, { childList: true, subtree: true });
51
+ setTimeout(() => { observer.disconnect(); resolve('timeout'); }, 8000);
52
+ })
53
+ `;
54
+
55
+ cli({
56
+ site: 'ctrip',
57
+ name: 'flight',
58
+ access: 'read',
59
+ description: '搜索携程一程机票(按出发/到达 IATA 三字码 + 日期)',
60
+ domain: 'flights.ctrip.com',
61
+ strategy: Strategy.COOKIE,
62
+ browser: true,
63
+ navigateBefore: false,
64
+ args: [
65
+ { name: 'from', required: true, positional: true, help: 'Departure IATA code (e.g. BJS / PEK)' },
66
+ { name: 'to', required: true, positional: true, help: 'Arrival IATA code (e.g. SHA / PVG)' },
67
+ { name: 'date', required: true, help: 'Departure date (YYYY-MM-DD)' },
68
+ { name: 'limit', type: 'int', default: DEFAULT_LIMIT, help: `Number of flights (${MIN_LIMIT}-${MAX_LIMIT})` },
69
+ ],
70
+ columns: [
71
+ 'rank',
72
+ 'airline', 'flightNo', 'aircraft',
73
+ 'departureTime', 'departureAirport',
74
+ 'arrivalTime', 'arrivalAirport', 'terminal',
75
+ 'price', 'currency', 'cabin',
76
+ 'url',
77
+ ],
78
+ func: async (page, kwargs) => {
79
+ const fromCode = parseIataCode('from', kwargs.from);
80
+ const toCode = parseIataCode('to', kwargs.to);
81
+ if (fromCode === toCode) {
82
+ throw new ArgumentError(`--from and --to must differ (got ${fromCode})`);
83
+ }
84
+ const date = parseIsoDate('date', kwargs.date);
85
+ const limit = parseFlightLimit(kwargs.limit);
86
+
87
+ const searchUrl =
88
+ `https://flights.ctrip.com/online/list/oneway-${fromCode.toLowerCase()}-${toCode.toLowerCase()}` +
89
+ `?depdate=${date}&cabin=Y_S_C_F&adult=1&child=0&infant=0`;
90
+ await page.goto(searchUrl);
91
+ const waitResult = await page.evaluate(WAIT_FOR_FLIGHTS_JS);
92
+ if (waitResult === 'captcha') {
93
+ throw new AuthRequiredError('flights.ctrip.com', 'Ctrip is asking for a captcha; complete it in your browser session and retry');
94
+ }
95
+ if (waitResult !== 'content') {
96
+ throw new CommandExecutionError(`Ctrip flight page did not render flight cards (state=${String(waitResult)})`);
97
+ }
98
+ // Scroll until enough flight cards rendered (Ctrip lazy-loads beyond ~8).
99
+ const renderedCardCount = await page.evaluate(buildScrollUntilJs('.flight-list > span > div', limit));
100
+ const raw = await page.evaluate(buildFlightExtractJs());
101
+ if (!Array.isArray(raw)) {
102
+ throw new CommandExecutionError('Ctrip flight DOM extraction returned malformed rows');
103
+ }
104
+ const rows = raw;
105
+ if (rows.length === 0) {
106
+ if (Number(renderedCardCount) > 0) {
107
+ throw new CommandExecutionError('Ctrip flight cards rendered but parser did not find required flight anchors');
108
+ }
109
+ throw new EmptyResultError('ctrip flight', `No flights for ${fromCode}→${toCode} on ${date}`);
110
+ }
111
+ const completeRows = rows
112
+ .filter((r) => r.departureTime && r.departureAirport && r.arrivalTime && r.arrivalAirport && r.airline && r.flightNo)
113
+ .slice(0, limit)
114
+ .map((r, i) => ({
115
+ rank: i + 1,
116
+ airline: r.airline,
117
+ flightNo: r.flightNo,
118
+ aircraft: r.aircraft,
119
+ departureTime: r.departureTime,
120
+ departureAirport: r.departureAirport,
121
+ arrivalTime: r.arrivalTime,
122
+ arrivalAirport: r.arrivalAirport,
123
+ terminal: r.terminal,
124
+ price: r.price,
125
+ currency: r.currency,
126
+ cabin: r.cabin,
127
+ url: searchUrl,
128
+ }));
129
+ if (completeRows.length === 0) {
130
+ throw new CommandExecutionError('Ctrip flight rows were missing required airline/flight/time/airport anchors');
131
+ }
132
+ return completeRows;
133
+ },
134
+ });
135
+
136
+ export const __test__ = { parseFlightLimit, WAIT_FOR_FLIGHTS_JS };