fix(wikipedia): fix search arg name + add random and trending commands#231
Conversation
- fix: search.ts referenced `args.keyword` but the argument is defined as `query`, causing the search term to always be undefined - feat: add `random` command (random article summary via REST API) - feat: add `trending` command (most-read articles, yesterday's data) All commands are PUBLIC strategy, no browser required, reuse wikiFetch.
df93bd1 to
f6a8214
Compare
|
Note: Wikipedia API commands verified via |
jackwener
left a comment
There was a problem hiding this comment.
PR #231 Review
总体来说这个 PR 改得不错,Bug fix 是正确的,两个新命令也遵循了现有模式。以下是一些改进建议:
✅ 做得好的
- Bug Fix 正确 —
args.keyword→args.query修复了 search 始终发送空 query 的问题 - 遵循现有模式 —
random.ts和trending.ts与summary.ts结构一致,都用wikiFetch+Strategy.PUBLIC - PR 描述清晰 — test plan 列了具体验证命令
⚠️ 建议改进
1. random.ts 与 summary.ts 有明显重复
random.ts 的返回结构和类型定义与 summary.ts 基本一致(title, description, extract, url),连 REST API 路径都在同一系列 (/api/rest_v1/page/...)。
建议提取一个共享的 response 类型到 utils.ts:
// utils.ts
export interface WikiSummary {
title?: string;
description?: string;
extract?: string;
content_urls?: { desktop?: { page?: string } };
}这样 summary.ts 和 random.ts 都可以复用,避免两处维护同样的类型。
2. trending.ts 日期计算:86400_000 不适用于所有时区
const d = new Date(Date.now() - 86400_000);在 DST(夏令时)切换日,一天可能是 23 或 25 小时。虽然这里用的是 getUTC* 所以实际上是安全的(UTC 没有 DST),但建议加一行注释说明为什么用 UTC 是正确的,避免未来维护者误改为本地时间方法:
// Use UTC to avoid DST issues — Wikipedia API expects UTC dates
const d = new Date(Date.now() - 86400_000);3. extract 截断长度 300 可以抽为常量
在 random.ts 和 summary.ts 里都硬编码了 .slice(0, 300),trending.ts 里 description 用了 .slice(0, 80)。建议统一为常量:
// utils.ts
export const EXTRACT_MAX_LEN = 300;
export const DESC_MAX_LEN = 80;4. 缺少文档更新
现在 docs/adapters/browser/wikipedia.md 只记录了 search 和 summary 两个命令。新增的 random 和 trending 也需要加上文档。
5. 缺少单元测试
PR 只做了手动验证。建议至少补一个对 trending.ts 日期路径拼接的单元测试,因为日期格式化是最容易出 bug 的地方。
📋 总结
| 维度 | 评价 |
|---|---|
| Bug Fix | ✅ 正确 |
| 新功能 | ✅ 遵循现有模式 |
| 代码复用 | |
| 文档 | |
| 测试 |
建议:补上文档和共享类型后可合并。测试和常量提取为可选改进。
- Extract WikiSummary, WikiMostReadArticle types to utils.ts - Extract EXTRACT_MAX_LEN/DESC_MAX_LEN constants - Add formatSummaryRow() helper to eliminate duplicate mapping in summary.ts and random.ts - Update docs with random and trending command examples
Summary
search.tsreferencedargs.keywordbut the argument is defined asquery, causing search to always send an empty queryrandomcommand — get a random article summary via REST APItrendingcommand — most-read articles (yesterday's data for availability)All three commands are
PUBLICstrategy, no browser required, reuse the existingwikiFetchutility.Test plan
opencli wikipedia search "TypeScript" --limit 3returns results (was broken before)opencli wikipedia randomreturns a random articleopencli wikipedia trending --limit 5returns most-read articlesopencli wikipedia random --lang zhworks with non-English languagesnpm run typecheckpasses