v4ConversationUserInputContent.test.ts 7.3 KB

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