Skip to content

The agent that grows with you

12.4 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest'

const focusOpenSession = vi.fn()
const frontMainIfSelected = vi.fn((storedSessionId: string) => false)
const openSessionTile = vi.fn()
const reuseBlankDraftTile = vi.fn()
const setSessionTileWorkspaceScope = vi.fn()

const focusedSessionWorkspaceScope = vi.fn<() => { workspaceMode: 'bots' | 'sessions'; workspaceOwnerKey?: string }>(
  () => ({ workspaceMode: 'sessions' })
)

const openSessionInNewWindow = vi.fn()
const canOpenSessionWindow = vi.fn(() => true)
const workspaceIsPageGet = vi.fn(() => false)

vi.mock('@/store/session-states', () => ({
  focusedSessionNeedsRoute: (focused: 'main' | 'tile' | null, workspaceIsPage: boolean) =>
    !focused || (focused === 'main' && workspaceIsPage),
  focusedSessionWorkspaceScope: () => focusedSessionWorkspaceScope(),
  focusOpenSession: (...args: unknown[]) => focusOpenSession(...args),
  frontMainIfSelected: (storedSessionId: string) => frontMainIfSelected(storedSessionId),
  openSessionTile: (...args: unknown[]) => openSessionTile(...args),
  reuseBlankDraftTile: (...args: unknown[]) => reuseBlankDraftTile(...args),
  setSessionTileWorkspaceScope: (...args: unknown[]) => setSessionTileWorkspaceScope(...args)
}))

vi.mock('@/store/windows', () => ({
  canOpenSessionWindow: () => canOpenSessionWindow(),
  openSessionInNewWindow: (...args: unknown[]) => openSessionInNewWindow(...args)
}))

vi.mock('./routes', () => ({
  $workspaceIsPage: { get: () => workspaceIsPageGet() },
  sessionRoute: (id: string) => `/c/${encodeURIComponent(id)}`
}))

import { $activeSessionId, $selectedStoredSessionId, $sessionResumeRequest } from '@/store/session'

import { openSession, openSessionFromPicker, openSessionIntentFromModifiers } from './open-session'

describe('openSessionIntentFromModifiers', () => {
  it('defaults to in-place', () => {
    expect(openSessionIntentFromModifiers()).toBe('in-place')
    expect(openSessionIntentFromModifiers(null)).toBe('in-place')
    expect(openSessionIntentFromModifiers({})).toBe('in-place')
  })

  it('returns the caller base for an unmodified select', () => {
    expect(openSessionIntentFromModifiers(undefined, 'stack')).toBe('stack')
    expect(openSessionIntentFromModifiers({}, 'stack')).toBe('stack')
  })

  it('reads ⌘/⌃ as tab and ⇧+mod as window', () => {
    expect(openSessionIntentFromModifiers({ metaKey: true })).toBe('tab')
    expect(openSessionIntentFromModifiers({ ctrlKey: true })).toBe('tab')
    expect(openSessionIntentFromModifiers({ metaKey: true, shiftKey: true })).toBe('window')
    expect(openSessionIntentFromModifiers({ shiftKey: true })).toBe('in-place')
  })

  it('lets modifiers override the base', () => {
    expect(openSessionIntentFromModifiers({ metaKey: true }, 'stack')).toBe('tab')
    expect(openSessionIntentFromModifiers({ metaKey: true, shiftKey: true }, 'stack')).toBe('window')
  })
})

describe('openSession', () => {
  const navigate = vi.fn()

  beforeEach(() => {
    navigate.mockClear()
    focusOpenSession.mockReset()
    frontMainIfSelected.mockClear()
    frontMainIfSelected.mockReturnValue(false)
    openSessionTile.mockReset()
    openSessionInNewWindow.mockReset()
    canOpenSessionWindow.mockReturnValue(true)
    workspaceIsPageGet.mockReturnValue(false)
    reuseBlankDraftTile.mockReset()
    setSessionTileWorkspaceScope.mockReset()
    focusedSessionWorkspaceScope.mockReset()
    focusedSessionWorkspaceScope.mockReturnValue({ workspaceMode: 'sessions' })
    $activeSessionId.set(null)
    $selectedStoredSessionId.set(null)
    $sessionResumeRequest.set(null)
  })

  it('in-place focuses an existing tile and does not navigate', () => {
    focusOpenSession.mockReturnValue('tile')
    openSession('s1', navigate)
    expect(focusOpenSession).toHaveBeenCalledWith('s1', { workspaceMode: 'sessions' })
    expect(navigate).not.toHaveBeenCalled()
    expect(openSessionTile).not.toHaveBeenCalled()
  })

  it('in-place focuses main when already selected and not on a page', () => {
    focusOpenSession.mockReturnValue('main')
    openSession('s1', navigate)
    expect(navigate).not.toHaveBeenCalled()
  })

  it('in-place routes when the main session is covered by a page', () => {
    focusOpenSession.mockReturnValue('main')
    workspaceIsPageGet.mockReturnValue(true)
    openSession('s1', navigate)
    expect(navigate).toHaveBeenCalledWith('/c/s1')
  })

  it('in-place routes when the session is not on screen', () => {
    focusOpenSession.mockReturnValue(null)
    openSession('s1', navigate)
    expect(navigate).toHaveBeenCalledWith('/c/s1')
  })

  it('in-place fronts main when a Bot-scoped open targets the chat main already holds', () => {
    // #125899: the bot lost its tile (closing main promoted it into the
    // workspace pane) and another bot's tab is active. focusOpenSession won't
    // claim 'main' for a Bot scope, and the route already points at the chat
    // so navigating changes nothing — only the explicit front makes the
    // roster click visible.
    const scope = { workspaceMode: 'bots' as const, workspaceOwnerKey: 'bot:local::emp' }
    $selectedStoredSessionId.set('s1')
    focusOpenSession.mockReturnValue(null)
    frontMainIfSelected.mockReturnValue(true)

    openSession('s1', navigate, 'in-place', scope)

    expect(navigate).toHaveBeenCalledWith('/c/s1')
    expect(frontMainIfSelected).toHaveBeenCalledWith('s1')
    expect(openSessionTile).not.toHaveBeenCalled()
  })

  it('in-place skips the main front when focus already landed somewhere', () => {
    focusOpenSession.mockReturnValue('tile')
    openSession('s1', navigate)
    expect(frontMainIfSelected).not.toHaveBeenCalled()
  })

  it('main routes to the workspace even when the session is already open as a tile', () => {
    focusOpenSession.mockReturnValue('tile')
    openSession('s1', navigate, 'main')
    expect(navigate).toHaveBeenCalledWith('/c/s1')
    expect(focusOpenSession).not.toHaveBeenCalled()
    expect(openSessionTile).not.toHaveBeenCalled()
  })

  it('tab focuses an existing open session instead of stacking another', () => {
    focusOpenSession.mockReturnValue('tile')
    openSession('s1', navigate, 'tab')
    expect(focusOpenSession).toHaveBeenCalledWith('s1', { workspaceMode: 'sessions' })
    expect(openSessionTile).not.toHaveBeenCalled()
    expect(navigate).not.toHaveBeenCalled()
  })

  it('tab opens a stacked session tile when not on screen', () => {
    focusOpenSession.mockReturnValue(null)
    openSession('s1', navigate, 'tab')
    expect(openSessionTile).toHaveBeenCalledWith('s1', 'center')
    expect(navigate).not.toHaveBeenCalled()
  })

  it('threads an exact Bot owner into a new session tile', () => {
    const scope = { workspaceMode: 'bots' as const, workspaceOwnerKey: 'connection-a::default' }
    focusOpenSession.mockReturnValue(null)

    openSession('s1', navigate, 'tab', scope)

    expect(setSessionTileWorkspaceScope).toHaveBeenCalledWith('s1', scope)
    expect(focusOpenSession).toHaveBeenCalledWith('s1', scope)
    expect(openSessionTile).toHaveBeenCalledWith('s1', 'center', undefined, undefined, scope)
  })

  it('stack focuses a session that is already on screen', () => {
    $selectedStoredSessionId.set('s0')
    focusOpenSession.mockReturnValue('tile')
    openSession('s1', navigate, 'stack')
    expect(openSessionTile).not.toHaveBeenCalled()
    expect(navigate).not.toHaveBeenCalled()
  })

  it.each(['stack', 'tab'] as const)('%s uncovers the existing main chat when a page is showing', intent => {
    $selectedStoredSessionId.set('s1')
    focusOpenSession.mockReturnValue('main')
    workspaceIsPageGet.mockReturnValue(true)

    openSession('s1', navigate, intent)

    expect(navigate).toHaveBeenCalledWith('/c/s1')
    expect(openSessionTile).not.toHaveBeenCalled()
  })

  it('stack opens a tab rather than taking main from a loaded chat', () => {
    $selectedStoredSessionId.set('s0')
    focusOpenSession.mockReturnValue(null)
    openSession('s1', navigate, 'stack')
    expect(openSessionTile).toHaveBeenCalledWith('s1', 'center')
    expect(navigate).not.toHaveBeenCalled()
  })

  it('stack spends an open blank draft tab before stacking a new one', () => {
    $selectedStoredSessionId.set('s0')
    focusOpenSession.mockReturnValue(null)
    reuseBlankDraftTile.mockReturnValue(true)
    openSession('s1', navigate, 'stack')
    expect(reuseBlankDraftTile).toHaveBeenCalledWith('s1')
    expect(openSessionTile).not.toHaveBeenCalled()
    expect(navigate).not.toHaveBeenCalled()
  })

  it('stack prefers the session already on screen over a blank draft tab', () => {
    $selectedStoredSessionId.set('s0')
    focusOpenSession.mockReturnValue('tile')
    reuseBlankDraftTile.mockReturnValue(true)
    openSession('s1', navigate, 'stack')
    expect(reuseBlankDraftTile).not.toHaveBeenCalled()
    expect(openSessionTile).not.toHaveBeenCalled()
  })

  it('stack opens a tab while main is mid-turn on an unsaved session', () => {
    $activeSessionId.set('runtime-a')
    focusOpenSession.mockReturnValue(null)
    openSession('s1', navigate, 'stack')
    expect(openSessionTile).toHaveBeenCalledWith('s1', 'center')
  })

  it('stack loads into main when it holds only a blank draft', () => {
    focusOpenSession.mockReturnValue(null)
    openSession('s1', navigate, 'stack')
    expect(navigate).toHaveBeenCalledWith('/c/s1')
    expect(openSessionTile).not.toHaveBeenCalled()
  })

  it('picker doors resume into the focused Bot workspace and stay in-place in Sessions', () => {
    const scope = { workspaceMode: 'bots' as const, workspaceOwnerKey: 'connection-a::default' }

    // /resume overlay and an artifact's "open chat" (unmodified = in-place) from a Bot tab.
    focusedSessionWorkspaceScope.mockReturnValue(scope)
    focusOpenSession.mockReturnValue(null)
    reuseBlankDraftTile.mockReturnValue(true)
    openSessionFromPicker('s1', navigate)

    expect(reuseBlankDraftTile).toHaveBeenCalledWith('s1', scope)
    expect(navigate).not.toHaveBeenCalled()

    // ⌘K session search (unmodified = stack) from the same Bot tab lands in the Bot workspace too.
    reuseBlankDraftTile.mockReturnValue(false)
    openSessionFromPicker('s2', navigate, 'stack')

    expect(openSessionTile).toHaveBeenCalledWith('s2', 'center', undefined, undefined, scope)
    expect(navigate).not.toHaveBeenCalled()

    // Control: the same doors in the Sessions workspace keep their in-place behaviour.
    $activeSessionId.set('runtime-current')
    focusedSessionWorkspaceScope.mockReturnValue({ workspaceMode: 'sessions' })
    openSessionFromPicker('s3', navigate)

    expect(navigate).toHaveBeenCalledWith('/c/s3')
  })

  // #62045: picking the session this window already shows must still be an
  // EXPLICIT reselect — the picker door queues the resume request the sidebar
  // door always does, so use-route-resume re-runs resumeSession instead of
  // treating the click as an already-active no-op.
  it('picker queues an explicit resume request even for the session already on screen', () => {
    // The active session, already selected in main — the in-place open path
    // below would front 'main' and navigate nowhere.
    $selectedStoredSessionId.set('s1')
    $activeSessionId.set('runtime-current')
    focusOpenSession.mockReturnValue('main')

    expect($sessionResumeRequest.get()).toBeNull()

    openSessionFromPicker('s1', navigate)

    expect($sessionResumeRequest.get()?.sessionId).toBe('s1')
    // The session still lands on screen exactly as before.
    expect(navigate).not.toHaveBeenCalled()
    expect(openSessionTile).not.toHaveBeenCalled()

    // Not just the active row: every picker pick is an explicit reselect, so a
    // DIFFERENT session's request is queued too (with a fresh sequence).
    openSessionFromPicker('s2', navigate)

    expect($sessionResumeRequest.get()?.sessionId).toBe('s2')
    expect($sessionResumeRequest.get()?.sequence ?? 0).toBeGreaterThan(1)
  })

  it('window pops out when the bridge supports it', () => {
    openSession('s1', navigate, 'window')
    expect(openSessionInNewWindow).toHaveBeenCalledWith('s1')
    expect(openSessionTile).not.toHaveBeenCalled()
  })

  it('window falls back to a tab when pop-out is unavailable', () => {
    canOpenSessionWindow.mockReturnValue(false)
    focusOpenSession.mockReturnValue(null)
    openSession('s1', navigate, 'window')
    expect(openSessionInNewWindow).not.toHaveBeenCalled()
    expect(openSessionTile).toHaveBeenCalledWith('s1', 'center')
  })

  it('no-ops on an empty id', () => {
    openSession('', navigate)
    expect(navigate).not.toHaveBeenCalled()
    expect(focusOpenSession).not.toHaveBeenCalled()
  })
})