@jackwener/opencli 1.7.12 → 1.7.14
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.
- package/README.md +8 -7
- package/README.zh-CN.md +9 -8
- package/cli-manifest.json +12128 -6665
- package/clis/1point3acres/digest.js +35 -0
- package/clis/1point3acres/forum.js +51 -0
- package/clis/1point3acres/forums.js +44 -0
- package/clis/1point3acres/hot.js +35 -0
- package/clis/1point3acres/latest.js +35 -0
- package/clis/1point3acres/notifications.js +64 -0
- package/clis/1point3acres/search.js +71 -0
- package/clis/1point3acres/thread.js +117 -0
- package/clis/1point3acres/user.js +77 -0
- package/clis/1point3acres/utils.js +247 -0
- package/clis/_shared/desktop-commands.js +4 -0
- package/clis/aibase/news.js +110 -0
- package/clis/aibase/news.test.js +59 -0
- package/clis/amazon/discussion.test.js +1 -28
- package/clis/antigravity/watch.js +3 -2
- package/clis/arxiv/author.js +44 -0
- package/clis/baidu-scholar/search.js +0 -1
- package/clis/bbc/topic.js +57 -0
- package/clis/bbc/utils.js +79 -0
- package/clis/chaoxing/assignments.js +1 -1
- package/clis/chaoxing/exams.js +1 -1
- package/clis/chatgpt/ask.js +57 -0
- package/clis/chatgpt/commands.test.js +45 -0
- package/clis/chatgpt/detail.js +46 -0
- package/clis/chatgpt/history.js +39 -0
- package/clis/chatgpt/image.js +12 -11
- package/clis/chatgpt/image.test.js +23 -0
- package/clis/chatgpt/new.js +25 -0
- package/clis/chatgpt/read.js +43 -0
- package/clis/chatgpt/send.js +46 -0
- package/clis/chatgpt/status.js +29 -0
- package/clis/chatgpt/utils.js +294 -4
- package/clis/chatgpt/utils.test.js +13 -0
- package/clis/chatgpt-app/ask.js +6 -3
- package/clis/chatwise/ask.js +16 -43
- package/clis/chatwise/composer.test.js +186 -0
- package/clis/chatwise/send.js +2 -24
- package/clis/chatwise/utils.js +143 -0
- package/clis/claude/ask.js +1 -1
- package/clis/claude/detail.js +1 -0
- package/clis/claude/history.js +1 -0
- package/clis/claude/new.js +1 -0
- package/clis/claude/read.js +1 -0
- package/clis/claude/send.js +1 -0
- package/clis/claude/status.js +1 -0
- package/clis/codex/ask.js +15 -9
- package/clis/codex/history.js +16 -33
- package/clis/codex/projects.js +28 -0
- package/clis/codex/read.js +10 -4
- package/clis/codex/send.js +10 -3
- package/clis/codex/sidebar.js +356 -0
- package/clis/codex/sidebar.test.js +329 -0
- package/clis/coingecko/categories.js +75 -0
- package/clis/coingecko/coin.js +107 -0
- package/clis/coingecko/coingecko.test.js +109 -0
- package/clis/coingecko/derivatives.js +84 -0
- package/clis/coingecko/exchanges.js +74 -0
- package/clis/coingecko/global.js +71 -0
- package/clis/coingecko/top.js +64 -0
- package/clis/coingecko/trending.js +55 -0
- package/clis/coupang/add-to-cart.js +21 -13
- package/clis/coupang/coupang.test.js +159 -0
- package/clis/coupang/product.js +257 -0
- package/clis/coupang/search.js +38 -16
- package/clis/coupang/utils.js +55 -1
- package/clis/crates/crate.js +62 -0
- package/clis/crates/search.js +44 -0
- package/clis/crates/utils.js +72 -0
- package/clis/ctrip/ctrip.test.js +234 -0
- package/clis/ctrip/hotel-suggest.js +45 -0
- package/clis/ctrip/search.js +22 -68
- package/clis/ctrip/utils.js +175 -0
- package/clis/cursor/ask.js +6 -3
- package/clis/dblp/author.js +133 -0
- package/clis/dblp/venue.js +64 -0
- package/clis/deepseek/ask.js +12 -7
- package/clis/deepseek/ask.test.js +13 -13
- package/clis/deepseek/detail.js +38 -0
- package/clis/deepseek/detail.test.js +81 -0
- package/clis/deepseek/history.js +1 -0
- package/clis/deepseek/new.js +1 -0
- package/clis/deepseek/read.js +1 -0
- package/clis/deepseek/send.js +140 -0
- package/clis/deepseek/send.test.js +107 -0
- package/clis/deepseek/status.js +1 -0
- package/clis/deepseek/utils.js +66 -0
- package/clis/deepseek/utils.test.js +107 -1
- package/clis/defillama/defillama.test.js +99 -0
- package/clis/defillama/protocol.js +84 -0
- package/clis/defillama/protocols.js +55 -0
- package/clis/defillama/utils.js +99 -0
- package/clis/devto/latest.js +74 -0
- package/clis/dockerhub/image.js +52 -0
- package/clis/dockerhub/search.js +47 -0
- package/clis/dockerhub/utils.js +100 -0
- package/clis/doubao/ask.js +7 -3
- package/clis/doubao/detail.js +1 -0
- package/clis/doubao/history.js +1 -0
- package/clis/doubao/meeting-summary.js +1 -0
- package/clis/doubao/meeting-transcript.js +1 -0
- package/clis/doubao/new.js +1 -0
- package/clis/doubao/read.js +1 -0
- package/clis/doubao/send.js +1 -0
- package/clis/doubao/status.js +1 -0
- package/clis/douyin/draft.test.js +1 -30
- package/clis/endoflife/endoflife.test.js +51 -0
- package/clis/endoflife/product.js +55 -0
- package/clis/endoflife/utils.js +89 -0
- package/clis/facebook/__fixtures__/notifications-page.html +13 -0
- package/clis/facebook/notifications.js +326 -30
- package/clis/facebook/notifications.test.js +458 -0
- package/clis/flathub/app.js +71 -0
- package/clis/flathub/flathub.test.js +90 -0
- package/clis/flathub/search.js +80 -0
- package/clis/flathub/utils.js +114 -0
- package/clis/gemini/ask.js +7 -3
- package/clis/gemini/ask.test.js +2 -2
- package/clis/gemini/deep-research-result.js +6 -2
- package/clis/gemini/deep-research-result.test.js +15 -14
- package/clis/gemini/deep-research.js +8 -4
- package/clis/gemini/deep-research.test.js +15 -18
- package/clis/gemini/image.js +7 -2
- package/clis/gemini/new.js +1 -0
- package/clis/gemini/utils.js +0 -4
- package/clis/google-scholar/cite.js +0 -1
- package/clis/google-scholar/profile.js +0 -1
- package/clis/google-scholar/search.js +0 -1
- package/clis/goproxy/goproxy.test.js +103 -0
- package/clis/goproxy/module.js +47 -0
- package/clis/goproxy/utils.js +165 -0
- package/clis/goproxy/versions.js +59 -0
- package/clis/gov-law/recent.js +0 -1
- package/clis/gov-law/search.js +0 -1
- package/clis/gov-policy/__fixtures__/recent.html +16 -0
- package/clis/gov-policy/__fixtures__/search.html +41 -0
- package/clis/gov-policy/gov-policy.test.js +224 -0
- package/clis/gov-policy/recent.js +66 -24
- package/clis/gov-policy/search.js +65 -23
- package/clis/gov-policy/utils.js +54 -0
- package/clis/grok/ask.js +49 -265
- package/clis/grok/ask.test.js +21 -46
- package/clis/grok/detail.js +60 -0
- package/clis/grok/history.js +48 -0
- package/clis/grok/{image.ts → image.js} +56 -70
- package/clis/grok/image.test.ts +20 -0
- package/clis/grok/new.js +20 -0
- package/clis/grok/read.js +39 -0
- package/clis/grok/send.js +50 -0
- package/clis/grok/status.js +41 -0
- package/clis/grok/utils.js +326 -0
- package/clis/grok/utils.test.js +103 -0
- package/clis/hf/datasets.js +88 -0
- package/clis/hf/hf.test.js +16 -0
- package/clis/hf/models.js +91 -0
- package/clis/hf/paper.js +79 -0
- package/clis/hf/spaces.js +101 -0
- package/clis/hf/top.js +1 -0
- package/clis/homebrew/cask.js +39 -0
- package/clis/homebrew/formula.js +41 -0
- package/clis/homebrew/popular.js +54 -0
- package/clis/homebrew/utils.js +100 -0
- package/clis/hupu/__fixtures__/hot-home.html +64 -0
- package/clis/hupu/detail.js +0 -1
- package/clis/hupu/hot.js +156 -35
- package/clis/hupu/hot.test.js +224 -0
- package/clis/hupu/search.js +0 -1
- package/clis/instagram/note.js +1 -1
- package/clis/instagram/note.test.js +1 -29
- package/clis/instagram/post.js +1 -1
- package/clis/instagram/post.test.js +1 -1
- package/clis/instagram/reel.js +1 -1
- package/clis/instagram/story.js +1 -1
- package/clis/instagram/story.test.js +1 -34
- package/clis/jd/commands.test.js +1 -24
- package/clis/lichess/lichess.test.js +85 -0
- package/clis/lichess/top.js +46 -0
- package/clis/lichess/user.js +91 -0
- package/clis/lichess/utils.js +97 -0
- package/clis/linkedin/search.js +107 -10
- package/clis/linkedin/search.test.js +222 -0
- package/clis/linux-do/feed.js +2 -5
- package/clis/linux-do/feed.test.js +35 -0
- package/clis/lobsters/domain.js +92 -0
- package/clis/maven/artifact.js +49 -0
- package/clis/maven/search.js +51 -0
- package/clis/maven/utils.js +110 -0
- package/clis/mdn/search.js +97 -0
- package/clis/medium/tag.js +135 -0
- package/clis/npm/downloads.js +59 -0
- package/clis/npm/package.js +70 -0
- package/clis/npm/search.js +49 -0
- package/clis/npm/utils.js +76 -0
- package/clis/nuget/nuget.test.js +111 -0
- package/clis/nuget/package.js +101 -0
- package/clis/nuget/search.js +69 -0
- package/clis/nuget/utils.js +87 -0
- package/clis/nvd/cve.js +121 -0
- package/clis/oeis/oeis.test.js +88 -0
- package/clis/oeis/search.js +63 -0
- package/clis/oeis/sequence.js +71 -0
- package/clis/oeis/utils.js +88 -0
- package/clis/openalex/search.js +69 -0
- package/clis/openalex/utils.js +160 -0
- package/clis/openalex/work.js +65 -0
- package/clis/openfda/drug-label.js +74 -0
- package/clis/openfda/food-recall.js +65 -0
- package/clis/openfda/openfda.test.js +114 -0
- package/clis/openfda/utils.js +67 -0
- package/clis/osv/osv.test.js +97 -0
- package/clis/osv/query.js +72 -0
- package/clis/osv/utils.js +169 -0
- package/clis/osv/vulnerability.js +54 -0
- package/clis/packagist/package.js +49 -0
- package/clis/packagist/search.js +43 -0
- package/clis/packagist/utils.js +113 -0
- package/clis/paperreview/feedback.js +1 -1
- package/clis/paperreview/review.js +1 -1
- package/clis/paperreview/submit.js +1 -1
- package/clis/pixiv/download.test.js +1 -1
- package/clis/pixiv/illusts.test.js +1 -1
- package/clis/pixiv/search.test.js +1 -1
- package/clis/pubmed/article.js +50 -0
- package/clis/pubmed/author.js +64 -0
- package/clis/pubmed/citations.js +36 -0
- package/clis/pubmed/pubmed.test.js +276 -0
- package/clis/pubmed/related.js +45 -0
- package/clis/pubmed/search.js +75 -0
- package/clis/pubmed/utils.js +309 -0
- package/clis/pypi/downloads.js +66 -0
- package/clis/pypi/package.js +79 -0
- package/clis/pypi/utils.js +55 -0
- package/clis/quark/mv.js +1 -1
- package/clis/quark/save.js +1 -1
- package/clis/qwen/ask.js +85 -0
- package/clis/qwen/detail.js +62 -0
- package/clis/qwen/history.js +61 -0
- package/clis/qwen/image.js +179 -0
- package/clis/qwen/new.js +23 -0
- package/clis/qwen/read.js +41 -0
- package/clis/qwen/send.js +55 -0
- package/clis/qwen/status.js +37 -0
- package/clis/qwen/utils.js +409 -0
- package/clis/qwen/utils.test.js +45 -0
- package/clis/rest-countries/country.js +65 -0
- package/clis/rest-countries/region.js +64 -0
- package/clis/rest-countries/rest-countries.test.js +83 -0
- package/clis/rest-countries/utils.js +126 -0
- package/clis/reuters/article-detail.js +53 -0
- package/clis/reuters/reuters.test.js +299 -0
- package/clis/reuters/search.js +45 -34
- package/clis/reuters/utils.js +159 -0
- package/clis/rfc/rfc.js +52 -0
- package/clis/rfc/rfc.test.js +74 -0
- package/clis/rfc/utils.js +72 -0
- package/clis/rubygems/gem.js +42 -0
- package/clis/rubygems/search.js +47 -0
- package/clis/rubygems/utils.js +86 -0
- package/clis/stackoverflow/related.js +66 -0
- package/clis/stackoverflow/stackoverflow.test.js +58 -0
- package/clis/stackoverflow/tag.js +60 -0
- package/clis/stackoverflow/user.js +50 -0
- package/clis/stackoverflow/utils.js +118 -0
- package/clis/steam/app.js +67 -0
- package/clis/steam/search.js +58 -0
- package/clis/steam/steam.test.js +46 -0
- package/clis/steam/utils.js +107 -0
- package/clis/taobao/commands.test.js +1 -24
- package/clis/test-utils.js +61 -0
- package/clis/tieba/hot.js +0 -1
- package/clis/tiktok/comment.js +128 -41
- package/clis/tiktok/creator-videos.js +270 -0
- package/clis/tiktok/creator-videos.test.js +113 -0
- package/clis/tiktok/explore.js +137 -29
- package/clis/tiktok/follow.js +115 -33
- package/clis/tiktok/following.js +157 -36
- package/clis/tiktok/friends.js +139 -37
- package/clis/tiktok/live.js +137 -41
- package/clis/tiktok/notifications.js +141 -38
- package/clis/tiktok/refactor.test.js +389 -0
- package/clis/tiktok/unfollow.js +124 -38
- package/clis/tiktok/user.js +203 -29
- package/clis/tiktok/utils.js +505 -0
- package/clis/tiktok/write-refactor.test.js +370 -0
- package/clis/toutiao/articles.js +36 -62
- package/clis/toutiao/hot.js +63 -0
- package/clis/toutiao/toutiao.test.js +378 -0
- package/clis/toutiao/utils.js +161 -0
- package/clis/tvmaze/search.js +61 -0
- package/clis/tvmaze/show.js +60 -0
- package/clis/tvmaze/tvmaze.test.js +93 -0
- package/clis/tvmaze/utils.js +110 -0
- package/clis/twitter/accept.js +1 -1
- package/clis/twitter/followers.js +134 -69
- package/clis/twitter/quote.js +139 -0
- package/clis/twitter/quote.test.js +106 -0
- package/clis/twitter/reply-dm.js +1 -1
- package/clis/twitter/reply.test.js +1 -29
- package/clis/twitter/retweet.js +99 -0
- package/clis/twitter/retweet.test.js +69 -0
- package/clis/twitter/shared.js +38 -0
- package/clis/twitter/shared.test.js +28 -1
- package/clis/twitter/unlike.js +87 -0
- package/clis/twitter/unlike.test.js +72 -0
- package/clis/twitter/unretweet.js +99 -0
- package/clis/twitter/unretweet.test.js +69 -0
- package/clis/uisdc/news.js +105 -0
- package/clis/uisdc/news.test.js +66 -0
- package/clis/wanfang/search.js +0 -1
- package/clis/web/read.js +47 -17
- package/clis/web/read.test.js +101 -1
- package/clis/weixin/create-draft.js +1 -1
- package/clis/weixin/drafts.js +1 -1
- package/clis/weixin/drafts.test.js +5 -1
- package/clis/weixin/search.js +157 -0
- package/clis/weixin/search.test.js +227 -0
- package/clis/wikidata/entity.js +60 -0
- package/clis/wikidata/search.js +50 -0
- package/clis/wikidata/utils.js +117 -0
- package/clis/wikidata/wikidata.test.js +83 -0
- package/clis/wikipedia/page.js +95 -0
- package/clis/wttr/current.js +63 -0
- package/clis/wttr/forecast.js +71 -0
- package/clis/wttr/utils.js +50 -0
- package/clis/wttr/wttr.test.js +84 -0
- package/clis/xianyu/chat.js +16 -4
- package/clis/xianyu/chat.test.js +64 -0
- package/clis/xianyu/publish.js +485 -0
- package/clis/xianyu/publish.test.js +220 -0
- package/clis/xiaoe/catalog.js +105 -40
- package/clis/xiaoe/content.js +164 -29
- package/clis/xiaoe/courses.js +86 -29
- package/clis/xiaoe/xiaoe.test.js +486 -0
- package/clis/xiaohongshu/creator-notes-summary.js +1 -1
- package/clis/xiaohongshu/publish.js +16 -3
- package/clis/xiaohongshu/publish.test.js +46 -1
- package/clis/youtube/transcript.js +13 -19
- package/clis/youtube/transcript.test.js +17 -0
- package/clis/yuanbao/ask.js +17 -66
- package/clis/yuanbao/ask.test.js +5 -5
- package/clis/yuanbao/detail.js +65 -0
- package/clis/yuanbao/history.js +51 -0
- package/clis/yuanbao/new.js +1 -0
- package/clis/yuanbao/read.js +38 -0
- package/clis/yuanbao/send.js +57 -0
- package/clis/yuanbao/shared.js +297 -5
- package/clis/yuanbao/shared.test.js +80 -0
- package/clis/yuanbao/status.js +44 -0
- package/clis/zlibrary/commands.test.js +1 -11
- package/dist/src/browser/base-page.d.ts +9 -0
- package/dist/src/browser/base-page.js +44 -1
- package/dist/src/browser/base-page.test.js +66 -0
- package/dist/src/browser/bridge.js +47 -45
- package/dist/src/browser/cdp.d.ts +1 -0
- package/dist/src/browser/cdp.js +51 -9
- package/dist/src/browser/daemon-client.d.ts +4 -0
- package/dist/src/browser/errors.js +1 -1
- package/dist/src/browser/page.d.ts +1 -1
- package/dist/src/browser/page.js +3 -1
- package/dist/src/browser/page.test.js +29 -0
- package/dist/src/browser/target-errors.d.ts +2 -1
- package/dist/src/browser/target-errors.js +1 -0
- package/dist/src/browser/target-resolver.d.ts +25 -0
- package/dist/src/browser/target-resolver.js +43 -0
- package/dist/src/browser.test.js +18 -0
- package/dist/src/build-manifest.js +9 -4
- package/dist/src/build-manifest.test.js +2 -8
- package/dist/src/capabilityRouting.d.ts +16 -1
- package/dist/src/capabilityRouting.js +24 -1
- package/dist/src/capabilityRouting.test.js +19 -1
- package/dist/src/cli.js +76 -11
- package/dist/src/cli.test.js +241 -1
- package/dist/src/commanderAdapter.js +23 -9
- package/dist/src/commanderAdapter.test.js +0 -1
- package/dist/src/discovery.js +2 -5
- package/dist/src/errors.js +1 -1
- package/dist/src/execution.d.ts +1 -1
- package/dist/src/execution.js +111 -27
- package/dist/src/execution.test.js +326 -17
- package/dist/src/help.d.ts +27 -2
- package/dist/src/help.js +196 -23
- package/dist/src/help.test.d.ts +1 -0
- package/dist/src/help.test.js +54 -0
- package/dist/src/main.js +14 -1
- package/dist/src/manifest-types.d.ts +5 -3
- package/dist/src/pipeline/executor.js +1 -1
- package/dist/src/pipeline/executor.test.js +8 -0
- package/dist/src/pipeline/registry.d.ts +9 -0
- package/dist/src/pipeline/registry.js +13 -1
- package/dist/src/pipeline/steps/browser.d.ts +1 -0
- package/dist/src/pipeline/steps/browser.js +10 -0
- package/dist/src/pipeline/steps/download.test.js +1 -0
- package/dist/src/registry-api.d.ts +1 -1
- package/dist/src/registry.d.ts +12 -11
- package/dist/src/registry.js +16 -6
- package/dist/src/registry.test.js +2 -2
- package/dist/src/runtime.d.ts +2 -1
- package/dist/src/runtime.js +1 -1
- package/dist/src/serialization.d.ts +2 -2
- package/dist/src/serialization.js +4 -6
- package/dist/src/serialization.test.js +17 -0
- package/dist/src/types.d.ts +17 -0
- package/dist/src/validate.js +15 -11
- package/dist/src/validate.test.d.ts +9 -0
- package/dist/src/validate.test.js +90 -0
- package/package.json +1 -1
- package/scripts/fetch-adapters.js +1 -1
- package/scripts/typed-error-lint-baseline.json +5 -77
- package/clis/ctrip/search.test.js +0 -64
- package/clis/gov-policy/commands.test.js +0 -27
- package/clis/linux-do/category.js +0 -37
- package/clis/linux-do/hot.js +0 -26
- package/clis/linux-do/latest.js +0 -19
- package/clis/pixiv/test-utils.js +0 -23
- package/clis/toutiao/articles.test.js +0 -30
- package/dist/src/analysis.d.ts +0 -40
- package/dist/src/analysis.js +0 -172
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { JSDOM } from 'jsdom';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
ArgumentError,
|
|
8
|
+
CommandExecutionError,
|
|
9
|
+
EmptyResultError,
|
|
10
|
+
} from '@jackwener/opencli/errors';
|
|
11
|
+
import { getRegistry } from '@jackwener/opencli/registry';
|
|
12
|
+
import { extractSearchRows } from './search.js';
|
|
13
|
+
import { extractRecentRows } from './recent.js';
|
|
14
|
+
|
|
15
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const SEARCH_FIXTURE = readFileSync(join(__dirname, '__fixtures__/search.html'), 'utf8');
|
|
17
|
+
const RECENT_FIXTURE = readFileSync(join(__dirname, '__fixtures__/recent.html'), 'utf8');
|
|
18
|
+
|
|
19
|
+
function createPageMock(evaluateResult, overrides = {}) {
|
|
20
|
+
const evaluate = typeof evaluateResult === 'function'
|
|
21
|
+
? vi.fn(evaluateResult)
|
|
22
|
+
: vi.fn().mockResolvedValue(evaluateResult);
|
|
23
|
+
return {
|
|
24
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
25
|
+
wait: vi.fn().mockResolvedValue(undefined),
|
|
26
|
+
evaluate,
|
|
27
|
+
...overrides,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('gov-policy commands — registration', () => {
|
|
32
|
+
it('registers search and recent as public browser commands', () => {
|
|
33
|
+
const search = getRegistry().get('gov-policy/search');
|
|
34
|
+
const recent = getRegistry().get('gov-policy/recent');
|
|
35
|
+
|
|
36
|
+
expect(search).toBeDefined();
|
|
37
|
+
expect(recent).toBeDefined();
|
|
38
|
+
expect(search.browser).toBe(true);
|
|
39
|
+
expect(recent.browser).toBe(true);
|
|
40
|
+
expect(search.strategy).toBe('public');
|
|
41
|
+
expect(recent.strategy).toBe('public');
|
|
42
|
+
expect(search.columns).toEqual(['rank', 'title', 'description', 'date', 'url']);
|
|
43
|
+
expect(recent.columns).toEqual(['rank', 'title', 'date', 'source', 'url']);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('rejects empty search queries before browser navigation', async () => {
|
|
47
|
+
const search = getRegistry().get('gov-policy/search');
|
|
48
|
+
const page = { goto: vi.fn() };
|
|
49
|
+
await expect(search.func(page, { query: ' ' })).rejects.toThrow(ArgumentError);
|
|
50
|
+
expect(page.goto).not.toHaveBeenCalled();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('rejects invalid limits before browser navigation', async () => {
|
|
54
|
+
const search = getRegistry().get('gov-policy/search');
|
|
55
|
+
const recent = getRegistry().get('gov-policy/recent');
|
|
56
|
+
const page = createPageMock({ ok: true, rows: [] });
|
|
57
|
+
|
|
58
|
+
await expect(search.func(page, { query: '数字经济', limit: '0' })).rejects.toThrow(ArgumentError);
|
|
59
|
+
await expect(search.func(page, { query: '数字经济', limit: '1.5' })).rejects.toThrow(ArgumentError);
|
|
60
|
+
await expect(search.func(page, { query: '数字经济', limit: '21' })).rejects.toThrow(ArgumentError);
|
|
61
|
+
await expect(recent.func(page, { limit: 'abc' })).rejects.toThrow(ArgumentError);
|
|
62
|
+
expect(page.goto).not.toHaveBeenCalled();
|
|
63
|
+
expect(page.evaluate).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('maps empty search pages, selector drift, and browser failures to typed errors', async () => {
|
|
67
|
+
const search = getRegistry().get('gov-policy/search');
|
|
68
|
+
const recent = getRegistry().get('gov-policy/recent');
|
|
69
|
+
|
|
70
|
+
await expect(search.func(createPageMock({
|
|
71
|
+
ok: false,
|
|
72
|
+
sample: '很抱歉,没有找到与 数字经济 相关的结果',
|
|
73
|
+
url: 'https://sousuo.www.gov.cn/sousuo/search.shtml?searchWord=x',
|
|
74
|
+
}), { query: '数字经济' })).rejects.toThrow(EmptyResultError);
|
|
75
|
+
|
|
76
|
+
await expect(recent.func(createPageMock({
|
|
77
|
+
ok: false,
|
|
78
|
+
sample: '<main>unexpected government page shell</main>',
|
|
79
|
+
url: 'https://www.gov.cn/zhengce/zuixin/index.htm',
|
|
80
|
+
}), {})).rejects.toThrow(CommandExecutionError);
|
|
81
|
+
|
|
82
|
+
await expect(search.func(createPageMock(
|
|
83
|
+
{ ok: true, rows: [] },
|
|
84
|
+
{ goto: vi.fn().mockRejectedValue(new Error('browser disconnected')) },
|
|
85
|
+
), { query: '数字经济' })).rejects.toThrow(CommandExecutionError);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* In-browser DOM extractors against frozen sanitized HTML fixtures.
|
|
91
|
+
*
|
|
92
|
+
* Mocked-page.evaluate tests can't catch silent bugs that live inside the
|
|
93
|
+
* extractor, since they feed pre-baked results to the func and the real
|
|
94
|
+
* DOM walk never runs.
|
|
95
|
+
*
|
|
96
|
+
* These tests replay the real (sanitized) HTML through JSDOM so changes
|
|
97
|
+
* to the extractor logic that re-introduce a silent regression fail in CI.
|
|
98
|
+
*/
|
|
99
|
+
describe('gov-policy adapter — extractors against frozen HTML fixtures', () => {
|
|
100
|
+
let originalDocument;
|
|
101
|
+
let originalLocation;
|
|
102
|
+
|
|
103
|
+
beforeEach(() => {
|
|
104
|
+
originalDocument = globalThis.document;
|
|
105
|
+
originalLocation = globalThis.location;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
afterEach(() => {
|
|
109
|
+
globalThis.document = originalDocument;
|
|
110
|
+
globalThis.location = originalLocation;
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
function loadFixture(html, url) {
|
|
114
|
+
const dom = new JSDOM(html, { url });
|
|
115
|
+
globalThis.document = dom.window.document;
|
|
116
|
+
globalThis.location = dom.window.location;
|
|
117
|
+
return dom;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
it('extractSearchRows returns three result-shaped rows with title, description, date, url', () => {
|
|
121
|
+
loadFixture(SEARCH_FIXTURE, 'https://sousuo.www.gov.cn/sousuo/search.shtml?searchWord=%E6%95%B0%E5%AD%97%E7%BB%8F%E6%B5%8E');
|
|
122
|
+
|
|
123
|
+
const result = extractSearchRows();
|
|
124
|
+
|
|
125
|
+
expect(result.ok).toBe(true);
|
|
126
|
+
expect(result.rows).toHaveLength(3);
|
|
127
|
+
|
|
128
|
+
// Rank 1 + 3 are the homogeneous "type tag + emphasized title" cards
|
|
129
|
+
// whose .description div carries only the date span (no real snippet).
|
|
130
|
+
expect(result.rows[0]).toMatchObject({
|
|
131
|
+
rank: 1,
|
|
132
|
+
title: '要闻经济数据速览:10组数字看一季度中国经济',
|
|
133
|
+
date: '2026-4-16',
|
|
134
|
+
url: 'https://www.gov.cn/zhengce/jiedu/tujie/202604/content_7065945.htm',
|
|
135
|
+
});
|
|
136
|
+
// Description for these rows is just the publish-time line.
|
|
137
|
+
expect(result.rows[0].description).toContain('发布时间');
|
|
138
|
+
expect(result.rows[0].description).toContain('2026-4-16');
|
|
139
|
+
|
|
140
|
+
// Rank 2 has a real article snippet inside .description > .detail > p,
|
|
141
|
+
// and the extractor must capture it (sliced to 120 chars).
|
|
142
|
+
expect(result.rows[1]).toMatchObject({
|
|
143
|
+
rank: 2,
|
|
144
|
+
title: '要闻何立峰会见法国经济、财政和工业、能源与数字主权部部长莱斯屈尔',
|
|
145
|
+
date: '2026-3-17',
|
|
146
|
+
url: 'https://www.gov.cn/yaowen/liebiao/202603/content_7062985.htm',
|
|
147
|
+
});
|
|
148
|
+
expect(result.rows[1].description.length).toBeLessThanOrEqual(120);
|
|
149
|
+
expect(result.rows[1].description).toContain('新华社巴黎');
|
|
150
|
+
|
|
151
|
+
expect(result.rows[2]).toMatchObject({
|
|
152
|
+
rank: 3,
|
|
153
|
+
title: '要闻经济数据速览:7组数字看1—2月份中国经济',
|
|
154
|
+
date: '2026-3-16',
|
|
155
|
+
url: 'https://www.gov.cn/zhengce/jiedu/tujie/202603/content_7062831.htm',
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Lock the no-collapse contract on the title: the type_title prefix
|
|
159
|
+
// ('要闻') is fused into the textContent because we read the whole <a>.
|
|
160
|
+
// If a future refactor strips the prefix, this assertion catches it
|
|
161
|
+
// before any field-level downstream surprises.
|
|
162
|
+
for (const row of result.rows) {
|
|
163
|
+
expect(row.title.startsWith('要闻')).toBe(true);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('extractRecentRows returns five rows with title, date, url and empty source', () => {
|
|
168
|
+
loadFixture(RECENT_FIXTURE, 'https://www.gov.cn/zhengce/zuixin/index.htm');
|
|
169
|
+
|
|
170
|
+
const result = extractRecentRows();
|
|
171
|
+
|
|
172
|
+
expect(result.ok).toBe(true);
|
|
173
|
+
expect(result.rows).toHaveLength(5);
|
|
174
|
+
|
|
175
|
+
expect(result.rows[0]).toMatchObject({
|
|
176
|
+
rank: 1,
|
|
177
|
+
title: '中共中央办公厅 国务院办公厅关于加强新就业群体服务管理的意见',
|
|
178
|
+
date: '2026-04-26',
|
|
179
|
+
url: 'https://www.gov.cn/zhengce/202604/content_7066998.htm',
|
|
180
|
+
});
|
|
181
|
+
expect(result.rows[1]).toMatchObject({
|
|
182
|
+
rank: 2,
|
|
183
|
+
date: '2026-04-23',
|
|
184
|
+
});
|
|
185
|
+
expect(result.rows[4]).toMatchObject({
|
|
186
|
+
rank: 5,
|
|
187
|
+
date: '2026-04-17',
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// gov.cn/zhengce/zuixin layout has no .source / .from elements, so
|
|
191
|
+
// the source field is always an empty string. Lock that contract:
|
|
192
|
+
// a future selector change that picks up unrelated text would break
|
|
193
|
+
// it and fail this assertion.
|
|
194
|
+
for (const row of result.rows) {
|
|
195
|
+
expect(row.source).toBe('');
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('extractSearchRows signals ok:false with a sample when result list is empty', () => {
|
|
200
|
+
loadFixture(
|
|
201
|
+
'<html><head><title>blocked</title></head><body><main>访问受限,请稍后再试</main></body></html>',
|
|
202
|
+
'https://sousuo.www.gov.cn/sousuo/search.shtml?searchWord=zzz',
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const result = extractSearchRows();
|
|
206
|
+
|
|
207
|
+
expect(result.ok).toBe(false);
|
|
208
|
+
expect(result.url).toBe('https://sousuo.www.gov.cn/sousuo/search.shtml?searchWord=zzz');
|
|
209
|
+
expect(result.sample).toContain('访问受限');
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('extractRecentRows signals ok:false with a sample when listing is empty', () => {
|
|
213
|
+
loadFixture(
|
|
214
|
+
'<html><head><title>not found</title></head><body><main>页面正在加载</main></body></html>',
|
|
215
|
+
'https://www.gov.cn/zhengce/zuixin/index.htm',
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const result = extractRecentRows();
|
|
219
|
+
|
|
220
|
+
expect(result.ok).toBe(false);
|
|
221
|
+
expect(result.url).toBe('https://www.gov.cn/zhengce/zuixin/index.htm');
|
|
222
|
+
expect(result.sample).toContain('页面正在加载');
|
|
223
|
+
});
|
|
224
|
+
});
|
|
@@ -1,5 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gov-policy recent — latest State Council policy releases.
|
|
3
|
+
*
|
|
4
|
+
* Targets www.gov.cn/zhengce/zuixin/index.htm. The listing is rendered
|
|
5
|
+
* server-side into one of `.news_box li`, `.list li`, `.list_item`,
|
|
6
|
+
* `.news-list li` (the page rotates between layouts).
|
|
7
|
+
*
|
|
8
|
+
* The DOM extractor is defined as a top-level function and injected
|
|
9
|
+
* into `page.evaluate` via `.toString()`, so the same code is exercised
|
|
10
|
+
* by a JSDOM-against-frozen-fixture unit test (see gov-policy.test.js).
|
|
11
|
+
*/
|
|
12
|
+
|
|
1
13
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
2
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
classifyExtractorFailure,
|
|
16
|
+
parseGovPolicyLimit,
|
|
17
|
+
requireRows,
|
|
18
|
+
wrapBrowserError,
|
|
19
|
+
} from './utils.js';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Pure DOM extractor for the gov-policy "latest policies" listing page.
|
|
23
|
+
*
|
|
24
|
+
* Uses bare `document` / `location` so it runs identically in:
|
|
25
|
+
* - the live browser (injected via `${extractRecentRows.toString()}`)
|
|
26
|
+
* - JSDOM unit tests (which swap `globalThis.document` / `globalThis.location`)
|
|
27
|
+
*/
|
|
28
|
+
export function extractRecentRows() {
|
|
29
|
+
const normalize = (v) => (v || '').replace(/\s+/g, ' ').trim();
|
|
30
|
+
const items = document.querySelectorAll('.news_box li, .list li, .list_item, .news-list li');
|
|
31
|
+
if (items.length === 0) {
|
|
32
|
+
const body = document.body;
|
|
33
|
+
const sampleText = (body && (body.innerText || body.textContent)) || '';
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
sample: sampleText.slice(0, 800),
|
|
37
|
+
url: location.href,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const rows = [];
|
|
41
|
+
for (const el of items) {
|
|
42
|
+
const titleEl = el.querySelector('a');
|
|
43
|
+
const title = normalize(titleEl?.textContent);
|
|
44
|
+
if (!title || title.length < 4) continue;
|
|
45
|
+
|
|
46
|
+
let url = titleEl?.getAttribute('href') || '';
|
|
47
|
+
if (url && !url.startsWith('http')) url = 'https://www.gov.cn' + url;
|
|
48
|
+
|
|
49
|
+
const date = (el.textContent || '').match(/(\d{4}[-./]\d{1,2}[-./]\d{1,2})/)?.[1] || '';
|
|
50
|
+
const source = normalize(el.querySelector('.source, .from')?.textContent);
|
|
51
|
+
|
|
52
|
+
rows.push({ rank: rows.length + 1, title, date, source, url });
|
|
53
|
+
}
|
|
54
|
+
return { ok: true, rows };
|
|
55
|
+
}
|
|
3
56
|
|
|
4
57
|
cli({
|
|
5
58
|
site: 'gov-policy',
|
|
@@ -13,36 +66,25 @@ cli({
|
|
|
13
66
|
{ name: 'limit', type: 'int', default: 10, help: '返回结果数量 (max 20)' },
|
|
14
67
|
],
|
|
15
68
|
columns: ['rank', 'title', 'date', 'source', 'url'],
|
|
16
|
-
navigateBefore: false,
|
|
17
69
|
func: async (page, kwargs) => {
|
|
18
|
-
const limit =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
70
|
+
const limit = parseGovPolicyLimit(kwargs.limit, 'recent');
|
|
71
|
+
try {
|
|
72
|
+
await page.goto('https://www.gov.cn/zhengce/zuixin/index.htm');
|
|
73
|
+
await page.wait(4);
|
|
74
|
+
// Poll until the SSR listing mounts.
|
|
75
|
+
await page.evaluate(`
|
|
22
76
|
(async () => {
|
|
23
|
-
const normalize = v => (v || '').replace(/\\s+/g, ' ').trim();
|
|
24
77
|
for (let i = 0; i < 20; i++) {
|
|
25
78
|
if (document.querySelector('.news_box li, .list li, .list_item, .news-list li')) break;
|
|
26
79
|
await new Promise(r => setTimeout(r, 500));
|
|
27
80
|
}
|
|
28
|
-
const results = [];
|
|
29
|
-
for (const el of document.querySelectorAll('.news_box li, .list li, .list_item, .news-list li')) {
|
|
30
|
-
const titleEl = el.querySelector('a');
|
|
31
|
-
const title = normalize(titleEl?.textContent);
|
|
32
|
-
if (!title || title.length < 4) continue;
|
|
33
|
-
|
|
34
|
-
let url = titleEl?.getAttribute('href') || '';
|
|
35
|
-
if (url && !url.startsWith('http')) url = 'https://www.gov.cn' + url;
|
|
36
|
-
|
|
37
|
-
const date = (el.textContent || '').match(/(\\d{4}[-./]\\d{1,2}[-./]\\d{1,2})/)?.[1] || '';
|
|
38
|
-
const source = normalize(el.querySelector('.source, .from')?.textContent);
|
|
39
|
-
|
|
40
|
-
results.push({ rank: results.length + 1, title, date, source, url });
|
|
41
|
-
if (results.length >= ${limit}) break;
|
|
42
|
-
}
|
|
43
|
-
return results;
|
|
44
81
|
})()
|
|
45
82
|
`);
|
|
46
|
-
|
|
83
|
+
const result = await page.evaluate(`(${extractRecentRows.toString()})()`);
|
|
84
|
+
if (!result || !result.ok) classifyExtractorFailure('recent', result);
|
|
85
|
+
return requireRows('recent', result.rows).slice(0, limit);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
wrapBrowserError('recent', error);
|
|
88
|
+
}
|
|
47
89
|
},
|
|
48
90
|
});
|
|
@@ -1,5 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gov-policy search — Chinese government policy full-text search.
|
|
3
|
+
*
|
|
4
|
+
* Targets sousuo.www.gov.cn. Results are server-rendered into
|
|
5
|
+
* `.basic_result_content .item` cards.
|
|
6
|
+
*
|
|
7
|
+
* The DOM extractor is defined as a top-level function and injected
|
|
8
|
+
* into `page.evaluate` via `.toString()`, so the same code is exercised
|
|
9
|
+
* by a JSDOM-against-frozen-fixture unit test (see gov-policy.test.js).
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
2
|
-
import {
|
|
13
|
+
import { requireNonEmptyQuery } from '../_shared/common.js';
|
|
14
|
+
import {
|
|
15
|
+
classifyExtractorFailure,
|
|
16
|
+
parseGovPolicyLimit,
|
|
17
|
+
requireRows,
|
|
18
|
+
wrapBrowserError,
|
|
19
|
+
} from './utils.js';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Pure DOM extractor for the gov-policy search-results page.
|
|
23
|
+
*
|
|
24
|
+
* Uses bare `document` / `location` so it runs identically in:
|
|
25
|
+
* - the live browser (injected via `${extractSearchRows.toString()}`)
|
|
26
|
+
* - JSDOM unit tests (which swap `globalThis.document` / `globalThis.location`)
|
|
27
|
+
*/
|
|
28
|
+
export function extractSearchRows() {
|
|
29
|
+
const normalize = (v) => (v || '').replace(/\s+/g, ' ').trim();
|
|
30
|
+
const items = document.querySelectorAll('.basic_result_content .item, .js_basic_result_content .item');
|
|
31
|
+
if (items.length === 0) {
|
|
32
|
+
const body = document.body;
|
|
33
|
+
const sampleText = (body && (body.innerText || body.textContent)) || '';
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
sample: sampleText.slice(0, 800),
|
|
37
|
+
url: location.href,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const rows = [];
|
|
41
|
+
for (const el of items) {
|
|
42
|
+
const titleEl = el.querySelector('a.title, .title a, a.log-anchor');
|
|
43
|
+
const title = normalize(titleEl?.textContent).replace(/<[^>]+>/g, '');
|
|
44
|
+
if (!title || title.length < 4) continue;
|
|
45
|
+
|
|
46
|
+
let url = titleEl?.getAttribute('href') || '';
|
|
47
|
+
if (url && !url.startsWith('http')) url = 'https://www.gov.cn' + url;
|
|
48
|
+
|
|
49
|
+
const description = normalize(el.querySelector('.description')?.textContent).slice(0, 120);
|
|
50
|
+
const date = (el.textContent || '').match(/(\d{4}[-./]\d{1,2}[-./]\d{1,2})/)?.[1] || '';
|
|
51
|
+
rows.push({ rank: rows.length + 1, title, description, date, url });
|
|
52
|
+
}
|
|
53
|
+
return { ok: true, rows };
|
|
54
|
+
}
|
|
3
55
|
|
|
4
56
|
cli({
|
|
5
57
|
site: 'gov-policy',
|
|
@@ -14,36 +66,26 @@ cli({
|
|
|
14
66
|
{ name: 'limit', type: 'int', default: 10, help: '返回结果数量 (max 20)' },
|
|
15
67
|
],
|
|
16
68
|
columns: ['rank', 'title', 'description', 'date', 'url'],
|
|
17
|
-
navigateBefore: false,
|
|
18
69
|
func: async (page, kwargs) => {
|
|
19
|
-
const limit =
|
|
70
|
+
const limit = parseGovPolicyLimit(kwargs.limit, 'search');
|
|
20
71
|
const query = requireNonEmptyQuery(kwargs.query);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
72
|
+
try {
|
|
73
|
+
await page.goto(`https://sousuo.www.gov.cn/sousuo/search.shtml?code=17da70961a7&dataTypeId=107&searchWord=${encodeURIComponent(query)}`);
|
|
74
|
+
await page.wait(5);
|
|
75
|
+
// Poll until the SSR result list mounts.
|
|
76
|
+
await page.evaluate(`
|
|
24
77
|
(async () => {
|
|
25
|
-
const normalize = v => (v || '').replace(/\\s+/g, ' ').trim();
|
|
26
78
|
for (let i = 0; i < 30; i++) {
|
|
27
79
|
if (document.querySelectorAll('.basic_result_content .item, .js_basic_result_content .item').length > 0) break;
|
|
28
80
|
await new Promise(r => setTimeout(r, 500));
|
|
29
81
|
}
|
|
30
|
-
const results = [];
|
|
31
|
-
for (const el of document.querySelectorAll('.basic_result_content .item, .js_basic_result_content .item')) {
|
|
32
|
-
const titleEl = el.querySelector('a.title, .title a, a.log-anchor');
|
|
33
|
-
const title = normalize(titleEl?.textContent).replace(/<[^>]+>/g, '');
|
|
34
|
-
if (!title || title.length < 4) continue;
|
|
35
|
-
|
|
36
|
-
let url = titleEl?.getAttribute('href') || '';
|
|
37
|
-
if (url && !url.startsWith('http')) url = 'https://www.gov.cn' + url;
|
|
38
|
-
|
|
39
|
-
const description = normalize(el.querySelector('.description')?.textContent).slice(0, 120);
|
|
40
|
-
const date = (el.textContent || '').match(/(\\d{4}[-./]\\d{1,2}[-./]\\d{1,2})/)?.[1] || '';
|
|
41
|
-
results.push({ rank: results.length + 1, title, description, date, url });
|
|
42
|
-
if (results.length >= ${limit}) break;
|
|
43
|
-
}
|
|
44
|
-
return results;
|
|
45
82
|
})()
|
|
46
83
|
`);
|
|
47
|
-
|
|
84
|
+
const result = await page.evaluate(`(${extractSearchRows.toString()})()`);
|
|
85
|
+
if (!result || !result.ok) classifyExtractorFailure('search', result);
|
|
86
|
+
return requireRows('search', result.rows).slice(0, limit);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
wrapBrowserError('search', error);
|
|
89
|
+
}
|
|
48
90
|
},
|
|
49
91
|
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
2
|
+
|
|
3
|
+
const EMPTY_RESULT_PATTERNS = [
|
|
4
|
+
/没有找到/,
|
|
5
|
+
/暂无/,
|
|
6
|
+
/无相关/,
|
|
7
|
+
/未找到/,
|
|
8
|
+
/搜索结果为\s*0/,
|
|
9
|
+
/很抱歉/,
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export function parseGovPolicyLimit(raw, command) {
|
|
13
|
+
const value = raw ?? 10;
|
|
14
|
+
const limit = Number(value);
|
|
15
|
+
if (!Number.isInteger(limit) || limit < 1) {
|
|
16
|
+
throw new ArgumentError(`gov-policy ${command} --limit must be a positive integer`);
|
|
17
|
+
}
|
|
18
|
+
if (limit > 20) {
|
|
19
|
+
throw new ArgumentError(`gov-policy ${command} --limit must be <= 20`);
|
|
20
|
+
}
|
|
21
|
+
return limit;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function classifyExtractorFailure(command, result) {
|
|
25
|
+
const sample = String(result?.sample || '').replace(/\s+/g, ' ').trim();
|
|
26
|
+
const url = String(result?.url || '').trim();
|
|
27
|
+
if (command === 'search' && EMPTY_RESULT_PATTERNS.some((pattern) => pattern.test(sample))) {
|
|
28
|
+
throw new EmptyResultError('gov-policy search', sample ? sample.slice(0, 160) : undefined);
|
|
29
|
+
}
|
|
30
|
+
const context = [url && `url=${url}`, sample && `sample=${sample.slice(0, 160)}`]
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
.join('; ');
|
|
33
|
+
throw new CommandExecutionError(
|
|
34
|
+
`gov-policy ${command} page did not expose readable result rows`,
|
|
35
|
+
context || 'The page structure may have changed or the page did not finish rendering.',
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function requireRows(command, rows) {
|
|
40
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
41
|
+
throw new CommandExecutionError(
|
|
42
|
+
`gov-policy ${command} extractor returned no result rows`,
|
|
43
|
+
'The page structure may have changed or all result cards were missing required title fields.',
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return rows;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function wrapBrowserError(command, error) {
|
|
50
|
+
if (error instanceof ArgumentError || error instanceof EmptyResultError || error instanceof CommandExecutionError) {
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
throw new CommandExecutionError(`gov-policy ${command} browser extraction failed: ${error?.message ?? error}`);
|
|
54
|
+
}
|