pluginStoreModeOrder.test.ts 4.5 KB

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