import { beforeEach, describe, expect, it } from 'vitest';
import { getTabHistory, resolveTabTarget, setTabHistory } from './tabHistory';
import { adminPath } from '../../constants';

describe('tabHistory', () => {
  beforeEach(() => {
    if (typeof window !== 'undefined') {
      window.localStorage.clear();
    }
  });

  it('stores tab history entries', () => {
    setTabHistory('home', adminPath('/mobile/dashboard'));
    setTabHistory('tasks', adminPath('/mobile/tasks'));
    const history = getTabHistory();
    expect(history.home).toBe(adminPath('/mobile/dashboard'));
    expect(history.tasks).toBe(adminPath('/mobile/tasks'));
  });

  it('returns fallback when no history exists', () => {
    const target = resolveTabTarget('uploads', null);
    expect(target).toBe(adminPath('/mobile/uploads'));
  });

  it('reuses stored event route when slug matches', () => {
    setTabHistory('uploads', adminPath('/mobile/events/summer/control-room'));
    const target = resolveTabTarget('uploads', 'summer');
    expect(target).toBe(adminPath('/mobile/events/summer/control-room'));
  });

  it('falls back to active slug when stored slug differs', () => {
    setTabHistory('tasks', adminPath('/mobile/events/winter/tasks'));
    const target = resolveTabTarget('tasks', 'summer');
    expect(target).toBe(adminPath('/mobile/events/summer/tasks'));
  });
});
