conversationShareConfirmationDock.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ConversationShareConfirmationDock } from "@/v4/ConversationShareConfirmationDock.js";
  6. vi.mock("@/i18n/IntlProvider.js", () => ({
  7. useZCodeIntl: () => ({
  8. intl: {
  9. formatMessage: ({ id }: { id: string }, values?: Record<string, string | number>) =>
  10. values ? `${id}:${JSON.stringify(values)}` : id,
  11. },
  12. }),
  13. }));
  14. afterEach(() => {
  15. cleanup();
  16. vi.unstubAllGlobals();
  17. });
  18. describe("ConversationShareConfirmationDock error details", () => {
  19. it.each([false, true])("浮窗 warning 与抖动尊重减少动态效果 %s", (reducedMotion) => {
  20. vi.stubGlobal(
  21. "matchMedia",
  22. vi.fn(() => ({ matches: reducedMotion })),
  23. );
  24. const onConfirm = vi.fn();
  25. render(
  26. createElement(ConversationShareConfirmationDock, {
  27. selectedCount: 1,
  28. totalCount: 1,
  29. onCancel: vi.fn(),
  30. onEditSelection: vi.fn(),
  31. onConfirm,
  32. }),
  33. );
  34. const section = screen.getByTestId("conversation-share-disclosure");
  35. section.animate = vi.fn();
  36. section.scrollIntoView = vi.fn();
  37. const row = section.querySelector("label")!;
  38. const cancel = vi.fn();
  39. row.animate = vi.fn(() => ({ cancel }) as unknown as Animation);
  40. fireEvent.click(screen.getByTestId("conversation-share-confirm"));
  41. const checkbox = screen.getByTestId("conversation-share-disclosure-checkbox");
  42. expect(checkbox.getAttribute("aria-invalid")).toBe("true");
  43. expect(checkbox.closest("label")?.className).toContain("text-warning");
  44. expect(checkbox.className).not.toContain("border-destructive");
  45. expect(document.activeElement).toBe(checkbox);
  46. expect(section.animate).not.toHaveBeenCalled();
  47. if (reducedMotion) {
  48. expect(row.animate).not.toHaveBeenCalled();
  49. } else {
  50. expect(row.animate).toHaveBeenCalledWith(
  51. expect.arrayContaining([{ transform: "translateX(-4px)" }]),
  52. { duration: 280, easing: "ease-out" },
  53. );
  54. fireEvent.click(screen.getByTestId("conversation-share-confirm"));
  55. expect(cancel).toHaveBeenCalledTimes(1);
  56. expect(row.animate).toHaveBeenCalledTimes(2);
  57. }
  58. expect(section.scrollIntoView).not.toHaveBeenCalled();
  59. expect(onConfirm).not.toHaveBeenCalled();
  60. fireEvent.click(checkbox);
  61. expect(checkbox.getAttribute("aria-invalid")).toBe("false");
  62. if (!reducedMotion) expect(cancel).toHaveBeenCalledTimes(2);
  63. expect(checkbox.closest("label")?.className).not.toContain("text-warning");
  64. expect(onConfirm).not.toHaveBeenCalled();
  65. });
  66. it("浮窗发布中只展示进度,失败后恢复重试操作", () => {
  67. const props = {
  68. selectedCount: 2,
  69. totalCount: 3,
  70. title: "当前任务",
  71. onCancel: vi.fn(),
  72. onEditSelection: vi.fn(),
  73. onConfirm: vi.fn(),
  74. };
  75. const { rerender } = render(
  76. createElement(ConversationShareConfirmationDock, { ...props, pending: true }),
  77. );
  78. expect(screen.queryByTestId("conversation-share-confirm")).toBeNull();
  79. expect(screen.getByTestId("conversation-share-publish-progress").textContent).toContain(
  80. "当前任务",
  81. );
  82. rerender(
  83. createElement(ConversationShareConfirmationDock, {
  84. ...props,
  85. error: { issues: [], issueCount: 0 },
  86. }),
  87. );
  88. expect(screen.getByTestId("conversation-share-confirm").textContent).toContain(
  89. "conversationShare.publish.retry",
  90. );
  91. });
  92. it("将检查状态放进分享范围按钮并在完成后恢复摘要", () => {
  93. const props = {
  94. selectedCount: 2,
  95. totalCount: 4,
  96. onEditSelection: vi.fn(),
  97. onCancel: vi.fn(),
  98. onConfirm: vi.fn(),
  99. };
  100. const { rerender } = render(
  101. createElement(ConversationShareConfirmationDock, { ...props, preflightChecking: true }),
  102. );
  103. const button = screen.getByTestId("conversation-share-adjust");
  104. expect(button.getAttribute("aria-busy")).toBe("true");
  105. expect(button.textContent).toContain("conversationShare.partial.preflightChecking");
  106. rerender(
  107. createElement(ConversationShareConfirmationDock, { ...props, preflightChecking: false }),
  108. );
  109. expect(button.getAttribute("aria-busy")).toBe("false");
  110. expect(button.textContent).toContain("conversationShare.range.selected");
  111. });
  112. it("keeps the failed publish phase and its last progress instead of showing 100%", () => {
  113. render(
  114. createElement(ConversationShareConfirmationDock, {
  115. selectedCount: 2,
  116. totalCount: 3,
  117. progressLabel: "Uploading artifacts",
  118. progressPhase: "uploading",
  119. error: {
  120. issueCount: 1,
  121. issues: [{ code: "upload_incomplete", scope: "artifact", phase: "uploading" }],
  122. },
  123. onCancel: vi.fn(),
  124. onEditSelection: vi.fn(),
  125. onConfirm: vi.fn(),
  126. }),
  127. );
  128. const progress = screen.getByRole("progressbar");
  129. expect(progress.getAttribute("aria-valuenow")).toBe("58");
  130. expect(
  131. progress
  132. .querySelector('[data-testid="conversation-share-progress-fill"]')
  133. ?.getAttribute("style"),
  134. ).toContain("width: 58%");
  135. expect(
  136. screen
  137. .getByTestId("conversation-share-publish-phase-uploading")
  138. .getAttribute("data-phase-state"),
  139. ).toBe("failed");
  140. expect(
  141. screen
  142. .getByTestId("conversation-share-publish-phase-collecting")
  143. .getAttribute("data-phase-state"),
  144. ).toBe("complete");
  145. });
  146. it("keeps retry available after an error even when the disclosure checkbox is not rendered", () => {
  147. const onConfirm = vi.fn();
  148. render(
  149. createElement(ConversationShareConfirmationDock, {
  150. selectedCount: 1,
  151. totalCount: 1,
  152. title: "Share",
  153. disclosureAccepted: false,
  154. error: {
  155. issueCount: 1,
  156. messageId: "conversationShare.error.network",
  157. issues: [{ code: "unknown", scope: "transport", phase: "collecting" }],
  158. },
  159. onCancel: vi.fn(),
  160. onEditSelection: vi.fn(),
  161. onConfirm,
  162. }),
  163. );
  164. const retry = screen.getByTestId("conversation-share-confirm");
  165. expect(retry).toHaveProperty("disabled", false);
  166. fireEvent.click(retry);
  167. expect(onConfirm).toHaveBeenCalledTimes(1);
  168. });
  169. it("keeps the publish progress in the same dock and removes edit actions", () => {
  170. render(
  171. createElement(ConversationShareConfirmationDock, {
  172. selectedCount: 2,
  173. totalCount: 3,
  174. progressLabel: "Uploading artifacts",
  175. progressPhase: "uploading",
  176. pending: true,
  177. onCancel: vi.fn(),
  178. onEditSelection: vi.fn(),
  179. onConfirm: vi.fn(),
  180. }),
  181. );
  182. expect(screen.getByTestId("conversation-share-publish-progress").textContent).toContain(
  183. "Uploading artifacts",
  184. );
  185. expect(screen.queryByTestId("conversation-share-publish-lock-note")).toBeNull();
  186. expect(screen.queryByTestId("conversation-share-disclosure-checkbox")).toBeNull();
  187. expect(screen.queryByTestId("conversation-share-back")).toBeNull();
  188. expect(screen.queryByText("conversationShare.partial.cancel")).toBeNull();
  189. expect(screen.queryByTestId("conversation-share-confirm")).toBeNull();
  190. });
  191. it("renders the uploading phase message while pending", () => {
  192. render(
  193. createElement(ConversationShareConfirmationDock, {
  194. selectedCount: 3,
  195. totalCount: 3,
  196. progressLabel: "Uploading artifacts",
  197. progressPhase: "uploading",
  198. completedArtifacts: 2,
  199. totalArtifacts: 3,
  200. pending: true,
  201. onCancel: vi.fn(),
  202. onEditSelection: vi.fn(),
  203. onConfirm: vi.fn(),
  204. }),
  205. );
  206. expect(screen.getByTestId("conversation-share-publish-phase-uploading").textContent).toContain(
  207. "conversationShare.phase.uploadingActive",
  208. );
  209. });
  210. it("keeps disclosure confirmation inside the dock before publishing", () => {
  211. const onDisclosureAcceptedChange = vi.fn();
  212. const onConfirm = vi.fn();
  213. const { rerender } = render(
  214. createElement(ConversationShareConfirmationDock, {
  215. selectedCount: 2,
  216. totalCount: 3,
  217. onCancel: vi.fn(),
  218. onEditSelection: vi.fn(),
  219. onConfirm,
  220. disclosureAccepted: false,
  221. onDisclosureAcceptedChange,
  222. }),
  223. );
  224. const disclosure = screen.getByTestId("conversation-share-disclosure-checkbox");
  225. const confirm = screen.getByTestId("conversation-share-confirm");
  226. expect(screen.getByTestId("conversation-share-disclosure").textContent).not.toContain(
  227. "conversationShare.publicWarning",
  228. );
  229. expect(disclosure.getAttribute("aria-checked")).toBe("false");
  230. expect(confirm).toHaveProperty("disabled", false);
  231. expect(screen.queryByTestId("conversation-share-disclosure-confirm")).toBeNull();
  232. fireEvent.click(disclosure);
  233. expect(onDisclosureAcceptedChange).toHaveBeenCalledWith(true);
  234. rerender(
  235. createElement(ConversationShareConfirmationDock, {
  236. selectedCount: 2,
  237. totalCount: 3,
  238. onCancel: vi.fn(),
  239. onEditSelection: vi.fn(),
  240. onConfirm,
  241. disclosureAccepted: true,
  242. onDisclosureAcceptedChange,
  243. }),
  244. );
  245. expect(screen.getByTestId("conversation-share-confirm")).toHaveProperty("disabled", false);
  246. fireEvent.click(screen.getByTestId("conversation-share-confirm"));
  247. expect(onConfirm).toHaveBeenCalledTimes(1);
  248. });
  249. it("默认权限为可导入并继续", () => {
  250. render(
  251. createElement(ConversationShareConfirmationDock, {
  252. selectedCount: 1,
  253. totalCount: 1,
  254. onCancel: vi.fn(),
  255. onEditSelection: vi.fn(),
  256. onConfirm: vi.fn(),
  257. }),
  258. );
  259. expect(screen.getByRole("combobox").textContent).toContain(
  260. "conversationShare.permission.linkEditor",
  261. );
  262. });
  263. it("opens the sensitive-content review scope above the disclosure and toggles it closed", () => {
  264. render(
  265. createElement(ConversationShareConfirmationDock, {
  266. selectedCount: 1,
  267. totalCount: 1,
  268. onCancel: vi.fn(),
  269. onEditSelection: vi.fn(),
  270. onConfirm: vi.fn(),
  271. }),
  272. );
  273. const trigger = screen.getByTestId("conversation-share-disclosure-scope-trigger");
  274. expect(trigger.getAttribute("aria-expanded")).toBe("false");
  275. expect(screen.queryByTestId("conversation-share-disclosure-scope-content")).toBeNull();
  276. fireEvent.click(trigger);
  277. expect(trigger.getAttribute("aria-expanded")).toBe("true");
  278. const content = screen.getByTestId("conversation-share-disclosure-scope-content");
  279. expect(content.textContent).toContain("conversationShare.disclosure.scope.title");
  280. expect(content.textContent).toContain("conversationShare.disclosure.scope.conversation");
  281. expect(content.textContent).toContain("conversationShare.disclosure.scope.sensitive");
  282. fireEvent.click(trigger);
  283. expect(trigger.getAttribute("aria-expanded")).toBe("false");
  284. expect(screen.queryByTestId("conversation-share-disclosure-scope-content")).toBeNull();
  285. });
  286. it("renders actionable artifact details and deselects the affected turn", () => {
  287. const onDeselectTurn = vi.fn();
  288. const onCopyRequestId = vi.fn();
  289. render(
  290. createElement(ConversationShareConfirmationDock, {
  291. selectedCount: 1,
  292. totalCount: 2,
  293. title: "Share",
  294. onCancel: vi.fn(),
  295. onEditSelection: vi.fn(),
  296. onConfirm: vi.fn(),
  297. error: {
  298. issueCount: 1,
  299. requestId: "server-request-123",
  300. issues: [
  301. {
  302. code: "artifact_type_not_allowed",
  303. scope: "artifact",
  304. // turnOrdinal 只用于文案;「取消该轮」按 productTurnId 定位。
  305. turnOrdinal: 2,
  306. productTurnId: "product-turn-2",
  307. artifactDisplayName: "晨报.pdf",
  308. artifactType: "pdf",
  309. extension: "pdf",
  310. mimeType: "application/pdf",
  311. allowedFormats: ["PPTX (.pptx)", "DOCX (.docx)"],
  312. },
  313. ],
  314. },
  315. onDeselectTurn,
  316. onCopyRequestId,
  317. }),
  318. );
  319. expect(screen.getByTestId("conversation-share-error-details").textContent).toContain(
  320. "晨报.pdf",
  321. );
  322. expect(screen.getByTestId("conversation-share-error-details").textContent).toContain("pdf");
  323. const errorDetails = screen.getByTestId("conversation-share-error-details");
  324. expect(errorDetails.textContent).not.toContain("server-request-123");
  325. expect(screen.queryByTestId("conversation-share-request-id-content")).toBeNull();
  326. const requestDetailsTrigger = screen.getByTestId("conversation-share-request-id-trigger");
  327. expect(requestDetailsTrigger.getAttribute("aria-label")).toBe(
  328. "conversationShare.issue.details",
  329. );
  330. fireEvent.click(requestDetailsTrigger);
  331. expect(screen.getByTestId("conversation-share-request-id-content").textContent).toContain(
  332. "server-request-123",
  333. );
  334. fireEvent.click(screen.getByRole("button", { name: "conversationShare.issue.copyRequestId" }));
  335. expect(onCopyRequestId).toHaveBeenCalledTimes(1);
  336. fireEvent.click(screen.getByRole("button", { name: "conversationShare.issue.deselectTurn" }));
  337. expect(onDeselectTurn).toHaveBeenCalledWith("product-turn-2");
  338. });
  339. it("shows a stable missing request id state without a copy action", () => {
  340. render(
  341. createElement(ConversationShareConfirmationDock, {
  342. selectedCount: 1,
  343. totalCount: 1,
  344. onCancel: vi.fn(),
  345. onEditSelection: vi.fn(),
  346. onConfirm: vi.fn(),
  347. error: {
  348. issueCount: 1,
  349. issues: [{ code: "unknown", scope: "transport" }],
  350. },
  351. }),
  352. );
  353. expect(screen.queryByTestId("conversation-share-request-id-content")).toBeNull();
  354. fireEvent.click(screen.getByTestId("conversation-share-request-id-trigger"));
  355. expect(screen.getByTestId("conversation-share-request-id-content").textContent).toContain(
  356. "conversationShare.issue.requestIdMissing",
  357. );
  358. expect(
  359. screen.queryByRole("button", { name: "conversationShare.issue.copyRequestId" }),
  360. ).toBeNull();
  361. });
  362. it("keeps the request id hidden until the inline error details button is opened", () => {
  363. render(
  364. createElement(ConversationShareConfirmationDock, {
  365. selectedCount: 1,
  366. totalCount: 1,
  367. onCancel: vi.fn(),
  368. onEditSelection: vi.fn(),
  369. onConfirm: vi.fn(),
  370. error: {
  371. messageId: "conversationShare.error.invalidConversation",
  372. issueCount: 1,
  373. requestId: "server-request-456",
  374. issues: [{ code: "invalid_conversation", scope: "conversation" }],
  375. },
  376. onCopyRequestId: vi.fn(),
  377. }),
  378. );
  379. expect(screen.getByTestId("conversation-share-error-message").textContent).toContain(
  380. "conversationShare.error.invalidConversation",
  381. );
  382. expect(screen.getByTestId("conversation-share-error-message").textContent).not.toContain(
  383. "server-request-456",
  384. );
  385. expect(screen.queryByText("server-request-456")).toBeNull();
  386. fireEvent.click(screen.getByTestId("conversation-share-request-id-trigger"));
  387. expect(screen.getByTestId("conversation-share-request-id-content").textContent).toContain(
  388. "server-request-456",
  389. );
  390. });
  391. it("shows the actionable publish error instead of a generic transport issue", () => {
  392. render(
  393. createElement(ConversationShareConfirmationDock, {
  394. selectedCount: 1,
  395. totalCount: 1,
  396. onCancel: vi.fn(),
  397. onEditSelection: vi.fn(),
  398. onConfirm: vi.fn(),
  399. error: {
  400. messageId: "conversationShare.error.authenticationRequired",
  401. issueCount: 1,
  402. issues: [{ code: "unknown", scope: "transport", phase: "collecting" }],
  403. },
  404. }),
  405. );
  406. const details = screen.getByTestId("conversation-share-error-details");
  407. expect(screen.getByTestId("conversation-share-confirmation-header").textContent).toContain(
  408. "conversationShare.publish.failedTitle",
  409. );
  410. expect(screen.getByTestId("conversation-share-publish-progress")).toBeTruthy();
  411. expect(screen.queryByTestId("conversation-share-permission-picker")).toBeNull();
  412. expect(details.textContent).toContain("conversationShare.error.authenticationRequired");
  413. expect(details.textContent).not.toContain("conversationShare.issue.unknown");
  414. });
  415. });
  416. it("浮窗分享范围整行触发调整,不显示独立调整文字", () => {
  417. const onEditSelection = vi.fn();
  418. render(
  419. createElement(ConversationShareConfirmationDock, {
  420. selectedCount: 2,
  421. totalCount: 4,
  422. onEditSelection,
  423. onCancel: vi.fn(),
  424. onConfirm: vi.fn(),
  425. }),
  426. );
  427. fireEvent.click(screen.getByTestId("conversation-share-range-summary"));
  428. expect(onEditSelection).toHaveBeenCalledTimes(1);
  429. expect(screen.queryByText("conversationShare.range.adjust")).toBeNull();
  430. });
  431. it("shows validation coordinates only inside error details", () => {
  432. render(
  433. createElement(ConversationShareConfirmationDock, {
  434. selectedCount: 1,
  435. totalCount: 1,
  436. onCancel: vi.fn(),
  437. onEditSelection: vi.fn(),
  438. onConfirm: vi.fn(),
  439. error: {
  440. issueCount: 1,
  441. issues: [],
  442. code: 3205,
  443. messageId: "conversationShare.error.invalidConversation",
  444. rejection: { rule: "field_type", path: "rows[0].state", field: "state", row_index: 0 },
  445. },
  446. }),
  447. );
  448. expect(screen.queryByText("rows[0].state", { exact: false })).toBeNull();
  449. fireEvent.click(screen.getByTestId("conversation-share-request-id-trigger"));
  450. const details = screen.getByTestId("conversation-share-request-id-content");
  451. expect(details.textContent).toContain("rows[0].state");
  452. expect(details.textContent).toContain("field_type");
  453. expect(details.textContent).toContain("rowLabel: 1");
  454. expect(details.textContent).toContain("3205");
  455. });