import { describe, expect, it } from 'vitest'; import { countChars, detectEncoding, segment } from './sms-segmenter'; describe('detectEncoding', () => { it('detects GSM-7 for basic Latin text', () => { expect(detectEncoding('Hello world!')).toBe('GSM-7'); }); it('detects GSM-7 for accented chars in the basic set', () => { expect(detectEncoding('Café à €')).toBe('GSM-7'); }); it('falls back to UCS-2 for emoji', () => { expect(detectEncoding('Hello 🎉')).toBe('UCS-2'); }); it('falls back to UCS-2 for non-GSM scripts', () => { expect(detectEncoding('こんにちは')).toBe('UCS-2'); }); it('treats empty string as GSM-7', () => { expect(detectEncoding('')).toBe('GSM-7'); }); }); describe('countChars', () => { it('counts basic GSM-7 chars as one each', () => { expect(countChars('abc', 'GSM-7')).toBe(3); }); it('counts GSM-7 extension chars as two each', () => { // '€' and '{' and '}' are extension-table characters. expect(countChars('€', 'GSM-7')).toBe(2); expect(countChars('{}', 'GSM-7')).toBe(4); }); it('counts UCS-2 in UTF-16 code units', () => { expect(countChars('abc', 'UCS-2')).toBe(3); expect(countChars('🎉', 'UCS-2')).toBe(2); // surrogate pair }); }); describe('segment', () => { it('reports zero segments for empty text', () => { const r = segment(''); expect(r.segments).toBe(0); expect(r.encoding).toBe('GSM-7'); expect(r.remaining).toBe(160); }); it('reports one GSM-7 segment under 160 chars', () => { const r = segment('a'.repeat(160)); expect(r.encoding).toBe('GSM-7'); expect(r.segments).toBe(1); expect(r.length).toBe(160); expect(r.remaining).toBe(0); }); it('splits GSM-7 into two segments at 161 chars (153 each)', () => { const r = segment('a'.repeat(161)); expect(r.segments).toBe(2); expect(r.maxMulti).toBe(153); expect(r.remaining).toBe(2 * 153 - 161); }); it('reports one UCS-2 segment under 70 chars', () => { const r = segment('café 😀'); // contains emoji → UCS-2 expect(r.encoding).toBe('UCS-2'); expect(r.segments).toBe(1); expect(r.maxSingle).toBe(70); }); it('splits UCS-2 at 71 chars (67 each)', () => { const r = segment('ñ'.repeat(71).concat('🎉')); // force UCS-2, > 70 units expect(r.encoding).toBe('UCS-2'); expect(r.maxMulti).toBe(67); expect(r.segments).toBeGreaterThanOrEqual(2); }); it('counts an extension char pair toward the segment length', () => { const r = segment('€'.repeat(80)); // 80 * 2 = 160 septets expect(r.encoding).toBe('GSM-7'); expect(r.length).toBe(160); expect(r.segments).toBe(1); }); });