Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.8 KB
Raw Blame
// @vitest-environment node

const mocks = vi.hoisted(() => ({
  loadResource: vi.fn(),
  cookies: vi.fn(),
  headers: vi.fn(),
  getCookie: vi.fn(),
}))

vi.mock('@/next/headers', () => ({ cookies: mocks.cookies, headers: mocks.headers }))
vi.mock('../load-resource', () => ({ loadI18nResource: mocks.loadResource }))

describe('server translations', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    vi.resetModules()
    mocks.loadResource.mockImplementation(async (locale: string, namespace: string) => ({
      default:
        namespace === 'common'
          ? locale === 'en-US'
            ? { 'operation.save': 'Save', 'operation.cancel': 'Cancel' }
            : { 'operation.save': '保存' }
          : {},
    }))
  })

  it('does not preload the complete catalog for namespaced server metadata', async () => {
    const { getTranslation } = await import('../server')
    await getTranslation('en-US', 'login')
    expect(mocks.loadResource).toHaveBeenCalledExactlyOnceWith('en-US', 'login')
  })

  it('loads only the requested tuple and translates across its namespaces', async () => {
    mocks.loadResource.mockImplementation(async (_locale: string, namespace: string) => ({
      default:
        namespace === 'common'
          ? { 'operation.save': 'Save' }
          : { 'marketplace.home.plugins': 'Plugins' },
    }))
    const { getTranslation } = await import('../server')
    const { t } = await getTranslation('en-US', ['common', 'plugin'])

    expect(t(($) => $['operation.save'])).toBe('Save')
    expect(t(($) => $['marketplace.home.plugins'], { ns: 'plugin' })).toBe('Plugins')
    expect(mocks.loadResource.mock.calls.map(([, ns]) => ns).sort()).toEqual(['common', 'plugin'])
  })

  it('keeps fallback and cross-namespace translations available', async () => {
    const { getTranslation } = await import('../server')
    const { t } = await getTranslation('zh-Hans', 'common')

    expect(t(($) => $['operation.save'], { ns: 'common' })).toBe('保存')
    expect(t(($) => $['operation.cancel'], { ns: 'common' })).toBe('Cancel')
  })

  it('initializes the requested language independently of an earlier consumer', async () => {
    const { getTranslation } = await import('../server')
    const english = await getTranslation('en-US', 'common')
    const chinese = await getTranslation('zh-Hans', 'common')

    expect(chinese.t(($) => $['operation.save'], { ns: 'common' })).toBe('保存')
    expect(english.t(($) => $['operation.save'], { ns: 'common' })).toBe('Save')
  })
})

describe('server locale', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    vi.resetModules()
    mocks.getCookie.mockReturnValue(undefined)
    mocks.cookies.mockResolvedValue({ get: mocks.getCookie })
    mocks.headers.mockResolvedValue(new Headers())
  })

  it('prefers the locale cookie over the browser language', async () => {
    mocks.getCookie.mockReturnValue({ value: 'zh-Hans' })
    mocks.headers.mockResolvedValue(new Headers({ 'accept-language': 'en-US' }))
    const { getLocaleOnServer } = await import('../server')

    await expect(getLocaleOnServer()).resolves.toBe('zh-Hans')
    expect(mocks.headers).not.toHaveBeenCalled()
  })

  it.each([undefined, 'bad value'])(
    'uses the default language for an absent or unusable preference: %s',
    async (locale) => {
      mocks.getCookie.mockReturnValue(locale ? { value: locale } : undefined)
      const { getLocaleOnServer } = await import('../server')

      await expect(getLocaleOnServer()).resolves.toBe('en-US')
    },
  )

  it.each([
    ['en_US', 'en-US'],
    ['zh_Hans', 'zh-Hans'],
    ['ja_JP', 'ja-JP'],
    ['ZH-hans', 'zh-Hans'],
  ])('normalizes the saved locale %s to %s', async (saved, expected) => {
    mocks.getCookie.mockReturnValue({ value: saved })
    const { getLocaleOnServer } = await import('../server')

    await expect(getLocaleOnServer()).resolves.toBe(expected)
    expect(mocks.headers).not.toHaveBeenCalled()
  })

  it.each(['en--US', 'en_US_invalid', '*', 'zz-ZZ'])(
    'falls back for an unusable cookie without consulting the browser: %s',
    async (saved) => {
      mocks.getCookie.mockReturnValue({ value: saved })
      mocks.headers.mockResolvedValue(new Headers({ 'accept-language': 'ja-JP' }))
      const { getLocaleOnServer } = await import('../server')

      await expect(getLocaleOnServer()).resolves.toBe('en-US')
      expect(mocks.headers).not.toHaveBeenCalled()
    },
  )

  it.each([
    ['en--US,ja-JP;q=0.9,en-US;q=0.8', 'ja-JP'],
    ['ja-JP,en-US;q=0.8,*;q=0.5', 'ja-JP'],
    ['en-US;q=0.5,zh-CN;q=0.9', 'zh-Hans'],
    ['*,en--US;q=0.5', 'en-US'],
    ['ja-JP;q=0,en-US;q=0.5', 'en-US'],
  ])('negotiates valid header preferences from %s', async (header, expected) => {
    mocks.headers.mockResolvedValue(new Headers({ 'accept-language': header }))
    const { getLocaleOnServer } = await import('../server')

    await expect(getLocaleOnServer()).resolves.toBe(expected)
  })
})