Newer
Older
navi-1 / webclient / tests / unit / utils / hash.test.js
import { describe, it, expect } from 'vitest'
import { isSettingsHash, settingsHash, settingsTabFromHash } from '@/utils/hash.js'

// The hash is what tells the app where it is, so the one thing that must never
// happen is `#settings/app` being read as the id of a session.
describe('settings hash', () => {
  it('accepts both the bare settings hash and a section', () => {
    expect(isSettingsHash('#settings')).toBe(true)
    expect(isSettingsHash('#settings/app')).toBe(true)
    expect(isSettingsHash('#settings/mcp')).toBe(true)
  })

  it('leaves anything else to the chat route', () => {
    expect(isSettingsHash('')).toBe(false)
    expect(isSettingsHash(undefined)).toBe(false)
    expect(isSettingsHash('#settingsX')).toBe(false)
    expect(isSettingsHash('#settings/app/extra')).toBe(false)
    expect(isSettingsHash('#3f9c2b1e-4d6a-4a1f-9c2e-8b7d0e5a1c33')).toBe(false)
  })

  it('reads the section out of the hash', () => {
    expect(settingsTabFromHash('#settings/app')).toBe('app')
    expect(settingsTabFromHash('#settings/notifications')).toBe('notifications')
    expect(settingsTabFromHash('#settings')).toBeNull()
    expect(settingsTabFromHash('#settings/')).toBeNull()
    expect(settingsTabFromHash('')).toBeNull()
  })

  it('writes a hash back', () => {
    expect(settingsHash('account')).toBe('#settings/account')
    expect(settingsHash(null)).toBe('#settings')
    expect(settingsHash('')).toBe('#settings')
  })

  // Whatever we write we must be able to read, or a link would open on the
  // wrong section.
  it('round-trips every section id', () => {
    for (const id of ['app', 'account', 'notifications', 'synapse', 'mcp']) {
      expect(isSettingsHash(settingsHash(id))).toBe(true)
      expect(settingsTabFromHash(settingsHash(id))).toBe(id)
    }
  })
})