pluginStoreModeOrder.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { groupItemsByCategory, type StorePluginItem } from "../src/settings/pluginStoreListing.js";
  5. function item(name: string, category?: string): StorePluginItem {
  6. return {
  7. id: `${name}@official`,
  8. name,
  9. marketplace: "official",
  10. installed: false,
  11. restorable: false,
  12. orphaned: false,
  13. listing: { category },
  14. };
  15. }
  16. vi.mock("@/i18n/IntlProvider.js", () => ({
  17. useZCodeIntl: () => ({
  18. locale: "en-US",
  19. intl: { formatMessage: ({ id }: { id: string }) => id },
  20. }),
  21. }));
  22. describe("公开分类排序", () => {
  23. it("详情页也将旧指南分类显示为实用工具", async () => {
  24. const { PluginStoreDetailView } = await import("../src/settings/PluginStoreDetailView.js");
  25. const html = renderToStaticMarkup(
  26. createElement(PluginStoreDetailView, {
  27. item: item("zcode-guide", "guides"),
  28. actions: {
  29. onOpenDetail() {},
  30. onInstall() {},
  31. onUninstall() {},
  32. onSetEnabled() {},
  33. onUpdate() {},
  34. activeOperations: [],
  35. togglingPluginId: null,
  36. },
  37. onRetryDescribe() {},
  38. onUsePrompt() {},
  39. }),
  40. );
  41. expect(html).toContain("settings.plugins.store.category.utilities");
  42. });
  43. it("旧指南类目并入实用工具且不改变插件身份,legal 无插件时不显示", () => {
  44. const entries = [item("zcode-guide", "guides"), item("skill-creator", "utilities")];
  45. const groups = groupItemsByCategory(entries, "zh-CN", {
  46. categoryOrder: ["legal", "guides", "utilities"],
  47. });
  48. expect(groups.map((group) => group.category)).toEqual(["utilities"]);
  49. expect(groups[0]?.items.map((entry) => entry.id).sort()).toEqual(
  50. entries.map((entry) => entry.id).sort(),
  51. );
  52. expect(entries[0]?.listing?.category).toBe("guides");
  53. });
  54. it("未来法律插件可按服务端分类及插件顺序展示", () => {
  55. const groups = groupItemsByCategory(
  56. [item("contract", "legal"), item("research", "legal"), item("util", "utilities")],
  57. "en-US",
  58. {
  59. categoryOrder: ["legal", "utilities"],
  60. pluginOrder: { legal: ["research@official", "contract@official"] },
  61. },
  62. );
  63. expect(groups.map((group) => group.category)).toEqual(["legal", "utilities"]);
  64. expect(groups[0]?.items.map((entry) => entry.name)).toEqual(["research", "contract"]);
  65. });
  66. it("未知分类碰巧与对象原型属性同名时仍可使用默认排序", () => {
  67. const entries = [item("b", "constructor"), item("a", "constructor")];
  68. expect(
  69. groupItemsByCategory(entries, "en-US", { pluginOrder: {} })[0]?.items.map(
  70. (entry) => entry.name,
  71. ),
  72. ).toEqual(["a", "b"]);
  73. });
  74. const items = [
  75. item("z", "productivity"),
  76. item("a", "productivity"),
  77. item("b", "developer-tools"),
  78. item("c"),
  79. item("d", "unknown"),
  80. ];
  81. it("分类及分类内插件独立排序,未配置项保留默认,显式 other 可置顶", () => {
  82. const before = structuredClone(items);
  83. const groups = groupItemsByCategory(items, "en-US", {
  84. categoryOrder: ["missing", "other", "developer-tools", "other"],
  85. pluginOrder: { productivity: ["missing@official", "z@official", "z@official"] },
  86. });
  87. expect(groups.map((group) => group.category)).toEqual([
  88. "other",
  89. "developer-tools",
  90. "productivity",
  91. "unknown",
  92. ]);
  93. expect(groups[2]?.items.map((entry) => entry.name)).toEqual(["z", "a"]);
  94. expect(items).toEqual(before);
  95. });
  96. it.each(["zh-CN", "en-US"])("无配置保持原排序 %s", (locale) => {
  97. const groups = groupItemsByCategory(items, locale);
  98. expect(groups.map((group) => group.category)).toEqual([
  99. "productivity",
  100. "developer-tools",
  101. "unknown",
  102. "other",
  103. ]);
  104. expect(groups[0]?.items.map((entry) => entry.name)).toEqual(["a", "z"]);
  105. });
  106. it("ID 必须完整匹配,不能跨分类搬动或加入插件", () => {
  107. const groups = groupItemsByCategory(items, "en-US", {
  108. pluginOrder: { productivity: ["z", "z@personal", "b@official"] },
  109. });
  110. expect(groups[0]?.items.map((entry) => entry.name)).toEqual(["a", "z"]);
  111. expect(groups.flatMap((group) => group.items)).toHaveLength(items.length);
  112. });
  113. });