Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.2 KB
Raw Blame
/**
 * @vitest-environment happy-dom
 */
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { MIN_PANEL_HEIGHT, useDevDockStore } from './store';

const STORAGE_KEY = 'LOBE_DEV_DOCK_UI';

const readPersisted = () => JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}');

describe('DevDock store', () => {
  beforeEach(() => {
    localStorage.clear();
    useDevDockStore.setState({
      activePanelId: null,
      expanded: true,
      maximized: false,
      mesurer: false,
      panelHeight: 360,
      pinOverrides: {},
      reactScan: false,
      scrollDebug: false,
    });
  });

  it('toggles a panel open and closed', () => {
    useDevDockStore.getState().togglePanel('feature-flags');
    expect(useDevDockStore.getState().activePanelId).toBe('feature-flags');

    useDevDockStore.getState().togglePanel('feature-flags');
    expect(useDevDockStore.getState().activePanelId).toBeNull();
  });

  it('switches between panels directly', () => {
    useDevDockStore.getState().togglePanel('feature-flags');
    useDevDockStore.getState().togglePanel('agent-mock');
    expect(useDevDockStore.getState().activePanelId).toBe('agent-mock');
  });

  it('clamps panel height to the minimum', () => {
    useDevDockStore.getState().setPanelHeight(10);
    expect(useDevDockStore.getState().panelHeight).toBe(MIN_PANEL_HEIGHT);
  });

  it('persists UI state to localStorage', () => {
    useDevDockStore.getState().setExpanded(false);
    useDevDockStore.getState().setMesurer(true);
    useDevDockStore.getState().setScrollDebug(true);
    useDevDockStore.getState().setReactScan(true);

    expect(readPersisted()).toMatchObject({
      expanded: false,
      mesurer: true,
      reactScan: true,
      scrollDebug: true,
    });
  });

  it('merges pin overrides per id and persists them', () => {
    useDevDockStore.getState().setPinned('reload', true);
    useDevDockStore.getState().setPinned('fps', false);

    expect(useDevDockStore.getState().pinOverrides).toEqual({ fps: false, reload: true });
    expect(readPersisted().pinOverrides).toEqual({ fps: false, reload: true });
  });

  it('flips an existing override without dropping others', () => {
    useDevDockStore.getState().setPinned('reload', true);
    useDevDockStore.getState().setPinned('reload', false);

    expect(useDevDockStore.getState().pinOverrides).toEqual({ reload: false });
  });

  it('never writes the open panel to storage', () => {
    useDevDockStore.getState().togglePanel('local-database');
    useDevDockStore.getState().togglePanel('feature-flags');

    expect(readPersisted()).not.toHaveProperty('activePanelId');
  });

  it('restores persisted state but boots with no panel open', async () => {
    localStorage.setItem(
      STORAGE_KEY,
      JSON.stringify({ activePanelId: 'local-database', expanded: false, panelHeight: 500 }),
    );

    vi.resetModules();
    // Boot-time state only exists at module init, so the store has to be re-imported.
    const { useDevDockStore: bootedStore } = await import('./store');

    expect(bootedStore.getState().activePanelId).toBeNull();
    expect(bootedStore.getState().expanded).toBe(false);
    expect(bootedStore.getState().panelHeight).toBe(500);
  });
});