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.

4.6 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { parseVaultQuery } from './ai-vault-session-filters'
import {
  hasAiVaultSearchQueryOperators,
  splitAiVaultSearchQuery
} from './ai-vault-search-query-operators'

describe('what counts as an operator', () => {
  it('splits repo: and path: out of the free text', () => {
    const split = splitAiVaultSearchQuery('relay capacity repo:orca path:/work/app')
    expect(split.text).toBe('relay capacity')
    expect(split.terms).toEqual(['relay', 'capacity'])
    expect(split.repoTerms).toEqual(['orca'])
    expect(split.pathTerms).toEqual(['/work/app'])
    expect(hasAiVaultSearchQueryOperators(split)).toBe(true)
  })

  it('keeps a value that only looks like an operator as ordinary text', () => {
    const split = splitAiVaultSearchQuery('myrepo:x https://host/path:y')
    expect(split.repoTerms).toEqual([])
    expect(split.pathTerms).toEqual([])
    expect(split.text).toBe('myrepo:x https://host/path:y')
  })

  it('reads a quoted operator value whole, including its spaces', () => {
    expect(splitAiVaultSearchQuery('path:"/Users/ada/My Project" needle').pathTerms).toEqual([
      '/Users/ada/My Project'
    ])
  })

  it('does not let an apostrophe in prose swallow the operator between quotes', () => {
    const split = splitAiVaultSearchQuery("it's a repo:orca thing's")
    expect(split.repoTerms).toEqual(['orca'])
  })

  it('preserves operator case, which the panel folds and the index must not', () => {
    // Keep execution-host case in cwd_key so distinct POSIX directories remain searchable.
    expect(splitAiVaultSearchQuery('path:/Work/App').pathTerms).toEqual(['/Work/App'])
    expect(parseVaultQuery('path:/Work/App').pathTerms).toEqual(['/work/app'])
  })

  it('has no operators when the query is plain text', () => {
    expect(hasAiVaultSearchQueryOperators(splitAiVaultSearchQuery('relay capacity'))).toBe(false)
  })
})

// Pin intentional differences from the former panel tokenizer; the shared parser now guarantees panel/index agreement.
describe('the shapes where the panel parser used to answer differently', () => {
  it.each([
    ['repo:"" x', 'repoTerms'],
    ['path:"" x', 'pathTerms']
  ] as const)('drops the empty operator value in %s instead of filtering on `""`', (query, key) => {
    // Drop empty operators; filtering on a quoted empty label once silently emptied the list.
    expect(splitAiVaultSearchQuery(query)[key]).toEqual([])
    expect(parseVaultQuery(query)[key]).toEqual([])
  })

  it.each([
    ['repo:"  " x', 'repoTerms'],
    ['path:"  " x', 'pathTerms']
  ] as const)('drops the whitespace-only operator value in %s too', (query, key) => {
    // Whitespace-only operator values also narrow nothing; retaining spaces once emptied results.
    expect(splitAiVaultSearchQuery(query)[key]).toEqual([])
    expect(parseVaultQuery(query)[key]).toEqual([])
  })

  it('trims a quoted operator value rather than searching for the spaces', () => {
    expect(splitAiVaultSearchQuery('repo:" session-search "').repoTerms).toEqual(['session-search'])
  })

  it.each(['"" empty', "'' empty", '"  " empty'])(
    'reads the empty quotes in %s as an empty term',
    (query) => {
      // Empty free-text terms match everything instead of searching literal quotes.
      expect(parseVaultQuery(query).terms).toEqual(['', 'empty'])
    }
  )

  it.each([
    ['"foo"bar', { terms: ['foo', 'bar'], repoTerms: [], pathTerms: [] }],
    ['"a b"c', { terms: ['a b', 'c'], repoTerms: [], pathTerms: [] }],
    ['repo:"a"b', { terms: ['b'], repoTerms: ['a'], pathTerms: [] }],
    ['path:"a"b', { terms: ['b'], repoTerms: [], pathTerms: ['a'] }],
    ['repo:"a b"c d', { terms: ['c', 'd'], repoTerms: ['a b'], pathTerms: [] }]
  ])('reads %s exactly as the panel always has', (query, expected) => {
    // Closing quotes may end mid-word; token-start rules protect apostrophes without retaining literal quotes in search terms.
    expect(parseVaultQuery(query)).toEqual(expected)
  })
})

describe('agrees with the sessions panel parser on operator recognition', () => {
  it.each([
    'relay capacity',
    'repo:orca needle',
    'path:/work/app needle',
    'myrepo:x',
    'needle repo:orca path:/work/app',
    'path:"/Users/ada/My Project"',
    'https://host/path:y'
  ])('reads the same operators out of %s', (query) => {
    const split = splitAiVaultSearchQuery(query)
    const parsed = parseVaultQuery(query)
    const fold = (values: readonly string[]): string[] => values.map((v) => v.toLowerCase()).sort()
    expect(fold(split.repoTerms)).toEqual(fold(parsed.repoTerms))
    expect(fold(split.pathTerms)).toEqual(fold(parsed.pathTerms))
  })
})