import { describe, it, expect, afterEach } from 'vitest'
import { readHistoryTab } from '../historyTab'

const KEYS = ['information', 'contact', 'address', 'accounting']

describe('readHistoryTab', () => {
    afterEach(() => {
        window.history.replaceState(null, '')
    })

    it('retourne la cle d\'onglet quand elle est presente et valide', () => {
        window.history.replaceState({ tab: 'address' }, '')
        expect(readHistoryTab(KEYS)).toBe('address')
    })

    it('retourne null quand l\'onglet n\'est pas dans les cles valides (ex: role sans Comptabilite)', () => {
        window.history.replaceState({ tab: 'accounting' }, '')
        expect(readHistoryTab(['information', 'contact', 'address'])).toBeNull()
    })

    it('retourne null sans onglet dans l\'etat d\'historique (navigation directe / refresh)', () => {
        window.history.replaceState(null, '')
        expect(readHistoryTab(KEYS)).toBeNull()

        window.history.replaceState({ foo: 'bar' }, '')
        expect(readHistoryTab(KEYS)).toBeNull()
    })

    it('retourne null quand la valeur n\'est pas une chaine', () => {
        window.history.replaceState({ tab: 42 }, '')
        expect(readHistoryTab(KEYS)).toBeNull()
    })
})
