v4ConversationUserInputEpilogue.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { fireEvent, render } from "@testing-library/react";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import type { UserInputRow } from "@zcode/shared/zcode-protocol-v4";
  7. import { TooltipProvider } from "@/components/ui/tooltip.js";
  8. import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. import { ConversationRowView } from "@/v4/ConversationRowView.js";
  11. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  12. // docs/dynamic-workflow/transcript-and-notifications.md:子代理 transcript 里的 ask 消息 = 指令正文 +
  13. // 引擎尾注;行上的 epilogueStart 说边界在哪。正文照常画,尾注折进默认收起的披露。
  14. const context: ConversationRowRenderContext = {
  15. codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
  16. sessionId: "session-1",
  17. theme: "system",
  18. workspacePath: "/workspace",
  19. };
  20. const ASK = "Summarize the repo";
  21. const EPILOGUE = "\n\n---\nStandard for this result:\n- Every finding cites what you read.";
  22. const NUDGE = "You ended your turn without submitting a result.";
  23. function userInputRow(overrides: Partial<UserInputRow> = {}): UserInputRow {
  24. return {
  25. rowId: 7,
  26. entityId: "message-user-7",
  27. turnId: "turn-1",
  28. createdAt: 1_700_000_000_000,
  29. createdAtSeq: 1,
  30. kind: "userInput",
  31. origin: "realUser",
  32. text: `${ASK}${EPILOGUE}`,
  33. ...overrides,
  34. };
  35. }
  36. function element(row: UserInputRow, locale: "en-US" | "zh-CN" = "en-US") {
  37. // 用户行的 MessageActions 挂着 Tooltip,需要 provider 在场(与真实宿主一致)。
  38. return createElement(
  39. ZCodeIntlProvider,
  40. { initialLocale: locale },
  41. createElement(TooltipProvider, null, createElement(ConversationRowView, { context, row })),
  42. );
  43. }
  44. afterEach(() => {
  45. document.body.innerHTML = "";
  46. });
  47. describe("ConversationRowView userInput epilogue fold", () => {
  48. it("shows only the ask as the message and folds the epilogue closed by default", () => {
  49. const html = renderToStaticMarkup(element(userInputRow({ epilogueStart: ASK.length })));
  50. expect(html).toContain('data-v4-user-input-epilogue="true"');
  51. expect(html).toContain(ASK);
  52. expect(html).not.toContain("Standard for this result");
  53. expect(html).toContain('aria-expanded="false"');
  54. expect(html).toContain("Workflow engine instructions");
  55. });
  56. it("opens the fold on click and hides the leading rule of the appended text", () => {
  57. const { container, getByRole } = render(element(userInputRow({ epilogueStart: ASK.length })));
  58. fireEvent.click(getByRole("button", { name: "Workflow engine instructions" }));
  59. const body = container.querySelector('[data-v4-user-input-epilogue-body="true"]');
  60. expect(body?.textContent).toContain("Standard for this result:");
  61. expect(body?.textContent?.startsWith("---")).toBe(false);
  62. expect(
  63. getByRole("button", { name: "Workflow engine instructions" }).getAttribute("aria-expanded"),
  64. ).toBe("true");
  65. });
  66. it("renders a bubble holding only the fold when the whole message is engine text (nudge)", () => {
  67. const html = renderToStaticMarkup(element(userInputRow({ text: NUDGE, epilogueStart: 0 })));
  68. expect(html).toContain('data-v4-user-input-bubble="true"');
  69. expect(html).toContain('data-v4-user-input-epilogue="true"');
  70. expect(html).not.toContain(NUDGE);
  71. });
  72. it("treats an out-of-range boundary as no epilogue and leaves rows without one untouched", () => {
  73. const outOfRange = renderToStaticMarkup(
  74. element(userInputRow({ epilogueStart: ASK.length + EPILOGUE.length + 5 })),
  75. );
  76. expect(outOfRange).not.toContain('data-v4-user-input-epilogue="true"');
  77. expect(outOfRange).toContain("Standard for this result:");
  78. const plain = renderToStaticMarkup(element(userInputRow()));
  79. expect(plain).not.toContain('data-v4-user-input-epilogue="true"');
  80. expect(plain).toContain("Standard for this result:");
  81. });
  82. it("localizes the fold label", () => {
  83. const html = renderToStaticMarkup(
  84. element(userInputRow({ epilogueStart: ASK.length }), "zh-CN"),
  85. );
  86. expect(html).toContain("工作流引擎附加说明");
  87. });
  88. });