Skip to content

The agent that grows with you

21.7 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import type { HermesConfigRecord } from '@/types/hermes'

import { BUILTIN_PERSONALITIES } from './constants'
import { defineFieldCopy, fieldCopyForSchemaKey, schemaKeyToFieldCopyKey } from './field-copy'
import {
  clearsEnabledToolsets,
  configFieldValue,
  credentialPreview,
  diffConfig,
  enumOptionsFor,
  getNested,
  isExternalMemoryProvider,
  providerGroup,
  sectionFieldEntries,
  setConfigField,
  setNested,
  stripToolsetLabel
} from './helpers'

describe('settings helpers', () => {
  it('does not shadow the backend schema options for memory.provider', () => {
    // memory.provider options are discovery-driven and served by the backend
    // config schema (merged per-request); enumOptionsFor must return undefined
    // so config-field consumes schema.options instead of a stale static list.
    expect(enumOptionsFor('memory.provider', '', {})).toBeUndefined()
    expect(enumOptionsFor('memory.provider', 'honcho', {})).toBeUndefined()
  })

  describe('isExternalMemoryProvider', () => {
    it('treats only real plugin names as external providers', () => {
      expect(isExternalMemoryProvider('honcho')).toBe(true)
      expect(isExternalMemoryProvider('hindsight')).toBe(true)
    })

    it('treats built-in aliases and empty values as not external', () => {
      for (const value of ['', 'builtin', 'built-in', 'Builtin', 'none', 'Default', '  ', undefined, null, 7]) {
        expect(isExternalMemoryProvider(value)).toBe(false)
      }
    })
  })

  describe('defineFieldCopy', () => {
    it('flattens nested field copy paths', () => {
      const copy = defineFieldCopy({
        display: {
          personality: 'Personality'
        },
        stt: {
          elevenlabs: {
            language_code: 'Language'
          }
        }
      })

      expect(copy[['display', 'personality'].join('.')]).toBe('Personality')
      expect(copy[['stt', 'elevenlabs', 'language_code'].join('.')]).toBe('Language')
    })

    it('maps schema keys to camelCase translation keys', () => {
      expect(schemaKeyToFieldCopyKey('model_context_length')).toBe('modelContextLength')
      expect(schemaKeyToFieldCopyKey('display.show_reasoning')).toBe('display.showReasoning')
      expect(schemaKeyToFieldCopyKey('tool_output.max_line_length')).toBe('toolOutput.maxLineLength')
      expect(schemaKeyToFieldCopyKey('updates.non_interactive_local_changes')).toBe(
        'updates.nonInteractiveLocalChanges'
      )
    })

    it('looks up camelCase field copy by schema key with legacy fallback', () => {
      const copy = defineFieldCopy({
        display: {
          showReasoning: 'Reasoning Blocks'
        },
        file_read_max_chars: 'Legacy File Read Limit',
        modelContextLength: 'Context Window',
        toolOutput: {
          maxLineLength: 'Line Length Limit'
        }
      })

      expect(fieldCopyForSchemaKey(copy, 'model_context_length')).toBe('Context Window')
      expect(fieldCopyForSchemaKey(copy, 'display.show_reasoning')).toBe('Reasoning Blocks')
      expect(fieldCopyForSchemaKey(copy, 'tool_output.max_line_length')).toBe('Line Length Limit')
      expect(fieldCopyForSchemaKey(copy, 'file_read_max_chars')).toBe('Legacy File Read Limit')
    })

    it('rejects duplicate flattened paths', () => {
      const duplicateKey = ['display', 'personality'].join('.')

      expect(() =>
        defineFieldCopy({
          display: {
            personality: 'Personality'
          },
          [duplicateKey]: 'Duplicate'
        })
      ).toThrow('Duplicate field copy key: display.personality')
    })
  })

  it('reads and writes nested config paths', () => {
    const config: HermesConfigRecord = { display: { theme: 'mono' } }
    const next = setNested(config, 'display.theme', 'slate')

    expect(getNested(next, 'display.theme')).toBe('slate')
    expect(getNested(config, 'display.theme')).toBe('mono')
  })

  it('Reasoning Blocks shows and writes the local key a local /reasoning saved', () => {
    // After a local `/reasoning hide`: the local key wins on every local surface.
    const config: HermesConfigRecord = { display: { show_reasoning: true, platforms: { cli: { show_reasoning: false } } } }

    expect(configFieldValue(config, 'display.show_reasoning')).toBe(false)
    const next = setConfigField(config, 'display.show_reasoning', true)

    expect(getNested(next, 'display.platforms.cli.show_reasoning')).toBe(true)
    expect(getNested(next, 'display.show_reasoning')).toBe(true)
    expect(configFieldValue(next, 'display.show_reasoning')).toBe(true)
    // The autosave patch carries the local key, so PUT /api/config's deep merge replaces it.
    expect(diffConfig(config, next)).toEqual({ display: { platforms: { cli: { show_reasoning: true } } } })
    // Other fields are untouched.
    expect(setConfigField({}, 'display.skin', 'ares')).toEqual({ display: { skin: 'ares' } })
  })

  it('Reasoning Blocks reads a quoted local value as the gateway does', () => {
    const shown = (value: unknown) =>
      configFieldValue({ display: { show_reasoning: true, platforms: { cli: { show_reasoning: value } } } } as HermesConfigRecord, 'display.show_reasoning')

    for (const off of ['false', 'False', '0', 'no', 'off', ' false ']) {
      expect(shown(off)).toBe(false)
    }

    for (const on of ['true', '1', 'yes', 'on']) {
      expect(shown(on)).toBe(true)
    }

    expect(configFieldValue({ display: { show_reasoning: 'false' } }, 'display.show_reasoning')).toBe(false)
  })

  it('rejects prototype-polluting config paths', () => {
    const config: HermesConfigRecord = {}

    expect(() => setNested(config, '__proto__.polluted', true)).toThrow('Unsafe config path')
    expect(() => setNested(config, 'constructor.prototype.polluted', true)).toThrow('Unsafe config path')
    expect(({} as Record<string, unknown>).polluted).toBeUndefined()
  })

  describe('stripToolsetLabel', () => {
    it('removes leading emoji prefixes from registry labels', () => {
      expect(stripToolsetLabel('⏰ Cron Jobs')).toBe('Cron Jobs')
      expect(stripToolsetLabel('⚡ Code Execution')).toBe('Code Execution')
      expect(stripToolsetLabel('❓ Clarifying Questions')).toBe('Clarifying Questions')
      expect(stripToolsetLabel('🌐 Browser Automation')).toBe('Browser Automation')
      expect(stripToolsetLabel('🎨 Image Generation')).toBe('Image Generation')
    })

    it('leaves plain titles unchanged', () => {
      expect(stripToolsetLabel('Terminal & Processes')).toBe('Terminal & Processes')
    })
  })

  describe('providerGroup', () => {
    it('prefers the longest matching prefix so CN/regional buckets win', () => {
      // MINIMAX_CN_ must beat the generic MINIMAX_ prefix.
      expect(providerGroup('MINIMAX_CN_API_KEY')).toBe('MiniMax (China)')
      expect(providerGroup('MINIMAX_API_KEY')).toBe('MiniMax')
      // KIMI_CN_ likewise must beat KIMI_.
      expect(providerGroup('KIMI_CN_API_KEY')).toBe('Kimi (China)')
      expect(providerGroup('KIMI_API_KEY')).toBe('Kimi / Moonshot')
      // HERMES_QWEN_ shares the HERMES_ stem with other integrations.
      expect(providerGroup('HERMES_QWEN_BASE_URL')).toBe('DashScope (Qwen)')
      expect(providerGroup('GEMINI_API_KEY')).toBe('Gemini')
    })

    it('falls back to "Other" for un-grouped env vars', () => {
      expect(providerGroup('SOMETHING_RANDOM')).toBe('Other')
    })
  })

  describe('enumOptionsFor — backend selector dropdowns', () => {
    const config: HermesConfigRecord = {}

    it('narrows OpenAI TTS voice suggestions to what the selected model supports', () => {
      // gpt-4o-mini-tts (and unset/unknown models): full 13-voice set.
      const full = enumOptionsFor('tts.openai.voice', 'alloy', { tts: { openai: { model: 'gpt-4o-mini-tts' } } })
      expect(full).toContain('marin')
      expect(full).toContain('cedar')
      expect(full).toContain('ballad')
      expect(full).toContain('verse')
      expect(full).toHaveLength(13)

      // tts-1 / tts-1-hd: the 9-voice set — no ballad/verse/marin/cedar.
      for (const model of ['tts-1', 'tts-1-hd']) {
        const narrowed = enumOptionsFor('tts.openai.voice', 'alloy', { tts: { openai: { model } } })
        expect(narrowed).toEqual(['alloy', 'ash', 'coral', 'echo', 'fable', 'nova', 'onyx', 'sage', 'shimmer'])
      }

      // A hand-typed custom voice still stays selectable on tts-1.
      const custom = enumOptionsFor('tts.openai.voice', 'my-cloned-voice', { tts: { openai: { model: 'tts-1' } } })
      expect(custom).toContain('my-cloned-voice')
    })

    it('appends a hand-typed value not in the known list so it stays selected', () => {
      const opts = enumOptionsFor('tts.provider', 'my-custom-command-tts', config)
      expect(opts).toContain('my-custom-command-tts')
      expect(opts).toContain('xai')
    })

    it('surfaces user-defined command-type TTS providers (canonical providers nesting + legacy)', () => {
      const withCustom: HermesConfigRecord = {
        tts: {
          provider: 'neutts',
          // canonical location the runtime resolves first: tts.providers.<name>
          providers: {
            higgs8: { type: 'command', command: 'curl …' },
            indextts2: { type: 'command', command: 'curl …' },
            // `type:` is optional at runtime — a bare command block still qualifies
            typeless: { command: 'curl …' },
            // misconfigured: type:command but no command → NOT a runtime provider
            noop: { type: 'command' }
          },
          // back-compat: a top-level tts.<name> command block still resolves at runtime
          mylegacy: { type: 'command', command: 'curl …' },
          // a non-command block (built-in config) must NOT be offered as a provider
          edge: { voice: 'en-US-JennyNeural' }
        }
      }

      const opts = enumOptionsFor('tts.provider', 'neutts', withCustom)
      expect(opts).toContain('higgs8') // canonical providers.<name>
      expect(opts).toContain('indextts2') // canonical providers.<name>
      expect(opts).toContain('typeless') // command block with no type: still surfaced
      expect(opts).toContain('mylegacy') // legacy top-level tts.<name>
      expect(opts).toContain('elevenlabs') // built-ins preserved
      expect(opts).not.toContain('noop') // type:command with no command is excluded
      // 'edge' appears once (the built-in), not duplicated by the config block
      expect(opts!.filter(o => o === 'edge')).toHaveLength(1)
      // the 'providers' container itself is never offered as a provider name
      expect(opts).not.toContain('providers')
    })

    it('surfaces command-type STT providers too (canonical providers nesting)', () => {
      const withCustom: HermesConfigRecord = {
        stt: {
          provider: 'local',
          providers: { myasr: { type: 'command', command: 'curl …' } }
        }
      }

      const opts = enumOptionsFor('stt.provider', 'local', withCustom)
      expect(opts).toContain('myasr')
      expect(opts).toContain('local')
      expect(opts).not.toContain('providers')
    })

    // The runtime rejects a built-in name as a command provider before any config
    // lookup, so such a block must never be offered — including the names the
    // display list omits (`deepinfra` for TTS; `deepinfra`/`local_command` for
    // STT), where filtering on ENUM_OPTIONS instead of the runtime's built-in set
    // would wrongly offer a provider that can never dispatch.
    it('never offers a built-in name as a command provider, even one absent from the dropdown list', () => {
      const shadowing: HermesConfigRecord = {
        tts: {
          provider: 'edge',
          providers: {
            // built-in and absent from ENUM_OPTIONS['tts.provider']
            deepinfra: { type: 'command', command: 'curl …' },
            // built-in guard is case-insensitive at runtime (provider.lower())
            EDGE: { type: 'command', command: 'curl …' },
            // a genuine custom provider alongside them still surfaces
            higgs8: { type: 'command', command: 'curl …' }
          }
        }
      }

      const opts = enumOptionsFor('tts.provider', 'edge', shadowing)
      expect(opts).not.toContain('deepinfra')
      expect(opts).not.toContain('EDGE')
      expect(opts).toContain('higgs8')
      expect(opts!.filter(o => o === 'edge')).toHaveLength(1)
    })

    it('never offers a built-in STT name absent from the dropdown list as a command provider', () => {
      const shadowing: HermesConfigRecord = {
        stt: {
          provider: 'local',
          providers: {
            // built-in STT names: local_command is unlisted, deepinfra is listed
            local_command: { type: 'command', command: 'curl …' },
            deepinfra: { type: 'command', command: 'curl …' },
            myasr: { type: 'command', command: 'curl …' }
          }
        }
      }

      const opts = enumOptionsFor('stt.provider', 'local', shadowing)
      expect(opts).not.toContain('local_command')
      // deepinfra is a listed built-in: it appears once, as the native provider.
      expect(opts!.filter(o => o === 'deepinfra')).toHaveLength(1)
      expect(opts).toContain('myasr')
    })
  })

  describe('enumOptionsFor — display.personality dropdown', () => {
    it('lists a root-level `personalities` block alongside the built-ins (#123297)', () => {
      // The Python spec (`hermes_cli.personality.available_personalities`) overlays
      // the built-ins with the root `personalities` block then `agent.personalities`;
      // the dropdown must surface a root-registered persona the CLI/gateway resolve.
      const config: HermesConfigRecord = { personalities: { root_persona: { prompt: 'hi' } } }
      const opts = enumOptionsFor('display.personality', '', config)

      // Derive the expected built-ins from the source of truth, per the repo's
      // change-detector rule — adding a built-in must not silently break this.
      for (const builtin of BUILTIN_PERSONALITIES) {
        expect(opts).toContain(builtin)
      }

      expect(opts).toContain('') // the "unset" sentinel
      expect(opts).toContain('root_persona')
    })

    it('merges root and agent personalities, deduping a clashing name', () => {
      const config: HermesConfigRecord = {
        personalities: { root_persona: {}, shared: {} },
        agent: { personalities: { agent_persona: {}, shared: {} } }
      }

      const opts = enumOptionsFor('display.personality', '', config)!
      expect(opts).toContain('root_persona')
      expect(opts).toContain('agent_persona')
      // a name in both blocks is offered exactly once
      expect(opts.filter(o => o === 'shared')).toHaveLength(1)
    })

    it('ignores a non-object or array `personalities` block', () => {
      for (const bad of [[], 'nope', 42, null]) {
        const opts = enumOptionsFor('display.personality', '', { personalities: bad } as HermesConfigRecord)!
        // still the built-ins + empty sentinel, no crash on a malformed block
        expect(opts).toContain('')

        for (const builtin of BUILTIN_PERSONALITIES) {
          expect(opts).toContain(builtin)
        }
      }
    })

    it('folds custom keys like the runtime so only resolvable rows are offered', () => {
      // The runtime folds each key (`str(name).strip().lower()`) and drops the neutral
      // spellings; without matching that, the dropdown offers a case-variant duplicate,
      // a whitespace-padded name, or a neutral name the runtime canonicalises away —
      // rows the user can pick but that never load the definition shown (#123297).
      const config: HermesConfigRecord = {
        personalities: { Catgirl: {}, '  Spaced  ': {}, none: {}, Default: {}, NEUTRAL: {} }
      } as HermesConfigRecord

      const opts = enumOptionsFor('display.personality', '', config)!

      // `Catgirl` folds to the built-in `catgirl` (offered once, not twice).
      expect(opts.filter(o => o === 'catgirl')).toHaveLength(1)
      expect(opts).not.toContain('Catgirl')
      // whitespace folded to the canonical key.
      expect(opts).toContain('spaced')
      expect(opts).not.toContain('  Spaced  ')

      // neutral spellings never surface as selectable rows (only the '' sentinel remains).
      for (const neutral of ['none', 'Default', 'NEUTRAL', 'default', 'neutral']) {
        expect(opts).not.toContain(neutral)
      }
    })
  })

  describe('sectionFieldEntries', () => {
    it('renders memory.provider from config even when the backend schema omits it', () => {
      const schema = { 'memory.memory_enabled': { type: 'boolean' as const } }
      const config: HermesConfigRecord = { memory: { memory_enabled: true, provider: '' } }

      const memoryKeys = (sectionFieldEntries(schema, config).get('memory') ?? []).map(([key]) => key)

      expect(memoryKeys).toContain('memory.provider')
    })

    it('infers the field type from the config value when the schema omits the key', () => {
      const config: HermesConfigRecord = { memory: { provider: '', memory_enabled: true, memory_char_limit: 2200 } }

      const fields = new Map(sectionFieldEntries({}, config).get('memory') ?? [])

      expect(fields.get('memory.provider')?.type).toBe('string')
      expect(fields.get('memory.memory_enabled')?.type).toBe('boolean')
      expect(fields.get('memory.memory_char_limit')?.type).toBe('number')
    })

    it('prefers the backend schema entry over inference when both exist', () => {
      const schema = { 'memory.provider': { type: 'select' as const, options: ['honcho'] } }
      const config: HermesConfigRecord = { memory: { provider: 'honcho' } }

      const field = new Map(sectionFieldEntries(schema, config).get('memory') ?? []).get('memory.provider')

      expect(field?.type).toBe('select')
      expect(field?.options).toEqual(['honcho'])
    })

    it('hides declared keys absent from both schema and config', () => {
      expect(sectionFieldEntries({}, {}).get('memory') ?? []).toHaveLength(0)
    })
  })

  describe('clearsEnabledToolsets', () => {
    it('flags a non-empty → empty transition', () => {
      const prev: HermesConfigRecord = { toolsets: ['memory', 'terminal', 'web_search'] }
      const next: HermesConfigRecord = { toolsets: [] }

      expect(clearsEnabledToolsets(prev, next)).toBe(true)
    })

    it('does not flag a non-empty → missing transition (deep-merge preserves the key)', () => {
      // PUT /api/config deep-merges the override onto the stored config, so an
      // import that omits `toolsets` keeps the existing list — no wipe happens,
      // so there is nothing to confirm.
      const prev: HermesConfigRecord = { toolsets: ['memory'] }
      const next: HermesConfigRecord = {}

      expect(clearsEnabledToolsets(prev, next)).toBe(false)
    })

    it('does not flag when at least one toolset remains', () => {
      const prev: HermesConfigRecord = { toolsets: ['memory', 'terminal'] }
      const next: HermesConfigRecord = { toolsets: ['memory'] }

      expect(clearsEnabledToolsets(prev, next)).toBe(false)
    })

    it('does not flag when the list was already empty', () => {
      const prev: HermesConfigRecord = { toolsets: [] }
      const next: HermesConfigRecord = { toolsets: [] }

      expect(clearsEnabledToolsets(prev, next)).toBe(false)
    })

    it('does not flag an unrelated edit that never touched toolsets', () => {
      const prev: HermesConfigRecord = { model: 'a', toolsets: ['memory'] }
      const next: HermesConfigRecord = { model: 'b', toolsets: ['memory'] }

      expect(clearsEnabledToolsets(prev, next)).toBe(false)
    })
  })

  describe('diffConfig', () => {
    it('omits a top-level key the draft never touched', () => {
      // The autosave baseline is a snapshot taken when Settings opened. A key
      // an agent set via `hermes config set` while the page sat open must not
      // come back in the patch just because it's still present in the draft.
      const baseline: HermesConfigRecord = { fallback_providers: ['nara1'], timezone: 'UTC' }
      const draft: HermesConfigRecord = { fallback_providers: ['nara1'], timezone: 'America/New_York' }

      expect(diffConfig(baseline, draft)).toEqual({ timezone: 'America/New_York' })
    })

    it('includes a nested key only when it actually changed, leaving siblings out', () => {
      const baseline: HermesConfigRecord = { display: { personality: 'default', show_reasoning: true } }
      const draft: HermesConfigRecord = { display: { personality: 'default', show_reasoning: false } }

      expect(diffConfig(baseline, draft)).toEqual({ display: { show_reasoning: false } })
    })

    it('sends a new key that was absent from the baseline', () => {
      const baseline: HermesConfigRecord = {}
      const draft: HermesConfigRecord = { timezone: 'UTC' }

      expect(diffConfig(baseline, draft)).toEqual({ timezone: 'UTC' })
    })

    it('returns an empty object when the draft matches the baseline exactly', () => {
      const baseline: HermesConfigRecord = { toolsets: ['memory'], display: { personality: 'default' } }
      const draft: HermesConfigRecord = { toolsets: ['memory'], display: { personality: 'default' } }

      expect(diffConfig(baseline, draft)).toEqual({})
    })

    it('treats an array as a whole value, not diffed element by element', () => {
      const baseline: HermesConfigRecord = { toolsets: ['memory', 'terminal'] }
      const draft: HermesConfigRecord = { toolsets: ['memory'] }

      expect(diffConfig(baseline, draft)).toEqual({ toolsets: ['memory'] })
    })
  })
})

describe('credentialPreview', () => {
  it('unwraps the backend preview sentinel and masks label-less forms', () => {
    expect(credentialPreview('«redacted:sk-h...JPJ8»')).toBe('sk-h...JPJ8')
    expect(credentialPreview('«redacted-secret»')).toBe('••••••••')
    expect(credentialPreview('«redacted-vault-secret»')).toBe('••••••••')
    expect(credentialPreview('sk-h...JPJ8')).toBe('sk-h...JPJ8')
    expect(credentialPreview(null)).toBeNull()
  })
})