v4ConversationQueuePanel.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import type { QueueState } from "@zcode/shared/zcode-protocol-v4";
  5. import {
  6. ConversationQueuePanel,
  7. resolveV4QueueReorderAnchor,
  8. } from "@/v4/ConversationQueuePanel.js";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. vi.mock("@/ControlHintTooltip.js", async () => {
  11. const React = await import("react");
  12. return {
  13. ControlHintTooltip: ({ children }: { children: ReactNode }) =>
  14. React.createElement(React.Fragment, null, children),
  15. };
  16. });
  17. function queueState(): QueueState {
  18. return {
  19. autoDrain: true,
  20. items: [
  21. {
  22. queueItemId: "q-1",
  23. kind: "sendText",
  24. text: "第一条问题",
  25. sourceCommandId: "cmd-1",
  26. clientId: "cli",
  27. attachments: [],
  28. delivery: { requested: "queue", admitted: "queue" },
  29. order: { admissionSeq: 1, queuePosition: 0 },
  30. steer: { state: "notRequested" },
  31. dispatch: { state: "queued" },
  32. admittedAt: 1,
  33. },
  34. {
  35. queueItemId: "q-2",
  36. kind: "sendText",
  37. text: "第二条问题",
  38. sourceCommandId: "cmd-2",
  39. clientId: "cli",
  40. attachments: [],
  41. delivery: { requested: "queue", admitted: "queue" },
  42. order: { admissionSeq: 2, queuePosition: 1 },
  43. steer: { state: "notRequested" },
  44. dispatch: { state: "queued" },
  45. admittedAt: 2,
  46. },
  47. ],
  48. };
  49. }
  50. function renderPanel(): string {
  51. return renderToStaticMarkup(
  52. createElement(
  53. ZCodeIntlProvider,
  54. { initialLocale: "zh-CN" },
  55. createElement(ConversationQueuePanel, {
  56. queue: queueState(),
  57. onDeleteItem: vi.fn(),
  58. onEditItem: vi.fn(),
  59. onSendNow: vi.fn(),
  60. onMoveItem: vi.fn(),
  61. }),
  62. ),
  63. );
  64. }
  65. describe("ConversationQueuePanel", () => {
  66. it("shows queued attachment names alongside the sender", () => {
  67. const queue = queueState();
  68. queue.items[0]!.attachments = [
  69. { ref: "attachment:test", fileName: "requirements.pdf", mime: "application/pdf", bytes: 100 },
  70. ];
  71. const html = renderToStaticMarkup(
  72. createElement(
  73. ZCodeIntlProvider,
  74. { initialLocale: "zh-CN" },
  75. createElement(ConversationQueuePanel, { queue }),
  76. ),
  77. );
  78. expect(html).toContain("requirements.pdf");
  79. });
  80. it("renders v4 queue with icon actions and drag handles", () => {
  81. const html = renderPanel();
  82. expect(html).toContain('data-testid="v4-queue"');
  83. expect(html).toContain('data-queue-item-id="q-1"');
  84. expect(html).toContain('data-queue-item-id="q-2"');
  85. expect(html).toContain('data-v4-queue-drag-handle="true"');
  86. expect(html).toContain("backdrop-blur-md");
  87. expect(html).toContain(">立即<");
  88. expect(html).not.toContain(">立即发送<");
  89. expect(html).not.toContain(">上移<");
  90. });
  91. it("renders stop-specific paused banner and resume action", () => {
  92. const queue = { ...queueState(), autoDrain: false, pauseReason: "stopped" as const };
  93. const html = renderToStaticMarkup(
  94. createElement(
  95. ZCodeIntlProvider,
  96. { initialLocale: "zh-CN" },
  97. createElement(ConversationQueuePanel, {
  98. queue,
  99. onResume: vi.fn(),
  100. }),
  101. ),
  102. );
  103. expect(html).toContain('data-testid="v4-queue-paused-banner"');
  104. expect(html).toContain("由于你中断了当前响应,队列已暂停");
  105. expect(html).toContain('data-testid="v4-queue-resume"');
  106. expect(html).toContain(">继续<");
  107. });
  108. it("uses authoritative dispatch to lock reserved/promoting item actions", () => {
  109. const queue = queueState();
  110. queue.items[0] = {
  111. ...queue.items[0]!,
  112. dispatch: { state: "reserved", reservationId: "send-now-1" },
  113. };
  114. const html = renderToStaticMarkup(
  115. createElement(
  116. ZCodeIntlProvider,
  117. { initialLocale: "zh-CN" },
  118. createElement(ConversationQueuePanel, {
  119. queue,
  120. onDeleteItem: vi.fn(),
  121. onEditItem: vi.fn(),
  122. onSendNow: vi.fn(),
  123. onMoveItem: vi.fn(),
  124. }),
  125. ),
  126. );
  127. expect(html).toContain('data-dispatch-state="reserved"');
  128. expect(html).toContain('data-dispatch-state="queued"');
  129. expect(html).toContain("disabled");
  130. });
  131. it("queue edit 只触发撤回动作,并在 ACK 期间锁定目标行", () => {
  132. const html = renderToStaticMarkup(
  133. createElement(
  134. ZCodeIntlProvider,
  135. { initialLocale: "zh-CN" },
  136. createElement(ConversationQueuePanel, {
  137. queue: queueState(),
  138. onDeleteItem: vi.fn(),
  139. onEditItem: vi.fn(),
  140. onSendNow: vi.fn(),
  141. onMoveItem: vi.fn(),
  142. pendingEditQueueItemId: "q-1",
  143. }),
  144. ),
  145. );
  146. expect(html).toContain('data-edit-pending="true"');
  147. expect(html).toContain('data-edit-pending="false"');
  148. expect(html).not.toContain("v4-queue-item-edit-input");
  149. });
  150. it("renders compact as a maintenance item with the shared immediate label and no edit action", () => {
  151. const queue = queueState();
  152. queue.items = [
  153. {
  154. ...queue.items[0]!,
  155. queueItemId: "q-compact",
  156. kind: "compact",
  157. text: "/compact",
  158. attachments: [],
  159. },
  160. ];
  161. const html = renderToStaticMarkup(
  162. createElement(
  163. ZCodeIntlProvider,
  164. { initialLocale: "zh-CN" },
  165. createElement(ConversationQueuePanel, {
  166. queue,
  167. onDeleteItem: vi.fn(),
  168. onEditItem: vi.fn(),
  169. onSendNow: vi.fn(),
  170. onMoveItem: vi.fn(),
  171. }),
  172. ),
  173. );
  174. expect(html).toContain("/compact");
  175. expect(html).toContain(">立即<");
  176. expect(html).not.toContain("立即执行");
  177. expect(html).not.toContain("lucide-minimize-2");
  178. expect(html).not.toContain('aria-label="编辑"');
  179. });
  180. it("converts drag targets into CLI beforeQueueItemId anchors", () => {
  181. const items = [
  182. ...queueState().items,
  183. {
  184. queueItemId: "q-3",
  185. kind: "sendText" as const,
  186. text: "第三条问题",
  187. sourceCommandId: "cmd-3",
  188. clientId: "cli",
  189. attachments: [],
  190. delivery: { requested: "queue" as const, admitted: "queue" as const },
  191. order: { admissionSeq: 3, queuePosition: 2 },
  192. steer: { state: "notRequested" as const },
  193. dispatch: { state: "queued" as const },
  194. admittedAt: 3,
  195. },
  196. {
  197. queueItemId: "q-4",
  198. kind: "sendText" as const,
  199. text: "第四条问题",
  200. sourceCommandId: "cmd-4",
  201. clientId: "cli",
  202. attachments: [],
  203. delivery: { requested: "queue" as const, admitted: "queue" as const },
  204. order: { admissionSeq: 4, queuePosition: 3 },
  205. steer: { state: "notRequested" as const },
  206. dispatch: { state: "queued" as const },
  207. admittedAt: 4,
  208. },
  209. ];
  210. expect(resolveV4QueueReorderAnchor(items, "q-3", "q-1")).toEqual({
  211. beforeQueueItemId: "q-1",
  212. queueItemId: "q-3",
  213. });
  214. expect(resolveV4QueueReorderAnchor(items, "q-1", "q-3")).toEqual({
  215. beforeQueueItemId: "q-4",
  216. queueItemId: "q-1",
  217. });
  218. expect(resolveV4QueueReorderAnchor(items, "q-1", "q-4")).toEqual({
  219. beforeQueueItemId: null,
  220. queueItemId: "q-1",
  221. });
  222. expect(resolveV4QueueReorderAnchor(items, "q-2", "q-2")).toBeNull();
  223. expect(resolveV4QueueReorderAnchor(items, "missing", "q-2")).toBeNull();
  224. expect(resolveV4QueueReorderAnchor(items, "q-2", "missing")).toBeNull();
  225. });
  226. });
  227. it("renders standard references without leaking the wire encoding into queue text", () => {
  228. const queue = queueState();
  229. queue.items[0]!.text =
  230. 'Review\n\n# userselect:\n```userselect\n[{"text":"quoted original"}]\n```';
  231. const html = renderToStaticMarkup(
  232. createElement(
  233. ZCodeIntlProvider,
  234. { initialLocale: "en-US" },
  235. createElement(ConversationQueuePanel, { queue }),
  236. ),
  237. );
  238. expect(html).toContain("data-conversation-selection-reference-count");
  239. expect(html).not.toContain("# userselect:");
  240. });