File size: 6,154 Bytes
1f21206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { describe, expect, it, vi, beforeEach } from 'vitest'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom'
import { FileSearchMenu } from './FileSearchMenu'
import { ApiError } from '../../api/client'
import { filesystemApi } from '../../api/filesystem'
import { useSettingsStore } from '../../stores/settingsStore'

vi.mock('../../api/filesystem', () => ({
  filesystemApi: {
    browse: vi.fn(),
    search: vi.fn(),
  },
}))

describe('FileSearchMenu', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    useSettingsStore.setState({ locale: 'en' })
  })

  it('shows an explicit error when directory browsing is denied', async () => {
    vi.mocked(filesystemApi.browse).mockRejectedValueOnce(
      new ApiError(403, { error: 'Access denied: path outside allowed directory' }),
    )

    render(
      <FileSearchMenu
        cwd="/private/tmp"
        onSelect={() => {}}
      />,
    )

    expect(await screen.findByText('Cannot access this directory')).toBeInTheDocument()
    expect(screen.queryByText('No files in this directory')).not.toBeInTheDocument()
  })

  it('renders returned files when browsing succeeds', async () => {
    vi.mocked(filesystemApi.browse).mockResolvedValueOnce({
      currentPath: '/tmp',
      parentPath: '/',
      entries: [
        { name: 'preview.png', path: '/tmp/preview.png', isDirectory: false },
      ],
    })

    render(
      <FileSearchMenu
        cwd="/tmp"
        onSelect={() => {}}
      />,
    )

    await waitFor(() => {
      expect(screen.getByText('preview.png')).toBeInTheDocument()
    })
  })

  it('selects directories from search results and keeps a separate drill-in action', async () => {
    const onSelect = vi.fn()
    const onNavigate = vi.fn()
    vi.mocked(filesystemApi.search).mockResolvedValueOnce({
      currentPath: '/repo',
      parentPath: '/',
      query: 'backend',
      entries: [
        { name: 'backend', path: '/repo/backend', relativePath: 'backend', isDirectory: true },
      ],
    })
    vi.mocked(filesystemApi.browse).mockResolvedValueOnce({
      currentPath: '/repo/backend',
      parentPath: '/repo',
      entries: [
        { name: 'src', path: '/repo/backend/src', isDirectory: true },
      ],
    })
    vi.mocked(filesystemApi.browse).mockResolvedValueOnce({
      currentPath: '/repo/backend/src',
      parentPath: '/repo/backend',
      entries: [
        { name: 'commands', path: '/repo/backend/src/commands', isDirectory: true },
      ],
    })

    render(
      <FileSearchMenu
        cwd="/repo"
        filter="backend"
        onSelect={onSelect}
        onNavigate={onNavigate}
      />,
    )

    fireEvent.click(await screen.findByText('backend/'))

    expect(onSelect).toHaveBeenCalledWith('/repo/backend', 'backend', true)
    expect(onNavigate).not.toHaveBeenCalled()

    fireEvent.click(screen.getByLabelText('Open folder'))

    expect(onNavigate).toHaveBeenCalledWith('backend/')
    await waitFor(() => {
      expect(filesystemApi.browse).toHaveBeenCalledWith('/repo/backend', { includeFiles: true })
    })

    fireEvent.click(await screen.findByText('src'))
    expect(onSelect).toHaveBeenLastCalledWith('/repo/backend/src', 'backend/src', true)

    fireEvent.click(screen.getByLabelText('Open folder'))
    expect(onNavigate).toHaveBeenLastCalledWith('backend/src/')
    await waitFor(() => {
      expect(filesystemApi.browse).toHaveBeenCalledWith('/repo/backend/src', { includeFiles: true })
    })
  })

  it('passes nested relative file paths when selecting a file', async () => {
    const onSelect = vi.fn()
    vi.mocked(filesystemApi.search).mockResolvedValueOnce({
      currentPath: '/repo',
      parentPath: '/',
      query: 'pictactic',
      entries: [
        { name: 'pictactic', path: '/repo/backend/src/pictactic', relativePath: 'backend/src/pictactic', isDirectory: false },
      ],
    })

    render(
      <FileSearchMenu
        cwd="/repo"
        filter="backend/src/pictactic"
        onSelect={onSelect}
      />,
    )

    fireEvent.click(await screen.findByText('backend/src/pictactic'))

    expect(onSelect).toHaveBeenCalledWith('/repo/backend/src/pictactic', 'backend/src/pictactic', false)
  })

  it('renders search results as insertable paths instead of repeated basenames', async () => {
    vi.mocked(filesystemApi.search).mockResolvedValueOnce({
      currentPath: '/repo',
      parentPath: '/',
      query: 'src',
      entries: [
        { name: 'src', path: '/repo/src', relativePath: 'src', isDirectory: true },
        { name: 'hooks', path: '/repo/src/hooks', relativePath: 'src/hooks', isDirectory: true },
        { name: 'src', path: '/repo/desktop/src', relativePath: 'desktop/src', isDirectory: true },
      ],
    })

    render(
      <FileSearchMenu
        cwd="/repo"
        filter="src"
        onSelect={() => {}}
      />,
    )

    expect(await screen.findByText('src/')).toBeInTheDocument()
    expect(screen.getByText('src/hooks/')).toBeInTheDocument()
    expect(screen.getByText('desktop/src/')).toBeInTheDocument()
  })

  it('uses the resolved home root for typed folder filters when no workspace is selected', async () => {
    vi.mocked(filesystemApi.search).mockResolvedValueOnce({
      currentPath: '/Users/nanmi',
      parentPath: '/',
      query: 'workspace',
      entries: [
        { name: 'workspace', path: '/Users/nanmi/workspace', relativePath: 'workspace', isDirectory: true },
      ],
    })
    vi.mocked(filesystemApi.browse).mockResolvedValueOnce({
      currentPath: '/Users/nanmi/workspace',
      parentPath: '/Users/nanmi',
      entries: [],
    })

    const { rerender } = render(
      <FileSearchMenu
        cwd=""
        filter="workspace"
        onSelect={() => {}}
      />,
    )

    expect(await screen.findByText('workspace/')).toBeInTheDocument()

    rerender(
      <FileSearchMenu
        cwd=""
        filter="workspace/"
        onSelect={() => {}}
      />,
    )

    await waitFor(() => {
      expect(filesystemApi.browse).toHaveBeenCalledWith('/Users/nanmi/workspace', { includeFiles: true })
    })
  })
})