v4ConversationUserInputContent.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // @vitest-environment jsdom
  2. import { fireEvent, render } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { describe, expect, it } from "vitest";
  6. import {
  7. ConversationUserInputContent,
  8. parseV4UserInputGoalQuery,
  9. } from "@/v4/ConversationUserInputContent.js";
  10. import { PluginReferenceIconProvider } from "@/v4/pluginReferenceIconContext.js";
  11. function pluginContent(
  12. text: string,
  13. iconByPluginId: ReadonlyMap<string, string>,
  14. ) {
  15. return createElement(
  16. PluginReferenceIconProvider,
  17. { value: { sessionId: "session-a", iconByPluginId } },
  18. createElement(ConversationUserInputContent, { text }),
  19. );
  20. }
  21. describe("V4 user input goal query rendering", () => {
  22. it("splits an authoritative goal query without changing its original text", () => {
  23. expect(parseV4UserInputGoalQuery(" /goal 看一下这是个啥项目 ")).toEqual({
  24. leadingText: " ",
  25. commandText: "/goal",
  26. trailingText: " 看一下这是个啥项目 ",
  27. });
  28. expect(parseV4UserInputGoalQuery("/TARGET replace 新目标")).toEqual({
  29. leadingText: "",
  30. commandText: "/TARGET",
  31. trailingText: " replace 新目标",
  32. });
  33. });
  34. it.each([
  35. ["普通正文提到 /goal 但不是命令", [], 0],
  36. ["/compact", [], 0],
  37. ["/goal 带文件", [{ ref: "artifact" }], 0],
  38. ["/goal 带上下文", [], 1],
  39. ] as const)("keeps non-goal-control input as plain text: %s", (text, attachments, count) => {
  40. expect(parseV4UserInputGoalQuery(text, attachments, count)).toBeNull();
  41. });
  42. it("renders the goal label without the slash while keeping its command semantics", () => {
  43. const html = renderToStaticMarkup(
  44. createElement(ConversationUserInputContent, {
  45. text: "/goal 看一下这是个啥项目",
  46. }),
  47. );
  48. expect(html).toContain('data-v4-user-input-command="goal"');
  49. expect(html).toContain("lucide-goal");
  50. expect(html).toContain("text-command-node-foreground");
  51. expect(html).toContain("text-ui-base");
  52. expect(html).toContain("leading-6");
  53. expect(html).not.toContain("text-ui-lg");
  54. expect(html).not.toContain("leading-5");
  55. expect(html).toContain("goal</span> 看一下这是个啥项目");
  56. expect(html).not.toContain("/goal");
  57. });
  58. it("does not render a goal icon when attachments change the command into a normal prompt", () => {
  59. const html = renderToStaticMarkup(
  60. createElement(ConversationUserInputContent, {
  61. attachments: [{ ref: "artifact" }],
  62. text: "/goal 只是普通正文",
  63. }),
  64. );
  65. expect(html).toBe("/goal 只是普通正文");
  66. expect(html).not.toContain("lucide-goal");
  67. });
  68. });
  69. describe("V4 user input mention rendering", () => {
  70. it("renders a trusted Session-authority Plugin icon without exposing the stable id", () => {
  71. const icon = "https://plugins.example.test/skill-creator.png";
  72. const html = renderToStaticMarkup(
  73. pluginContent(
  74. "使用 [@skill-creator](plugin://skill-creator@official)",
  75. new Map([["skill-creator@official", icon]]),
  76. ),
  77. );
  78. expect(html).toContain('data-plugin-mention-id="skill-creator@official"');
  79. expect(html).toContain('data-plugin-mention-icon="true"');
  80. expect(html).toContain(`src="${icon}"`);
  81. expect(html).toContain("skill-creator");
  82. expect(html).not.toContain("plugin://skill-creator@official");
  83. expect(html).not.toContain("lucide-cable");
  84. });
  85. it.each([
  86. ["missing", undefined],
  87. ["untrusted", "http://plugins.example.test/skill-creator.png"],
  88. ])("falls back to Cable for a %s Plugin icon", (_label, icon) => {
  89. const html = renderToStaticMarkup(
  90. pluginContent(
  91. "[@skill-creator](plugin://skill-creator@official)",
  92. icon
  93. ? new Map([["skill-creator@official", icon]])
  94. : new Map<string, string>(),
  95. ),
  96. );
  97. expect(html).toContain("lucide-cable");
  98. expect(html).not.toContain('data-plugin-mention-icon="true"');
  99. });
  100. it("falls back to Cable when the trusted Plugin image fails to load", () => {
  101. const view = render(
  102. pluginContent(
  103. "[@skill-creator](plugin://skill-creator@official)",
  104. new Map([
  105. [
  106. "skill-creator@official",
  107. "https://plugins.example.test/missing.png",
  108. ],
  109. ]),
  110. ),
  111. );
  112. const image = view.container.querySelector<HTMLImageElement>(
  113. "img[data-plugin-mention-icon='true']",
  114. );
  115. expect(image).not.toBeNull();
  116. fireEvent.error(image as HTMLImageElement);
  117. expect(
  118. view.container.querySelector("img[data-plugin-mention-icon='true']"),
  119. ).toBeNull();
  120. expect(view.container.querySelector(".lucide-cable")).not.toBeNull();
  121. });
  122. it("renders skill markdown as a structured label without exposing its path", () => {
  123. const skillPath =
  124. "/Users/dev/.zcode/cli/plugins/cache/zcode-plugins-official/cloudbase-skills/0.1.0/skills/cloudbase/references/ai-model-nodejs/SKILL.md";
  125. const html = renderToStaticMarkup(
  126. createElement(ConversationUserInputContent, {
  127. text: `[$ai-model-nodejs](${skillPath}) 111`,
  128. }),
  129. );
  130. expect(html).toContain("lucide-wand-sparkles");
  131. expect(html).toContain("text-skill-node-foreground");
  132. expect(html).toContain("Ai Model Nodejs");
  133. expect(html).toContain("111");
  134. expect(html).not.toContain("$ai-model-nodejs");
  135. expect(html).not.toContain(skillPath);
  136. });
  137. it("renders selected and legacy session mentions with conversation semantics", () => {
  138. const html = renderToStaticMarkup(
  139. createElement(ConversationUserInputContent, {
  140. text: "跟 [#Review mailbox flow](#sess_visible_target) 聊,再看 #sess_legacy_target",
  141. }),
  142. );
  143. expect(html.match(/lucide-messages-square/g)).toHaveLength(2);
  144. expect(html.match(/text-session-node-foreground/g)).toHaveLength(2);
  145. expect(html).toContain("Review mailbox flow");
  146. expect(html).toContain("sess_legacy_target");
  147. expect(html).not.toContain("sess_visible_target");
  148. });
  149. it("renders files, directories, and subagents with their previous UI variants", () => {
  150. const html = renderToStaticMarkup(
  151. createElement(ConversationUserInputContent, {
  152. text: "查看 [demo.ts](./src/demo.ts)、[components](./src/components/),再交给 @reviewer",
  153. }),
  154. );
  155. expect(html.match(/text-file-node-foreground/g)).toHaveLength(2);
  156. expect(html).toContain("demo.ts");
  157. expect(html).toContain("components");
  158. expect(html).not.toContain("./src/demo.ts");
  159. expect(html).not.toContain("./src/components/");
  160. expect(html).toContain("lucide-bot");
  161. expect(html).toContain("text-subagent-node-foreground");
  162. expect(html).toContain("reviewer");
  163. });
  164. it("renders command-specific icons while omitting only the authoritative goal slash", () => {
  165. const html = renderToStaticMarkup(
  166. createElement(ConversationUserInputContent, {
  167. text: "/goal 优化性能,之后 /compact 再 /review",
  168. }),
  169. );
  170. expect(html).toContain('data-v4-user-input-command="goal"');
  171. expect(html).not.toContain("/goal");
  172. expect(html).toContain("lucide-goal");
  173. expect(html).toContain("lucide-scroll-text");
  174. expect(html).toContain("lucide-square-slash");
  175. expect(html.match(/text-command-node-foreground/g)).toHaveLength(3);
  176. });
  177. it("does not treat arbitrary hash text or a non-authoritative goal prefix as mentions", () => {
  178. const html = renderToStaticMarkup(
  179. createElement(ConversationUserInputContent, {
  180. attachments: [{ ref: "artifact" }],
  181. text: "/goal 普通正文里提到 #archived",
  182. }),
  183. );
  184. expect(html).toBe("/goal 普通正文里提到 #archived");
  185. expect(html).not.toContain("lucide-goal");
  186. expect(html).not.toContain("lucide-messages-square");
  187. });
  188. });