/** * @file AlertsNotifications.roles.test.tsx * @description Server mode, not an admin: alert rules and channels fire for * every user, so neither tab is shown nor loaded, and an analyst's feed offers * Acknowledge only on alerts the server marks theirs (`can_ack`). * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect, vi } from "vitest"; import { render, screen } from "@testing-library/react"; import { MemoryRouter } from "react-router-dom"; const rulesList = vi.fn(async () => ({ rules: [] })); const alert = (id: number, can_ack: boolean) => ({ id, rule_id: "r1", rule_name: `rule ${id}`, rule_type: "error_rate", session_id: null, agent_id: null, message: `boom ${id}`, details: null, triggered_at: "2026-10-08T00:00:00.000Z", acknowledged_at: null, can_ack, }); vi.mock("../../lib/api", () => ({ api: { alerts: { rules: { list: () => rulesList() }, list: vi.fn(async () => ({ alerts: [alert(1, true), alert(2, false)], total: 2, unacked: 2, })), ack: vi.fn(), ackAll: vi.fn(), }, }, })); vi.mock("../AuthGate", () => ({ usePermissions: () => ({ isAdmin: false, seesAll: true, canEditLane: () => false }), })); import { AlertsNotifications } from "../AlertsNotifications"; describe("AlertsNotifications for a non-admin", () => { it("shows only the feed, and Acknowledge only where the server allows it", async () => { render( ); expect(await screen.findByText("boom 2")).toBeInTheDocument(); expect(screen.getAllByRole("button", { name: /^acknowledge$/i })).toHaveLength(1); expect(screen.queryByRole("button", { name: /rules/i })).toBeNull(); expect(screen.queryByRole("button", { name: /channels/i })).toBeNull(); expect(rulesList).not.toHaveBeenCalled(); }); });