Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

12.0 KB
Raw Blame
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent, within } from '@testing-library/react';
import Drawer from './Drawer';
import Modal from './ui/Modal';

const TABS = [
  { id: 'general', label: 'General' },
  { id: 'ports', label: 'Ports & TLS' },
  { id: 'jira', label: 'JIRA' },
];

describe('Drawer', () => {
  it('renders nothing when closed', () => {
    const { container } = render(<Drawer open={false} onClose={() => {}} title="X">body</Drawer>);
    expect(container).toBeEmptyDOMElement();
  });

  it('renders title, children and an accessible dialog when open', () => {
    render(<Drawer open onClose={() => {}} title="Edit App"><p>form body</p></Drawer>);
    const dialog = screen.getByRole('dialog');
    expect(dialog).toHaveAttribute('aria-modal', 'true');
    expect(dialog).toHaveAttribute('aria-label', 'Edit App');
    expect(screen.getByText('form body')).toBeInTheDocument();
  });

  it('renders an optional subtitle as a second header line', () => {
    render(<Drawer open onClose={() => {}} title="Sync Details" subtitle="My Collection">b</Drawer>);
    // subtitle is a header concern (not smuggled into the scrollable body)
    expect(screen.getByText('My Collection')).toBeInTheDocument();
    // aria-label still reflects only the title, not the subtitle
    expect(screen.getByRole('dialog')).toHaveAttribute('aria-label', 'Sync Details');
  });

  it('omits the subtitle line when none is provided', () => {
    render(<Drawer open onClose={() => {}} title="X">b</Drawer>);
    // header holds only the title (an <h2>), no stray <p>
    expect(screen.getByRole('heading', { name: 'X' })).toBeInTheDocument();
  });

  describe('size variants (back-compat)', () => {
    it('defaults to the original sm (~520px) width', () => {
      render(<Drawer open onClose={() => {}} title="X">b</Drawer>);
      expect(screen.getByRole('dialog').className).toContain('sm:w-[520px]');
    });

    it('applies wider brackets for lg/xl', () => {
      const { rerender } = render(<Drawer open onClose={() => {}} title="X" size="lg">b</Drawer>);
      expect(screen.getByRole('dialog').className).toContain('lg:w-[880px]');
      rerender(<Drawer open onClose={() => {}} title="X" size="xl">b</Drawer>);
      expect(screen.getByRole('dialog').className).toContain('xl:w-[1100px]');
    });

    it('honors an explicit widthClass escape hatch over size', () => {
      render(<Drawer open onClose={() => {}} title="X" size="lg" widthClass="sm:w-[640px]">b</Drawer>);
      const cls = screen.getByRole('dialog').className;
      expect(cls).toContain('sm:w-[640px]');
      expect(cls).not.toContain('lg:w-[880px]');
    });
  });

  describe('close behavior', () => {
    it('closes on backdrop click and Esc by default', () => {
      const onClose = vi.fn();
      render(<Drawer open onClose={onClose} title="X">b</Drawer>);
      fireEvent.click(document.querySelector('[aria-hidden="true"]'));
      fireEvent.keyDown(window, { key: 'Escape' });
      expect(onClose).toHaveBeenCalledTimes(2);
    });

    it('opts out of Esc and backdrop dismissal for long-lived forms', () => {
      const onClose = vi.fn();
      render(<Drawer open onClose={onClose} title="X" closeOnEsc={false} closeOnBackdrop={false}>b</Drawer>);
      fireEvent.click(document.querySelector('[aria-hidden="true"]'));
      fireEvent.keyDown(window, { key: 'Escape' });
      expect(onClose).not.toHaveBeenCalled();
    });

    it('leaves the drawer open when an inner widget consumed Escape via preventDefault()', () => {
      // EntityCombobox / native <select> close their own popover on Esc and
      // call preventDefault(); the drawer (and the form it holds) must survive.
      const onClose = vi.fn();
      render(
        <Drawer open onClose={onClose} title="X">
          <input aria-label="popover" onKeyDown={(e) => { if (e.key === 'Escape') e.preventDefault(); }} />
          <input aria-label="plain" />
        </Drawer>
      );
      fireEvent.keyDown(screen.getByLabelText('popover'), { key: 'Escape' });
      expect(onClose).not.toHaveBeenCalled();
      // A plain Escape from a field that doesn't own the key still closes.
      fireEvent.keyDown(screen.getByLabelText('plain'), { key: 'Escape' });
      expect(onClose).toHaveBeenCalledTimes(1);
    });

    it('shares the Esc stack with Modal so nested surfaces close innermost-first', () => {
      const closeDrawer = vi.fn();
      const closeModal = vi.fn();
      // The confirm modal opens from inside the already-open drawer, as a
      // user action would — it lands on top of the stack.
      const tree = (modalOpen) => (
        <Drawer open onClose={closeDrawer} title="X">
          <Modal open={modalOpen} onClose={closeModal} ariaLabel="confirm"><p>sure?</p></Modal>
        </Drawer>
      );
      const { rerender } = render(tree(false));
      rerender(tree(true));
      fireEvent.keyDown(window, { key: 'Escape' });
      expect(closeModal).toHaveBeenCalledTimes(1);
      expect(closeDrawer).not.toHaveBeenCalled();

      rerender(tree(false));
      fireEvent.keyDown(window, { key: 'Escape' });
      expect(closeDrawer).toHaveBeenCalledTimes(1);
      expect(closeModal).toHaveBeenCalledTimes(1);
    });

    it('absorbs Escape at a closeOnEsc=false drawer instead of dismissing the layer beneath', () => {
      const closeDrawer = vi.fn();
      const closeModal = vi.fn();
      render(
        <>
          <Modal open onClose={closeModal} ariaLabel="base"><p>base</p></Modal>
          <Drawer open onClose={closeDrawer} title="X" closeOnEsc={false}>b</Drawer>
        </>
      );
      fireEvent.keyDown(window, { key: 'Escape' });
      expect(closeDrawer).not.toHaveBeenCalled();
      expect(closeModal).not.toHaveBeenCalled();
    });

    it('fires onClose from the header close button', () => {
      const onClose = vi.fn();
      render(<Drawer open onClose={onClose} title="X">b</Drawer>);
      fireEvent.click(screen.getByRole('button', { name: 'Close settings' }));
      expect(onClose).toHaveBeenCalledTimes(1);
    });
  });

  describe('tabbed layout', () => {
    it('renders no tablist when tabs are omitted', () => {
      render(<Drawer open onClose={() => {}} title="X">b</Drawer>);
      expect(screen.queryByRole('tablist')).not.toBeInTheDocument();
      expect(screen.getByText('b').closest('[role="tabpanel"]')).toBeNull();
    });

    it('renders a tablist and a labelled tabpanel when tabs are provided (uncontrolled)', () => {
      render(<Drawer open onClose={() => {}} title="Edit App" tabs={TABS}><div>panel body</div></Drawer>);
      expect(screen.getByRole('tablist')).toBeInTheDocument();
      const panel = screen.getByRole('tabpanel');
      expect(within(panel).getByText('panel body')).toBeInTheDocument();
      // defaults to the first tab
      expect(panel).toHaveAttribute('id', 'drawer-tabpanel-general');
    });

    it('switches tabs internally when uncontrolled', () => {
      render(<Drawer open onClose={() => {}} title="X" tabs={TABS}>body</Drawer>);
      // TabPills renders a mobile <select> plus the desktop tab buttons.
      fireEvent.click(screen.getByRole('tab', { name: 'JIRA' }));
      expect(screen.getByRole('tabpanel')).toHaveAttribute('id', 'drawer-tabpanel-jira');
    });

    it('renders the mobile <select> fallback for tabbed drawers', () => {
      // The drawer uses the default underline variant; the mobile <select>
      // collapse must still render (regression guard for the variant-agnostic
      // mobileCompact wiring).
      render(<Drawer open onClose={() => {}} title="X" tabs={TABS} activeTab="ports" onTabChange={() => {}}>body</Drawer>);
      const select = screen.getByRole('combobox');
      expect(select).toHaveAttribute('id', 'drawer-tab-select');
      expect(select).toHaveValue('ports');
    });

    it('is controllable via activeTab + onTabChange', () => {
      const onTabChange = vi.fn();
      const { rerender } = render(
        <Drawer open onClose={() => {}} title="X" tabs={TABS} activeTab="ports" onTabChange={onTabChange}>body</Drawer>
      );
      expect(screen.getByRole('tabpanel')).toHaveAttribute('id', 'drawer-tabpanel-ports');
      fireEvent.click(screen.getByRole('tab', { name: 'JIRA' }));
      expect(onTabChange).toHaveBeenCalledWith('jira');
      // controlled: nothing changes until the parent updates activeTab
      expect(screen.getByRole('tabpanel')).toHaveAttribute('id', 'drawer-tabpanel-ports');
      rerender(<Drawer open onClose={() => {}} title="X" tabs={TABS} activeTab="jira" onTabChange={onTabChange}>body</Drawer>);
      expect(screen.getByRole('tabpanel')).toHaveAttribute('id', 'drawer-tabpanel-jira');
    });

    it('applies bodyClassName to the scroll region for multi-column layouts', () => {
      render(<Drawer open onClose={() => {}} title="X" tabs={TABS} bodyClassName="lg:grid lg:grid-cols-2">body</Drawer>);
      expect(screen.getByRole('tabpanel').className).toContain('lg:grid-cols-2');
    });
  });

  describe('portaled overlay', () => {
    // A `backdrop-filter` ancestor becomes the containing block for
    // position:fixed descendants. On the "glass" themes index.css gives every
    // bordered/rounded `.bg-port-card` a backdrop-filter, so an inline drawer
    // nested under such a card was sized to the card, not the viewport.
    const renderInGlassCard = (props = {}) => render(
      <div style={{ backdropFilter: 'blur(22px)' }} data-testid="glass-card">
        <Drawer open onClose={() => {}} title="Edit App" {...props}><p>form body</p></Drawer>
      </div>
    );

    it('portals both the backdrop and the panel out of a backdrop-filter ancestor', () => {
      const { container } = renderInGlassCard();

      const glassCard = screen.getByTestId('glass-card');
      const dialog = screen.getByRole('dialog');
      const backdrop = document.querySelector('[aria-hidden="true"]');

      expect(glassCard.contains(dialog)).toBe(false);
      // The backdrop is the trapped element the bug is named for — it must
      // escape too, or it renders as a card-sized scrim instead of covering
      // the viewport. Assert it EXISTS first: `Node.contains(null)` is `false`,
      // so the escape assertion below would pass vacuously if the backdrop were
      // dropped altogether.
      expect(backdrop).not.toBeNull();
      expect(glassCard.contains(backdrop)).toBe(false);
      expect(container.querySelector('[role="dialog"]')).toBeNull();
      expect(container.querySelector('[aria-hidden="true"]')).toBeNull();
      // The glass card keeps only its own (empty) subtree.
      expect(glassCard).toBeEmptyDOMElement();
    });

    it('keeps backdrop + Esc dismissal working through the portal', () => {
      const onClose = vi.fn();
      render(
        <div style={{ backdropFilter: 'blur(22px)' }}>
          <Drawer open onClose={onClose} title="X">b</Drawer>
        </div>
      );
      fireEvent.click(document.querySelector('[aria-hidden="true"]'));
      fireEvent.keyDown(window, { key: 'Escape' });
      expect(onClose).toHaveBeenCalledTimes(2);
    });

    it('portals straight into <body> with no wrapper element by default', () => {
      renderInGlassCard();
      // Backdrop and panel are direct children of <body> — matching the shared
      // Modal's portal shape.
      expect(screen.getByRole('dialog').parentElement).toBe(document.body);
      expect(document.querySelector('[aria-hidden="true"]').parentElement).toBe(document.body);
    });

    it('re-applies a page-scoped theming class inside the portal via portalClassName', () => {
      // The OpenWorld HUD palette remap in index.css is written as
      // `.openworld-themed <utility>` descendant selectors, which the portal
      // would otherwise leave behind.
      renderInGlassCard({ portalClassName: 'openworld-themed' });

      const wrapper = screen.getByRole('dialog').parentElement;
      expect(wrapper.className).toBe('openworld-themed');
      expect(wrapper.parentElement).toBe(document.body);
      // The backdrop shares the themed wrapper so the scrim is remapped too.
      expect(wrapper.contains(document.querySelector('[aria-hidden="true"]'))).toBe(true);
    });
  });
});