pluginsMentionProvider.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // PLG(Picker 半区)UI 单测(docs/plugin-reference-mention.md §2/§5,
  2. // docs/conversation-session-case-catalog.md PLG03/S01):
  3. // enabled 过滤、同名冲突禁选与原因、canonical markdown 载体、身份只在 destination、
  4. // parseMentionMarkdown 的 plugin 分支不落 file/外链。
  5. import { describe, expect, it } from "vitest";
  6. import type { ZCodePluginReferenceCatalogEntry } from "@zcode/shared";
  7. import { mapPluginCatalogToMentionItemsForTest } from "../src/mentions/providers/pluginsMentionProvider.js";
  8. import {
  9. coerceEnabledMentionIndex,
  10. getNextEnabledMentionIndex,
  11. shouldFreezeMentionRecalcWhileComposing,
  12. } from "../src/mentions/MentionPlugin.js";
  13. import {
  14. buildMentionMarkdown,
  15. buildPluginMentionMarkdown,
  16. parseMentionMarkdown,
  17. } from "../src/mentions/mentionMarkdown.js";
  18. import { normalizePromptMentionDisplayLabel } from "../src/mentions/promptMentionLabel.js";
  19. import { filterMentionItemsWithOptions } from "@/mentions/mentionSearch.js";
  20. import { groupItemsByCategory, type StorePluginItem } from "@/settings/pluginStoreListing.js";
  21. const LABELS = {
  22. conflictReason: "同名插件冲突,暂不可引用",
  23. };
  24. function entry(
  25. overrides: Partial<ZCodePluginReferenceCatalogEntry>,
  26. ): ZCodePluginReferenceCatalogEntry {
  27. return {
  28. pluginId: "demo@mkt-a",
  29. name: "demo",
  30. marketplace: "mkt-a",
  31. icon: "https://cdn.example.com/demo.png",
  32. enabled: true,
  33. conflictingPluginIds: [],
  34. skillQualifiedNames: ["demo:search"],
  35. mcpServerNames: ["plugin:demo:main"],
  36. subagentNames: [],
  37. ...overrides,
  38. };
  39. }
  40. describe("plugins mention provider mapping", () => {
  41. it("按界面语言显示描述,缺翻译回退默认描述,旧 Host 留空", () => {
  42. const plugin = entry({
  43. description: "Default description",
  44. descriptionI18n: { "zh-CN": "处理文档", "en-US": "Work with documents" },
  45. });
  46. for (const [locale, description] of [
  47. ["zh-CN", "处理文档"],
  48. ["zh-TW", "处理文档"],
  49. ["en-US", "Work with documents"],
  50. ["fr-FR", "Default description"],
  51. ] as const) {
  52. const items = mapPluginCatalogToMentionItemsForTest([plugin], LABELS, locale);
  53. expect(items[0]?.description).toBe(description);
  54. expect(filterMentionItemsWithOptions(items, description)).toEqual([]);
  55. }
  56. expect(
  57. mapPluginCatalogToMentionItemsForTest([entry({})], LABELS, "en-US")[0]?.description,
  58. ).toBe("");
  59. });
  60. it("maps enabled catalog entries with the canonical plugin:// markdown carrier", () => {
  61. const items = mapPluginCatalogToMentionItemsForTest([entry({})], LABELS, "en-US");
  62. expect(items).toHaveLength(1);
  63. expect(items[0]).toMatchObject({
  64. id: "plugin:demo@mkt-a",
  65. category: "plugins",
  66. // label 固定 entry.name(chip/markdown 载体);面板展示名 displayLabel 走全 app 统一的
  67. // resolvePluginDisplayName,无 listing 时回退 canonical 化 slug。
  68. label: "demo",
  69. displayLabel: "Demo",
  70. value: "demo@mkt-a",
  71. markdown: "[@demo](plugin://demo@mkt-a)",
  72. data: {
  73. pluginId: "demo@mkt-a",
  74. icon: "https://cdn.example.com/demo.png",
  75. },
  76. });
  77. expect(items[0]?.disabled).toBeUndefined();
  78. });
  79. it("resolves the localized display name for the picker but keeps label and markdown on entry.name", () => {
  80. const items = mapPluginCatalogToMentionItemsForTest(
  81. [
  82. entry({
  83. displayName: "Demo Plugin",
  84. displayNameI18n: { "zh-CN": "演示插件" },
  85. }),
  86. ],
  87. LABELS,
  88. "zh-CN",
  89. );
  90. expect(items[0]?.displayLabel).toBe("演示插件");
  91. // Bugfix:chip 节点复用 item.label、消息气泡按 markdown label 重建,二者必须同源;
  92. // 因此 label 与载体 label 固定 entry.name,不随 locale 变化,只有面板展示名本地化。
  93. expect(items[0]?.label).toBe("demo");
  94. expect(items[0]?.markdown).toBe("[@demo](plugin://demo@mkt-a)");
  95. });
  96. it("keeps every locale's display name searchable via keywords regardless of the active locale", () => {
  97. const items = mapPluginCatalogToMentionItemsForTest(
  98. [
  99. entry({
  100. displayName: "Demo Plugin",
  101. displayNameI18n: { "zh-CN": "演示插件" },
  102. }),
  103. ],
  104. LABELS,
  105. "en-US",
  106. );
  107. expect(items[0]?.displayLabel).toBe("Demo Plugin");
  108. expect(items[0]?.label).toBe("demo");
  109. // 英文界面下打中文也能搜到(常开双通道),英文 name/id 通道保持不变。
  110. expect(items[0]?.keywords).toEqual(
  111. expect.arrayContaining(["demo", "demo@mkt-a", "mkt-a", "Demo Plugin", "演示插件"]),
  112. );
  113. });
  114. it("filters out disabled catalog entries entirely", () => {
  115. const items = mapPluginCatalogToMentionItemsForTest(
  116. [entry({ enabled: false })],
  117. LABELS,
  118. "en-US",
  119. );
  120. expect(items).toHaveLength(0);
  121. });
  122. it("keeps conflicted plugins visible but disabled with a reason (fail closed)", () => {
  123. const items = mapPluginCatalogToMentionItemsForTest(
  124. [
  125. entry({ pluginId: "demo@mkt-a", conflictingPluginIds: ["demo@mkt-b"] }),
  126. entry({
  127. pluginId: "demo@mkt-b",
  128. marketplace: "mkt-b",
  129. conflictingPluginIds: ["demo@mkt-a"],
  130. }),
  131. ],
  132. LABELS,
  133. "en-US",
  134. );
  135. expect(items).toHaveLength(2);
  136. for (const item of items) {
  137. expect(item.disabled).toBe(true);
  138. expect(item.disabledReason).toBe(LABELS.conflictReason);
  139. }
  140. });
  141. it("never leaves keyboard selection on a conflicted plugin when another option is enabled", () => {
  142. const items = [
  143. { disabled: true },
  144. { disabled: true },
  145. { disabled: false },
  146. { disabled: false },
  147. ];
  148. expect(coerceEnabledMentionIndex(0, items)).toBe(2);
  149. expect(getNextEnabledMentionIndex(2, -1, items)).toBe(3);
  150. expect(getNextEnabledMentionIndex(3, 1, items)).toBe(2);
  151. });
  152. });
  153. describe("mention panel recalc during IME composition", () => {
  154. const DESKTOP_UA =
  155. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/128.0 Safari/537.36";
  156. const ANDROID_UA =
  157. "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 Chrome/128.0 Mobile Safari/537.36";
  158. it("freezes recalculation while a transforming IME (pinyin) is composing on desktop", () => {
  159. expect(shouldFreezeMentionRecalcWhileComposing(true, DESKTOP_UA)).toBe(true);
  160. expect(shouldFreezeMentionRecalcWhileComposing(false, DESKTOP_UA)).toBe(false);
  161. });
  162. it("keeps live filtering on Android where Gboard composes every Latin word", () => {
  163. // Android Chrome + Gboard 对英文单词也走 composition,冻结重算会让手机 Web 失去逐字过滤。
  164. expect(shouldFreezeMentionRecalcWhileComposing(true, ANDROID_UA)).toBe(false);
  165. });
  166. });
  167. describe("plugin mention markdown carrier", () => {
  168. it("serializes via buildMentionMarkdown using the stable id, not the label", () => {
  169. expect(
  170. buildMentionMarkdown({
  171. category: "plugins",
  172. label: "Fancy Display Name",
  173. value: "demo@mkt-a",
  174. data: { pluginId: "demo@mkt-a" },
  175. }),
  176. ).toBe("[@Fancy Display Name](plugin://demo@mkt-a)");
  177. });
  178. it("escapes markdown-sensitive label characters without touching the destination", () => {
  179. expect(buildPluginMentionMarkdown("we[i]rd", "demo@mkt-a")).toBe(
  180. "[@we\\[i\\]rd](plugin://demo@mkt-a)",
  181. );
  182. });
  183. it("parses plugin links into plugin chips, never file or external-link parts", () => {
  184. const parts = parseMentionMarkdown(
  185. "before [@Demo](plugin://demo@mkt-a) after [readme](./README.md)",
  186. );
  187. expect(parts).toEqual([
  188. { type: "text", text: "before " },
  189. { type: "plugin", label: "Demo", pluginId: "demo@mkt-a" },
  190. { type: "text", text: " after " },
  191. { type: "file", label: "readme" },
  192. ]);
  193. });
  194. it("keeps an invalid plugin destination display-only without exposing it as a catalog key", () => {
  195. expect(parseMentionMarkdown("[@Demo](plugin://demo@mkt?inject=1)")).toEqual([
  196. { type: "plugin", label: "Demo" },
  197. ]);
  198. });
  199. it("strips the @ prefix for plugin chip display labels", () => {
  200. expect(normalizePromptMentionDisplayLabel("plugins", "@demo", "demo@mkt-a")).toBe("demo");
  201. });
  202. });
  203. describe("输入框复用市场排序", () => {
  204. const official = "zcode-plugins-official";
  205. const entries = [
  206. entry({ pluginId: "personal@personal", marketplace: "personal" }),
  207. entry({ name: "b", pluginId: `b@${official}`, marketplace: official, category: "utilities" }),
  208. entry({ name: "a", pluginId: `a@${official}`, marketplace: official, category: "utilities" }),
  209. entry({
  210. name: "dev",
  211. pluginId: `dev@${official}`,
  212. marketplace: official,
  213. category: "developer-tools",
  214. conflictingPluginIds: ["dev@personal"],
  215. }),
  216. entry({
  217. name: "off",
  218. pluginId: `off@${official}`,
  219. marketplace: official,
  220. category: "utilities",
  221. enabled: false,
  222. }),
  223. ];
  224. it("配置重排官方,个人随后保序;不添加目录外插件或启用禁用项", () => {
  225. const result = mapPluginCatalogToMentionItemsForTest(entries, LABELS, "en-US", {
  226. categoryOrder: ["utilities", "developer-tools"],
  227. pluginOrder: { utilities: [`missing@${official}`, `b@${official}`] },
  228. });
  229. expect(result.map((x) => x.value)).toEqual([
  230. `b@${official}`,
  231. `a@${official}`,
  232. `dev@${official}`,
  233. "personal@personal",
  234. ]);
  235. expect(result[2]?.disabled).toBe(true);
  236. });
  237. it("无配置和旧 Host 没分类时保留发现顺序", () => {
  238. expect(
  239. mapPluginCatalogToMentionItemsForTest(entries, LABELS, "en-US").map((x) => x.value),
  240. ).toEqual(entries.filter((x) => x.enabled).map((x) => x.pluginId));
  241. const old = entries.map(({ category: _category, ...item }) => item);
  242. expect(
  243. mapPluginCatalogToMentionItemsForTest(old, LABELS, "en-US", {
  244. categoryOrder: ["utilities"],
  245. }).map((x) => x.value),
  246. ).toEqual(old.filter((x) => x.enabled).map((x) => x.pluginId));
  247. });
  248. });
  249. it("市场和 Picker 的官方子集同序,查询同分保序且匹配度优先", () => {
  250. const official = "zcode-plugins-official";
  251. const catalog = [
  252. entry({ name: "b", pluginId: `b@${official}`, marketplace: official, category: "guides" }),
  253. entry({ name: "a", pluginId: `a@${official}`, marketplace: official, category: "utilities" }),
  254. entry({
  255. name: "dev",
  256. pluginId: `dev@${official}`,
  257. marketplace: official,
  258. category: "developer-tools",
  259. }),
  260. ];
  261. for (const categoryOrder of [
  262. ["utilities", "developer-tools"],
  263. ["developer-tools", "utilities"],
  264. ]) {
  265. const order = { categoryOrder, pluginOrder: { utilities: [`b@${official}`] } };
  266. const picker = mapPluginCatalogToMentionItemsForTest(
  267. catalog,
  268. { conflictReason: "conflict" },
  269. "zh-CN",
  270. order,
  271. );
  272. const store = catalog.map(
  273. (x) =>
  274. ({
  275. id: x.pluginId,
  276. name: x.name,
  277. marketplace: x.marketplace,
  278. installed: true,
  279. restorable: false,
  280. orphaned: false,
  281. listing: { category: x.category },
  282. }) satisfies StorePluginItem,
  283. );
  284. expect(picker.map((x) => x.value)).toEqual(
  285. groupItemsByCategory(store, "zh-CN", order).flatMap((x) => x.items.map((y) => y.id)),
  286. );
  287. const sameScoreItems = picker.map((item) => ({
  288. ...item,
  289. keywords: [...(item.keywords ?? []), "same"],
  290. }));
  291. expect(filterMentionItemsWithOptions(sameScoreItems, "same").map((x) => x.value)).toEqual(
  292. picker.map((x) => x.value),
  293. );
  294. expect(filterMentionItemsWithOptions(picker, "dev").map((x) => x.value)).toEqual([
  295. `dev@${official}`,
  296. ]);
  297. }
  298. expect(catalog.map((x) => x.name)).toEqual(["b", "a", "dev"]);
  299. });