v4ConversationTurnNavigator.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { describe, expect, it } from "vitest";
  2. import type {
  3. AssistantTextRow,
  4. ConversationRow,
  5. ReasoningRow,
  6. TimelineMarkerRow,
  7. TurnHeaderRow,
  8. UserInputRow,
  9. } from "@zcode/shared/zcode-protocol-v4";
  10. import { buildConversationTurnRenderUnits } from "@/v4/conversationTurnRenderUnits.js";
  11. import {
  12. buildConversationTurnNavigatorItems,
  13. resolveConversationTurnNavigatorActiveQueryRowId,
  14. resolveConversationTurnNavigatorActiveUnitIndex,
  15. resolveConversationTurnNavigatorHydrationRetryDelayMs,
  16. shouldHydrateConversationTurnNavigatorDirectory,
  17. } from "@/v4/conversationTurnNavigatorHelpers.js";
  18. function baseRow<T extends ConversationRow>(
  19. row: Omit<T, "createdAt" | "createdAtSeq" | "rowId" | "turnId"> & {
  20. rowId: number;
  21. turnId?: string;
  22. },
  23. ): T {
  24. return {
  25. createdAt: 1_700_000_000_000 + row.rowId,
  26. createdAtSeq: row.rowId,
  27. turnId: row.turnId ?? "turn-1",
  28. ...row,
  29. } as T;
  30. }
  31. function turnHeader(
  32. rowId: number,
  33. turnId: string,
  34. overrides: Partial<TurnHeaderRow> = {},
  35. ): TurnHeaderRow {
  36. return baseRow<TurnHeaderRow>({
  37. rowId,
  38. turnId,
  39. kind: "turnHeader",
  40. origin: "userInput",
  41. state: "completedSuccess",
  42. startedAt: 1_700_000_000_000 + rowId,
  43. activeMs: 1_000,
  44. ...overrides,
  45. });
  46. }
  47. function userInput(
  48. rowId: number,
  49. turnId: string,
  50. text: string,
  51. origin: UserInputRow["origin"] = "realUser",
  52. overrides: Partial<UserInputRow> = {},
  53. ): UserInputRow {
  54. return baseRow<UserInputRow>({
  55. rowId,
  56. turnId,
  57. kind: "userInput",
  58. text,
  59. origin,
  60. ...overrides,
  61. });
  62. }
  63. function assistantText(rowId: number, turnId: string, text: string): AssistantTextRow {
  64. return baseRow<AssistantTextRow>({
  65. rowId,
  66. turnId,
  67. kind: "assistantText",
  68. text,
  69. state: "complete",
  70. });
  71. }
  72. function reasoning(rowId: number, turnId: string, text: string): ReasoningRow {
  73. return baseRow<ReasoningRow>({
  74. rowId,
  75. turnId,
  76. kind: "reasoning",
  77. text,
  78. state: "complete",
  79. });
  80. }
  81. function compactMarker(rowId: number, turnId: string): TimelineMarkerRow {
  82. return baseRow<TimelineMarkerRow>({
  83. rowId,
  84. turnId,
  85. kind: "timelineMarker",
  86. lane: "assistantWork",
  87. marker: {
  88. type: "compact",
  89. origin: "manual",
  90. status: "success",
  91. },
  92. });
  93. }
  94. function units(rows: ConversationRow[]) {
  95. return buildConversationTurnRenderUnits(rows, { nowMs: 1_700_000_100_000 });
  96. }
  97. describe("conversation turn navigator helpers", () => {
  98. it("TN01/TN07:宽 conversation 允许探测完整目录,窄 conversation 零请求", () => {
  99. const base = {
  100. canLoadOlder: true,
  101. containerWidthPx: 864,
  102. hasLoadHandler: true,
  103. loadingOlder: false,
  104. };
  105. expect(
  106. shouldHydrateConversationTurnNavigatorDirectory({
  107. ...base,
  108. containerWidthPx: 863,
  109. }),
  110. ).toBe(false);
  111. expect(
  112. shouldHydrateConversationTurnNavigatorDirectory({
  113. ...base,
  114. }),
  115. ).toBe(true);
  116. });
  117. it("TN07:暂时失败只安排两次有界退避重试", () => {
  118. expect(resolveConversationTurnNavigatorHydrationRetryDelayMs(1)).toBe(250);
  119. expect(resolveConversationTurnNavigatorHydrationRetryDelayMs(2)).toBe(1_000);
  120. expect(resolveConversationTurnNavigatorHydrationRetryDelayMs(3)).toBeNull();
  121. });
  122. it("TN02/TN06:每条 realUser query 独立建项并共享所属 turn 的 assistant preview", () => {
  123. const items = buildConversationTurnNavigatorItems(
  124. units([
  125. turnHeader(1, "turn-1"),
  126. userInput(2, "turn-1", "Alpha query"),
  127. reasoning(3, "turn-1", "private reasoning should not leak"),
  128. assistantText(
  129. 4,
  130. "turn-1",
  131. "Assistant paragraph one has the useful answer.\n\nAssistant paragraph two keeps more context.\n\nAssistant paragraph three should be trimmed.",
  132. ),
  133. userInput(5, "turn-1", "Guide query stays separate", "realUser", {
  134. guided: true,
  135. }),
  136. assistantText(6, "turn-1", "Guide answer must not enter preview"),
  137. compactMarker(7, "marker-only"),
  138. turnHeader(8, "turn-2", { state: "running", activeMs: undefined }),
  139. userInput(9, "turn-2", "Gamma running query"),
  140. ]),
  141. {
  142. assistantEmptyPreview: "No assistant text yet",
  143. assistantRunningPreview: "Assistant is still working",
  144. maxPreviewChars: 88,
  145. maxPreviewParagraphs: 2,
  146. userFallbackPreview: "User turn",
  147. },
  148. );
  149. expect(items.map((item) => item.turnId)).toEqual(["turn-1", "turn-1", "turn-2"]);
  150. expect(items[0]).toMatchObject({
  151. unitIndex: 0,
  152. rowId: 2,
  153. userPreview: "Alpha query",
  154. assistantPreviewKind: "text",
  155. isRunning: false,
  156. });
  157. expect(items[1]).toMatchObject({
  158. unitIndex: 0,
  159. rowId: 5,
  160. userPreview: "Guide query stays separate",
  161. assistantPreviewKind: "text",
  162. isRunning: false,
  163. });
  164. expect(items[2]).toMatchObject({
  165. unitIndex: 2,
  166. rowId: 9,
  167. userPreview: "Gamma running query",
  168. assistantPreview: "Assistant is still working",
  169. assistantPreviewKind: "running",
  170. isRunning: true,
  171. });
  172. expect(items[0]?.assistantPreview).toContain("Assistant paragraph one");
  173. expect(items[0]?.assistantPreview).toContain("Assistant paragraph two");
  174. expect(items[0]?.assistantPreview).not.toContain("private reasoning");
  175. expect(items[1]?.assistantPreview).toBe(items[0]?.assistantPreview);
  176. expect(items.map((item) => item.userPreview).join(" ")).not.toContain("Assistant paragraph");
  177. expect(new Set(items.map((item) => item.key)).size).toBe(3);
  178. });
  179. it("TN05:只为 realUser turn 建目录,排除 assistant-only 与系统 role=user", () => {
  180. const items = buildConversationTurnNavigatorItems(
  181. units([
  182. assistantText(1, "turn-restored", "Restored assistant only body"),
  183. turnHeader(2, "turn-background", { origin: "backgroundResult" }),
  184. userInput(3, "turn-background", "system background payload", "backgroundResult"),
  185. assistantText(4, "turn-background", "Background result"),
  186. turnHeader(5, "turn-goal", { origin: "goalContinuation" }),
  187. userInput(6, "turn-goal", "continue goal", "goalContinuation"),
  188. assistantText(7, "turn-goal", "Goal result"),
  189. turnHeader(8, "turn-mailbox"),
  190. userInput(9, "turn-mailbox", "mailbox payload", "mailbox"),
  191. assistantText(10, "turn-mailbox", "Mailbox result"),
  192. turnHeader(11, "turn-real"),
  193. userInput(12, "turn-real", "Actual user query"),
  194. assistantText(13, "turn-real", "Actual answer"),
  195. ]),
  196. {
  197. assistantEmptyPreview: "No assistant text yet",
  198. assistantRunningPreview: "Assistant is still working",
  199. userFallbackPreview: "User turn",
  200. },
  201. );
  202. expect(items).toEqual([
  203. expect.objectContaining({
  204. turnId: "turn-real",
  205. rowId: 12,
  206. userPreview: "Actual user query",
  207. assistantPreview: "Actual answer",
  208. }),
  209. ]);
  210. });
  211. it("resolves the active turn from the currently visible virtual rows", () => {
  212. const items = [
  213. { key: "turn-1", turnId: "turn-1", unitIndex: 0 },
  214. { key: "turn-2", turnId: "turn-2", unitIndex: 4 },
  215. { key: "turn-3", turnId: "turn-3", unitIndex: 7 },
  216. ].map((item) => ({
  217. ...item,
  218. assistantPreview: "answer",
  219. assistantPreviewKind: "text" as const,
  220. isRunning: false,
  221. rowId: item.unitIndex + 10,
  222. userPreview: item.turnId,
  223. }));
  224. expect(
  225. resolveConversationTurnNavigatorActiveUnitIndex({
  226. items,
  227. scrollOffsetPx: 390,
  228. viewportHeightPx: 360,
  229. virtualItems: [
  230. { index: 4, start: 360, size: 220 },
  231. { index: 7, start: 760, size: 160 },
  232. ],
  233. }),
  234. ).toBe(4);
  235. expect(
  236. resolveConversationTurnNavigatorActiveUnitIndex({
  237. items,
  238. scrollOffsetPx: 650,
  239. viewportHeightPx: 280,
  240. virtualItems: [
  241. { index: 5, start: 610, size: 120 },
  242. { index: 6, start: 730, size: 80 },
  243. ],
  244. }),
  245. ).toBe(7);
  246. expect(
  247. resolveConversationTurnNavigatorActiveUnitIndex({
  248. items,
  249. scrollOffsetPx: 0,
  250. viewportHeightPx: 400,
  251. virtualItems: [],
  252. }),
  253. ).toBe(0);
  254. });
  255. it("TN06:同一 turn 内根据可见 row 锚点解析 active query", () => {
  256. expect(
  257. resolveConversationTurnNavigatorActiveQueryRowId({
  258. positions: [
  259. { rowId: 11, start: 80, end: 140 },
  260. { rowId: 12, start: 360, end: 420 },
  261. { rowId: 13, start: 700, end: 760 },
  262. ],
  263. scrollOffsetPx: 330,
  264. viewportHeightPx: 300,
  265. }),
  266. ).toBe(12);
  267. expect(
  268. resolveConversationTurnNavigatorActiveQueryRowId({
  269. positions: [
  270. { rowId: 11, start: 80, end: 140 },
  271. { rowId: 12, start: 360, end: 420 },
  272. { rowId: 13, start: 700, end: 760 },
  273. ],
  274. scrollOffsetPx: 520,
  275. viewportHeightPx: 120,
  276. }),
  277. ).toBe(12);
  278. });
  279. });