Skip to content

The agent that grows with you

5.5 KB
Raw Blame
import { JsonRpcGatewayError } from '@hermes/shared'
import { afterEach, describe, expect, it, vi } from 'vitest'

import { type BackendLocaleRequest, syncBackendLocalePacks } from './backend-packs'
import { TRANSLATIONS } from './catalog'
import { isSupportedLocaleValue, languageOptions } from './languages'
import { isRegisteredLocale, registerAppLocale, resetAppLocaleRegistry, resolveTranslations } from './registry'

afterEach(() => {
  resetAppLocaleRegistry()
  vi.restoreAllMocks()
})

type Call = [string, Record<string, unknown>]

const backend =
  (languages: unknown, catalogs: Record<string, Record<string, string>>, calls: Call[] = []): BackendLocaleRequest =>
  async <T>(method: string, params: Record<string, unknown>) => {
    calls.push([method, params])

    if (method === 'i18n.languages') {
      return languages as T
    }

    if (method === 'i18n.catalog') {
      const lang = String(params.lang)

      return { lang, surface: params.surface, messages: catalogs[lang] ?? {} } as T
    }

    throw new JsonRpcGatewayError(`unknown method: ${method}`, { code: -32601 })
  }

describe('syncBackendLocalePacks', () => {
  it('registers the backend language list and the desktop pack for the requested language', async () => {
    const calls: Call[] = []

    const request = backend(
      {
        languages: [
          { id: 'en', endonym: 'English', rtl: false, source: 'bundled' },
          { id: 'pl', endonym: 'Polski', rtl: false, source: 'plugin:hermes-lang-pl' }
        ]
      },
      { pl: { 'common.save': 'Zapisz', 'connectorsPage.searchPlaceholder': '{0} wyników' } },
      calls
    )

    await syncBackendLocalePacks(request, 'pl')

    expect(calls).toContainEqual(['i18n.catalog', { lang: 'pl', surface: 'desktop' }])
    expect(isRegisteredLocale('pl')).toBe(true)
    expect(languageOptions().find(option => option.id === 'pl')).toMatchObject({ endonym: 'Polski', source: 'backend' })
    expect(resolveTranslations('pl').common.save).toBe('Zapisz')
    expect(resolveTranslations('pl').connectorsPage.searchPlaceholder(5)).toBe('5 wyników')
    expect(resolveTranslations('pl').common.cancel).toBe(TRANSLATIONS.en.common.cancel)
  })

  it('accepts a bare array from i18n.languages and skips the catalog call without a requested language', async () => {
    const calls: Call[] = []

    await syncBackendLocalePacks(backend([{ id: 'uk', endonym: 'Українська' }], {}, calls), null)

    expect(calls).toEqual([['i18n.languages', {}]])
    expect(isRegisteredLocale('uk')).toBe(true)
  })

  it('leaves out a language the backend has no Desktop strings for, and keeps rows from older backends', async () => {
    const request = backend(
      {
        languages: [
          { id: 'ko', endonym: '한국어', rtl: false, source: 'bundled', surfaces: ['core', 'tui'] },
          { id: 'it', endonym: 'Italiano', rtl: false, source: 'plugin:hermes-lang-it', surfaces: ['core', 'desktop'] },
          { id: 'ja', endonym: '日本語', rtl: false, source: 'bundled', surfaces: ['core', 'tui'] },
          { id: 'uk', endonym: 'Українська' }
        ]
      },
      {}
    )

    await syncBackendLocalePacks(request, 'ko')

    // Picking ko here would only paint English: it is not offered and not renderable.
    expect(isRegisteredLocale('ko')).toBe(false)
    expect(isSupportedLocaleValue('ko')).toBe(false)
    expect(languageOptions().map(option => option.id)).not.toContain('ko')
    // A pack with Desktop strings, a Desktop-bundled language, and a row without `surfaces` stay.
    expect(isRegisteredLocale('it')).toBe(true)
    expect(isRegisteredLocale('ja')).toBe(true)
    expect(isRegisteredLocale('uk')).toBe(true)
  })

  it('offers a language once its Desktop pack arrives', async () => {
    const request = backend(
      {
        languages: [
          {
            id: 'ko',
            endonym: '한국어',
            rtl: false,
            source: 'plugin:hermes-lang-ko',
            surfaces: ['core', 'tui', 'desktop']
          }
        ]
      },
      { ko: { 'settings.appearance.modelPricingTitle': '모델 가격' } }
    )

    await syncBackendLocalePacks(request, 'ko')

    expect(languageOptions().find(option => option.id === 'ko')).toMatchObject({ endonym: '한국어', source: 'backend' })
    expect(resolveTranslations('ko').settings.appearance.modelPricingTitle).toBe('모델 가격')
  })

  it('is silent on a backend that predates the i18n methods and leaves the registry alone', async () => {
    const debug = vi.spyOn(console, 'debug').mockImplementation(() => {})
    registerAppLocale('pl', { endonym: 'Polski' }, 'backend')

    const request: BackendLocaleRequest = async () => {
      throw new JsonRpcGatewayError('unknown method: i18n.languages', { code: -32601 })
    }

    await expect(syncBackendLocalePacks(request, 'pl')).resolves.toBeUndefined()

    expect(debug).not.toHaveBeenCalled()
    expect(isRegisteredLocale('pl')).toBe(true)
  })

  it('replaces the previous backend layer and never applies a stale reply', async () => {
    await syncBackendLocalePacks(backend([{ id: 'pl', endonym: 'Polski' }], {}), null)
    expect(isRegisteredLocale('pl')).toBe(true)

    await syncBackendLocalePacks(backend([{ id: 'uk', endonym: 'Українська' }], {}), null)
    expect(isRegisteredLocale('pl')).toBe(false)
    expect(isRegisteredLocale('uk')).toBe(true)

    await syncBackendLocalePacks(backend([{ id: 'pt-br', endonym: 'Português' }], {}), null, () => false)
    expect(isRegisteredLocale('uk')).toBe(true)
    expect(isRegisteredLocale('pt-br')).toBe(false)
  })
})