Skip to content

fix(ui): NewsView 按 epoch 秒渲染新闻时效,不再恒显示「N 年前」 - #234

Merged
helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-newsview-relative-time
Oct 18, 2026
Merged

helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-newsview-relative-time

Conversation

@wxrbyte

@wxrbyte wxrbyte commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

问题与修复

NewsView 的 relativeTime()(packages/ui/src/components/workspace/NewsView.tsx:10)用 Date.now() - timestamp 直接相减,但 NewsItem.timestamp 的单位是 epoch 秒。

依据是仓库自己的两处自述契约:

  • packages/core/src/market-data.ts:10-11:「Timestamps are epoch SECONDS unless noted otherwise, matching the existing core convention (Quote.timestamp, Kline.timestamp, NewsItem.timestamp).」
  • 能力层在同一份数据上就是这么换算的:packages/shared/src/capabilities/manifests/research-news.ts:40 marketTime: latest.timestamp * 1000、:57 new Date(item.timestamp * 1000)。

于是 diff 被放大 1000 倍,minutes → hours → days → months 四级分支全部跳过,恒定落到 yearsAgo;连 if (diff < 0) return justNow 的兜底也永不触发。

修复:按能力层的既有写法先换算再相减。

const diff = Date.now() - timestamp * 1000;

Closes #233

可复现测试报告

环境:Windows 11 / Bun 1.4.2;本地基线 = 上游 main ba5dcdfd31b162f5edb8b908f7f099a560389326(2026-09-24T11:05:17Z),工作树已按 main 字节对齐。

新增回归用例 packages/ui/src/components/workspace/NewsView.test.tsx(渲染真实 NewsView 组件 + createSyncI18n,3 例):

阶段 命令 结果
修复前 bun test ./packages/ui/src/components/workspace/NewsView.test.tsx 0 pass / 3 fail
修复后 同上 3 pass / 0 fail

修复前三条用例收到的渲染文本完全相同,都是 "Apple ships a thingA summary57y ago":

NewsItem.timestamp 输入 修复前渲染 修复后渲染 预期
now - 5*60(5 分钟前) 57y ago 5m ago 5m ago
now - 3*86400(3 天前) 57y ago 3d ago 3d ago
now + 30(略超前,时钟漂移) 57y ago just now just now

硬门禁:

  • bun run typecheck —— core / i18n / shared / ui / electron 五个工作区全部 exit 0。
  • bun test packages/ui --isolate —— 324 pass / 0 fail(对齐 main 的本机基线 321 pass / 0 fail + 本轮新增 3 例)。

未运行的测试:未跑 bun test --isolate 全量(本机 6 核 / 8GB 负载下超时抖动明显,且本 PR 只动 packages/ui);apps/electron e2e 未跑,本机沙箱无法 spawn cmd.exe(已知环境限制)。

UI 变化

新闻列表右侧的相对时间文案由恒定的「57y ago」变为真实时效(5m ago / 3d ago / hours / just now 等)。无布局、样式或 DOM 结构变化;无新增依赖;未改动任何公共导出面。

`relativeTime()` 用 `Date.now() - timestamp` 直接相减,而
`NewsItem.timestamp` 的单位是 epoch 秒(core/market-data.ts:10-11),
于是 diff 被放大 1000 倍,`minutes/hours/days/months` 四级分支全部跳过,
恒定落到 `yearsAgo` —— 5 分钟前的新闻也显示「57y ago」,
连 `diff < 0` 的「just now」兜底都永不触发。

按能力层 `research-news.ts:57` 的既有写法先换算再相减。

新增 `NewsView.test.tsx`(3 例,渲染真实组件 + createSyncI18n):
修复前 0 pass / 3 fail(三条均渲染成同一句 "57y ago")
→ 修复后 3 pass / 0 fail。

@helsome helsome left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本轮独立源码审查及精确 HEAD focused 3/0 通过。最新main累计组合完整测试1959 pass / 7 skip / 0 fail,全workspace typecheck、i18n通过,secret扫描零命中。仅按本PR声明范围验收,不把离线回归当真实模型/生产API验收。

@helsome
helsome merged commit 45cc852 into helsome:main Oct 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(ui): NewsView 用 Date.now() 直接减 epoch 秒时间戳,新闻时效恒显示「N 年前」

2 participants