modelProviderNavigationLoading.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { createElement, type ReactElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { TooltipProvider } from "@/components/ui/tooltip.js";
  5. import {
  6. getSortableProviderId,
  7. ModelProviderSectionNavigation,
  8. resolveReorderedProviderIdsForGroup,
  9. shouldShowModelProviderGroupLoadingIndicator,
  10. } from "@/settings/model-provider-section/Navigation.js";
  11. vi.mock("@/i18n/IntlProvider.js", () => ({
  12. useZCodeIntl: () => ({
  13. intl: {
  14. formatMessage: ({ id }: { id: string }) => id,
  15. },
  16. }),
  17. }));
  18. function renderWithTooltipProvider(element: ReactElement) {
  19. return renderToStaticMarkup(createElement(TooltipProvider, null, element));
  20. }
  21. describe("model provider navigation loading indicator", () => {
  22. it("没有 Personal Provider 时不渲染空分组", () => {
  23. const html = renderWithTooltipProvider(
  24. createElement(ModelProviderSectionNavigation, {
  25. navigationGroups: [
  26. { id: "preset", title: "Preset", items: [] },
  27. { id: "custom", title: "Custom Providers", items: [] },
  28. ],
  29. selectedNodeKey: null,
  30. presetLoading: false,
  31. customLoading: false,
  32. onSelectNavItem: vi.fn(),
  33. }),
  34. );
  35. expect(html).not.toContain("Custom Providers");
  36. });
  37. it("只对 Settings View 允许项启用拖拽", () => {
  38. const item = {
  39. key: "custom:official-api",
  40. type: "custom" as const,
  41. label: "Official API",
  42. statusActive: true,
  43. provider: {
  44. providerId: "official-api",
  45. executable: true,
  46. enabled: true,
  47. hasPersonalConfig: true,
  48. personalConfig: { enabled: true },
  49. config: {
  50. label: "Official API",
  51. group: "standard-personal",
  52. templateId: "official-api",
  53. access: { type: "api-key", apiKey: "key" },
  54. api: { type: "anthropic-messages", baseUrl: "https://example.com" },
  55. builtinModelIds: [],
  56. },
  57. models: [],
  58. },
  59. };
  60. expect(getSortableProviderId(item, new Set(["personal-api"]))).toBeNull();
  61. expect(getSortableProviderId(item, new Set(["official-api"]))).toBe("official-api");
  62. });
  63. it("按分组返回对应 loading 状态", () => {
  64. expect(
  65. shouldShowModelProviderGroupLoadingIndicator({
  66. groupId: "preset",
  67. presetLoading: true,
  68. customLoading: false,
  69. }),
  70. ).toBe(true);
  71. expect(
  72. shouldShowModelProviderGroupLoadingIndicator({
  73. groupId: "custom",
  74. presetLoading: true,
  75. customLoading: false,
  76. }),
  77. ).toBe(false);
  78. });
  79. it("拖拽结束时只重排当前分组 provider id", () => {
  80. expect(
  81. resolveReorderedProviderIdsForGroup({
  82. activeProviderId: "provider-c",
  83. overProviderId: "provider-a",
  84. providerIds: ["provider-a", "provider-b", "provider-c"],
  85. }),
  86. ).toEqual(["provider-c", "provider-a", "provider-b"]);
  87. });
  88. it("允许调序的自定义供应商渲染可访问 sortable 行", () => {
  89. const html = renderWithTooltipProvider(
  90. createElement(ModelProviderSectionNavigation, {
  91. navigationGroups: [
  92. {
  93. id: "custom",
  94. title: "Custom Providers",
  95. items: [
  96. {
  97. key: "custom:provider-a",
  98. type: "custom",
  99. label: "Provider A",
  100. provider: {
  101. providerId: "provider-a",
  102. executable: true,
  103. enabled: true,
  104. hasPersonalConfig: true,
  105. personalConfig: {
  106. group: "standard-personal",
  107. personalModelIds: ["model-a"],
  108. },
  109. config: {
  110. label: "Provider A",
  111. group: "standard-personal",
  112. access: { type: "api-key", apiKey: "token" },
  113. api: { type: "anthropic-messages", baseUrl: "https://example.com" },
  114. personalModelIds: ["model-a"],
  115. },
  116. models: [{ modelId: "model-a", config: {}, hasPersonalConfig: true }],
  117. },
  118. statusActive: true,
  119. },
  120. ],
  121. },
  122. ],
  123. selectedNodeKey: null,
  124. presetLoading: false,
  125. customLoading: false,
  126. onSelectNavItem: vi.fn(),
  127. reorderableProviderIds: new Set(["provider-a"]),
  128. }),
  129. );
  130. expect(html).toContain('aria-label="Provider A"');
  131. expect(html).toMatch(
  132. /data-testid="model-provider-nav-item-custom:provider-a"[^>]*aria-roledescription="sortable"[\s\S]*Provider A/,
  133. );
  134. });
  135. });