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

6.7 KB
Raw Blame
import { describe, it, expect, vi } from 'vitest';
import { act, render, screen, fireEvent, within } from '@testing-library/react';

vi.mock('../../services/api', async (importOriginal) => ({
  ...await importOriginal(),
  getInstanceFeatures: vi.fn().mockResolvedValue({ features: [], groups: [] }),
}));

import SectionNav from './SectionNav';
import { TABS, SECTION_GROUPS, groupSections, sectionGroupId } from './constants';

describe('digital-twin section taxonomy', () => {
  it('assigns every section to exactly one group', () => {
    const grouped = SECTION_GROUPS.flatMap((g) => g.sectionIds);
    expect([...grouped].sort()).toEqual(TABS.map((t) => t.id).sort());
    expect(new Set(grouped).size).toBe(grouped.length);
  });

  it('names no section id that has no tab behind it', () => {
    const ids = new Set(TABS.map((t) => t.id));
    const orphans = SECTION_GROUPS.flatMap((g) => g.sectionIds).filter((id) => !ids.has(id));
    expect(orphans).toEqual([]);
  });

  it('keeps every group materially shorter than the old flat strip', () => {
    // The point of the grouping: no single row asks the user to scan 19 labels.
    for (const g of SECTION_GROUPS) expect(g.sectionIds.length).toBeLessThanOrEqual(6);
    expect(SECTION_GROUPS.length).toBeLessThanOrEqual(6);
  });

  it('resolves a section to its owning group', () => {
    expect(sectionGroupId('documents')).toBe('sources');
    expect(sectionGroupId('time-capsule')).toBe('legacy');
    expect(sectionGroupId('personality')).toBe('assessment');
  });

  it('falls back to the first group for an unknown section', () => {
    expect(sectionGroupId('not-a-section')).toBe(SECTION_GROUPS[0].id);
  });

  it('yields an empty section list for an unknown group instead of throwing', () => {
    expect(groupSections(undefined)).toEqual([]);
    expect(groupSections({ id: 'nope' })).toEqual([]);
  });

  it('expands a group to its tab objects in declared order', () => {
    const sources = groupSections(SECTION_GROUPS.find((g) => g.id === 'sources'));
    expect(sources.map((t) => t.id)).toEqual(['documents', 'import', 'accounts', 'interview', 'autobiography', 'enrich']);
    expect(sources.every((t) => t.label && t.icon)).toBe(true);
  });
});

// "Legacy" names both a group and a section inside it, so every tab lookup is
// scoped to its own row rather than searched across the whole nav.
const groupTab = (name) => within(screen.getByRole('tablist', { name: 'Digital Twin groups' })).getByRole('tab', { name });

describe('SectionNav', () => {
  it('derives the active group from the section in the URL, with no local state', async () => {
    const { rerender } = render(<SectionNav activeSection="documents" onChange={() => {}} />);
    await act(async () => {});
    expect(groupTab('Sources').getAttribute('aria-selected')).toBe('true');
    const sourcesRow = screen.getByRole('tablist', { name: 'Sources sections' });
    expect(within(sourcesRow).getByRole('tab', { name: /^Documents/ }).getAttribute('aria-selected')).toBe('true');

    // Changing only the prop (i.e. the route param) moves the group with it.
    rerender(<SectionNav activeSection="time-capsule" onChange={() => {}} />);
    expect(groupTab('Legacy').getAttribute('aria-selected')).toBe('true');
    expect(groupTab('Sources').getAttribute('aria-selected')).toBe('false');
  });

  it('shows only the active group’s sections, not all 19', () => {
    render(<SectionNav activeSection="voice" onChange={() => {}} />);
    const sectionRow = screen.getByRole('tablist', { name: 'Presence sections' });
    expect(within(sectionRow).getAllByRole('tab').map((b) => b.textContent)).toEqual(['Voice', 'Appearance', 'Avatar Bio']);
    expect(within(sectionRow).queryByRole('tab', { name: 'Documents' })).toBeNull();
  });

  it('navigates to a group’s first section when the group changes', () => {
    const onChange = vi.fn();
    render(<SectionNav activeSection="overview" onChange={onChange} />);
    fireEvent.click(groupTab('Assessment'));
    expect(onChange).toHaveBeenCalledWith('test');
  });

  it('does not re-navigate when the active section already lives in the clicked group', () => {
    const onChange = vi.fn();
    render(<SectionNav activeSection="taste" onChange={onChange} />);
    fireEvent.click(groupTab('Profile'));
    expect(onChange).not.toHaveBeenCalled();
  });

  it('reports the picked section id when a section is clicked', () => {
    const onChange = vi.fn();
    render(<SectionNav activeSection="documents" onChange={onChange} />);
    const sourcesRow = screen.getByRole('tablist', { name: 'Sources sections' });
    fireEvent.click(within(sourcesRow).getByRole('tab', { name: /^Interview/ }));
    expect(onChange).toHaveBeenCalledWith('interview');
  });

  it('collapses both rows to icon links on mobile rather than a select', () => {
    render(<SectionNav activeSection="overview" onChange={() => {}} />);
    expect(screen.queryByRole('combobox')).toBeNull();

    // Compact mode hides each label visually below `sm` but keeps it in the
    // accessible name, so the icon row is still one tablist of named tabs.
    for (const row of ['Digital Twin groups', 'Profile sections']) {
      for (const tab of within(screen.getByRole('tablist', { name: row })).getAllByRole('tab')) {
        expect(tab.querySelector('svg')).toBeTruthy();
        expect(tab.querySelector('.max-sm\\:sr-only')).toBeTruthy();
      }
    }
    expect(groupTab('Profile')).toHaveAccessibleName('Profile');
  });
});

// The section strip is a browse surface, so it applies the same feature gate the
// sidebar and ⌘K do (client/src/lib/navFeatures.js) — the `feature` tag rides
// through getPageNavTabs onto these tab objects, exactly as it does for the
// Messages strip. Mocked per-test rather than globally so the default-on case
// above keeps exercising the real hook's "no override" answer.
describe('SectionNav feature gating', () => {
  it('drops a section whose feature is off, and keeps it when the feature is on', async () => {
    vi.resetModules();
    vi.doMock('../../hooks/useInstanceFeatures', () => ({
      useInstanceFeatures: () => ({ isFeatureEnabled: (id) => id !== 'autobiography' })
    }));
    const Gated = (await import('./SectionNav')).default;

    render(<Gated activeSection="documents" onChange={() => {}} />);
    const row = screen.getByRole('tablist', { name: 'Sources sections' });
    expect(within(row).queryByRole('tab', { name: /Autobiography/ })).toBeNull();
    // The gate must be scoped to the tagged section — an untagged sibling in the
    // same group still renders, so this isn't just an empty strip.
    expect(within(row).getByRole('tab', { name: /^Interview/ })).toBeTruthy();

    vi.doUnmock('../../hooks/useInstanceFeatures');
    vi.resetModules();
  });
});