Skip to content

The agent that grows with you

18.9 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it } from 'vitest'

import {
  type CommandCatalogMeta,
  type CommandsCatalogLike,
  desktopSkinSlashCompletions,
  type DesktopSlashArgumentMode,
  desktopSlashCommandArgumentMode,
  desktopSlashUnavailableMessage,
  desktopSubcommandUnavailableMessage,
  filterDesktopCommandsCatalog,
  filterDesktopSubcommandCompletions,
  isDesktopSlashCommand,
  isDesktopSlashExtensionCommand,
  isDesktopSlashSuggestion,
  isDesktopSlashSuggestionWithOptions,
  isModelPickerCommand,
  isPickerCommand,
  rankSkillCommands,
  rememberDesktopCommandsCatalog,
  resolveDesktopCommand,
  slashCompletionGroup,
  TS_ONLY_NO_DESKTOP_SURFACE
} from './desktop-slash-commands'
import desktopSlashRegistry from './desktop-slash-registry.json'

function registryCatalog(
  modes: Record<string, DesktopSlashArgumentMode | null>,
  aliases: Record<string, string> = {}
): CommandsCatalogLike {
  const commands: Record<string, CommandCatalogMeta> = {}
  const canon: Record<string, string> = {}

  for (const [name, argument_mode] of Object.entries(modes)) {
    commands[name] = { argument_mode, desktop: null }
    canon[name] = name
  }

  for (const [alias, target] of Object.entries(aliases)) {
    commands[alias] = commands[target]
    canon[alias] = target
  }

  return { commands, canon }
}

const REGISTRY_CATALOG = registryCatalog(
  {
    '/approvals': 'options',
    '/review': 'text',
    '/refine': 'text',
    '/usage': null,
    '/version': null,
    '/agents': null,
    '/steer': 'text',
    '/stop': null,
    '/bg': 'text',
    '/btw': 'text',
    '/debug': null,
    '/goal': 'mixed',
    '/personality': 'options',
    '/queue': 'text',
    '/retry': null,
    '/rollback': null,
    '/tools': 'options',
    '/undo': null,
    '/loop': 'mixed',
    '/lcm': 'text'
  },
  { '/tasks': '/agents', '/background': '/bg', '/q': '/queue', '/proactive': '/loop' }
)

describe('desktop slash command curation', () => {
  beforeEach(() => {
    rememberDesktopCommandsCatalog(REGISTRY_CATALOG)
  })

  afterEach(() => {
    rememberDesktopCommandsCatalog(undefined)
  })

  it('treats registry and plugin commands as exec when the catalog says so', () => {
    expect(resolveDesktopCommand('/refine')?.argumentMode).toBe('text')
    expect(isDesktopSlashSuggestion('/refine')).toBe(true)
    expect(isDesktopSlashSuggestion('/background')).toBe(false)
    expect(isDesktopSlashCommand('/bg')).toBe(true)
    expect(desktopSlashCommandArgumentMode('/bg')).toBe('text')
    expect(isDesktopSlashCommand('/btw')).toBe(true)
    expect(desktopSlashCommandArgumentMode('/btw')).toBe('text')
    expect(resolveDesktopCommand('/lcm')?.surface).toEqual({ kind: 'exec' })
    expect(desktopSlashCommandArgumentMode('/lcm')).toBe('text')
  })

  it('hands the /skills write-approval subcommands to the backend, keeping the hub mutations off the wire', () => {
    // #98330: the registry declares `desktop_subcommands` on /skills so the
    // review slice (pending/approve/reject/diff/approval) is exec-routed
    // while the CLI-hub mutations (search/install/…) stay refused — an
    // allowlisted invocation execs, everything else renders the gate message.
    expect(isDesktopSlashCommand('/skills', 'pending')).toBe(true)
    expect(isDesktopSlashCommand('/skills', '  approve 9f2c1a ')).toBe(true)

    for (const sub of ['reject 9f2c1a', 'diff 9f2c1a', 'approval on']) {
      expect(isDesktopSlashCommand('/skills', sub)).toBe(true)
    }

    // The exec gate is desktopSubcommandUnavailableMessage: the hub mutations
    // render a refusal message instead of reaching the wire, and a bare
    // /skills never execs into the interactive hub from the desktop.
    expect(desktopSubcommandUnavailableMessage('/skills', 'search')).toContain('not available in the desktop app')
    expect(desktopSubcommandUnavailableMessage('/skills', 'install gif-search')).toContain(
      'not available in the desktop app'
    )
    expect(desktopSubcommandUnavailableMessage('/skills', 'audit')).toContain('not available in the desktop app')
    expect(desktopSubcommandUnavailableMessage('/skills', '')).toContain('needs a subcommand here')
    expect(desktopSubcommandUnavailableMessage('/skills', '   ')).toContain('needs a subcommand here')
    expect(desktopSubcommandUnavailableMessage('/skills', 'pending')).toBeNull()

    // Completion never suggests a subcommand the gate would refuse.
    const subs = (items: readonly { text?: string }[]) => items.map(item => item.text)
    expect(
      subs(
        filterDesktopSubcommandCompletions('/skills ', [{ text: 'pending review' }, { text: 'install demo' }], {
          isArgCompletion: true
        })
      )
    ).toEqual(['pending review'])
    expect(
      subs(filterDesktopSubcommandCompletions('/skills ap', [{ text: 'approval on' }], { isArgCompletion: true }))
    ).toEqual(['approval on'])
    // Value completion for a blocked hub mutation stays empty.
    expect(
      filterDesktopSubcommandCompletions('/skills install ', [{ text: 'demo' }], { isArgCompletion: true })
    ).toEqual([])
    // Command-token completions pass through untouched.
    expect(subs(filterDesktopSubcommandCompletions('/sk', [{ text: '/skills' }], { isArgCompletion: false }))).toEqual([
      '/skills'
    ])
  })

  it('groups complete.slash rows by backend kind, not the desktop table', () => {
    // A registry command the table has never heard of is still a command.
    expect(slashCompletionGroup('/refine', 'command')).toBe('Commands')
    expect(slashCompletionGroup('/docx', 'skill')).toBe('Skills')
    // Older backends omit kind — fall back to the table.
    expect(slashCompletionGroup('/new')).toBe('Commands')
    expect(slashCompletionGroup('/docx')).toBe('Skills')
  })

  it('surfaces skill and quick commands (extensions) in suggestions and lets them run', () => {
    expect(isDesktopSlashSuggestion('/my-skill')).toBe(true)
    expect(isDesktopSlashSuggestion('/gif-search')).toBe(true)
    expect(isDesktopSlashCommand('/my-skill')).toBe(true)
  })

  it('does not run /login on desktop before the catalog is loaded', () => {
    rememberDesktopCommandsCatalog(undefined)
    expect(isDesktopSlashCommand('/login')).toBe(false)
    expect(desktopSlashUnavailableMessage('/login')).not.toBeNull()
  })

  it('routes /compress through the session-compression action', () => {
    // /compress must be an action (session.compress RPC), not exec: the slash
    // worker route times out on large sessions (#44456).
    expect(resolveDesktopCommand('/compress')?.surface).toEqual({ kind: 'action', action: 'compress' })
    expect(desktopSlashCommandArgumentMode('/compress')).toBe('text')
    expect(isDesktopSlashCommand('/compress')).toBe(true)
    expect(isDesktopSlashSuggestion('/compress')).toBe(true)
    expect(desktopSlashUnavailableMessage('/compress')).toBeNull()
    // /compact is an alias — executes but stays out of the popover.
    expect(resolveDesktopCommand('/compact')?.surface).toEqual({ kind: 'action', action: 'compress' })
    expect(isDesktopSlashCommand('/compact')).toBe(true)
    expect(isDesktopSlashSuggestion('/compact')).toBe(false)
  })

  it('routes only stateless session commands through dedicated gateway RPCs', () => {
    // /save is an action now: it renders on the owner (session.export) and writes through Electron (T11).
    const expected = {
      '/status': 'session.status'
    } as const

    for (const [name, rpcName] of Object.entries(expected)) {
      const surface = resolveDesktopCommand(name)?.surface
      expect(surface?.kind).toBe('rpc')

      if (surface?.kind !== 'rpc') {
        continue
      }

      expect(surface.rpc).toBe(rpcName)
      expect(surface.buildParams({ arg: 'topic A', command: name, name: name.slice(1), sessionId: 's-1' })).toEqual({
        session_id: 's-1'
      })
    }
  })

  it('still routes commands without dedicated RPCs through exec()', () => {
    // /background deliberately NOT here — it has the dedicated
    // prompt.background RPC since #97635 (the slash worker's completion
    // print lands after the capture window closes, so the result never
    // reached the originating conversation).
    // /undo /retry /steer /queue are local actions now: the shared gateway refuses them as exec.
    const execNames = ['/debug', '/goal', '/personality', '/tools', '/version']

    for (const name of execNames) {
      expect(resolveDesktopCommand(name)?.surface).toEqual({ kind: 'exec' })
    }
  })

  it('distinguishes free prose from finite slash option lists', () => {
    expect(desktopSlashCommandArgumentMode('/goal')).toBe('mixed')
    expect(desktopSlashCommandArgumentMode('/steer')).toBe('text')
    expect(desktopSlashCommandArgumentMode('/queue')).toBe('text')
    expect(desktopSlashCommandArgumentMode('/background')).toBe('text')
    expect(desktopSlashCommandArgumentMode('/personality')).toBe('options')
    expect(desktopSlashCommandArgumentMode('/handoff')).toBe('options')
    expect(desktopSlashCommandArgumentMode('/version')).toBeNull()
  })

  it('allows aliases to execute without cluttering the popover', () => {
    expect(isDesktopSlashSuggestion('/reset')).toBe(false)
    expect(isDesktopSlashCommand('/reset')).toBe(true)
    // Help advertises `/clear` as "start a new session". On the TUI that
    // also clears the terminal screen; on desktop it must take the same
    // path as `/new` instead of being marked terminal-only (#95779).
    expect(isDesktopSlashSuggestion('/clear')).toBe(false)
    expect(isDesktopSlashCommand('/clear')).toBe(true)
    expect(resolveDesktopCommand('/clear')?.surface).toEqual({ kind: 'action', action: 'new' })
  })

  it('surfaces an alias the user typed exactly, gated on desktop availability (#57641)', () => {
    // Browsing (no exact query): aliases stay hidden — popover stays lean.
    expect(isDesktopSlashSuggestionWithOptions('/reset')).toBe(false)
    // Exact typed query: the alias must appear, not "no matches".
    expect(isDesktopSlashSuggestionWithOptions('/reset', { exactAlias: '/reset' })).toBe(true)
    expect(isDesktopSlashSuggestionWithOptions('/reset', { exactAlias: 'reset' })).toBe(true)
    // Partial prefixes and other queries keep aliases hidden.
    expect(isDesktopSlashSuggestionWithOptions('/reset', { exactAlias: 're' })).toBe(false)
    expect(isDesktopSlashSuggestionWithOptions('/reset', { exactAlias: '/new' })).toBe(false)
    // A different alias never rides along with this query.
    expect(isDesktopSlashSuggestionWithOptions('/fork', { exactAlias: '/reset' })).toBe(false)
    // Aliases whose canonical has no desktop surface stay hidden even on an
    // exact match (isDesktopSlashCommand gate).
    expect(isDesktopSlashSuggestionWithOptions('/reload_mcp', { exactAlias: '/reload_mcp' })).toBe(false)
  })

  it('filters built-in catalog noise but keeps skill / quick-command extensions', () => {
    const filtered = filterDesktopCommandsCatalog({
      categories: [
        {
          name: 'Session',
          pairs: [
            ['/new', 'Start a new session'],
            ['/clear', 'Clear terminal screen']
          ]
        },
        {
          name: 'User commands',
          pairs: [['/ship-it', 'Run release checklist']]
        }
      ],
      pairs: [
        ['/new', 'Start a new session'],
        ['/model', 'Switch model'],
        ['/ship-it', 'Run release checklist']
      ],
      skill_count: 2
    })

    expect(filtered.categories).toEqual([
      { name: 'Session', pairs: [['/new', 'Start a new desktop chat']] },
      { name: 'User commands', pairs: [['/ship-it', 'Run release checklist']] }
    ])
    expect(filtered.pairs).toEqual([
      ['/new', 'Start a new desktop chat'],
      ['/ship-it', 'Run release checklist']
    ])
    // skill_count is recomputed from the filtered output (only /ship-it is an
    // extension command — /new is a built-in) so the /help footer matches what
    // the user actually sees rather than echoing the unfiltered backend total.
    expect(filtered.skill_count).toBe(1)
  })

  it('recomputes skill_count to reflect only extensions surfaced on desktop', () => {
    const filtered = filterDesktopCommandsCatalog({
      pairs: [
        ['/new', 'Start a new session'],
        ['/clear', 'Clear terminal screen'],
        ['/gif-search', 'Search for a gif'],
        ['/ship-it', 'Run release checklist']
      ],
      skill_count: 12
    })

    expect(filtered.pairs?.map(([cmd]) => cmd)).toEqual(['/new', '/gif-search', '/ship-it'])
    expect(filtered.skill_count).toBe(2)
  })

  it('builds /skin completions from desktop themes', () => {
    const completions = desktopSkinSlashCompletions(
      [
        { name: 'mono', label: 'Mono', description: 'Clean grayscale' },
        { name: 'midnight', label: 'Midnight', description: 'Deep blue' },
        { name: 'slate', label: 'Slate', description: 'Cool slate blue' }
      ],
      'mono',
      'm'
    )

    expect(completions).toEqual([
      {
        text: '/skin mono',
        display: '/skin mono',
        meta: 'Mono (current) - Clean grayscale'
      },
      {
        text: '/skin midnight',
        display: '/skin midnight',
        meta: 'Midnight - Deep blue'
      }
    ])
  })

  it('flags /model as a picker-owned command so the desktop opens the overlay', () => {
    expect(isModelPickerCommand('/model')).toBe(true)
    expect(isModelPickerCommand('/model sonnet')).toBe(true)
    expect(isModelPickerCommand('/new')).toBe(false)
    expect(isModelPickerCommand('/skills')).toBe(false)
  })

  it('gives /resume (and its aliases) a first-class session picker surface', () => {
    expect(isPickerCommand('/resume', 'session')).toBe(true)
    expect(isPickerCommand('/sessions', 'session')).toBe(true)
    expect(isPickerCommand('/switch', 'session')).toBe(true)
    // Unlike /model, /resume shows in the popover; its aliases stay hidden.
    expect(isDesktopSlashSuggestion('/resume')).toBe(true)
    expect(isDesktopSlashSuggestion('/sessions')).toBe(false)
    expect(isDesktopSlashCommand('/switch')).toBe(true)
    // The session picker is distinct from the model picker.
    expect(isModelPickerCommand('/resume')).toBe(false)
  })

  it('resolves commands and aliases to their declared surface', () => {
    expect(resolveDesktopCommand('/new')?.surface).toEqual({ kind: 'action', action: 'new' })
    expect(resolveDesktopCommand('/reset')?.surface).toEqual({ kind: 'action', action: 'new' })
    expect(resolveDesktopCommand('/clear')?.surface).toEqual({ kind: 'action', action: 'new' })
    expect(resolveDesktopCommand('/resume')?.surface).toEqual({ kind: 'picker', picker: 'session' })
    expect(resolveDesktopCommand('/usage')?.surface).toEqual({ kind: 'exec' })
    expect(desktopSlashUnavailableMessage('/clear')).toBeNull()
    // Skill / quick commands aren't in the registry.
    expect(resolveDesktopCommand('/gif-search')).toBeNull()
  })
})

describe('rankSkillCommands', () => {
  const rows = [
    { text: '/research' },
    { text: '/research-paper-writing' },
    { text: '/work' },
    { text: '/ship-it' },
    { text: '/manim-video' },
    { text: '/docx' }
  ]

  const skills = {
    '/research': { usage: 60, origin: 'local' as const },
    '/research-paper-writing': { usage: 0, origin: 'bundled' as const },
    '/work': { usage: 172, origin: 'local' as const },
    '/manim-video': { usage: 0, origin: 'bundled' as const },
    '/docx': { usage: 0, origin: 'local' as const }
  }

  it('puts the most-used skill first and breaks ties alphabetically', () => {
    expect(rankSkillCommands(rows, skills).map(row => row.text)).toEqual([
      '/work',
      '/research',
      '/docx',
      '/manim-video',
      '/research-paper-writing',
      '/ship-it'
    ])
  })

  it('drops never-used built-ins when browsing, keeping everything else', () => {
    const browsing = rankSkillCommands(rows, skills, { pruneUnusedBuiltins: true }).map(row => row.text)

    expect(browsing).toEqual(['/work', '/research', '/docx', '/ship-it'])
    // A user's own unused skill survives — only shipped-and-ignored goes.
    expect(browsing).toContain('/docx')
    // Unclassified rows (quick commands, skills newer than the map) survive too.
    expect(browsing).toContain('/ship-it')
  })

  it('leaves the backend order untouched when the catalog carries no usage', () => {
    expect(rankSkillCommands(rows, undefined, { pruneUnusedBuiltins: true })).toEqual(rows)
  })

  it('ranks an alias by the canonical command it resolves to', () => {
    const ranked = rankSkillCommands([{ text: '/sessions' }, { text: '/research' }], {
      '/research': { usage: 5, origin: 'local' },
      '/resume': { usage: 900, origin: 'local' }
    })

    expect(ranked.map(row => row.text)).toEqual(['/sessions', '/research'])
  })
})

describe('registry-derived block-list (contract with hermes_cli/commands.py)', () => {
  beforeEach(() => rememberDesktopCommandsCatalog(undefined))

  it('marks every registry row with a reason unavailable offline, without a hand-typed copy', () => {
    for (const [name, reason] of Object.entries(desktopSlashRegistry)) {
      if (reason === null || reason === 'hidden') {
        continue
      }

      const spec = resolveDesktopCommand(name)

      // A desktop-owned action (e.g. /model picker) may override the registry.
      if (spec?.surface.kind === 'unavailable') {
        expect(spec.surface.reason).toBe(reason)
      }

      expect(isDesktopSlashSuggestion(name)).toBe(false)
    }
  })

  it('runs a typed /approve or /deny on the owner like every surface (the approval card stays approval.respond)', () => {
    for (const name of ['/approve', '/deny']) {
      expect(desktopSlashRegistry[name as keyof typeof desktopSlashRegistry]).toBeNull()
      expect(isDesktopSlashCommand(name, 'all')).toBe(true)
      expect(resolveDesktopCommand(name)?.surface.kind).toBe('exec')
      expect(desktopSlashUnavailableMessage(name)).toBeNull()
    }
  })

  it('recognizes offered built-ins and their aliases offline as Commands, never as skills (#116159)', () => {
    // Cold catalog: nothing remembered, nothing cached. /context has no
    // desktop disposition and no hand-typed TS row, so only the dump can
    // vouch for it — and it must, or the popover files it under Skills and
    // Enter takes the extension path.
    for (const name of ['/context', '/ctx', '/usage']) {
      expect(desktopSlashRegistry[name as keyof typeof desktopSlashRegistry]).toBeNull()
      expect(isDesktopSlashExtensionCommand(name)).toBe(false)
      expect(slashCompletionGroup(name)).toBe('Commands')
      expect(isDesktopSlashCommand(name)).toBe(true)
      expect(resolveDesktopCommand(name)?.surface.kind).toBe('exec')
    }

    // Control: an unknown skill command still groups as a skill offline.
    expect(slashCompletionGroup('/gif-search')).toBe('Skills')
  })

  it('keeps the TS-only list disjoint from the registry dump', () => {
    for (const names of Object.values(TS_ONLY_NO_DESKTOP_SURFACE)) {
      for (const name of names) {
        expect(name in desktopSlashRegistry, `${name} is in the Python registry — drop the TS row`).toBe(false)
        expect(isDesktopSlashSuggestion(name)).toBe(false)
        expect(isDesktopSlashCommand(name)).toBe(false)
      }
    }
  })
})