File size: 10,886 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | import { afterEach, describe, expect, it } from 'bun:test'
import { execFileSync } from 'child_process'
import * as fs from 'fs'
import * as fsp from 'fs/promises'
import * as os from 'os'
import * as path from 'path'
import { handleFilesystemRoute } from '../api/filesystem.js'
import { clearFilesystemAccessRootsForTests } from '../services/filesystemAccessRoots.js'
import { getRepositoryContext } from '../services/repositoryLaunchService.js'
const cleanupDirs = new Set<string>()
function makeUrl(route: string, params: Record<string, string>): URL {
const url = new URL(`http://localhost${route}`)
for (const [key, value] of Object.entries(params)) {
url.searchParams.set(key, value)
}
return url
}
afterEach(async () => {
for (const dir of cleanupDirs) {
await fsp.rm(dir, { recursive: true, force: true })
}
cleanupDirs.clear()
clearFilesystemAccessRootsForTests()
})
function git(cwd: string, ...args: string[]): string {
return execFileSync('git', args, {
cwd,
encoding: 'utf8',
})
}
function isWithinPath(targetPath: string, rootPath: string): boolean {
const target = path.resolve(targetPath)
const root = path.resolve(rootPath)
return target === root || target.startsWith(`${root}${path.sep}`)
}
async function makeExternalFixtureDir(): Promise<string | null> {
const candidates = ['/var/tmp', '/private/var/tmp', '/Users/Shared']
for (const baseDir of candidates) {
try {
const stat = await fsp.stat(baseDir)
if (!stat.isDirectory()) continue
const fixtureDir = await fsp.mkdtemp(path.join(baseDir, 'claude-filesystem-test-'))
const isDefaultAllowed =
isWithinPath(fixtureDir, os.homedir()) ||
isWithinPath(fixtureDir, '/tmp') ||
(process.platform === 'darwin' && isWithinPath(fixtureDir, '/private/tmp'))
if (!isDefaultAllowed) return fixtureDir
await fsp.rm(fixtureDir, { recursive: true, force: true })
} catch {
// Try the next common writable system directory.
}
}
return null
}
describe('filesystem API', () => {
it('allows browsing a directory under the user home directory', async () => {
const homeFixtureDir = await fsp.mkdtemp(path.join(os.homedir(), 'claude-filesystem-test-'))
cleanupDirs.add(homeFixtureDir)
await fsp.writeFile(path.join(homeFixtureDir, 'note.txt'), 'hello')
const res = await handleFilesystemRoute(
'/api/filesystem/browse',
makeUrl('/api/filesystem/browse', {
path: homeFixtureDir,
includeFiles: 'true',
}),
)
expect(res.status).toBe(200)
const body = await res.json() as { entries: Array<{ name: string }> }
expect(body.entries.some((entry) => entry.name === 'note.txt')).toBe(true)
})
it('allows browsing a selected workspace outside the default home/tmp roots', async () => {
const externalFixtureDir = await makeExternalFixtureDir()
if (!externalFixtureDir) return
cleanupDirs.add(externalFixtureDir)
await fsp.writeFile(path.join(externalFixtureDir, 'note.txt'), 'hello')
const deniedRes = await handleFilesystemRoute(
'/api/filesystem/browse',
makeUrl('/api/filesystem/browse', {
path: externalFixtureDir,
includeFiles: 'true',
}),
)
expect(deniedRes.status).toBe(403)
await getRepositoryContext(externalFixtureDir)
const res = await handleFilesystemRoute(
'/api/filesystem/browse',
makeUrl('/api/filesystem/browse', {
path: externalFixtureDir,
includeFiles: 'true',
}),
)
expect(res.status).toBe(200)
const body = await res.json() as { entries: Array<{ name: string }> }
expect(body.entries.some((entry) => entry.name === 'note.txt')).toBe(true)
})
it('fuzzy searches files and directories below the selected root', async () => {
const homeFixtureDir = await fsp.mkdtemp(path.join(os.homedir(), 'claude-filesystem-test-'))
cleanupDirs.add(homeFixtureDir)
git(homeFixtureDir, 'init')
await fsp.mkdir(path.join(homeFixtureDir, 'src', 'commands'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'src', 'commands', 'files'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'src', 'constants'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'src', 'hooks'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'desktop', 'src'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'scripts', 'quality-gate', 'baseline', 'fixtures', 'cross-module-refactor', 'src'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, '__pycache__'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'node_modules', 'pkg'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, '.venv', 'lib'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'tmp-ignore'), { recursive: true })
await fsp.writeFile(path.join(homeFixtureDir, '.gitignore'), ['__pycache__/', 'node_modules/', '.venv/', 'venv/'].join('\n'))
await fsp.writeFile(path.join(homeFixtureDir, '.ignore'), 'tmp-ignore/')
await fsp.writeFile(path.join(homeFixtureDir, 'src', 'commands', 'files.ts'), 'export {}')
await fsp.writeFile(path.join(homeFixtureDir, 'src', 'commands', 'files', 'index.ts'), 'export {}')
await fsp.writeFile(path.join(homeFixtureDir, 'src', 'constants', 'fileSearch.ts'), 'export {}')
await fsp.writeFile(path.join(homeFixtureDir, 'src', 'hooks', 'useFileSearch.ts'), 'export {}')
await fsp.writeFile(path.join(homeFixtureDir, 'desktop', 'src', 'main.ts'), 'export {}')
await fsp.writeFile(path.join(homeFixtureDir, 'scripts', 'quality-gate', 'baseline', 'fixtures', 'cross-module-refactor', 'src', 'index.ts'), 'export {}')
await fsp.writeFile(path.join(homeFixtureDir, '__pycache__', 'fileSearch.cpython-311.pyc'), '')
await fsp.writeFile(path.join(homeFixtureDir, 'node_modules', 'pkg', 'files.js'), '')
await fsp.writeFile(path.join(homeFixtureDir, '.venv', 'lib', 'files.py'), '')
await fsp.writeFile(path.join(homeFixtureDir, 'tmp-ignore', 'files.tmp'), '')
const res = await handleFilesystemRoute(
'/api/filesystem/browse',
makeUrl('/api/filesystem/browse', {
path: homeFixtureDir,
search: 'files',
includeFiles: 'true',
}),
)
expect(res.status).toBe(200)
const body = await res.json() as { entries: Array<{ name: string; relativePath?: string; isDirectory: boolean }> }
expect(body.entries).toEqual(expect.arrayContaining([
expect.objectContaining({
name: 'files.ts',
relativePath: 'src/commands/files.ts',
isDirectory: false,
}),
]))
expect(body.entries.some((entry) => entry.relativePath === 'src/constants/fileSearch.ts')).toBe(true)
expect(body.entries.find((entry) => entry.relativePath === 'src/commands/files')?.isDirectory).toBe(true)
expect(body.entries.some((entry) => entry.relativePath === '__pycache__/fileSearch.cpython-311.pyc')).toBe(false)
expect(body.entries.some((entry) => entry.relativePath === 'node_modules/pkg/files.js')).toBe(false)
expect(body.entries.some((entry) => entry.relativePath === '.venv/lib/files.py')).toBe(false)
expect(body.entries.some((entry) => entry.relativePath === 'tmp-ignore/files.tmp')).toBe(false)
const srcRes = await handleFilesystemRoute(
'/api/filesystem/browse',
makeUrl('/api/filesystem/browse', {
path: homeFixtureDir,
search: 'src',
includeFiles: 'true',
}),
)
expect(srcRes.status).toBe(200)
const srcBody = await srcRes.json() as { entries: Array<{ relativePath?: string }> }
const srcPaths = srcBody.entries.map(entry => entry.relativePath)
expect(srcPaths[0]).toBe('src')
expect(srcPaths.indexOf('src/hooks')).toBeGreaterThan(-1)
expect(srcPaths.indexOf('desktop/src')).toBeGreaterThan(-1)
expect(srcPaths.indexOf('scripts/quality-gate/baseline/fixtures/cross-module-refactor/src')).toBeGreaterThan(-1)
expect(srcPaths.indexOf('src/hooks')).toBeLessThan(srcPaths.indexOf('desktop/src'))
expect(srcPaths.indexOf('src/hooks')).toBeLessThan(srcPaths.indexOf('scripts/quality-gate/baseline/fixtures/cross-module-refactor/src'))
})
it('falls back to ripgrep search outside git and still respects ignore files', async () => {
const homeFixtureDir = await fsp.mkdtemp(path.join(os.homedir(), 'claude-filesystem-test-'))
cleanupDirs.add(homeFixtureDir)
await fsp.mkdir(path.join(homeFixtureDir, 'app'), { recursive: true })
await fsp.mkdir(path.join(homeFixtureDir, 'node_modules', 'pkg'), { recursive: true })
await fsp.writeFile(path.join(homeFixtureDir, '.gitignore'), 'node_modules/')
await fsp.writeFile(path.join(homeFixtureDir, 'app', 'cache-result.ts'), 'export {}')
await fsp.writeFile(path.join(homeFixtureDir, 'node_modules', 'pkg', 'cache-result.js'), '')
const res = await handleFilesystemRoute(
'/api/filesystem/browse',
makeUrl('/api/filesystem/browse', {
path: homeFixtureDir,
search: 'cache',
includeFiles: 'true',
}),
)
expect(res.status).toBe(200)
const body = await res.json() as { entries: Array<{ relativePath?: string; isDirectory: boolean }> }
expect(body.entries).toEqual(expect.arrayContaining([
expect.objectContaining({
relativePath: 'app/cache-result.ts',
isDirectory: false,
}),
]))
expect(body.entries.some((entry) => entry.relativePath === 'node_modules/pkg/cache-result.js')).toBe(false)
})
it('accepts /private/tmp aliases on macOS for browsing and file serving', async () => {
if (process.platform !== 'darwin') return
const tmpFixtureDir = await fsp.mkdtemp('/tmp/claude-filesystem-test-')
cleanupDirs.add(tmpFixtureDir)
const canonicalTmpDir = fs.realpathSync(tmpFixtureDir)
const imagePath = path.join(canonicalTmpDir, 'preview.png')
await fsp.writeFile(
imagePath,
Buffer.from('89504e470d0a1a0a0000000d4948445200000001000000010802000000907753de0000000c49444154789c63606060000000040001f61738550000000049454e44ae426082', 'hex'),
)
const browseRes = await handleFilesystemRoute(
'/api/filesystem/browse',
makeUrl('/api/filesystem/browse', {
path: canonicalTmpDir,
includeFiles: 'true',
}),
)
expect(browseRes.status).toBe(200)
const browseBody = await browseRes.json() as { entries: Array<{ name: string }> }
expect(browseBody.entries.some((entry) => entry.name === 'preview.png')).toBe(true)
const fileRes = await handleFilesystemRoute(
'/api/filesystem/file',
makeUrl('/api/filesystem/file', {
path: imagePath,
}),
)
expect(fileRes.status).toBe(200)
expect(fileRes.headers.get('Content-Type')).toBe('image/png')
})
})
|