v4QueueEditExtraction.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { ConversationSnapshot } from "@zcode/shared/zcode-protocol-v4";
  3. import {
  4. applyExternalTextInsertRequestToComposer,
  5. applyComposerRestoreRequestToComposer,
  6. restorePersistedComposerDraftIntoInput,
  7. type ComposerRestoreRequest,
  8. } from "@/v4/ConversationComposer.js";
  9. import {
  10. resolveQueuedComposerRestore,
  11. shouldRestoreQueuedComposerFromAck,
  12. } from "@/v4/SessionPane.js";
  13. function makeSnapshot(): ConversationSnapshot {
  14. return {
  15. protocolVersion: 1,
  16. sessionId: "session-1",
  17. logEpoch: "epoch-1",
  18. seq: 1,
  19. revision: 7,
  20. control: {
  21. phase: "running",
  22. sessionEnded: false,
  23. canStop: true,
  24. stopState: "stoppable",
  25. stopTargetKind: "primaryTurn",
  26. activeWorks: [],
  27. lastError: null,
  28. apiRetry: null,
  29. },
  30. availability: {
  31. fork: { allowed: true },
  32. compact: { allowed: true },
  33. switchModelConfig: { allowed: true },
  34. setFollowupMode: { allowed: true },
  35. queueEdit: { allowed: true },
  36. sendQueuedNow: { allowed: true },
  37. pauseGoal: { allowed: false, reasonCode: "noGoalToPause" },
  38. resumeGoal: { allowed: false, reasonCode: "noGoalToResume" },
  39. },
  40. inputRouting: { mode: "enqueue" },
  41. meta: { title: "Session", titleSource: "generated" },
  42. config: { provider: "", model: "", thought: "", followupMode: "queue", mode: "build" },
  43. usage: {
  44. contextWindow: null,
  45. cumulative: {
  46. inputTokens: 0,
  47. outputTokens: 0,
  48. cacheReadTokens: 0,
  49. cacheWriteTokens: 0,
  50. },
  51. },
  52. queue: {
  53. autoDrain: true,
  54. items: [
  55. {
  56. queueItemId: "q-1",
  57. kind: "sendText",
  58. text: "排队原文",
  59. sourceCommandId: "cmd-q1",
  60. clientId: "cli",
  61. attachments: [
  62. { ref: "ref-b", fileName: "b.png", mime: "image/png", bytes: 2 },
  63. { ref: "ref-a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
  64. ],
  65. delivery: { requested: "queue", admitted: "queue" },
  66. order: { admissionSeq: 1, queuePosition: 0 },
  67. steer: { state: "notRequested" },
  68. dispatch: { state: "queued" },
  69. admittedAt: 1,
  70. },
  71. ],
  72. },
  73. pendingInteractions: [],
  74. pendingCommands: [],
  75. backgroundWorks: [],
  76. goal: null,
  77. plan: null,
  78. rows: { window: [], totalCount: 0, firstRowId: null },
  79. };
  80. }
  81. describe("v4 queue edit extraction", () => {
  82. it("Workspace 插件页重挂载时从共享草稿恢复结构化 mention", () => {
  83. const mention = {
  84. id: "plugin:document-skills@official",
  85. category: "plugins" as const,
  86. label: "文档技能",
  87. value: "document-skills@official",
  88. markdown: "[@文档技能](plugin://document-skills@official)",
  89. data: { pluginId: "document-skills@official" },
  90. };
  91. const inputApi = {
  92. getMarkdown: vi.fn(() => "unused"),
  93. setEditorStateJson: vi.fn(),
  94. setMention: vi.fn(),
  95. setText: vi.fn(),
  96. };
  97. const text = `${mention.markdown} 整理文档`;
  98. expect(
  99. restorePersistedComposerDraftIntoInput({
  100. draft: { text, mention, updatedAt: 1 },
  101. inputApi,
  102. }),
  103. ).toBe(text);
  104. expect(inputApi.setMention).toHaveBeenCalledWith(mention, " 整理文档");
  105. expect(inputApi.setText).not.toHaveBeenCalled();
  106. });
  107. it("商店试用把 canonical 前缀恢复为结构化 Plugin mention", () => {
  108. const mention = {
  109. id: "plugin:demo@mkt",
  110. category: "plugins" as const,
  111. label: "Demo",
  112. value: "demo@mkt",
  113. markdown: "[@Demo](plugin://demo@mkt)",
  114. data: {
  115. pluginId: "demo@mkt",
  116. icon: "https://cdn.example.com/demo.png",
  117. },
  118. };
  119. const inputApi = {
  120. setMention: vi.fn(),
  121. setText: vi.fn(),
  122. };
  123. const updateText = vi.fn();
  124. const requestFocus = vi.fn();
  125. const scheduleDraftPersist = vi.fn();
  126. const text = `${mention.markdown} Diagnose this workspace`;
  127. expect(
  128. applyExternalTextInsertRequestToComposer({
  129. appliedRequestId: null,
  130. inputApi,
  131. request: { requestId: 7, text, mention },
  132. requestFocus,
  133. scheduleDraftPersist,
  134. updateText,
  135. }),
  136. ).toBe(7);
  137. expect(inputApi.setMention).toHaveBeenCalledWith(mention, " Diagnose this workspace");
  138. expect(inputApi.setText).not.toHaveBeenCalled();
  139. expect(updateText).toHaveBeenCalledWith(text);
  140. expect(scheduleDraftPersist).toHaveBeenCalledTimes(1);
  141. expect(requestFocus).toHaveBeenCalledTimes(1);
  142. });
  143. it("插件安装完成后把唯一 chip 前置到编辑器当前正文而不回放旧 prompt", () => {
  144. const mention = {
  145. id: "plugin:document-skills@zcode-plugins-official",
  146. category: "plugins" as const,
  147. label: "文档技能",
  148. value: "document-skills@zcode-plugins-official",
  149. markdown: "[@文档技能](plugin://document-skills@zcode-plugins-official)",
  150. data: { pluginId: "document-skills@zcode-plugins-official" },
  151. };
  152. const inputApi = {
  153. getMarkdown: vi.fn(() => `${mention.markdown} 用户安装期间编辑后的正文`),
  154. prependMentionIfMissing: vi.fn(() => true),
  155. setMention: vi.fn(),
  156. setText: vi.fn(),
  157. };
  158. const updateText = vi.fn();
  159. const requestFocus = vi.fn();
  160. const scheduleDraftPersist = vi.fn();
  161. expect(
  162. applyExternalTextInsertRequestToComposer({
  163. appliedRequestId: null,
  164. inputApi,
  165. request: {
  166. requestId: 8,
  167. text: mention.markdown,
  168. mention,
  169. mode: "prepend-if-missing",
  170. },
  171. requestFocus,
  172. scheduleDraftPersist,
  173. updateText,
  174. }),
  175. ).toBe(8);
  176. expect(inputApi.prependMentionIfMissing).toHaveBeenCalledWith(mention);
  177. expect(updateText).toHaveBeenCalledWith(`${mention.markdown} 用户安装期间编辑后的正文`);
  178. expect(inputApi.setMention).not.toHaveBeenCalled();
  179. expect(inputApi.setText).not.toHaveBeenCalled();
  180. });
  181. it("当前草稿已有同一 Plugin canonical 文本时不重复插入 chip", () => {
  182. const mention = {
  183. id: "plugin:demo@mkt",
  184. category: "plugins" as const,
  185. label: "Demo",
  186. value: "demo@mkt",
  187. markdown: "[@Demo](plugin://demo@mkt)",
  188. data: { pluginId: "demo@mkt" },
  189. };
  190. const inputApi = {
  191. // label 是 display-only;即使语言切换导致 label 不同,同一 stable ID 也不能重复插入。
  192. getMarkdown: vi.fn(() => "[@旧名称](plugin://demo@mkt) 当前正文"),
  193. prependMentionIfMissing: vi.fn(() => false),
  194. setMention: vi.fn(),
  195. setText: vi.fn(),
  196. };
  197. const updateText = vi.fn();
  198. expect(
  199. applyExternalTextInsertRequestToComposer({
  200. appliedRequestId: null,
  201. inputApi,
  202. request: {
  203. requestId: 9,
  204. text: mention.markdown,
  205. mention,
  206. mode: "prepend-if-missing",
  207. },
  208. requestFocus: vi.fn(),
  209. scheduleDraftPersist: vi.fn(),
  210. updateText,
  211. }),
  212. ).toBe(9);
  213. expect(inputApi.prependMentionIfMissing).toHaveBeenCalledWith(mention);
  214. expect(inputApi.setMention).not.toHaveBeenCalled();
  215. expect(updateText).not.toHaveBeenCalled();
  216. });
  217. it("PA158 撤回使用队列项自身权限/Plan/模型,不借用当前 Composer 配置", () => {
  218. const snapshot = makeSnapshot();
  219. Object.assign(snapshot.queue.items[0]!, {
  220. mode: "yolo",
  221. planEnabled: false,
  222. modelSelection: {
  223. providerId: "provider",
  224. modelId: "model",
  225. options: { reasoningLevel: "high" },
  226. },
  227. });
  228. expect(resolveQueuedComposerRestore(snapshot, "q-1")?.config).toEqual({
  229. mode: "yolo",
  230. planEnabled: false,
  231. modelSelection: {
  232. providerId: "provider",
  233. modelId: "model",
  234. options: { reasoningLevel: "high" },
  235. },
  236. });
  237. });
  238. it("旧 Highspeed 队列项撤回编辑时不得把临时 Provider 恢复到 Composer", () => {
  239. const snapshot = makeSnapshot();
  240. Object.assign(snapshot.queue.items[0]!, {
  241. highspeed: {
  242. schemaVersion: 1,
  243. cardId: "card-1",
  244. taskId: "session-1",
  245. provider: "bigmodel",
  246. model: "GLM-5.3",
  247. issuedAt: 1,
  248. expiresAt: Date.now() + 60_000,
  249. },
  250. mode: "yolo",
  251. modelSelection: {
  252. providerId: "account:bigmodel-highspeed-card",
  253. modelId: "GLM-5.3",
  254. },
  255. });
  256. expect(resolveQueuedComposerRestore(snapshot, "q-1")?.config).toEqual({ mode: "yolo" });
  257. });
  258. it("旧队列项只带临时 Provider、不带卡元数据时,撤回编辑同样不得恢复该 Provider", () => {
  259. const snapshot = makeSnapshot();
  260. Object.assign(snapshot.queue.items[0]!, {
  261. mode: "yolo",
  262. modelSelection: {
  263. providerId: "account:bigmodel-highspeed-card",
  264. modelId: "GLM-5.3",
  265. },
  266. });
  267. expect(resolveQueuedComposerRestore(snapshot, "q-1")?.config).toEqual({ mode: "yolo" });
  268. });
  269. it("提取完整输入并保持附件顺序,供权威删除后恢复 composer", () => {
  270. expect(resolveQueuedComposerRestore(makeSnapshot(), "q-1")).toEqual({
  271. baseRevision: 7,
  272. queueItemId: "q-1",
  273. sourceCommandId: "cmd-q1",
  274. inputKind: "sendText",
  275. text: "排队原文",
  276. attachments: [
  277. { ref: "ref-b", fileName: "b.png", mime: "image/png", bytes: 2 },
  278. { ref: "ref-a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
  279. ],
  280. });
  281. expect(resolveQueuedComposerRestore(makeSnapshot(), "missing")).toBeNull();
  282. });
  283. it("goal 恢复可见 slash 文本,compact 明确剪枝", () => {
  284. const goal = makeSnapshot();
  285. goal.queue.items[0] = {
  286. ...goal.queue.items[0]!,
  287. kind: "sendGoalCommand",
  288. text: "修复登录",
  289. };
  290. expect(resolveQueuedComposerRestore(goal, "q-1")?.text).toBe("/goal 修复登录");
  291. const compact = makeSnapshot();
  292. compact.queue.items[0] = {
  293. ...compact.queue.items[0]!,
  294. kind: "compact",
  295. text: "/compact",
  296. attachments: [],
  297. };
  298. expect(resolveQueuedComposerRestore(compact, "q-1")).toBeNull();
  299. });
  300. it("只允许 accepted/duplicate 恢复,其他 ACK 分支保持 composer 不变", () => {
  301. expect(shouldRestoreQueuedComposerFromAck("accepted")).toBe(true);
  302. expect(shouldRestoreQueuedComposerFromAck("duplicate")).toBe(true);
  303. expect(shouldRestoreQueuedComposerFromAck("noop")).toBe(false);
  304. expect(shouldRestoreQueuedComposerFromAck("stale")).toBe(false);
  305. expect(shouldRestoreQueuedComposerFromAck("rejected")).toBe(false);
  306. expect(shouldRestoreQueuedComposerFromAck("failed")).toBe(false);
  307. });
  308. it("只在绑定 session/workspace 的空 composer 幂等恢复文字与附件", () => {
  309. const request: ComposerRestoreRequest = {
  310. requestId: 1,
  311. sessionId: "session-1",
  312. workspaceKey: "remote:ssh:host:/workspace",
  313. inputKind: "sendText",
  314. text: "排队原文",
  315. attachments: [{ ref: "ref-1", fileName: "a.png", mime: "image/png", bytes: 1 }],
  316. config: { mode: "yolo", planEnabled: false },
  317. };
  318. const inputApi = {
  319. focus: vi.fn(),
  320. setText: vi.fn(),
  321. };
  322. const updateText = vi.fn();
  323. const requestFocus = vi.fn();
  324. const scheduleDraftPersist = vi.fn();
  325. const restoreSessionOwnedAttachments = vi.fn(() => true);
  326. const restoreDraftConfig = vi.fn();
  327. expect(
  328. applyComposerRestoreRequestToComposer({
  329. appliedRequestId: null,
  330. currentSessionId: "session-1",
  331. currentWorkspaceKey: "remote:ssh:host:/workspace",
  332. hasDraftContent: false,
  333. inputApi,
  334. request,
  335. requestFocus,
  336. restoreSessionOwnedAttachments,
  337. restoreDraftConfig,
  338. scheduleDraftPersist,
  339. updateText,
  340. }),
  341. ).toBe(1);
  342. expect(inputApi.setText).toHaveBeenCalledWith("排队原文");
  343. expect(updateText).toHaveBeenCalledWith("排队原文");
  344. expect(requestFocus).toHaveBeenCalledTimes(1);
  345. expect(scheduleDraftPersist).toHaveBeenCalledTimes(1);
  346. expect(restoreSessionOwnedAttachments).toHaveBeenCalledWith(request.attachments);
  347. expect(
  348. applyComposerRestoreRequestToComposer({
  349. appliedRequestId: 1,
  350. currentSessionId: "session-1",
  351. currentWorkspaceKey: "remote:ssh:host:/workspace",
  352. hasDraftContent: false,
  353. inputApi,
  354. request,
  355. requestFocus,
  356. restoreSessionOwnedAttachments,
  357. restoreDraftConfig,
  358. scheduleDraftPersist,
  359. updateText,
  360. }),
  361. ).toBe(1);
  362. expect(inputApi.setText).toHaveBeenCalledTimes(1);
  363. expect(restoreSessionOwnedAttachments).toHaveBeenCalledTimes(1);
  364. expect(
  365. applyComposerRestoreRequestToComposer({
  366. appliedRequestId: null,
  367. currentSessionId: "session-other",
  368. currentWorkspaceKey: "remote:ssh:host:/workspace",
  369. hasDraftContent: false,
  370. inputApi,
  371. request,
  372. requestFocus,
  373. restoreSessionOwnedAttachments,
  374. restoreDraftConfig,
  375. scheduleDraftPersist,
  376. updateText,
  377. }),
  378. ).toBeNull();
  379. expect(
  380. applyComposerRestoreRequestToComposer({
  381. appliedRequestId: null,
  382. currentSessionId: "session-1",
  383. currentWorkspaceKey: "remote:ssh:host:/workspace",
  384. hasDraftContent: true,
  385. inputApi,
  386. request,
  387. requestFocus,
  388. restoreSessionOwnedAttachments,
  389. restoreDraftConfig,
  390. scheduleDraftPersist,
  391. updateText,
  392. }),
  393. ).toBeNull();
  394. expect(inputApi.setText).toHaveBeenCalledTimes(1);
  395. expect(restoreDraftConfig).toHaveBeenCalledExactlyOnceWith(request.config);
  396. });
  397. });