v4ConversationFindIndex.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { describe, expect, it } from "vitest";
  2. import type {
  3. AssistantTextRow,
  4. ConversationRow,
  5. ReasoningRow,
  6. ToolCallRow,
  7. TurnHeaderRow,
  8. UserInputRow,
  9. } from "@zcode/shared/zcode-protocol-v4";
  10. import {
  11. buildConversationFindIndex,
  12. findConversationMatchIndexByKey,
  13. getConversationFindMatchKey,
  14. resolveConversationFindActiveMatch,
  15. resolveSearchResultHighlightMatch,
  16. } from "@/v4/conversationFindIndex.js";
  17. import { buildConversationTurnRenderUnits } from "@/v4/conversationTurnRenderUnits.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(rowId: number, turnId = "turn-1"): TurnHeaderRow {
  32. return baseRow<TurnHeaderRow>({
  33. rowId,
  34. turnId,
  35. kind: "turnHeader",
  36. origin: "userInput",
  37. state: "completedSuccess",
  38. startedAt: 1_700_000_000_000 + rowId,
  39. });
  40. }
  41. function userInput(rowId: number, text: string, turnId = "turn-1"): UserInputRow {
  42. return baseRow<UserInputRow>({
  43. rowId,
  44. turnId,
  45. kind: "userInput",
  46. text,
  47. origin: "realUser",
  48. });
  49. }
  50. function assistantText(rowId: number, text: string, turnId = "turn-1"): AssistantTextRow {
  51. return baseRow<AssistantTextRow>({
  52. rowId,
  53. turnId,
  54. kind: "assistantText",
  55. text,
  56. state: "complete",
  57. });
  58. }
  59. function reasoning(rowId: number, text: string): ReasoningRow {
  60. return baseRow<ReasoningRow>({
  61. rowId,
  62. kind: "reasoning",
  63. text,
  64. state: "complete",
  65. });
  66. }
  67. function toolCall(rowId: number, inputText: string): ToolCallRow {
  68. return baseRow<ToolCallRow>({
  69. rowId,
  70. kind: "toolCall",
  71. toolCallId: `tool-${rowId}`,
  72. toolName: "Read",
  73. status: "success",
  74. inputText,
  75. });
  76. }
  77. function units(rows: ConversationRow[]) {
  78. return buildConversationTurnRenderUnits(rows, { nowMs: 1_700_000_100_000 });
  79. }
  80. describe("buildConversationFindIndex", () => {
  81. it("indexes user and assistant body text in render order", () => {
  82. const index = buildConversationFindIndex(
  83. units([
  84. turnHeader(1),
  85. userInput(2, "Find alpha"),
  86. reasoning(3, "alpha in reasoning is ignored"),
  87. toolCall(4, "alpha in tool input is ignored"),
  88. assistantText(5, "assistant Alpha and alpha"),
  89. ]),
  90. "alpha",
  91. );
  92. expect(index.matches.map((match) => [match.rowId, match.rowKind, match.start])).toEqual([
  93. [2, "userInput", 5],
  94. [5, "assistantText", 10],
  95. [5, "assistantText", 20],
  96. ]);
  97. expect(index.matchCount).toBe(3);
  98. });
  99. it("uses non-overlapping case-insensitive matches", () => {
  100. const index = buildConversationFindIndex(
  101. units([turnHeader(1), assistantText(2, "BANANA banana")]),
  102. "ana",
  103. );
  104. expect(index.matches.map((match) => [match.start, match.end])).toEqual([
  105. [1, 4],
  106. [8, 11],
  107. ]);
  108. });
  109. it("feature enabled 时不索引隐藏的 code-comment 协议原文", () => {
  110. const directive =
  111. '::code-comment{title="Hidden title" body="Hidden body" file="src/main.ts"}';
  112. const renderUnits = units([
  113. turnHeader(1),
  114. assistantText(2, `visible text\n${directive}`),
  115. ]);
  116. expect(buildConversationFindIndex(renderUnits, "Hidden body").matchCount).toBe(1);
  117. expect(
  118. buildConversationFindIndex(renderUnits, "Hidden body", {
  119. projectAssistantCodeComments: true,
  120. }).matchCount,
  121. ).toBe(0);
  122. expect(
  123. buildConversationFindIndex(renderUnits, "visible", {
  124. projectAssistantCodeComments: true,
  125. }).matchCount,
  126. ).toBe(1);
  127. const failedUnits = units([
  128. turnHeader(3, "turn-failed"),
  129. assistantText(
  130. 4,
  131. '::code-comment{title="Early title" body="Early hidden body" file="src/early.ts"}',
  132. "turn-failed",
  133. ),
  134. {
  135. ...assistantText(5, directive, "turn-failed"),
  136. state: "failed" as const,
  137. },
  138. ]);
  139. expect(
  140. buildConversationFindIndex(failedUnits, "Hidden title", {
  141. projectAssistantCodeComments: true,
  142. }).matchCount,
  143. ).toBe(1);
  144. expect(
  145. buildConversationFindIndex(failedUnits, "Early hidden body", {
  146. projectAssistantCodeComments: true,
  147. }).matchCount,
  148. ).toBe(1);
  149. });
  150. it("rebases an active match after older rows are prepended", () => {
  151. const initial = buildConversationFindIndex(
  152. units([
  153. turnHeader(10, "turn-2"),
  154. userInput(11, "current alpha", "turn-2"),
  155. assistantText(12, "answer alpha", "turn-2"),
  156. ]),
  157. "alpha",
  158. );
  159. const activeKey = getConversationFindMatchKey(
  160. resolveConversationFindActiveMatch(initial, 1),
  161. );
  162. const prepended = buildConversationFindIndex(
  163. units([
  164. turnHeader(1, "turn-1"),
  165. userInput(2, "older alpha", "turn-1"),
  166. assistantText(3, "older answer alpha", "turn-1"),
  167. turnHeader(10, "turn-2"),
  168. userInput(11, "current alpha", "turn-2"),
  169. assistantText(12, "answer alpha", "turn-2"),
  170. ]),
  171. "alpha",
  172. );
  173. expect(findConversationMatchIndexByKey(prepended, activeKey)).toBe(3);
  174. });
  175. });
  176. describe("resolveSearchResultHighlightMatch", () => {
  177. it("prefers the match whose source text contains the command center snippet", () => {
  178. const index = buildConversationFindIndex(
  179. units([
  180. turnHeader(1),
  181. userInput(2, "alpha first"),
  182. assistantText(3, "target snippet with alpha inside"),
  183. ]),
  184. "alpha",
  185. );
  186. expect(
  187. resolveSearchResultHighlightMatch(index, {
  188. requestId: 1,
  189. taskId: "task",
  190. workspacePath: "/repo",
  191. query: "alpha",
  192. snippet: "...target snippet with alpha...",
  193. })?.rowId,
  194. ).toBe(3);
  195. });
  196. });