import { countStatusCharacters } from 'src/lib/text/count-characters'

describe('countStatusCharacters', () => {
  test('raw counting (Pixelfed: charactersReservedPerUrl=null)', () => {
    expect(countStatusCharacters('hello', null)).toBe(5)
    expect(countStatusCharacters('', null)).toBe(0)
  })

  test('raw counting includes URL characters as-is', () => {
    const t = 'see https://example.com/foo/bar/baz here'
    expect(countStatusCharacters(t, null)).toBe(t.length)
  })

  test('Mastodon counting reserves a fixed cost per URL', () => {
    const text = 'see https://example.com/foo/bar/baz here'
    const url = 'https://example.com/foo/bar/baz'
    expect(countStatusCharacters(text, 23)).toBe(text.length - url.length + 23)
  })

  test('multiple URLs each reserve the fixed cost', () => {
    const text = 'a https://one.org b https://two.com/long/path'
    const urls = ['https://one.org', 'https://two.com/long/path']
    const payload = urls.reduce((s, u) => s + u.length, 0)
    expect(countStatusCharacters(text, 23)).toBe(text.length - payload + 23 * urls.length)
  })

  test('counts by Unicode code points, not UTF-16 units', () => {
    // 4 emoji glyphs, each one code point but two UTF-16 units.
    const emojis = '🌳🌲🌴🌵'
    expect(countStatusCharacters(emojis, null)).toBe(4)
    expect(countStatusCharacters(emojis, 23)).toBe(4)
  })

  test('a single URL collapses to exactly the reservation', () => {
    expect(countStatusCharacters('https://example.com', 23)).toBe(23)
  })

  test('charactersReservedPerUrl <= 0 falls through to raw counting', () => {
    const text = 'https://example.com/x'
    expect(countStatusCharacters(text, 0)).toBe(text.length)
    expect(countStatusCharacters(text, -1)).toBe(text.length)
  })
})
