/** * @file Tests for ArtifactsHubSettings — the admin's Hub install switch in * Settings → AI tools: hidden in local mode and for a non-manager, and a * toggle saves through PUT /api/artifacts/settings. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, expect, it, vi, beforeEach } from "vitest"; import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import "../../../i18n"; import { ArtifactsHubSettings } from "../ArtifactsHubSettings"; import { api } from "../../../lib/api"; vi.mock("../../../lib/api", () => ({ api: { artifacts: { settings: vi.fn(), setInstallsEnabled: vi.fn() } }, })); const settings = vi.mocked(api.artifacts.settings); const setInstalls = vi.mocked(api.artifacts.setInstallsEnabled); const base = { serverMode: true, canShare: true, installsEnabled: true, canManage: true }; describe("ArtifactsHubSettings", () => { beforeEach(() => vi.clearAllMocks()); it("lets an admin turn Hub installs off", async () => { settings.mockResolvedValue(base); setInstalls.mockResolvedValue({ installsEnabled: false }); render(); const sw = await screen.findByRole("switch", { name: "Allow installing from the Hub" }); fireEvent.click(sw); await waitFor(() => expect(setInstalls).toHaveBeenCalledWith(false)); await waitFor(() => expect(sw).toHaveAttribute("aria-checked", "false")); }); it("renders nothing for a non-manager or in local mode", async () => { settings.mockResolvedValue({ ...base, canManage: false }); const { container, unmount } = render(); await waitFor(() => expect(settings).toHaveBeenCalled()); expect(container).toBeEmptyDOMElement(); unmount(); settings.mockResolvedValue({ ...base, serverMode: false }); const r = render(); await waitFor(() => expect(settings).toHaveBeenCalledTimes(2)); expect(r.container).toBeEmptyDOMElement(); }); });