// @vitest-environment jsdom
import { beforeEach, describe, expect, it } from 'vitest';
import { applyThemeMode, getThemeMode, nextThemeMode } from './theme';

describe('bi-thème (le choix explicite doit gagner dans les deux sens)', () => {
  beforeEach(() => {
    localStorage.clear();
    document.documentElement.removeAttribute('data-theme');
  });

  it('cycle auto → clair → sombre → auto', () => {
    expect(nextThemeMode('auto')).toBe('light');
    expect(nextThemeMode('light')).toBe('dark');
    expect(nextThemeMode('dark')).toBe('auto');
  });

  it('pose et retire data-theme sur <html>', () => {
    applyThemeMode('dark');
    expect(document.documentElement.getAttribute('data-theme')).toBe('dark');
    expect(getThemeMode()).toBe('dark');

    applyThemeMode('auto');
    expect(document.documentElement.hasAttribute('data-theme')).toBe(false);
    expect(getThemeMode()).toBe('auto');
  });

  it('ignore une valeur persistée invalide', () => {
    localStorage.setItem('siop.theme', 'néon');
    expect(getThemeMode()).toBe('auto');
  });
});
