Files
Jyotisha/frontend/tests/chat-markdown-split.test.ts
ClaudeandClaude Fable 5.2 c461305b36 fix: chat status animation and streamed answer stop stuttering and jumping
Measured in headless Chromium against the real transcript, scroll hook and CSS.

- Status highlight: transform-only sweep (compositor) instead of
  background-position under background-clip:text, which repainted the label
  on the main thread every frame; labels share one sweep phase so a reworded
  step no longer restarts it.
- Step timeline above the answer: folds to its summary line once the answer
  has text, and folds away over 180ms when the run ends instead of being
  dropped between two frames (the answer jumped up 164-260px); a half-written
  heading is no longer a step.
- Streamed markdown: unclosed ** is bold as written, a table is held until
  its delimiter row, bare markers and the half-typed 判断依据 heading wait.
- Answers render as memoised blocks, streaming and settled alike: a frame
  parses only the tail, settling does not rebuild the answer's DOM.
- The report layer (from the first H2) is written out while streaming; it
  used to stay empty until the run ended.
- Follow mode scrolls in the resize callback, before paint; each new line
  used to be painted once below its place.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
2026-11-16 10:31:52 +08:00

90 lines
4.8 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import { createElement } from "react";
import { renderToString } from "react-dom/server";
import { StableMarkdownPrefix, StreamingMarkdown } from "../src/components/chat-message-content.tsx";
import { splitStableMarkdown } from "../src/lib/chat-markdown-split.ts";
const contentSource = readFileSync(new URL("../src/components/chat-message-content.tsx", import.meta.url), "utf8");
const messageRowSource = readFileSync(new URL("../src/components/chat-message-row.tsx", import.meta.url), "utf8");
test("the stable prefix ends at the last completed paragraph and the tail keeps streaming", () => {
assert.deepEqual(splitStableMarkdown("只有一段还没写完"), { stable: "", tail: "只有一段还没写完" });
assert.deepEqual(splitStableMarkdown("第一段。\n\n第二段还在"), { stable: "第一段。", tail: "第二段还在" });
assert.deepEqual(
splitStableMarkdown("## 标题\n\n第一段。\n\n第二段。\n\n第三"),
{ stable: "## 标题\n\n第一段。\n\n第二段。", tail: "第三" },
);
});
test("the cut never lands inside a fence, between list items, or inside a table", () => {
const fenced = "前言。\n\n```txt\n第一行\n\n第二行";
assert.deepEqual(splitStableMarkdown(fenced), { stable: "前言。", tail: "```txt\n第一行\n\n第二行" });
const looseList = "- 甲\n\n- 乙\n\n- 丙还在";
assert.deepEqual(splitStableMarkdown(looseList), { stable: "", tail: looseList });
const listThenParagraph = "- 甲\n- 乙\n\n总结一下";
assert.deepEqual(splitStableMarkdown(listThenParagraph), { stable: "- 甲\n- 乙", tail: "总结一下" });
const orderedListContinues = "1. 甲\n\n2. 乙\n\n 缩进的补充";
assert.deepEqual(splitStableMarkdown(orderedListContinues), { stable: "", tail: orderedListContinues });
const table = "说明。\n\n| 技法 | 状态 |\n| --- | --- |\n| 甲 | 已执行 |\n\n> 引用";
assert.deepEqual(
splitStableMarkdown(table),
{ stable: "说明。\n\n| 技法 | 状态 |\n| --- | --- |\n| 甲 | 已执行 |", tail: "> 引用" },
);
});
test("growing the tail keeps the prefix string identical so the memoised prefix is not re-parsed", () => {
const before = splitStableMarkdown("第一段。\n\n第二段。\n\n第三段正在");
const after = splitStableMarkdown("第一段。\n\n第二段。\n\n第三段正在写,还没有换段");
assert.equal(before.stable, after.stable);
assert.notEqual(before.tail, after.tail);
// Only once a new paragraph completes does the prefix move forward.
const later = splitStableMarkdown("第一段。\n\n第二段。\n\n第三段写完了。\n\n第四");
assert.equal(later.stable, "第一段。\n\n第二段。\n\n第三段写完了。");
});
// 原值: 流式渲染把回答分成「一个不断变长的前缀 + 尾巴」两份文档
// 新值: 每个写完的块各自一份,尾巴一份;结算后的回答走同一个组件
// 原因: 2026-11-16 流式输出卡顿——每写完一段,前缀整段重新 parse、整棵树重新比对
test("the streaming renderer parses every completed block and the tail as separate documents", () => {
const calls: string[] = [];
const renderMarkdown = (text: string) => {
calls.push(text);
return createElement("p", null, text);
};
renderToString(createElement(StreamingMarkdown, {
text: "第一段。\n\n第二段。\n\n第三段还在",
renderMarkdown,
}));
assert.deepEqual([...calls].sort(), ["第一段。", "第二段。", "第三段还在"].sort());
// Each block is memoised on its text, so an unchanged block costs no parse.
assert.match(contentSource, /const StableMarkdownPrefix = memo\(function StableMarkdownPrefix/);
assert.match(contentSource, /splitMarkdownBlocks\(text, !streaming\)/);
// 原值: `streaming ? <StreamingMarkdown …/> : <StableMarkdownPrefix text={spoken} …/>`
// 新值: 流式和结算都是 `<StreamingMarkdown … streaming={streaming} />`,结算时不换组件
// 原因: 结算那一帧换组件会把整段回答的 DOM 重建一遍;BUG-725 的记忆化仍在(按块)
assert.match(contentSource, /<StreamingMarkdown text=\{spoken\} renderMarkdown=\{renderMarkdown\} streaming=\{streaming\} \/>/);
assert.match(messageRowSource, /streaming=\{message\.state !== "settled"\}/);
});
test("settled markdown parses the same text only once across consecutive renders", () => {
const calls: string[] = [];
const renderMarkdown = (text: string) => {
calls.push(text);
return createElement("p", null, text);
};
const text = "已结算的一段回答,对照了 D9 和 D10。";
renderToString(createElement(StableMarkdownPrefix, { text, renderMarkdown }));
renderToString(createElement(StableMarkdownPrefix, { text, renderMarkdown }));
assert.equal(calls.length, 1);
assert.deepEqual(calls, [text]);
});