pluginCapabilityProjection.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { describe, expect, it } from "vitest";
  2. import type {
  3. SkillSummary,
  4. ZCodeCommand,
  5. ZCodeInstalledPluginSummary,
  6. ZCodePluginInfo,
  7. } from "@zcode/shared";
  8. import {
  9. filterPluginsByQuery,
  10. groupScopedSkillsBySource,
  11. needsMcpAttention,
  12. partitionPluginsForSettings,
  13. selectBuiltInPlugins,
  14. selectPluginsForScope,
  15. selectSkillsForScope,
  16. selectCommandsForScope,
  17. } from "../src/settings/pluginCapabilityProjection.js";
  18. const plugins = [
  19. {
  20. id: "lovable@market",
  21. name: "lovable",
  22. enabled: true,
  23. enabledSource: "user",
  24. },
  25. {
  26. id: "docs@market",
  27. name: "docs",
  28. enabled: false,
  29. enabledSource: "workspace",
  30. },
  31. {
  32. id: "browser@official",
  33. name: "browser",
  34. enabled: true,
  35. source: "official",
  36. },
  37. ] as ZCodePluginInfo[];
  38. const installations = [
  39. { id: "lovable@market", scope: "user" },
  40. { id: "docs@market", scope: "workspace" },
  41. ] as ZCodeInstalledPluginSummary[];
  42. const skills = [
  43. { id: "user", name: "user-skill", scope: "user", path: "/u" },
  44. { id: "workspace", name: "workspace-skill", scope: "workspace", path: "/w" },
  45. {
  46. id: "lovable",
  47. name: "lovable-skill",
  48. scope: "plugin",
  49. pluginName: "Lovable",
  50. path: "/l",
  51. },
  52. {
  53. id: "docs",
  54. name: "docs-skill",
  55. scope: "plugin",
  56. pluginName: "docs",
  57. path: "/d",
  58. },
  59. {
  60. id: "browser",
  61. name: "browser-skill",
  62. scope: "plugin",
  63. pluginName: "browser",
  64. path: "/b",
  65. },
  66. ] as SkillSummary[];
  67. // Bug 根因:能力投影会先过滤 disabled Plugin;同名来源 fixture 必须携带协议必填的
  68. // enabled=true,否则类型强转会把缺失字段伪装成合法数据并错误测试成空投影。
  69. const sameNamePlugins = [
  70. {
  71. id: "computer-use@zcode-plugins-official",
  72. name: "computer-use",
  73. enabled: true,
  74. source: "official",
  75. },
  76. {
  77. id: "zcode-cua@custom-marketplace",
  78. name: "zcode-cua",
  79. enabled: true,
  80. source: "marketplace",
  81. },
  82. ] as ZCodePluginInfo[];
  83. describe("Plugin capability projection", () => {
  84. it("keeps missing package projections out of Settings groups", () => {
  85. const missing = {
  86. id: "missing@market",
  87. name: "missing",
  88. enabled: true,
  89. source: "missing",
  90. packageStatus: "missing",
  91. } as ZCodePluginInfo;
  92. const installed = {
  93. id: "installed@market",
  94. name: "installed",
  95. enabled: true,
  96. source: "marketplace",
  97. } as ZCodePluginInfo;
  98. const builtIn = {
  99. id: "builtin@official",
  100. name: "builtin",
  101. enabled: true,
  102. source: "official",
  103. } as ZCodePluginInfo;
  104. expect(
  105. partitionPluginsForSettings([missing, installed, builtIn], new Set([builtIn.id])),
  106. ).toEqual({
  107. installed: [installed],
  108. builtIn: [builtIn],
  109. });
  110. });
  111. it("puts only MCP resources requiring authorization into Needs Attention", () => {
  112. expect(
  113. needsMcpAttention({
  114. authorization: { authorizationUrl: "https://auth.example" },
  115. }),
  116. ).toBe(true);
  117. expect(needsMcpAttention({ authorization: {} })).toBe(false);
  118. expect(needsMcpAttention({})).toBe(false);
  119. });
  120. it("filters plugins by name and marketplace", () => {
  121. const searchablePlugins = [
  122. { id: "lovable", name: "Lovable", marketplace: "OpenAI" },
  123. { id: "docs", name: "Docs Canvas", marketplace: "Cursor" },
  124. ] as ZCodePluginInfo[];
  125. expect(filterPluginsByQuery(searchablePlugins, "LOVA")).toEqual([searchablePlugins[0]]);
  126. expect(filterPluginsByQuery(searchablePlugins, "cUrSoR")).toEqual([searchablePlugins[1]]);
  127. });
  128. it("shows the complete scope-specific projection returned by the Agent", () => {
  129. expect(selectPluginsForScope(plugins, installations, "user").map((item) => item.name)).toEqual([
  130. "lovable",
  131. "docs",
  132. "browser",
  133. ]);
  134. expect(
  135. selectPluginsForScope(plugins, installations, "workspace").map((item) => item.name),
  136. ).toEqual(["lovable", "docs", "browser"]);
  137. });
  138. it("does not use legacy installation scope as configuration-view ownership", () => {
  139. const legacyPlugin = {
  140. id: "legacy@market",
  141. name: "legacy",
  142. enabled: true,
  143. enabledSource: "user",
  144. } as ZCodePluginInfo;
  145. const legacyInstallation = {
  146. id: legacyPlugin.id,
  147. scope: "workspace",
  148. } as ZCodeInstalledPluginSummary;
  149. expect(selectPluginsForScope([legacyPlugin], [legacyInstallation], "user")).toEqual([
  150. legacyPlugin,
  151. ]);
  152. expect(selectPluginsForScope([legacyPlugin], [legacyInstallation], "workspace")).toEqual([
  153. legacyPlugin,
  154. ]);
  155. });
  156. it("keeps inherited User and Workspace override rows together in Workspace view", () => {
  157. const scopedPlugins = [
  158. {
  159. id: "workspace-root@inline",
  160. name: "workspace-root",
  161. enabled: true,
  162. rootSource: "workspace",
  163. },
  164. {
  165. id: "workspace-option@market",
  166. name: "workspace-option",
  167. enabled: true,
  168. optionSources: { token: "workspace" },
  169. },
  170. {
  171. id: "inherited-user@market",
  172. name: "inherited-user",
  173. enabled: true,
  174. enabledSource: "user",
  175. },
  176. ] as unknown as ZCodePluginInfo[];
  177. expect(selectPluginsForScope(scopedPlugins, [], "workspace").map((item) => item.name)).toEqual([
  178. "workspace-root",
  179. "workspace-option",
  180. "inherited-user",
  181. ]);
  182. });
  183. it("keeps built-ins in user scope but outside installed groups", () => {
  184. expect(selectBuiltInPlugins(plugins, installations).map((item) => item.name)).toEqual([
  185. "browser",
  186. ]);
  187. });
  188. it("lets an explicit installation override official-source built-in classification", () => {
  189. const officialInstallation = {
  190. id: "browser@official",
  191. scope: "workspace",
  192. } as ZCodeInstalledPluginSummary;
  193. const installed = [...installations, officialInstallation];
  194. expect(selectBuiltInPlugins(plugins, installed)).toEqual([]);
  195. expect(selectPluginsForScope(plugins, installed, "workspace").map((item) => item.name)).toEqual(
  196. ["lovable", "docs", "browser"],
  197. );
  198. expect(selectPluginsForScope(plugins, installed, "user").map((item) => item.name)).toEqual([
  199. "lovable",
  200. "docs",
  201. "browser",
  202. ]);
  203. });
  204. it("keeps direct and plugin-contributed skills inside the selected scope", () => {
  205. const userPlugins = selectPluginsForScope(plugins, installations, "user");
  206. expect(selectSkillsForScope(skills, userPlugins, "user").map((item) => item.name)).toEqual([
  207. "user-skill",
  208. "lovable-skill",
  209. "browser-skill",
  210. ]);
  211. });
  212. it("groups skills by direct source and concrete plugin name", () => {
  213. const groups = groupScopedSkillsBySource(selectSkillsForScope(skills, plugins, "user"), "User");
  214. expect(groups.map((group) => [group.label, group.skills.length])).toEqual([
  215. ["User", 1],
  216. ["browser", 1],
  217. ["Lovable", 1],
  218. ]);
  219. });
  220. it("keeps direct and plugin-contributed commands inside the selected scope", () => {
  221. const commands = [
  222. { id: "user", source: "user", location: { scope: "user" } },
  223. { id: "workspace", source: "user", location: { scope: "project" } },
  224. { id: "lovable", source: "plugin", pluginName: "Lovable" },
  225. { id: "docs", source: "plugin", pluginName: "docs" },
  226. ] as ZCodeCommand[];
  227. expect(
  228. selectCommandsForScope(
  229. commands,
  230. selectPluginsForScope(plugins, installations, "workspace"),
  231. "workspace",
  232. ).map((command) => command.id),
  233. ).toEqual(["workspace", "lovable"]);
  234. });
  235. it("hides capabilities contributed by disabled plugins", () => {
  236. expect(selectSkillsForScope(skills, plugins, "workspace").map((item) => item.name)).toEqual([
  237. "workspace-skill",
  238. "lovable-skill",
  239. "browser-skill",
  240. ]);
  241. });
  242. it("keeps same-name plugin skills tied to their full plugin id", () => {
  243. const sameNameSkills = [
  244. {
  245. id: "official-skill",
  246. name: "official-skill",
  247. scope: "plugin",
  248. pluginName: "computer-use",
  249. pluginId: "computer-use@zcode-plugins-official",
  250. path: "/official",
  251. },
  252. {
  253. id: "custom-skill",
  254. name: "custom-skill",
  255. scope: "plugin",
  256. pluginName: "zcode-cua",
  257. pluginId: "zcode-cua@custom-marketplace",
  258. path: "/custom",
  259. },
  260. ] as SkillSummary[];
  261. const selected = selectSkillsForScope(sameNameSkills, sameNamePlugins, "user");
  262. const groups = groupScopedSkillsBySource(selected, "User");
  263. expect(groups.map((group) => [group.id, group.label])).toEqual([
  264. ["plugin:computer-use@zcode-plugins-official", "computer-use"],
  265. ["plugin:zcode-cua@custom-marketplace", "zcode-cua"],
  266. ]);
  267. expect(groups.map((group) => group.skills[0]?.id)).toEqual(["official-skill", "custom-skill"]);
  268. });
  269. it("does not select a same-name plugin command from another marketplace", () => {
  270. const commands = [
  271. {
  272. id: "official-command",
  273. source: "plugin",
  274. pluginName: "computer-use",
  275. pluginMarketplace: "zcode-plugins-official",
  276. },
  277. {
  278. id: "custom-command",
  279. source: "plugin",
  280. pluginName: "zcode-cua",
  281. pluginMarketplace: "custom-marketplace",
  282. },
  283. ] as ZCodeCommand[];
  284. expect(
  285. selectCommandsForScope(commands, [sameNamePlugins[0]!], "user").map((command) => command.id),
  286. ).toEqual(["official-command"]);
  287. });
  288. });
  289. describe("localized managed plugin search", () => {
  290. const plugin = {
  291. id: "lark-cli@official",
  292. name: "lark-cli",
  293. marketplace: "official",
  294. description: "English description",
  295. } as ZCodePluginInfo;
  296. const item = {
  297. id: plugin.id,
  298. name: plugin.name,
  299. marketplace: plugin.marketplace,
  300. installed: true,
  301. orphaned: false,
  302. restorable: false,
  303. info: plugin,
  304. listing: {
  305. displayName: "Lark CLI",
  306. displayNameI18n: { "zh-CN": "飞书 CLI" },
  307. descriptionI18n: { "zh-CN": "文档协作" },
  308. },
  309. };
  310. it("matches the displayed localized name and description as well as stable identity", () => {
  311. const items = new Map([[plugin.id, item]]);
  312. for (const query of ["飞书", "文档协作", "LARK-CLI", "lark-cli@official"]) {
  313. expect(filterPluginsByQuery([plugin], query, items, "zh-CN")).toEqual([plugin]);
  314. }
  315. expect(filterPluginsByQuery([plugin], "文档协作", items, "en-US")).toEqual([]);
  316. });
  317. it("does not borrow listing from a same-name plugin in another marketplace", () => {
  318. const other = { ...plugin, id: "lark-cli@other", marketplace: "other" };
  319. expect(filterPluginsByQuery([other], "飞书", new Map([[plugin.id, item]]), "zh-CN")).toEqual(
  320. [],
  321. );
  322. expect(filterPluginsByQuery([other], "English description", new Map(), "zh-CN")).toEqual([
  323. other,
  324. ]);
  325. });
  326. });
  327. it("puts official PDF, PPT, Excel and Word first in the preset group", () => {
  328. const names = ["aaa", "documents", "spreadsheets", "presentations", "pdf"];
  329. const plugins = names.map(
  330. (name) =>
  331. ({
  332. id: `${name}@zcode-plugins-official`,
  333. name,
  334. source: "official",
  335. enabled: false,
  336. }) as ZCodePluginInfo,
  337. );
  338. const groups = partitionPluginsForSettings(plugins, new Set(plugins.map((plugin) => plugin.id)));
  339. expect(groups.builtIn.map((plugin) => plugin.name)).toEqual([
  340. "pdf",
  341. "presentations",
  342. "spreadsheets",
  343. "documents",
  344. "aaa",
  345. ]);
  346. expect(plugins[0]?.name).toBe("aaa");
  347. });