Skip to content

test(ui): add streaming Markdown regression and long-answer fixtures (#28) - #285

Merged
kbkb628 merged 2 commits into
helsome:mainfrom
yu1128-L:feat/copilot-streaming-markdown
Oct 3, 2026
Merged

kbkb628 merged 2 commits into
helsome:mainfrom
yu1128-L:feat/copilot-streaming-markdown

Conversation

@yu1128-L

@yu1128-L yu1128-L commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

改动说明

本 PR 为 #28 增加生产可验证的 streaming Markdown 回归面,不改变渲染实现:

  • 新增 streaming-markdown-fixtures.ts:提供未闭合 fenced code / GFM table / list / link / citation marker / blockquote 等真实流式中间态,以及一个长金融报告 fixture(含 headings、lists、blockquote、table、code、links 和恶意 HTML / unsafe URL);
  • 新增 MarkdownContent.streaming.test.tsx 回归测试:
    • 每个 partial 片段以 streaming 渲染不崩溃、不丢前文;
    • complete 内容 streaming 与 static 渲染 textContent 语义一致;
    • 长报告可完整渲染,且 raw HTML、event handler、javascript: URL、协议相对 URL 不进入 DOM。

关联 Issue

Refs #28

测试报告(正式审核前必填)

环境

  • Bun:1.4.2
  • OS:Windows

实际执行命令与结果

bun test packages/ui/src/components/chat/MarkdownContent.streaming.test.tsx packages/ui/src/components/chat/MarkdownContent.test.tsx packages/ui/src/lib/citations.test.ts
→ 23 passed / 0 failed

bun run --filter '@finagent/ui' typecheck
→ exit 0

已知失败 / Baseline(如有)

无。

  • 已提供实际测试命令与 pass/fail 结果
  • 已说明测试环境
  • 如果存在已知 baseline / 环境失败,已提供 main 对照或说明:无
  • 核心改动已有对应 focused test / smoke / integration 验证

UI 截图(仅可见 UI 变化时必填)

  • 本 PR 无可见 UI 变化(无需截图)

本 PR 只新增 fixtures 与测试,不改变可见渲染结果。

Scope / 后续

本 PR 覆盖 #28 中“流式不崩溃 / 不丢前文”和“streamed / static 语义一致”两条验收项,以及 XSS 安全回归的一部分。真实模型长 Markdown 回答录屏 / 截图、auto-scroll 行为、性能 benchmark 仍属 issue 后续工作,不声明 #28 整体完成。

@kbkb628 kbkb628 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

我按仓库测试规范在本机独立复现并做了变异检查,先说结论:结构类断言是真实有效的,但"流式"和"安全"这两半目前守不住任何东西,建议改完再合。

实测(bun 1.4.2 / Windows,worktree 检出 aad35a73)

命令 结果
bun test packages/ui/src/components/chat/MarkdownContent.streaming.test.tsx exit 0 — 11 pass / 0 fail(941ms)
bun run typecheck exit 0,5 个 workspace 全 0
bun test packages/ui 326 pass / 0 fail / 1 error,exit 1 —— 这个 error 是既有的:把本 PR 的测试文件移走后控制组同样是 315 pass / 1 error、exit 1,差值正好是本 PR 的 +11

需要改的(两条阻塞)

1)安全断言是空转的 —— streaming-markdown-fixtures.ts:100-103

100: <script>alert(1)</script>
101: <img src="x" onerror="alert(1)">
102: [Unsafe](javascript:alert(1))
103: [Phish](//evil.example)

第 101 行的 <img> 会开启一个 CommonMark HTML block(一直延续到下一个空行,也就是 104 行),于是 102/103 两行被当成惰性 HTML-block 文本吞掉,根本没有变成 <a>。因此 test:96-97 的 a[href^="javascript:"] / a[href^="//"] 永远为 null —— 断言恒真。

证据:把 safeUrl 改成完全直通(等于删掉所有 URL 消毒)后,11/11 依然全过;把这两行链接从 <img> 后面移出来,两个恶意锚点才真的出现。test:98-99 的 textContent 包含 alert(1) / Phish 也是同一个原因。

修法:在 raw HTML 片段与链接之间加一个空行(或把恶意链接单独成段),让它们真正被解析成链接。另外 packages/ui/src/components/chat/MarkdownContent.test.tsx:50-87 已经用会正常解析的 fixtures 覆盖了 javascript:/data:/协议相对 URL —— 建议直接复用那批输入,避免更弱的重复。

2)没有任何流式中转覆盖

把 useStreamingContent 改成"永远返回挂载时的内容"(等价于流被冻结)后,11/11 依然全过。根因有两层:

  • MarkdownContent.tsx:41 用 useState(content) 以最终串初始化,所以"streaming"和"static"两条路径拿到的是同一个字符串,test:54、test:108-110 的 streaming ≡ static 接近恒真;
  • test:65-69 的 coalesces rapid deltas 把 partial 和 complete 两次 render 放进了同一个 act(),React 只 commit 最后一次 props,partial 从未挂载,所以这条用例其实什么都没 coalesce。

修法:先用 partial 挂载一个 root,再用 complete 重渲染同一个 root,并在两次之间断言中间态 DOM(例如 partial 阶段 code block 是否已闭合、表格行数/列表项数是否递增、pre code 内容是否已部分出现)。这样上面的冻结流变异才会被抓住。

建议(不阻塞)

3)test:43 的 stableText 都是 partial 的字面子串,用它断言分不出"解析过的 Markdown"和纯文本:把 ReactMarkdown 换成 <div>{displayContent}</div> 之后仍有 8/11 通过(6 个 fixture 用例 + matches static semantics + 安全用例都还过),只有 3 个结构断言会失败。建议 partial 阶段也断结构(pre code、table tbody tr、li),而不是只断 textContent。

4)streaming-markdown-fixtures.ts 是 112 行 test-only 代码,放在 packages/ui/src/lib/(生产源码目录)。我确认过它只被测试文件 import、不在 src/index.ts 里,也真实跑了 vite build 在产物里 grep(无命中)——不进 bundle,所以不是打包风险,但建议移到测试目录或直接并入测试文件。仓库既有惯例是放 testing/ 或 fixtures/(如 packages/longbridge-tools/src/testing/)。

5)测试里创建的 root 没有 unmount()(与相邻的 MarkdownContent.test.tsx 同风格,可选修)。

顺带

  • 实际 parent 是 ba5dcdf,不是描述里的 2b5b88d3(#284 合并前的一个提交);因为 #284 不碰这几个文件,merge-tree 无冲突,不需要 rebase,只是提一下。
  • 目前能被变异抓住的只有结构断言:h1 渲染成 <div> 会失败(10 pass / 1 fail),这部分是真的覆盖率,请保留。

改完上面 1、2 两条就够了,其余可以按你的判断取舍。

The hostile-URL fixture put the <img> tag directly above the javascript: and protocol-relative links, so CommonMark swallowed both lines into the HTML block and the URL assertions could never fail. Separate them with blank lines and add a control safe link so the paragraph is proven to parse as links.

The stream tests also only rendered a single frame: mounting partial and complete content in one act() meant partial never committed, and the hook was initialised with the final string, so a frozen stream passed every case. Mount the partial frame, assert the intermediate DOM, then push the completed delta on the same root and assert the transition.

Verified by mutation: bypassing safeUrl now fails the hostile-URL case, and freezing the stream at first render fails the two delta tests.

Refs helsome#28
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.

2 participants