import './test-theme-colors.js';

describe('test-theme-colors asset script', () => {
  beforeAll(() => {
    spyOn(console, 'log').and.stub();
  });

  beforeEach(() => {
    document.body.className = '';
    document.body.style.cssText = '';
    document.documentElement.className = '';
    document.documentElement.style.cssText = '';
  });

  it('expose les utilitaires globaux check et force', () => {
    const api = (globalThis as any).failDailyThemeTest;
    expect(api).toBeDefined();
    expect(typeof api.check).toBe('function');
    expect(typeof api.force).toBe('function');
  });

  it('force applique la classe de theme clair', () => {
    const api = (globalThis as any).failDailyThemeTest;
    document.body.classList.remove('force-light-theme');
    api.force();
    expect(document.body.classList.contains('force-light-theme')).toBeTrue();
    expect(document.body.style.backgroundColor).toBe('rgb(219, 234, 254)');
  });

  it('check retourne true quand la couleur detectee correspond au theme clair', () => {
    const api = (globalThis as any).failDailyThemeTest;
    spyOn(globalThis as any, 'getComputedStyle').and.returnValue({
      backgroundColor: 'rgb(219, 234, 254)',
      color: 'rgb(30, 41, 59)',
    } as CSSStyleDeclaration);

    expect(api.check()).toBeTrue();
  });

  it('check retourne false quand la couleur detectee est differente', () => {
    const api = (globalThis as any).failDailyThemeTest;
    spyOn(globalThis as any, 'getComputedStyle').and.returnValue({
      backgroundColor: 'rgb(0, 0, 0)',
      color: 'rgb(255, 255, 255)',
    } as CSSStyleDeclaration);

    expect(api.check()).toBeFalse();
  });

  it('declenche force via DOMContentLoaded quand check echoue', () => {
    jasmine.clock().install();
    try {
      spyOn(globalThis as any, 'getComputedStyle').and.returnValue({
        backgroundColor: 'rgb(0, 0, 0)',
        color: 'rgb(255, 255, 255)',
      } as CSSStyleDeclaration);
      document.body.classList.remove('force-light-theme');

      document.dispatchEvent(new Event('DOMContentLoaded'));
      jasmine.clock().tick(1000);

      expect(document.body.classList.contains('force-light-theme')).toBeTrue();
    } finally {
      jasmine.clock().uninstall();
    }
  });
});
