import { describe, expect, it } from 'vitest'
import {
  clampUtf16Boundary,
  clipSegmentsToMaxX,
  padToCells,
  truncateWithEllipsis,
} from './text-layout'

describe('truncateWithEllipsis', () => {
  it('truncates long text and preserves short text', () => {
    expect(truncateWithEllipsis('hello world', 5)).toBe('hell…')
    expect(truncateWithEllipsis('hi', 5)).toBe('hi')
  })
})

describe('padToCells', () => {
  it('pads shorter text up to the requested width', () => {
    expect(padToCells('abc', 5)).toBe('abc  ')
  })
})

describe('clampUtf16Boundary', () => {
  it('avoids splitting surrogate pairs', () => {
    const text = 'A😀B'
    expect(clampUtf16Boundary(text, 2)).toBe(1)
    expect(clampUtf16Boundary(text, 3)).toBe(3)
  })
})

describe('clipSegmentsToMaxX', () => {
  it('clips segments that overflow the maximum column', () => {
    expect(
      clipSegmentsToMaxX(
        [
          { text: 'hello', x: 0, w: 5 },
          { text: 'world', x: 5, w: 5 },
        ],
        7,
      ),
    ).toEqual([
      { text: 'hello', x: 0, w: 5 },
      { text: 'wo', x: 5, w: 2 },
    ])
  })
})
