import { describe, it, expect, vi, beforeEach } from "vitest"; import { render, screen, fireEvent, waitFor } from "@testing-library/react"; import { MailboxModal } from "../MailboxModal"; import * as apiModule from "../../api"; import type { Agent } from "../../api"; import type { Message } from "@fusion/core"; // Mock the API module vi.mock("../../api", () => ({ fetchInbox: vi.fn(), fetchOutbox: vi.fn(), fetchUnreadCount: vi.fn(), fetchAgentMailbox: vi.fn(), markMessageRead: vi.fn(), markAllMessagesRead: vi.fn(), deleteMessage: vi.fn(), fetchConversation: vi.fn(), sendMessage: vi.fn(), })); // Mock lucide-react icons vi.mock("lucide-react", () => ({ X: () => X, Mail: () => Mail, Send: () => Send, Inbox: () => Inbox, Bot: () => Bot, Trash2: () => Trash, Check: () => Check, CheckCheck: () => CheckCheck, Loader2: ({ className }: { className?: string }) => ( Loader ), RefreshCw: () => Refresh, MessageSquare: () => Message, User: () => User, AlertCircle: () => Alert, })); const mockFetchInbox = vi.mocked(apiModule.fetchInbox); const mockFetchOutbox = vi.mocked(apiModule.fetchOutbox); const mockFetchUnreadCount = vi.mocked(apiModule.fetchUnreadCount); const mockFetchAgentMailbox = vi.mocked(apiModule.fetchAgentMailbox); const mockMarkMessageRead = vi.mocked(apiModule.markMessageRead); const mockMarkAllMessagesRead = vi.mocked(apiModule.markAllMessagesRead); const mockDeleteMessage = vi.mocked(apiModule.deleteMessage); const mockFetchConversation = vi.mocked(apiModule.fetchConversation); const mockAgents: Agent[] = [ { id: "agent-001", name: "Test Agent 1", role: "executor", state: "idle", createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), metadata: {}, }, { id: "agent-002", name: "Test Agent 2", role: "triage", state: "active", taskId: "FN-001", createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), metadata: {}, }, ]; const mockMessage: Message = { id: "msg-001", fromId: "agent-001", fromType: "agent", toId: "dashboard", toType: "user", content: "Hello, this is a test message from the agent.", type: "agent-to-user", read: false, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), }; const mockReadMessage: Message = { ...mockMessage, id: "msg-002", read: true, content: "This message has been read already.", }; const defaultProps = { isOpen: true, onClose: vi.fn(), addToast: vi.fn(), agents: mockAgents, }; describe("MailboxModal", () => { beforeEach(() => { vi.clearAllMocks(); mockFetchInbox.mockResolvedValue({ messages: [mockMessage, mockReadMessage], total: 2, unreadCount: 1 }); mockFetchOutbox.mockResolvedValue({ messages: [], total: 0 }); mockFetchUnreadCount.mockResolvedValue({ unreadCount: 1 }); mockFetchConversation.mockResolvedValue([mockMessage]); mockMarkMessageRead.mockResolvedValue({ ...mockMessage, read: true }); mockMarkAllMessagesRead.mockResolvedValue({ markedAsRead: 1 }); mockDeleteMessage.mockResolvedValue(undefined); }); it("renders nothing when isOpen is false", () => { render(); expect(screen.queryByTestId("mailbox-modal")).toBeNull(); }); it("renders the modal when isOpen is true", () => { render(); expect(screen.getByTestId("mailbox-modal")).toBeDefined(); }); it("shows the Mailbox title with unread count badge", async () => { render(); expect(screen.getByText("Mailbox")).toBeDefined(); // Wait for inbox to load which sets unreadCount await waitFor(() => { expect(screen.getByTestId("mailbox-unread-badge")).toBeDefined(); }); expect(screen.getByTestId("mailbox-unread-badge").textContent).toBe("1"); }); it("renders all three tabs", () => { render(); expect(screen.getByTestId("mailbox-tab-inbox")).toBeDefined(); expect(screen.getByTestId("mailbox-tab-outbox")).toBeDefined(); expect(screen.getByTestId("mailbox-tab-agents")).toBeDefined(); }); it("shows inbox tab as active by default", () => { render(); const inboxTab = screen.getByTestId("mailbox-tab-inbox"); expect(inboxTab.classList.contains("active")).toBe(true); }); it("loads inbox on mount", async () => { render(); await waitFor(() => { expect(mockFetchInbox).toHaveBeenCalledWith({ limit: 50 }, undefined); }); }); it("shows inbox messages after loading", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-inbox-list")).toBeDefined(); }); // Should show both messages expect(screen.getByTestId("mailbox-item-msg-001")).toBeDefined(); expect(screen.getByTestId("mailbox-item-msg-002")).toBeDefined(); }); it("shows unread dot for unread messages", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-unread-dot-msg-001")).toBeDefined(); }); }); it("does not show unread dot for read messages", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-item-msg-002")).toBeDefined(); }); expect(screen.queryByTestId("mailbox-unread-dot-msg-002")).toBeNull(); }); it("switches to outbox tab on click", async () => { render(); const outboxTab = screen.getByTestId("mailbox-tab-outbox"); fireEvent.click(outboxTab); await waitFor(() => { expect(mockFetchOutbox).toHaveBeenCalledWith({ limit: 50 }, undefined); }); }); it("shows empty state for empty outbox", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-outbox")); await waitFor(() => { expect(screen.getByTestId("mailbox-outbox-empty")).toBeDefined(); }); }); it("switches to agents tab on click", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByTestId("mailbox-agents")).toBeDefined(); }); }); it("shows agent dropdown in agents tab", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByTestId("mailbox-agent-select")).toBeDefined(); }); // Should have placeholder plus two agent options const select = screen.getByTestId("mailbox-agent-select") as HTMLSelectElement; expect(select.options.length).toBe(3); // placeholder + 2 agents expect(select.options[0].textContent).toBe("Select an agent…"); expect(select.options[1].textContent).toBe("Test Agent 1"); expect(select.options[2].textContent).toBe("Test Agent 2"); }); it("shows Select an agent… placeholder in dropdown", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { const select = screen.getByTestId("mailbox-agent-select") as HTMLSelectElement; expect(select.value).toBe(""); expect(select.options[0].textContent).toBe("Select an agent…"); }); }); it("loads agent mailbox when selecting an agent from dropdown", async () => { mockFetchAgentMailbox.mockResolvedValue({ ownerId: "agent-001", ownerType: "agent", unreadCount: 0, messages: [], }); render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByTestId("mailbox-agent-select")).toBeDefined(); }); fireEvent.change(screen.getByTestId("mailbox-agent-select"), { target: { value: "agent-001" } }); await waitFor(() => { expect(mockFetchAgentMailbox).toHaveBeenCalledWith("agent-001", undefined); }); }); it("shows empty state when no agents exist", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByText("No agents found")).toBeDefined(); }); }); it("opens message detail when clicking a message", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-item-msg-001")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-item-msg-001")); await waitFor(() => { expect(screen.getByTestId("mailbox-message-detail")).toBeDefined(); }); }); it("marks message as read when opening unread message", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-item-msg-001")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-item-msg-001")); await waitFor(() => { expect(mockMarkMessageRead).toHaveBeenCalledWith("msg-001", undefined); }); }); it("shows back button in message detail", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-item-msg-001")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-item-msg-001")); await waitFor(() => { expect(screen.getByTestId("mailbox-back-to-list")).toBeDefined(); }); }); it("returns to list when clicking back button", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-item-msg-001")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-item-msg-001")); await waitFor(() => { expect(screen.getByTestId("mailbox-back-to-list")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-back-to-list")); await waitFor(() => { expect(screen.queryByTestId("mailbox-message-detail")).toBeNull(); expect(screen.getByTestId("mailbox-inbox-list")).toBeDefined(); }); }); it("shows mark all read button when there are unread messages", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-mark-all-read")).toBeDefined(); }); }); it("calls markAllMessagesRead when clicking mark all read", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-mark-all-read")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-mark-all-read")); await waitFor(() => { expect(mockMarkAllMessagesRead).toHaveBeenCalledWith(undefined); }); }); it("deletes message when clicking delete in detail view", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-item-msg-001")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-item-msg-001")); await waitFor(() => { expect(screen.getByTestId("mailbox-delete")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-delete")); await waitFor(() => { expect(mockDeleteMessage).toHaveBeenCalledWith("msg-001", undefined); }); }); it("shows compose FAB in inbox tab", async () => { render(); await waitFor(() => { expect(screen.getByTestId("mailbox-compose-fab")).toBeDefined(); }); }); it("does not show compose FAB in agents tab", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.queryByTestId("mailbox-compose-fab")).toBeNull(); }); }); it("shows compose button in Agents tab", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByTestId("mailbox-compose-btn")).toBeDefined(); }); }); it("compose opened from Agents tab without selected agent shows recipient select", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByTestId("mailbox-compose-btn")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-compose-btn")); await waitFor(() => { expect(screen.getByTestId("message-composer")).toBeDefined(); }); // Should show recipient dropdown (not pre-filled) expect(screen.getByTestId("message-composer-recipient")).toBeDefined(); }); it("compose opened from Agents tab pre-fills selected agent recipient", async () => { mockFetchAgentMailbox.mockResolvedValue({ ownerId: "agent-001", ownerType: "agent", unreadCount: 0, messages: [], }); render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByTestId("mailbox-agent-select")).toBeDefined(); }); // Select an agent fireEvent.change(screen.getByTestId("mailbox-agent-select"), { target: { value: "agent-001" } }); await waitFor(() => { expect(mockFetchAgentMailbox).toHaveBeenCalledWith("agent-001", undefined); }); // Click compose fireEvent.click(screen.getByTestId("mailbox-compose-btn")); await waitFor(() => { expect(screen.getByTestId("message-composer")).toBeDefined(); }); // Should show pre-filled recipient (not dropdown) expect(screen.getByText("agent-001")).toBeDefined(); }); it("successful send from Agents tab keeps user on Agents tab and preserves selected agent", async () => { render(); fireEvent.click(screen.getByTestId("mailbox-tab-agents")); await waitFor(() => { expect(screen.getByTestId("mailbox-agent-select")).toBeDefined(); }); // Select an agent fireEvent.change(screen.getByTestId("mailbox-agent-select"), { target: { value: "agent-001" } }); await waitFor(() => { expect(mockFetchAgentMailbox).toHaveBeenCalledWith("agent-001", undefined); }); // Open compose (pre-filled) fireEvent.click(screen.getByTestId("mailbox-compose-btn")); await waitFor(() => { expect(screen.getByTestId("message-composer")).toBeDefined(); }); // Type and send message fireEvent.change(screen.getByTestId("message-composer-content"), { target: { value: "Hello agent!" }, }); fireEvent.click(screen.getByTestId("message-composer-send")); await waitFor(() => { expect(screen.queryByTestId("message-composer")).toBeNull(); }); // Verify still on Agents tab and agent is still selected expect(screen.getByTestId("mailbox-agents")).toBeDefined(); const select = screen.getByTestId("mailbox-agent-select") as HTMLSelectElement; expect(select.value).toBe("agent-001"); }); it("shows loading skeleton while loading", async () => { mockFetchInbox.mockImplementation(() => new Promise(() => {})); // Never resolves render(); await waitFor(() => { expect(screen.getByTestId("mailbox-skeleton")).toBeDefined(); }); }); it("shows empty inbox state when no messages", async () => { mockFetchInbox.mockResolvedValue({ messages: [], total: 0, unreadCount: 0 }); render(); await waitFor(() => { expect(screen.getByTestId("mailbox-inbox-empty")).toBeDefined(); }); }); it("calls onClose when clicking close button", async () => { const onClose = vi.fn(); render(); await waitFor(() => { expect(screen.getByTestId("mailbox-close")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-close")); expect(onClose).toHaveBeenCalledOnce(); }); it("passes projectId to API calls", async () => { render(); await waitFor(() => { expect(mockFetchInbox).toHaveBeenCalledWith({ limit: 50 }, "proj-1"); }); }); describe("mobile layout CSS regressions", () => { it("defines mailbox base flex layout for modal and content containers", async () => { const fs = await import("fs"); const path = await import("path"); const cssPath = path.resolve(__dirname, "../../styles.css"); const css = fs.readFileSync(cssPath, "utf-8"); const modalBlockMatch = css.match(/\.mailbox-modal\s*\{([^}]*)\}/); expect(modalBlockMatch).toBeTruthy(); const modalBlock = modalBlockMatch![1]; expect(modalBlock).toContain("display: flex;"); expect(modalBlock).toContain("flex-direction: column;"); const contentBlockMatch = css.match(/\.mailbox-content\s*\{([^}]*)\}/); expect(contentBlockMatch).toBeTruthy(); const contentBlock = contentBlockMatch![1]; expect(contentBlock).toContain("flex: 1;"); expect(contentBlock).toContain("min-height: 0;"); }); it("keeps mobile mailbox overrides in the dedicated media-query section", async () => { const fs = await import("fs"); const path = await import("path"); const cssPath = path.resolve(__dirname, "../../styles.css"); const css = fs.readFileSync(cssPath, "utf-8"); const sectionStart = css.indexOf("/* ── Mailbox — Mobile"); expect(sectionStart).toBeGreaterThan(-1); const sectionEnd = css.indexOf("/* ── Message Composer", sectionStart); expect(sectionEnd).toBeGreaterThan(sectionStart); const mailboxMobileSection = css.slice(sectionStart, sectionEnd); expect(mailboxMobileSection).toContain("@media (max-width: 768px)"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-header"); expect(mailboxMobileSection).toContain("flex-wrap: wrap;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-title"); expect(mailboxMobileSection).toContain("flex-shrink: 0;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-header-actions"); expect(mailboxMobileSection).toContain("overflow-x: auto;"); expect(mailboxMobileSection).toContain("-webkit-overflow-scrolling: touch;"); expect(mailboxMobileSection).toContain("scrollbar-width: none;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-tabs::-webkit-scrollbar"); expect(mailboxMobileSection).toContain("display: none;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-tab"); expect(mailboxMobileSection).toContain("padding: 8px 12px;"); expect(mailboxMobileSection).toContain("font-size: 0.8rem;"); expect(mailboxMobileSection).toContain("max-height: calc(100dvh - 120px);"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-message-detail-header"); expect(mailboxMobileSection).toContain("flex-direction: column;"); expect(mailboxMobileSection).toContain("align-items: flex-start;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-message-detail-actions"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-message-participants"); expect(mailboxMobileSection).toContain("gap: 8px;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-conversation-msg"); expect(mailboxMobileSection).toContain("padding: 6px 10px;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-agent-select"); expect(mailboxMobileSection).toContain("max-width: 100%;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-agents"); expect(mailboxMobileSection).toContain("min-height: 200px;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-compose-fab"); expect(mailboxMobileSection).toContain("bottom: 16px;"); expect(mailboxMobileSection).toContain("right: 16px;"); expect(mailboxMobileSection).toContain(".mailbox-modal .mailbox-empty"); expect(mailboxMobileSection).toContain("padding: 32px 12px;"); }); it("renders detail-view structural hooks targeted by mobile overrides", async () => { const { container } = render(); await waitFor(() => { expect(screen.getByTestId("mailbox-item-msg-001")).toBeDefined(); }); fireEvent.click(screen.getByTestId("mailbox-item-msg-001")); await waitFor(() => { expect(container.querySelector(".mailbox-message-detail-header")).toBeTruthy(); expect(container.querySelector(".mailbox-message-detail-actions")).toBeTruthy(); expect(container.querySelector(".mailbox-message-participants")).toBeTruthy(); }); }); }); describe("theme-awareness CSS regressions", () => { // Read CSS file once for all tests in this block let css: string; beforeAll(async () => { const fs = await import("fs"); const path = await import("path"); const cssPath = path.resolve(__dirname, "../../styles.css"); css = fs.readFileSync(cssPath, "utf-8"); }); it("mailbox unread badge uses theme-aware text token", () => { const blockMatch = css.match(/\.mailbox-unread-badge\s*\{([^}]*)\}/); expect(blockMatch).toBeTruthy(); expect(blockMatch![1]).toContain("var(--fab-text)"); expect(blockMatch![1]).not.toContain("color: white"); }); it("mailbox tab badge uses theme-aware text token", () => { const blockMatch = css.match(/\.mailbox-tab-badge\s*\{([^}]*)\}/); expect(blockMatch).toBeTruthy(); expect(blockMatch![1]).toContain("var(--fab-text)"); expect(blockMatch![1]).not.toContain("color: white"); }); it("mailbox compose FAB uses theme-aware tokens", () => { const blockMatch = css.match(/\.mailbox-compose-fab\s*\{([^}]*)\}/); expect(blockMatch).toBeTruthy(); expect(blockMatch![1]).toContain("var(--fab-bg)"); expect(blockMatch![1]).toContain("var(--fab-text)"); expect(blockMatch![1]).not.toContain("background: var(--todo)"); expect(blockMatch![1]).not.toContain("color: white"); // Check hover state const hoverMatch = css.match(/\.mailbox-compose-fab:hover\s*\{([^}]*)\}/); expect(hoverMatch).toBeTruthy(); expect(hoverMatch![1]).toContain("var(--fab-bg)"); expect(hoverMatch![1]).toContain("var(--fab-text)"); }); it("mission event type error uses CSS custom properties", () => { const blockMatch = css.match(/\.mission-event__type--error\s*\{([^}]*)\}/); expect(blockMatch).toBeTruthy(); expect(blockMatch![1]).toContain("var(--event-error-text)"); expect(blockMatch![1]).toContain("var(--event-error-bg)"); expect(blockMatch![1]).not.toContain("#fca5a5"); expect(blockMatch![1]).not.toContain("rgba(239, 68, 68, 0.15)"); }); it("mission autopilot pulse uses CSS custom property", () => { const blockMatch = css.match(/\.mission-detail__autopilot-pulse\s*\{([^}]*)\}/); expect(blockMatch).toBeTruthy(); expect(blockMatch![1]).toContain("var(--autopilot-pulse)"); expect(blockMatch![1]).not.toContain("#22c55e"); }); it("terminal container uses CSS custom property", () => { const blockMatch = css.match(/\.terminal-container\s*\{([^}]*)\}/); expect(blockMatch).toBeTruthy(); expect(blockMatch![1]).toContain("var(--terminal-bg)"); expect(blockMatch![1]).not.toContain("#1e1e1e"); }); it("new tokens are defined in :root", () => { // Find the :root block at the start of the file (before any other selectors) const rootStart = css.indexOf(":root {"); const afterRoot = css.slice(rootStart); // Match until we find the closing } followed by html, const rootMatch = afterRoot.match(/:root\s*\{([\s\S]*?)^}\s*\n\s*html,/m); expect(rootMatch).toBeTruthy(); const rootContent = rootMatch![1]; expect(rootContent).toContain("--autopilot-icon"); expect(rootContent).toContain("--event-error-text"); expect(rootContent).toContain("--terminal-bg"); expect(rootContent).toContain("--star-idle"); expect(rootContent).toContain("--fab-text"); expect(rootContent).toContain("--badge-mission-text"); }); it("light theme overrides new tokens", () => { // Find the base [data-theme="light"] block (not combined with other selectors) const lightBlockMatch = css.match(/^\[data-theme="light"\]\s*\{[\s\S]*?^\}\s*$/m); expect(lightBlockMatch).toBeTruthy(); const lightContent = lightBlockMatch![0]; expect(lightContent).toContain("--terminal-bg"); expect(lightContent).toContain("--event-error-text"); expect(lightContent).toContain("--autopilot-icon"); expect(lightContent).toContain("--star-active"); }); }); });