import {describe, expect, test} from 'bun:test'

import {emailLocalPart, pluralize, pluralNoun, truncate} from './text'

describe('pluralNoun', () => {
  test('uses the singular for exactly one', () => {
    expect(pluralNoun(1, 'crumb')).toBe('crumb')
  })

  test('appends s by default otherwise', () => {
    expect(pluralNoun(0, 'crumb')).toBe('crumbs')
    expect(pluralNoun(3, 'crumb')).toBe('crumbs')
  })

  test('honours an explicit plural', () => {
    expect(pluralNoun(2, 'person', 'people')).toBe('people')
  })
})

describe('pluralize', () => {
  test('uses the singular for exactly one', () => {
    expect(pluralize(1, 'crumb')).toBe('1 crumb')
  })

  test('appends s by default otherwise', () => {
    expect(pluralize(0, 'crumb')).toBe('0 crumbs')
    expect(pluralize(3, 'crumb')).toBe('3 crumbs')
  })

  test('honours an explicit plural', () => {
    expect(pluralize(2, 'person', 'people')).toBe('2 people')
  })
})

describe('truncate', () => {
  test('returns short text unchanged', () => {
    expect(truncate('hello', 10)).toBe('hello')
  })

  test('cuts long text to max with an ellipsis', () => {
    expect(truncate('hello world', 8)).toBe('hello w…')
    expect(truncate('hello world', 8)).toHaveLength(8)
  })

  test('trims trailing whitespace before the ellipsis', () => {
    expect(truncate('hello world', 7)).toBe('hello…')
  })
})

describe('emailLocalPart', () => {
  test('returns the part before the @', () => {
    expect(emailLocalPart('sunny@seed.crumbs.test')).toBe('sunny')
  })

  test('returns the input when there is no @', () => {
    expect(emailLocalPart('sunny')).toBe('sunny')
  })
})
