import { describe, it, expect } from 'vitest' import { getByteLength, truncateToMaxBytes } from '../../src/utils/byteUtils.js' describe('byteUtils', () => { it('counts ASCII bytes', () => { expect(getByteLength('hello')).toBe(5) expect(getByteLength('')).toBe(0) }) it('counts UTF-8 multibyte characters', () => { expect(getByteLength('中')).toBe(3) expect(getByteLength('hello世界')).toBe(11) }) it('returns original string when within limit', () => { expect(truncateToMaxBytes('hello', 10)).toBe('hello') }) it('truncates by bytes without breaking multibyte chars', () => { const text = 'a'.repeat(10) + '中' const truncated = truncateToMaxBytes(text, 11) expect(getByteLength(truncated)).toBeLessThanOrEqual(11) expect(truncated).not.toContain('中') }) it('handles exact byte boundary', () => { expect(truncateToMaxBytes('ab', 2)).toBe('ab') expect(truncateToMaxBytes('abc', 2)).toBe('ab') }) })