/** * @file Tests for MemberPolicySettings — the admin's "allow members to run * tests" switch in Settings → AI tools: hidden in local mode, for a non-admin * and outside S3, and a toggle saves through PUT /api/ai-tools/member-policy. * @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 { MemberPolicySettings } from "../MemberPolicySettings"; import { api } from "../../../lib/api"; const perms = vi.hoisted(() => ({ isAdmin: true, local: false })); vi.mock("../../AuthGate", () => ({ usePermissions: () => perms })); vi.mock("../../../lib/api", () => ({ api: { aiTools: { memberPolicy: vi.fn(), setMemberPolicy: vi.fn() } }, })); const get = vi.mocked(api.aiTools.memberPolicy); const set = vi.mocked(api.aiTools.setMemberPolicy); describe("MemberPolicySettings", () => { beforeEach(() => { vi.clearAllMocks(); perms.isAdmin = true; perms.local = false; }); it("lets an admin allow tests, off by default", async () => { get.mockResolvedValue({ available: true, allowTests: false }); set.mockResolvedValue({ available: true, allowTests: true }); render(); const sw = await screen.findByRole("switch", { name: "Allow members to run tests" }); expect(sw).toHaveAttribute("aria-checked", "false"); fireEvent.click(sw); await waitFor(() => expect(set).toHaveBeenCalledWith(true)); await waitFor(() => expect(sw).toHaveAttribute("aria-checked", "true")); }); it("hides outside S3", async () => { get.mockResolvedValue({ available: false, allowTests: false }); const { container } = render(); await waitFor(() => expect(get).toHaveBeenCalled()); expect(container).toBeEmptyDOMElement(); }); it("asks nothing for a non-admin or in local mode", () => { perms.isAdmin = false; const { container } = render(); expect(container).toBeEmptyDOMElement(); perms.isAdmin = true; perms.local = true; render(); expect(get).not.toHaveBeenCalled(); }); });