Files
ToolBox/tests/unit/byteUtils.test.js
T
renjue f1502ed08c
CI / docker (push) Successful in 11m42s
初始化 ToolBox 开发者工具箱。
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-25 11:15:38 +08:00

31 lines
973 B
JavaScript

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')
})
})