Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

11.0 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  buildExcludePathPrefixes,
  buildGitLsFilesArgsForQuickOpen,
  buildHiddenDirExcludeGlobs,
  buildRgArgsForQuickOpen,
  normalizeQuickOpenRgLine,
  shouldExcludeQuickOpenRelPath,
  shouldIncludeQuickOpenPath
} from './quick-open-filter'

describe('shouldIncludeQuickOpenPath', () => {
  it('includes normal source paths', () => {
    expect(shouldIncludeQuickOpenPath('src/index.ts')).toBe(true)
    expect(shouldIncludeQuickOpenPath('.github/workflows/ci.yml')).toBe(true)
    expect(shouldIncludeQuickOpenPath('.env')).toBe(true)
  })

  it('excludes node_modules and blocklisted dirs at any depth', () => {
    expect(shouldIncludeQuickOpenPath('node_modules/a/b.js')).toBe(false)
    expect(shouldIncludeQuickOpenPath('packages/x/node_modules/a.js')).toBe(false)
    expect(shouldIncludeQuickOpenPath('.git/config')).toBe(false)
    expect(shouldIncludeQuickOpenPath('foo/.cache/bar')).toBe(false)
  })

  it('hides generated npm cache dir from Quick Open', () => {
    expect(shouldIncludeQuickOpenPath('.npm/pkg/index.js')).toBe(false)
  })
  it('hides npm-global install state dir from Quick Open', () => {
    expect(shouldIncludeQuickOpenPath('.npm-global/bin/foo')).toBe(false)
  })
  it('hides GNOME virtual FS runtime mount from Quick Open', () => {
    expect(shouldIncludeQuickOpenPath('.gvfs/mount/file')).toBe(false)
  })
  it('hides local share runtime state without hiding all .local files', () => {
    expect(shouldIncludeQuickOpenPath('.local/share/app/state.db')).toBe(false)
    expect(shouldIncludeQuickOpenPath('nested/.local/share/app/state.db')).toBe(false)
    expect(shouldIncludeQuickOpenPath('.local/bin/tool')).toBe(true)
  })
})

describe('buildExcludePathPrefixes', () => {
  it('returns root-relative POSIX prefixes', () => {
    expect(
      buildExcludePathPrefixes('/home/u/repo', [
        '/home/u/repo/packages/app',
        '/home/u/repo/worktrees/b'
      ])
    ).toEqual(['packages/app', 'worktrees/b'])
  })

  it('ignores malformed input', () => {
    expect(buildExcludePathPrefixes('/home/u/repo', undefined)).toEqual([])
    expect(buildExcludePathPrefixes('/home/u/repo', 'not-array' as unknown)).toEqual([])
    expect(buildExcludePathPrefixes('/home/u/repo', [null, 42, '', '/outside'])).toEqual([])
  })

  it('ignores root-equal and outside-root values', () => {
    expect(buildExcludePathPrefixes('/home/u/repo', ['/home/u/repo'])).toEqual([])
    expect(buildExcludePathPrefixes('/home/u/repo', ['/home/u/other'])).toEqual([])
  })

  it('keeps dot-dot-prefixed names inside the root while rejecting parent escapes', () => {
    expect(
      buildExcludePathPrefixes('/home/u/repo', [
        '/home/u/repo/..env',
        '/home/u/repo/..workspace/app',
        '/home/u/repo/../outside'
      ])
    ).toEqual(['..env', '..workspace/app'])
  })

  it('handles Windows-style roots and paths', () => {
    expect(buildExcludePathPrefixes('C:\\repo', ['C:\\repo\\packages\\app'])).toEqual([
      'packages/app'
    ])
    expect(
      buildExcludePathPrefixes('//Server/Share/Repo', ['//server/share/repo/packages/app'])
    ).toEqual(['packages/app'])
  })

  it('strips trailing slashes', () => {
    expect(buildExcludePathPrefixes('/r', ['/r/a/', '/r/b///'])).toEqual(['a', 'b'])
  })
})

describe('shouldExcludeQuickOpenRelPath', () => {
  it('preserves normalized Windows and UNC exclusion boundaries', () => {
    for (const [root, excluded] of [
      ['C:\\Repo', 'C:\\Repo\\trees\\one'],
      ['\\\\server\\share\\repo', '\\\\server\\share\\repo\\trees\\one']
    ]) {
      const prefixes = buildExcludePathPrefixes(root, [excluded])
      expect(prefixes).toEqual(['trees/one'])
      expect(shouldExcludeQuickOpenRelPath('trees/one/file.ts', prefixes)).toBe(true)
      expect(shouldExcludeQuickOpenRelPath('trees/one-more/file.ts', prefixes)).toBe(false)
    }
  })

  it('matches exact and boundary paths only', () => {
    expect(shouldExcludeQuickOpenRelPath('packages/app', ['packages/app'])).toBe(true)
    expect(shouldExcludeQuickOpenRelPath('packages/app/x.ts', ['packages/app'])).toBe(true)
  })

  it('does not match sibling paths with a shared prefix', () => {
    expect(shouldExcludeQuickOpenRelPath('packages/app2/x.ts', ['packages/app'])).toBe(false)
    expect(shouldExcludeQuickOpenRelPath('packages/application', ['packages/app'])).toBe(false)
  })
})

describe('buildHiddenDirExcludeGlobs', () => {
  it('includes node_modules plus blocklist as directory-match globs', () => {
    const globs = buildHiddenDirExcludeGlobs()
    expect(globs).toContain('!**/node_modules')
    expect(globs).toContain('!**/.git')
    expect(globs).toContain('!**/.cache')
    expect(globs).toContain('!**/.local/share')
    // Directory matches prune rg traversal; contents-only globs still descend.
    expect(globs).not.toContain('!**/node_modules/**')
  })
})

describe('buildRgArgsForQuickOpen', () => {
  it.each([
    { searchRoot: '.', excludePathPrefixes: [], forceSlashSeparator: false },
    {
      searchRoot: '/root',
      excludePathPrefixes: ['packages/app', 'feature[1]'],
      forceSlashSeparator: true
    }
  ])('broadens only VCS ignore handling for $searchRoot', (options) => {
    const { primary, ignoredPass } = buildRgArgsForQuickOpen(options)
    expect(ignoredPass).toContain('--no-ignore-vcs')
    expect(ignoredPass.filter((arg) => arg !== '--no-ignore-vcs')).toEqual(primary)
  })

  it('primary pass includes --files, --hidden, hidden-dir excludes, no --follow', () => {
    const { primary } = buildRgArgsForQuickOpen({
      searchRoot: '/root',
      excludePathPrefixes: [],
      forceSlashSeparator: false
    })
    expect(primary).toContain('--files')
    expect(primary).toContain('--hidden')
    expect(primary).toContain('!**/node_modules')
    expect(primary).not.toContain('--follow')
  })

  it('ignored pass includes --no-ignore-vcs without .env* whitelist globs, no --follow', () => {
    const { ignoredPass } = buildRgArgsForQuickOpen({
      searchRoot: '/root',
      excludePathPrefixes: [],
      forceSlashSeparator: false
    })
    expect(ignoredPass).toContain('--no-ignore-vcs')
    expect(ignoredPass).not.toContain('.env*')
    expect(ignoredPass).not.toContain('**/.env*')
    expect(ignoredPass).not.toContain('--follow')
  })

  it('forceSlashSeparator emits --path-separator /', () => {
    const { primary } = buildRgArgsForQuickOpen({
      searchRoot: '/r',
      excludePathPrefixes: [],
      forceSlashSeparator: true
    })
    const idx = primary.indexOf('--path-separator')
    expect(idx).toBeGreaterThanOrEqual(0)
    expect(primary[idx + 1]).toBe('/')
  })

  it('excludePathPrefixes are escaped as directory-match globs', () => {
    const { primary } = buildRgArgsForQuickOpen({
      searchRoot: '/r',
      excludePathPrefixes: ['packages/app', 'feature[1]'],
      forceSlashSeparator: false
    })
    expect(primary).toContain('!packages/app')
    expect(primary).toContain('!packages/app/**')
    // Glob metacharacters in a literal name must be escaped.
    expect(primary).toContain('!feature\\[1\\]')
  })
})

it('pins both Quick Open passes to config-independent NUL output', () => {
  const { primary, ignoredPass } = buildRgArgsForQuickOpen({
    searchRoot: '.',
    excludePathPrefixes: [],
    forceSlashSeparator: true
  })
  for (const args of [primary, ignoredPass]) {
    expect(args).toContain('--no-config')
    expect(args).toContain('--null')
  }
})

describe('normalizeQuickOpenRgLine', () => {
  it('strips absolute root prefix', () => {
    expect(
      normalizeQuickOpenRgLine('/root/src/a.ts', { kind: 'absolute', rootPath: '/root' })
    ).toBe('src/a.ts')
  })

  it('strips Windows drive absolute root prefixes', () => {
    expect(
      normalizeQuickOpenRgLine('C:\\repo\\src\\a.ts', {
        kind: 'absolute',
        rootPath: 'C:\\repo'
      })
    ).toBe('src/a.ts')
  })

  it('preserves Windows UNC roots while stripping absolute root prefixes', () => {
    expect(
      normalizeQuickOpenRgLine('\\\\server\\share\\repo\\src\\a.ts', {
        kind: 'absolute',
        rootPath: '\\\\server\\share\\repo'
      })
    ).toBe('src/a.ts')
  })

  it('strips ./ prefix in cwd-relative mode', () => {
    expect(normalizeQuickOpenRgLine('./src/a.ts', { kind: 'cwd-relative' })).toBe('src/a.ts')
  })

  it('keeps cwd-relative dot-dot-prefixed names but rejects parent escapes', () => {
    expect(normalizeQuickOpenRgLine('./..fixtures/a.ts', { kind: 'cwd-relative' })).toBe(
      '..fixtures/a.ts'
    )
    expect(normalizeQuickOpenRgLine('..env', { kind: 'cwd-relative' })).toBe('..env')
    expect(normalizeQuickOpenRgLine('..workspace/file.ts', { kind: 'cwd-relative' })).toBe(
      '..workspace/file.ts'
    )
    expect(normalizeQuickOpenRgLine('../outside.ts', { kind: 'cwd-relative' })).toBeNull()
    expect(normalizeQuickOpenRgLine('..', { kind: 'cwd-relative' })).toBeNull()
  })

  it('preserves a trailing carriage return in a NUL-delimited filename', () => {
    expect(normalizeQuickOpenRgLine('/root/a.ts\r', { kind: 'absolute', rootPath: '/root' })).toBe(
      'a.ts\r'
    )
  })

  it('returns null for paths outside the absolute root', () => {
    expect(
      normalizeQuickOpenRgLine('/other/a.ts', { kind: 'absolute', rootPath: '/root' })
    ).toBeNull()
  })

  it('returns null for empty or root-equal lines', () => {
    expect(normalizeQuickOpenRgLine('', { kind: 'cwd-relative' })).toBeNull()
    expect(normalizeQuickOpenRgLine('.', { kind: 'cwd-relative' })).toBeNull()
  })

  it('returns null for cwd-relative parent-directory escapes', () => {
    expect(normalizeQuickOpenRgLine('../outside/a.ts', { kind: 'cwd-relative' })).toBeNull()
    expect(normalizeQuickOpenRgLine('./../outside/a.ts', { kind: 'cwd-relative' })).toBeNull()
  })
})

describe('buildGitLsFilesArgsForQuickOpen', () => {
  it('primary pass is --cached --others --exclude-standard', () => {
    const { primary } = buildGitLsFilesArgsForQuickOpen()
    expect(primary).toEqual([
      '-z',
      '-s',
      '--cached',
      '--others',
      '--exclude-standard',
      '--directory',
      '--no-empty-directory'
    ])
  })

  it('ignored pass surfaces ignored files without .env* pathspec whitelist', () => {
    const { ignoredPass } = buildGitLsFilesArgsForQuickOpen()
    expect(ignoredPass).toEqual([
      '-z',
      '-s',
      '--others',
      '--ignored',
      '--exclude-standard',
      '--directory',
      '--no-empty-directory'
    ])
    expect(ignoredPass).not.toContain('.env*')
    expect(ignoredPass).not.toContain(':(glob)**/.env*')
  })

  it('exclude prefixes prepend positive "." pathspec', () => {
    const { primary, ignoredPass } = buildGitLsFilesArgsForQuickOpen(['packages/app'])
    const dashDashIdx = primary.indexOf('--')
    expect(dashDashIdx).toBeGreaterThanOrEqual(0)
    // Positive pathspec must appear before any exclude pathspec.
    expect(primary[dashDashIdx + 1]).toBe('.')
    expect(primary).toContain(':(exclude,glob)packages/app')
    expect(primary).toContain(':(exclude,glob)packages/app/**')
    expect(ignoredPass).toContain(':(exclude,glob)packages/app')
    expect(ignoredPass).toContain(':(exclude,glob)packages/app/**')
  })
})