v4ConversationTurnNavigator.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import type {
  5. AssistantTextRow,
  6. ConversationRow,
  7. ReasoningRow,
  8. TimelineMarkerRow,
  9. TurnHeaderRow,
  10. UserInputRow,
  11. } from "@zcode/shared/zcode-protocol-v4";
  12. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  13. import { ConversationTurnNavigator } from "@/v4/ConversationTurnNavigator.js";
  14. import { buildConversationTurnRenderUnits } from "@/v4/conversationTurnRenderUnits.js";
  15. import {
  16. buildConversationTurnNavigatorItems,
  17. resolveConversationTurnNavigatorActiveQueryRowId,
  18. resolveConversationTurnNavigatorActiveUnitIndex,
  19. resolveConversationTurnNavigatorBarVisualState,
  20. resolveConversationTurnNavigatorHydrationRetryDelayMs,
  21. resolveConversationTurnNavigatorVisualFocusItemIndex,
  22. shouldHydrateConversationTurnNavigatorDirectory,
  23. } from "@/v4/conversationTurnNavigatorHelpers.js";
  24. function baseRow<T extends ConversationRow>(
  25. row: Omit<T, "createdAt" | "createdAtSeq" | "rowId" | "turnId"> & {
  26. rowId: number;
  27. turnId?: string;
  28. },
  29. ): T {
  30. return {
  31. createdAt: 1_700_000_000_000 + row.rowId,
  32. createdAtSeq: row.rowId,
  33. turnId: row.turnId ?? "turn-1",
  34. ...row,
  35. } as T;
  36. }
  37. function turnHeader(
  38. rowId: number,
  39. turnId: string,
  40. overrides: Partial<TurnHeaderRow> = {},
  41. ): TurnHeaderRow {
  42. return baseRow<TurnHeaderRow>({
  43. rowId,
  44. turnId,
  45. kind: "turnHeader",
  46. origin: "userInput",
  47. state: "completedSuccess",
  48. startedAt: 1_700_000_000_000 + rowId,
  49. activeMs: 1_000,
  50. ...overrides,
  51. });
  52. }
  53. function userInput(
  54. rowId: number,
  55. turnId: string,
  56. text: string,
  57. origin: UserInputRow["origin"] = "realUser",
  58. overrides: Partial<UserInputRow> = {},
  59. ): UserInputRow {
  60. return baseRow<UserInputRow>({
  61. rowId,
  62. turnId,
  63. kind: "userInput",
  64. text,
  65. origin,
  66. ...overrides,
  67. });
  68. }
  69. function assistantText(rowId: number, turnId: string, text: string): AssistantTextRow {
  70. return baseRow<AssistantTextRow>({
  71. rowId,
  72. turnId,
  73. kind: "assistantText",
  74. text,
  75. state: "complete",
  76. });
  77. }
  78. function reasoning(rowId: number, turnId: string, text: string): ReasoningRow {
  79. return baseRow<ReasoningRow>({
  80. rowId,
  81. turnId,
  82. kind: "reasoning",
  83. text,
  84. state: "complete",
  85. });
  86. }
  87. function compactMarker(rowId: number, turnId: string): TimelineMarkerRow {
  88. return baseRow<TimelineMarkerRow>({
  89. rowId,
  90. turnId,
  91. kind: "timelineMarker",
  92. lane: "assistantWork",
  93. marker: {
  94. type: "compact",
  95. origin: "manual",
  96. status: "success",
  97. },
  98. });
  99. }
  100. function units(rows: ConversationRow[]) {
  101. return buildConversationTurnRenderUnits(rows, { nowMs: 1_700_000_100_000 });
  102. }
  103. describe("conversation turn navigator helpers", () => {
  104. it("TN01/TN07:宽 conversation 允许探测完整目录,窄 conversation 零请求", () => {
  105. const base = {
  106. canLoadOlder: true,
  107. containerWidthPx: 864,
  108. hasLoadHandler: true,
  109. loadingOlder: false,
  110. };
  111. expect(
  112. shouldHydrateConversationTurnNavigatorDirectory({
  113. ...base,
  114. containerWidthPx: 863,
  115. }),
  116. ).toBe(false);
  117. expect(
  118. shouldHydrateConversationTurnNavigatorDirectory({
  119. ...base,
  120. }),
  121. ).toBe(true);
  122. expect(
  123. shouldHydrateConversationTurnNavigatorDirectory({
  124. ...base,
  125. }),
  126. ).toBe(true);
  127. });
  128. it("TN07:暂时失败只安排两次有界退避重试", () => {
  129. expect(resolveConversationTurnNavigatorHydrationRetryDelayMs(1)).toBe(250);
  130. expect(resolveConversationTurnNavigatorHydrationRetryDelayMs(2)).toBe(1_000);
  131. expect(resolveConversationTurnNavigatorHydrationRetryDelayMs(3)).toBeNull();
  132. });
  133. it("TN02/TN06:每条 realUser query 独立建项并共享所属 turn 的 assistant preview", () => {
  134. const items = buildConversationTurnNavigatorItems(
  135. units([
  136. turnHeader(1, "turn-1"),
  137. userInput(2, "turn-1", "Alpha query"),
  138. reasoning(3, "turn-1", "private reasoning should not leak"),
  139. assistantText(
  140. 4,
  141. "turn-1",
  142. "Assistant paragraph one has the useful answer.\n\nAssistant paragraph two keeps more context.\n\nAssistant paragraph three should be trimmed.",
  143. ),
  144. userInput(5, "turn-1", "Guide query stays separate", "realUser", {
  145. guided: true,
  146. }),
  147. assistantText(6, "turn-1", "Guide answer must not enter preview"),
  148. compactMarker(7, "marker-only"),
  149. turnHeader(8, "turn-2", { state: "running", activeMs: undefined }),
  150. userInput(9, "turn-2", "Gamma running query"),
  151. ]),
  152. {
  153. assistantEmptyPreview: "No assistant text yet",
  154. assistantRunningPreview: "Assistant is still working",
  155. maxPreviewChars: 88,
  156. maxPreviewParagraphs: 2,
  157. userFallbackPreview: "User turn",
  158. },
  159. );
  160. expect(items.map((item) => item.turnId)).toEqual(["turn-1", "turn-1", "turn-2"]);
  161. expect(items[0]).toMatchObject({
  162. unitIndex: 0,
  163. rowId: 2,
  164. userPreview: "Alpha query",
  165. assistantPreviewKind: "text",
  166. isRunning: false,
  167. });
  168. expect(items[1]).toMatchObject({
  169. unitIndex: 0,
  170. rowId: 5,
  171. userPreview: "Guide query stays separate",
  172. assistantPreviewKind: "text",
  173. isRunning: false,
  174. });
  175. expect(items[2]).toMatchObject({
  176. unitIndex: 2,
  177. rowId: 9,
  178. userPreview: "Gamma running query",
  179. assistantPreview: "Assistant is still working",
  180. assistantPreviewKind: "running",
  181. isRunning: true,
  182. });
  183. expect(items[0]?.assistantPreview).toContain("Assistant paragraph one");
  184. expect(items[0]?.assistantPreview).toContain("Assistant paragraph two");
  185. expect(items[0]?.assistantPreview).not.toContain("private reasoning");
  186. expect(items[1]?.assistantPreview).toBe(items[0]?.assistantPreview);
  187. expect(items.map((item) => item.userPreview).join(" ")).not.toContain(
  188. "Assistant paragraph",
  189. );
  190. expect(new Set(items.map((item) => item.key)).size).toBe(3);
  191. });
  192. it("TN05:只为 realUser turn 建目录,排除 assistant-only 与系统 role=user", () => {
  193. const items = buildConversationTurnNavigatorItems(
  194. units([
  195. assistantText(1, "turn-restored", "Restored assistant only body"),
  196. turnHeader(2, "turn-background", { origin: "backgroundResult" }),
  197. userInput(3, "turn-background", "system background payload", "backgroundResult"),
  198. assistantText(4, "turn-background", "Background result"),
  199. turnHeader(5, "turn-goal", { origin: "goalContinuation" }),
  200. userInput(6, "turn-goal", "continue goal", "goalContinuation"),
  201. assistantText(7, "turn-goal", "Goal result"),
  202. turnHeader(8, "turn-mailbox"),
  203. userInput(9, "turn-mailbox", "mailbox payload", "mailbox"),
  204. assistantText(10, "turn-mailbox", "Mailbox result"),
  205. turnHeader(11, "turn-real"),
  206. userInput(12, "turn-real", "Actual user query"),
  207. assistantText(13, "turn-real", "Actual answer"),
  208. ]),
  209. {
  210. assistantEmptyPreview: "No assistant text yet",
  211. assistantRunningPreview: "Assistant is still working",
  212. userFallbackPreview: "User turn",
  213. },
  214. );
  215. expect(items).toEqual([
  216. expect.objectContaining({
  217. turnId: "turn-real",
  218. rowId: 12,
  219. userPreview: "Actual user query",
  220. assistantPreview: "Actual answer",
  221. }),
  222. ]);
  223. });
  224. it("resolves the active turn from the currently visible virtual rows", () => {
  225. const items = [
  226. { key: "turn-1", turnId: "turn-1", unitIndex: 0 },
  227. { key: "turn-2", turnId: "turn-2", unitIndex: 4 },
  228. { key: "turn-3", turnId: "turn-3", unitIndex: 7 },
  229. ].map((item) => ({
  230. ...item,
  231. assistantPreview: "answer",
  232. assistantPreviewKind: "text" as const,
  233. isRunning: false,
  234. rowId: item.unitIndex + 10,
  235. userPreview: item.turnId,
  236. }));
  237. expect(
  238. resolveConversationTurnNavigatorActiveUnitIndex({
  239. items,
  240. scrollOffsetPx: 390,
  241. viewportHeightPx: 360,
  242. virtualItems: [
  243. { index: 4, start: 360, size: 220 },
  244. { index: 7, start: 760, size: 160 },
  245. ],
  246. }),
  247. ).toBe(4);
  248. expect(
  249. resolveConversationTurnNavigatorActiveUnitIndex({
  250. items,
  251. scrollOffsetPx: 650,
  252. viewportHeightPx: 280,
  253. virtualItems: [
  254. { index: 5, start: 610, size: 120 },
  255. { index: 6, start: 730, size: 80 },
  256. ],
  257. }),
  258. ).toBe(7);
  259. expect(
  260. resolveConversationTurnNavigatorActiveUnitIndex({
  261. items,
  262. scrollOffsetPx: 0,
  263. viewportHeightPx: 400,
  264. virtualItems: [],
  265. }),
  266. ).toBe(0);
  267. });
  268. it("TN06:同一 turn 内根据可见 row 锚点解析 active query", () => {
  269. expect(
  270. resolveConversationTurnNavigatorActiveQueryRowId({
  271. positions: [
  272. { rowId: 11, start: 80, end: 140 },
  273. { rowId: 12, start: 360, end: 420 },
  274. { rowId: 13, start: 700, end: 760 },
  275. ],
  276. scrollOffsetPx: 330,
  277. viewportHeightPx: 300,
  278. }),
  279. ).toBe(12);
  280. expect(
  281. resolveConversationTurnNavigatorActiveQueryRowId({
  282. positions: [
  283. { rowId: 11, start: 80, end: 140 },
  284. { rowId: 12, start: 360, end: 420 },
  285. { rowId: 13, start: 700, end: 760 },
  286. ],
  287. scrollOffsetPx: 520,
  288. viewportHeightPx: 120,
  289. }),
  290. ).toBe(12);
  291. });
  292. it("keeps scroll active compact until hover or focus creates a mountain", () => {
  293. expect(
  294. resolveConversationTurnNavigatorVisualFocusItemIndex({
  295. activeItemIndex: 3,
  296. interactionItemIndex: undefined,
  297. }),
  298. ).toBeUndefined();
  299. expect(
  300. resolveConversationTurnNavigatorVisualFocusItemIndex({
  301. activeItemIndex: 3,
  302. interactionItemIndex: 1,
  303. }),
  304. ).toBe(1);
  305. expect(
  306. resolveConversationTurnNavigatorBarVisualState({
  307. itemIndex: 3,
  308. visualFocusItemIndex: undefined,
  309. }),
  310. ).toMatchObject({ tone: "idle", scaleX: 1 });
  311. expect(
  312. resolveConversationTurnNavigatorBarVisualState({
  313. itemIndex: 3,
  314. visualFocusItemIndex: 3,
  315. }),
  316. ).toMatchObject({ colorTone: "focus", tone: "peak", scaleX: 2.6 });
  317. expect(
  318. resolveConversationTurnNavigatorBarVisualState({
  319. itemIndex: 2,
  320. visualFocusItemIndex: 3,
  321. }),
  322. ).toMatchObject({ colorTone: "muted", tone: "near", scaleX: 1.7 });
  323. expect(
  324. resolveConversationTurnNavigatorBarVisualState({
  325. itemIndex: 1,
  326. visualFocusItemIndex: 3,
  327. }),
  328. ).toMatchObject({ colorTone: "muted", tone: "mid", scaleX: 1.25 });
  329. expect(
  330. resolveConversationTurnNavigatorBarVisualState({
  331. itemIndex: 0,
  332. visualFocusItemIndex: 3,
  333. }),
  334. ).toMatchObject({ tone: "idle", scaleX: 1 });
  335. expect(
  336. resolveConversationTurnNavigatorBarVisualState({
  337. itemIndex: 0,
  338. visualFocusItemIndex: undefined,
  339. }),
  340. ).toMatchObject({ tone: "idle", scaleX: 1 });
  341. });
  342. it("positions the compact rail at the owning timeline vertical center", () => {
  343. const html = renderToStaticMarkup(
  344. createElement(
  345. ZCodeIntlProvider,
  346. { initialLocale: "zh-CN" },
  347. createElement(ConversationTurnNavigator, {
  348. onJumpToQuery: () => {},
  349. renderUnits: units([
  350. turnHeader(1, "turn-1"),
  351. userInput(2, "turn-1", "First query"),
  352. assistantText(3, "turn-1", "First answer"),
  353. turnHeader(4, "turn-2"),
  354. userInput(5, "turn-2", "Second query"),
  355. assistantText(6, "turn-2", "Second answer"),
  356. ]),
  357. scrollOffsetPx: 0,
  358. viewportHeightPx: 500,
  359. virtualItems: [
  360. { index: 0, size: 120, start: 0 },
  361. { index: 1, size: 120, start: 120 },
  362. ],
  363. }),
  364. ),
  365. );
  366. expect(html).toContain("top-1/2");
  367. expect(html).toContain("-translate-y-1/2");
  368. expect(html).not.toContain("top-12");
  369. expect(html).toContain("invisible");
  370. expect(html).toContain("opacity-0");
  371. expect(html).toContain("-translate-x-2");
  372. expect(html).toContain("@min-[864px]/conversation:visible");
  373. expect(html).toContain("@min-[864px]/conversation:opacity-100");
  374. expect(html).toContain("@min-[864px]/conversation:translate-x-0");
  375. expect(html).toContain("transition-[opacity,transform,visibility]");
  376. expect(html).toContain("motion-reduce:transition-none");
  377. });
  378. it("TN03:rail 只允许纵向滚动,山峰放大不产生横向滚动条", () => {
  379. const html = renderToStaticMarkup(
  380. createElement(
  381. ZCodeIntlProvider,
  382. { initialLocale: "zh-CN" },
  383. createElement(ConversationTurnNavigator, {
  384. onJumpToQuery: () => {},
  385. renderUnits: units([
  386. turnHeader(1, "turn-1"),
  387. userInput(2, "turn-1", "First query"),
  388. turnHeader(3, "turn-2"),
  389. userInput(4, "turn-2", "Second query"),
  390. ]),
  391. scrollOffsetPx: 0,
  392. viewportHeightPx: 500,
  393. virtualItems: [
  394. { index: 0, size: 120, start: 0 },
  395. { index: 1, size: 120, start: 120 },
  396. ],
  397. }),
  398. ),
  399. );
  400. expect(html).toContain("overflow-x-hidden");
  401. expect(html).toContain("overflow-y-auto");
  402. expect(html).toContain("!scrollbar-hide");
  403. });
  404. });