Skip to content

The agent that grows with you

1.2 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import { truncateText } from './job-state'

describe('truncateText', () => {
  it('truncates on Unicode code-point boundaries', () => {
    const result = truncateText('๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€', 3)

    expect(result).toBe('๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€โ€ฆ')
    expect(Array.from(result)).toEqual(['๐Ÿ˜€', '๐Ÿ˜€', '๐Ÿ˜€', 'โ€ฆ'])
  })

  it('does not add an ellipsis at the limit', () => {
    expect(truncateText('๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€', 3)).toBe('๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€')
  })

  // Grapheme clusters, not code points: the same shapes profile-glyph.test.tsx pins for the
  // profile rail. A job name is raw user input, so these are plausible values, not exotica.
  it('never cuts a grapheme cluster mid-sequence', () => {
    const cases: Array<[string, number, string]> = [
      ['๐Ÿ‡จ๐Ÿ‡ณxy', 1, '๐Ÿ‡จ๐Ÿ‡ณโ€ฆ'],
      ['๐Ÿ‡จ๐Ÿ‡ณxy', 3, '๐Ÿ‡จ๐Ÿ‡ณxy'],
      ['๐Ÿ‘๐Ÿฝx', 1, '๐Ÿ‘๐Ÿฝโ€ฆ'],
      ['e\u0301x', 1, 'e\u0301โ€ฆ'],
      ['๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆxyz', 1, '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆโ€ฆ'],
      ['๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆxyz', 3, '๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆxyโ€ฆ'],
      ['1๏ธโƒฃ ๆฏๆ—ฅ็ฎ€ๆŠฅ', 1, '1๏ธโƒฃโ€ฆ']
    ]

    for (const [value, max, expected] of cases) {
      expect(truncateText(value, max)).toBe(expected)
    }
  })
})