import { describe, it, expect } from 'vitest';
import {
  clampSplitLeft,
  splitFitsWidth,
  SPLIT_MIN_LEFT,
  SPLIT_MIN_RIGHT,
  SPLIT_HANDLE_PX,
  SPLIT_MIN_TOTAL,
} from './ChatDetailSplit';

describe('clampSplitLeft', () => {
  // 広いコンテナ: 望む幅をそのまま使える範囲
  const WIDE = 1400;

  it('returns the desired width when within bounds', () => {
    expect(clampSplitLeft(600, WIDE)).toBe(600);
  });

  it('clamps to the left minimum', () => {
    expect(clampSplitLeft(50, WIDE)).toBe(SPLIT_MIN_LEFT);
    expect(clampSplitLeft(-100, WIDE)).toBe(SPLIT_MIN_LEFT);
  });

  it('clamps to leave at least the right minimum (+ handle)', () => {
    const maxLeft = WIDE - SPLIT_HANDLE_PX - SPLIT_MIN_RIGHT;
    expect(clampSplitLeft(WIDE, WIDE)).toBe(maxLeft);
    expect(clampSplitLeft(maxLeft + 200, WIDE)).toBe(maxLeft);
  });

  it('when the container is too narrow to satisfy both minimums, falls back to min-left', () => {
    // total < minLeft + handle + minRight → maxLeft would dip below minLeft;
    // Math.max keeps it at minLeft so the left pane is never below its floor.
    const narrow = SPLIT_MIN_LEFT + SPLIT_MIN_RIGHT - 50; // intentionally too small
    expect(clampSplitLeft(1000, narrow)).toBe(SPLIT_MIN_LEFT);
    expect(clampSplitLeft(10, narrow)).toBe(SPLIT_MIN_LEFT);
  });

  it('honors custom min/handle arguments', () => {
    expect(clampSplitLeft(100, 1000, 200, 200, 10)).toBe(200); // below custom minLeft
    expect(clampSplitLeft(900, 1000, 200, 200, 10)).toBe(1000 - 10 - 200); // capped by custom minRight
  });
});

describe('splitFitsWidth', () => {
  it('is optimistic before measurement (width 0)', () => {
    expect(splitFitsWidth(0)).toBe(true);
  });

  it('allows the split only when both minimums + handle fit', () => {
    expect(splitFitsWidth(SPLIT_MIN_TOTAL)).toBe(true);
    expect(splitFitsWidth(SPLIT_MIN_TOTAL + 200)).toBe(true);
    expect(splitFitsWidth(SPLIT_MIN_TOTAL - 1)).toBe(false);
  });

  it('rejects a cramped mid-width container (~488px = rail+list eat the row)', () => {
    // regression: 1024px window − 256 rail − 280 list ≈ 488px container
    expect(splitFitsWidth(488)).toBe(false);
    expect(SPLIT_MIN_TOTAL).toBe(SPLIT_MIN_LEFT + SPLIT_HANDLE_PX + SPLIT_MIN_RIGHT);
  });
});
