Skip to content

The agent that grows with you

8.1 KB
Raw Blame
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import type { ReactNode } from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'

import type { SessionInfo } from '@/hermes'
import type * as ProjectsStore from '@/store/projects'

import type * as Model from './model'
import { ProjectOverviewRow } from './overview-row'
import type { SidebarProjectTree } from './workspace-groups'

afterEach(cleanup)

const workspaceOpen = vi.hoisted(() => ({ value: false }))

const projectsStore = vi.hoisted(() => ({
  fetchProjectSessions:
    vi.fn<(id: string, options?: { supersedable?: boolean }) => Promise<null | SidebarProjectTree>>(),
  projectProfile: vi.fn<() => null | string>(() => 'default')
}))

vi.mock('@/i18n', () => ({
  useI18n: () => ({
    t: {
      sidebar: {
        newSessionIn: (label: string) => `New session in ${label}`,
        projects: {
          enter: (label: string) => `Enter ${label}`,
          reorder: (label: string) => `Reorder ${label}`,
          toggle: (label: string, open: boolean) => `${open ? 'Show' : 'Hide'} ${label} sessions`,
          showAllCount: (count: number) => `Show all ${count} sessions`,
          autoDiscovered: 'Auto-discovered'
        },
        showMoreIn: (count: number, label: string) => `Show ${count} more in ${label}`
      }
    }
  })
}))

vi.mock('@/store/projects', async importOriginal => ({
  ...(await importOriginal<typeof ProjectsStore>()),
  ...projectsStore
}))

// Keep the pure helpers real (they are the logic under test); stub only the
// persisted open/collapse hook and the in-memory fallback preview.
vi.mock('./model', async () => ({
  ...(await vi.importActual<typeof Model>('./model')),
  latestProjectSessions: () => [],
  useWorkspaceNodeOpen: () => [workspaceOpen.value, vi.fn()]
}))

// ProjectMenu (the kebab) has its own dedicated test file — stub it here so
// this file only exercises overview-row's own Tip usage (the disclosure
// toggle) plus the WorkspaceAddButton wiring. ProjectContextMenu (the row's
// right-click wrapper) is stubbed as a pass-through so the row still renders.
vi.mock('./project-menu', () => ({
  ProjectContextMenu: ({ children }: { children: ReactNode }) => children,
  ProjectMenu: () => null
}))

const project = { id: 'p1', label: 'Test D' } as unknown as SidebarProjectTree

const session = (id: string, updated: number): SessionInfo => ({ id, updated_at: updated }) as unknown as SessionInfo

describe('ProjectOverviewRow', () => {
  afterEach(() => {
    workspaceOpen.value = false
    projectsStore.fetchProjectSessions.mockReset()
    projectsStore.projectProfile.mockReset().mockReturnValue('default')
  })

  it('does not render the disclosure toggle when there is nothing to preview', () => {
    render(<ProjectOverviewRow project={project} />)

    expect(screen.queryByRole('button', { name: 'Show Test D sessions' })).toBeNull()
  })

  // Group by → Projects previews only the 3 most-recent sessions per project;
  // sessions 4+ need a visible, in-place way to be reached (#112406).
  it('offers "Show all N sessions" past the preview cap and reveals the rest of the project inline', async () => {
    workspaceOpen.value = true
    const five = Array.from({ length: 5 }, (_, index) => session(`s${index + 1}`, 500 - index))
    const busy = { ...project, sessionCount: 5 } as SidebarProjectTree
    projectsStore.fetchProjectSessions.mockResolvedValue({
      ...busy,
      repos: [{ groups: [{ sessions: five }] }]
    } as unknown as SidebarProjectTree)

    render(
      <ProjectOverviewRow
        previewSessions={five.slice(0, 3)}
        project={busy}
        renderRows={items => <div data-testid="rows">{items.map(item => item.id).join(',')}</div>}
      />
    )

    expect(screen.getByTestId('rows').textContent).toBe('s1,s2,s3')

    fireEvent.click(screen.getByRole('button', { name: 'Show all 5 sessions' }))

    await waitFor(() => expect(screen.getByTestId('rows').textContent).toBe('s1,s2,s3,s4,s5'))
    expect(projectsStore.fetchProjectSessions).toHaveBeenCalledWith('p1', { supersedable: false })
    expect(screen.queryByRole('button', { name: 'Show all 5 sessions' })).toBeNull()
  })

  // A project with hundreds of chats hydrates them all, but the overview must
  // not mount every row at once: it reveals them a page at a time, with a
  // labeled row to the next page, until every session is on screen (#70421).
  it('pages a large hydrated project instead of mounting every session at once', async () => {
    workspaceOpen.value = true
    const all = Array.from({ length: 120 }, (_, index) => session(`s${index + 1}`, 1000 - index))
    const busy = { ...project, sessionCount: 120 } as SidebarProjectTree
    projectsStore.fetchProjectSessions.mockResolvedValue({
      ...busy,
      repos: [{ groups: [{ sessions: all }] }]
    } as unknown as SidebarProjectTree)

    render(
      <ProjectOverviewRow
        previewSessions={all.slice(0, 3)}
        project={busy}
        renderRows={items => <div data-testid="rows">{items.map(item => item.id).join(',')}</div>}
      />
    )

    const shown = () => screen.getByTestId('rows').textContent?.split(',').length

    fireEvent.click(screen.getByText('Show all 120 sessions'))

    await waitFor(() => expect(shown()).toBe(50))
    fireEvent.click(screen.getByText('Show 50 more in Test D'))
    expect(shown()).toBe(100)
    fireEvent.click(screen.getByText('Show 20 more in Test D'))
    expect(shown()).toBe(120)
    expect(screen.queryByText(/Show .* more in Test D/)).toBeNull()
  })

  // The hydrated lanes are the raw backend payload: pinned, filtered-out and
  // just-deleted sessions must go through the same exclusion the previews did,
  // and N must not promise rows the view hides.
  it('"Show all" runs the hydrated lanes through the tree exclusion and counts only what it will render', async () => {
    workspaceOpen.value = true
    const five = Array.from({ length: 5 }, (_, index) => session(`s${index + 1}`, 500 - index))
    const busy = { ...project, sessionCount: 5 } as SidebarProjectTree
    projectsStore.fetchProjectSessions.mockResolvedValue({
      ...busy,
      repos: [{ groups: [{ sessions: five }] }]
    } as unknown as SidebarProjectTree)
    // s2 is pinned (renders in Pinned), s5 was just deleted.
    const hidden = new Set(['s2', 's5'])

    render(
      <ProjectOverviewRow
        hiddenSessionCount={hidden.size}
        isSessionHidden={item => hidden.has(item.id)}
        previewSessions={[five[0], five[2]]}
        project={busy}
        renderRows={items => <div data-testid="rows">{items.map(item => item.id).join(',')}</div>}
      />
    )

    fireEvent.click(screen.getByRole('button', { name: 'Show all 3 sessions' }))

    await waitFor(() => expect(screen.getByTestId('rows').textContent).toBe('s1,s3,s4'))
  })

  it('offers the "new session" add button on Home, which starts one with no folder', () => {
    const home = {
      id: '__no_project__',
      isNoProject: true,
      label: 'Home',
      path: null
    } as unknown as SidebarProjectTree

    const onNewSession = vi.fn()

    render(<ProjectOverviewRow onNewSession={onNewSession} project={home} />)
    fireEvent.click(screen.getByRole('button', { name: 'New session in Home' }))

    expect(onNewSession).toHaveBeenCalledWith(null)
  })

  // #124808: a real project whose primary_path was never set (multi-folder /
  // path-less explicit project) still carries repo roots. Its trunk "+" must
  // anchor the new session at the first repo root, not pass the null wire
  // path through — null is the reserved Home/detached signal downstream, so
  // the click silently created a global detached session.
  it('anchors the trunk "+" at the first repo root when the project has no primary path', () => {
    const multi = {
      id: 'p_multi',
      label: 'Multi',
      path: null,
      repos: [{ id: 'r1', label: 'app', path: '/work/app', groups: [], sessionCount: 0 }],
      sessionCount: 0
    } as unknown as SidebarProjectTree

    const onNewSession = vi.fn()

    render(<ProjectOverviewRow onNewSession={onNewSession} project={multi} />)
    fireEvent.click(screen.getByRole('button', { name: 'New session in Multi' }))

    expect(onNewSession).toHaveBeenCalledWith('/work/app')
  })
})