import { beforeEach, describe, expect, test } from 'vitest';
import { clampInspectorHeight, inspectorHeightValue, useAppStore } from './AppStore';

const store = () => useAppStore.getState();
beforeEach(() => store().setTheme('light'));

describe('theme', () => {
  test('setTheme sets the theme explicitly', () => {
    store().setTheme('dark');
    expect(store().uiTheme).toBe('dark');
  });

  test('toggleTheme flips light ⇄ dark', () => {
    expect(store().uiTheme).toBe('light');
    store().toggleTheme();
    expect(store().uiTheme).toBe('dark');
    store().toggleTheme();
    expect(store().uiTheme).toBe('light');
  });
});

describe('data inspector height (divider math)', () => {
  // availForBoth = 600 → inspector max = 600 - 120 (chart min) = 480; min = 96.
  test('clamps within [inspector min, availForBoth − chart min]', () => {
    expect(clampInspectorHeight(300, 600)).toBe(300);
    expect(clampInspectorHeight(10, 600)).toBe(96); // floor at inspector min
    expect(clampInspectorHeight(5000, 600)).toBe(480); // leave the chart its minimum
  });

  test('never returns below the inspector minimum even in a tiny pane', () => {
    // Span too small for both minimums → keep the inspector minimum.
    expect(clampInspectorHeight(300, 150)).toBe(96);
  });

  test('aria-valuenow maps min→0, max→100', () => {
    expect(inspectorHeightValue(96, 600)).toBe(0);
    expect(inspectorHeightValue(480, 600)).toBe(100);
    expect(inspectorHeightValue(288, 600)).toBe(50); // midpoint of [96, 480]
  });

  test('aria-valuenow is null when the span has no range', () => {
    expect(inspectorHeightValue(96, 150)).toBeNull();
  });
});
