v4ConversationLatestEditableUserRow.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. import type { ConversationRow, UserInputRow } from "@zcode/shared/zcode-protocol-v4";
  5. import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
  6. import { ConversationTurnGroup } from "@/v4/ConversationTurnGroup.js";
  7. import { buildConversationTurnRenderUnits } from "@/v4/conversationTurnRenderUnits.js";
  8. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. const { rowViewProps } = vi.hoisted(() => ({
  11. rowViewProps: [] as Array<{
  12. row: ConversationRow;
  13. onRetry?: (rowId: number) => void;
  14. onEdit?: (
  15. rowId: number,
  16. newText: string,
  17. attachments?: readonly unknown[],
  18. ) => Promise<boolean | void> | boolean | void;
  19. }>,
  20. }));
  21. vi.mock("@/v4/ConversationRowView.js", async () => {
  22. const React = await import("react");
  23. return {
  24. ConversationAssistantTextActions: () =>
  25. React.createElement("div", {
  26. "data-testid": "mock-assistant-actions",
  27. }),
  28. readAssistantFeedback: () => null,
  29. ConversationRowView: (props: (typeof rowViewProps)[number]) => {
  30. rowViewProps.push(props);
  31. return React.createElement("div", {
  32. "data-testid": `row-${props.row.rowId}`,
  33. });
  34. },
  35. };
  36. });
  37. const context: ConversationRowRenderContext = {
  38. codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
  39. sessionId: "session-1",
  40. theme: "system",
  41. workspaceIdentity: "workspace-identity",
  42. workspacePath: "/workspace",
  43. };
  44. function userInputRow(
  45. rowId: number,
  46. turnId: string,
  47. origin: UserInputRow["origin"] = "realUser",
  48. canEdit = false,
  49. ): UserInputRow {
  50. return {
  51. rowId,
  52. turnId,
  53. createdAt: 1_700_000_000_000 + rowId,
  54. createdAtSeq: rowId,
  55. kind: "userInput",
  56. origin,
  57. text: `question ${rowId}`,
  58. actions: { canEdit },
  59. };
  60. }
  61. function turnHeaderRow(rowId: number, turnId: string): ConversationRow {
  62. return {
  63. rowId,
  64. turnId,
  65. createdAt: 1_700_000_000_000 + rowId,
  66. createdAtSeq: rowId,
  67. kind: "turnHeader",
  68. origin: "userInput",
  69. state: "completedSuccess",
  70. startedAt: 1_700_000_000_000 + rowId,
  71. endedAt: 1_700_000_000_100 + rowId,
  72. };
  73. }
  74. function assistantTextRow(rowId: number, turnId: string, canRetry = false): ConversationRow {
  75. return {
  76. rowId,
  77. turnId,
  78. createdAt: 1_700_000_000_000 + rowId,
  79. createdAtSeq: rowId,
  80. kind: "assistantText",
  81. state: "complete",
  82. text: `answer ${rowId}`,
  83. actions: { canRetry },
  84. };
  85. }
  86. beforeEach(() => {
  87. rowViewProps.length = 0;
  88. });
  89. describe("authoritative conversation row actions", () => {
  90. it("passes onEdit only to the row authorized by the CLI projection", () => {
  91. const rows = [
  92. turnHeaderRow(1, "turn-1"),
  93. userInputRow(2, "turn-1"),
  94. assistantTextRow(3, "turn-1"),
  95. turnHeaderRow(4, "turn-2"),
  96. userInputRow(5, "turn-2", "realUser", true),
  97. assistantTextRow(6, "turn-2"),
  98. ];
  99. const units = buildConversationTurnRenderUnits(rows);
  100. const onEdit = vi.fn();
  101. for (const unit of units) {
  102. renderToStaticMarkup(
  103. createElement(
  104. ZCodeIntlProvider,
  105. { initialLocale: "zh-CN" },
  106. createElement(ConversationTurnGroup, {
  107. unit,
  108. context,
  109. onEdit,
  110. }),
  111. ),
  112. );
  113. }
  114. const userRows = rowViewProps.filter(
  115. (props): props is (typeof rowViewProps)[number] & { row: UserInputRow } =>
  116. props.row.kind === "userInput",
  117. );
  118. expect(userRows.find((props) => props.row.rowId === 2)?.onEdit).toBeUndefined();
  119. expect(userRows.find((props) => props.row.rowId === 5)?.onEdit).toBe(onEdit);
  120. });
  121. it("passes onRetry only to the row authorized by the CLI projection", () => {
  122. const rows = [
  123. turnHeaderRow(1, "turn-1"),
  124. userInputRow(2, "turn-1"),
  125. assistantTextRow(3, "turn-1"),
  126. turnHeaderRow(4, "turn-2"),
  127. userInputRow(5, "turn-2"),
  128. assistantTextRow(6, "turn-2", true),
  129. ];
  130. const units = buildConversationTurnRenderUnits(rows);
  131. const onRetry = vi.fn();
  132. for (const unit of units) {
  133. renderToStaticMarkup(
  134. createElement(
  135. ZCodeIntlProvider,
  136. { initialLocale: "zh-CN" },
  137. createElement(ConversationTurnGroup, {
  138. unit,
  139. context,
  140. onRetry,
  141. }),
  142. ),
  143. );
  144. }
  145. const assistantRows = rowViewProps.filter((props) => props.row.kind === "assistantText");
  146. expect(assistantRows.find((props) => props.row.rowId === 3)?.onRetry).toBeUndefined();
  147. expect(assistantRows.find((props) => props.row.rowId === 6)?.onRetry).toBe(onRetry);
  148. });
  149. });