provider-endpoint-hover.test.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @vitest-environment happy-dom
  3. */
  4. import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
  5. import type { ReactNode } from "react";
  6. import { act } from "react";
  7. import { createRoot } from "react-dom/client";
  8. import { NextIntlClientProvider } from "next-intl";
  9. import { beforeEach, describe, expect, test, vi } from "vitest";
  10. import { ProviderEndpointHover } from "@/app/[locale]/settings/providers/_components/provider-endpoint-hover";
  11. import type { ProviderEndpoint } from "@/types/provider";
  12. import enMessages from "../../../../messages/en";
  13. vi.mock("@/components/ui/tooltip", () => ({
  14. TooltipProvider: ({ children }: { children: ReactNode }) => <>{children}</>,
  15. Tooltip: ({ children, open }: { children: ReactNode; open: boolean }) => (
  16. <div data-testid="tooltip" data-state={open ? "open" : "closed"}>
  17. {children}
  18. </div>
  19. ),
  20. TooltipTrigger: ({ children }: { children: ReactNode }) => (
  21. <div data-testid="tooltip-trigger">{children}</div>
  22. ),
  23. TooltipContent: ({ children }: { children: ReactNode }) => (
  24. <div data-testid="tooltip-content">{children}</div>
  25. ),
  26. }));
  27. const providerEndpointsActionMocks = vi.hoisted(() => ({
  28. getProviderEndpointsByVendor: vi.fn(),
  29. getEndpointCircuitInfo: vi.fn(),
  30. }));
  31. vi.mock("@/actions/provider-endpoints", () => providerEndpointsActionMocks);
  32. function loadMessages() {
  33. const endpointStatus = {
  34. viewDetails: "View Details",
  35. activeEndpoints: "Active Endpoints",
  36. noEndpoints: "No Endpoints",
  37. healthy: "Healthy",
  38. unhealthy: "Unhealthy",
  39. unknown: "Unknown",
  40. circuitOpen: "Circuit Open",
  41. circuitHalfOpen: "Circuit Half-Open",
  42. };
  43. return {
  44. settings: {
  45. ...enMessages.settings,
  46. providers: {
  47. ...(enMessages.settings.providers || {}),
  48. endpointStatus,
  49. },
  50. },
  51. };
  52. }
  53. let queryClient: QueryClient;
  54. function renderWithProviders(node: ReactNode) {
  55. const container = document.createElement("div");
  56. document.body.appendChild(container);
  57. const root = createRoot(container);
  58. act(() => {
  59. root.render(
  60. <QueryClientProvider client={queryClient}>
  61. <NextIntlClientProvider locale="en" messages={loadMessages()} timeZone="UTC">
  62. {node}
  63. </NextIntlClientProvider>
  64. </QueryClientProvider>
  65. );
  66. });
  67. return {
  68. container,
  69. unmount: () => {
  70. act(() => root.unmount());
  71. container.remove();
  72. },
  73. };
  74. }
  75. async function flushTicks(times = 3) {
  76. for (let i = 0; i < times; i++) {
  77. await act(async () => {
  78. await new Promise((r) => setTimeout(r, 0));
  79. });
  80. }
  81. }
  82. describe("ProviderEndpointHover", () => {
  83. beforeEach(() => {
  84. queryClient = new QueryClient({
  85. defaultOptions: {
  86. queries: { retry: false },
  87. },
  88. });
  89. vi.clearAllMocks();
  90. while (document.body.firstChild) {
  91. document.body.removeChild(document.body.firstChild);
  92. }
  93. });
  94. const mockEndpoints: ProviderEndpoint[] = [
  95. {
  96. id: 1,
  97. vendorId: 1,
  98. providerType: "claude",
  99. url: "https://api.anthropic.com/v1",
  100. label: "Healthy Endpoint",
  101. sortOrder: 10,
  102. isEnabled: true,
  103. deletedAt: null,
  104. lastProbeOk: true,
  105. lastProbeLatencyMs: 100,
  106. lastProbeStatusCode: null,
  107. lastProbeErrorType: null,
  108. lastProbeErrorMessage: null,
  109. createdAt: new Date("2024-01-01"),
  110. updatedAt: new Date("2024-01-01"),
  111. lastProbedAt: null,
  112. },
  113. {
  114. id: 2,
  115. vendorId: 1,
  116. providerType: "claude",
  117. url: "https://api.anthropic.com/v2",
  118. label: "Unhealthy Endpoint",
  119. sortOrder: 20,
  120. isEnabled: true,
  121. deletedAt: null,
  122. lastProbeOk: false,
  123. lastProbeLatencyMs: null,
  124. lastProbeStatusCode: null,
  125. lastProbeErrorType: null,
  126. lastProbeErrorMessage: null,
  127. createdAt: new Date("2024-01-01"),
  128. updatedAt: new Date("2024-01-01"),
  129. lastProbedAt: null,
  130. },
  131. {
  132. id: 3,
  133. vendorId: 1,
  134. providerType: "claude",
  135. url: "https://api.anthropic.com/v3",
  136. label: "Unknown Endpoint",
  137. sortOrder: 5,
  138. isEnabled: true,
  139. deletedAt: null,
  140. lastProbeOk: null,
  141. lastProbeLatencyMs: null,
  142. lastProbeStatusCode: null,
  143. lastProbeErrorType: null,
  144. lastProbeErrorMessage: null,
  145. createdAt: new Date("2024-01-01"),
  146. updatedAt: new Date("2024-01-01"),
  147. lastProbedAt: null,
  148. },
  149. {
  150. id: 4,
  151. vendorId: 1,
  152. providerType: "openai-compatible",
  153. url: "https://api.openai.com",
  154. label: "Wrong Type",
  155. sortOrder: 0,
  156. isEnabled: true,
  157. deletedAt: null,
  158. lastProbeOk: true,
  159. lastProbeLatencyMs: 50,
  160. lastProbeStatusCode: null,
  161. lastProbeErrorType: null,
  162. lastProbeErrorMessage: null,
  163. createdAt: new Date("2024-01-01"),
  164. updatedAt: new Date("2024-01-01"),
  165. lastProbedAt: null,
  166. },
  167. {
  168. id: 5,
  169. vendorId: 1,
  170. providerType: "claude",
  171. url: "https://api.anthropic.com/v4",
  172. label: "Disabled Endpoint",
  173. sortOrder: 0,
  174. isEnabled: false,
  175. deletedAt: null,
  176. lastProbeOk: true,
  177. lastProbeLatencyMs: 50,
  178. lastProbeStatusCode: null,
  179. lastProbeErrorType: null,
  180. lastProbeErrorMessage: null,
  181. createdAt: new Date("2024-01-01"),
  182. updatedAt: new Date("2024-01-01"),
  183. lastProbedAt: null,
  184. },
  185. ];
  186. test("renders trigger with correct count and filters correctly", async () => {
  187. providerEndpointsActionMocks.getProviderEndpointsByVendor.mockResolvedValue(mockEndpoints);
  188. const { unmount, container } = renderWithProviders(
  189. <ProviderEndpointHover vendorId={1} providerType="claude" />
  190. );
  191. await flushTicks();
  192. const triggerText = container.textContent;
  193. expect(triggerText).toContain("3");
  194. unmount();
  195. });
  196. test("sorts endpoints correctly: Healthy > Unknown > Unhealthy", async () => {
  197. providerEndpointsActionMocks.getProviderEndpointsByVendor.mockResolvedValue(mockEndpoints);
  198. const { unmount } = renderWithProviders(
  199. <ProviderEndpointHover vendorId={1} providerType="claude" />
  200. );
  201. await flushTicks();
  202. const tooltipContent = document.querySelector("[data-testid='tooltip-content']");
  203. expect(tooltipContent).not.toBeNull();
  204. const labels = Array.from(
  205. document.querySelectorAll("[data-testid='tooltip-content'] span.truncate")
  206. ).map((el) => el.textContent);
  207. expect(labels).toEqual([
  208. "https://api.anthropic.com/v1",
  209. "https://api.anthropic.com/v3",
  210. "https://api.anthropic.com/v2",
  211. ]);
  212. unmount();
  213. });
  214. test("does not fetch circuit info initially (when closed)", async () => {
  215. providerEndpointsActionMocks.getProviderEndpointsByVendor.mockResolvedValue(mockEndpoints);
  216. const { unmount } = renderWithProviders(
  217. <ProviderEndpointHover vendorId={1} providerType="claude" />
  218. );
  219. await flushTicks();
  220. expect(providerEndpointsActionMocks.getEndpointCircuitInfo).not.toHaveBeenCalled();
  221. unmount();
  222. });
  223. });