|
| 1 | +import { estimatePromptTokens } from '../constants/context-window'; |
| 2 | +import { |
| 3 | + EMBEDDING_CHUNK_TOKEN_BUDGET, |
| 4 | + MIN_TEXT_SPLITTER_CHUNK_SIZE, |
| 5 | + TEXT_SPLITTER_CHUNK_SIZE, |
| 6 | +} from '../constants/retrieval'; |
| 7 | +import { |
| 8 | + capChunksToTokenBudget, |
| 9 | + getChunkCharOverlap, |
| 10 | + getChunkCharSize, |
| 11 | + truncateToTokenBudget, |
| 12 | +} from '../utils/textChunking'; |
| 13 | + |
| 14 | +const LATIN = |
| 15 | + 'The quarterly report covers revenue, headcount and logistics across three plants. '; |
| 16 | +const CHINESE = |
| 17 | + '泽菲里亚能源公司在波兰的三个城市设有生产基地,主要生产光伏组件和储能系统。'; |
| 18 | +const POLISH = |
| 19 | + 'Dyrektorem finansowym spółki Zephyria jest Marta Kowalczyk-Nowak, powołana w marcu. '; |
| 20 | + |
| 21 | +describe('getChunkCharSize', () => { |
| 22 | + it('keeps the full character size for Latin prose', () => { |
| 23 | + expect(getChunkCharSize(LATIN.repeat(20))).toBe(TEXT_SPLITTER_CHUNK_SIZE); |
| 24 | + }); |
| 25 | + |
| 26 | + it('shrinks the size for Chinese, which costs ~4x the tokens per character', () => { |
| 27 | + const size = getChunkCharSize(CHINESE.repeat(20)); |
| 28 | + |
| 29 | + expect(size).toBeLessThanOrEqual(EMBEDDING_CHUNK_TOKEN_BUDGET); |
| 30 | + expect(size).toBeGreaterThanOrEqual(MIN_TEXT_SPLITTER_CHUNK_SIZE); |
| 31 | + }); |
| 32 | + |
| 33 | + it('lands between the two for a mixed-script document', () => { |
| 34 | + const mixed = `${LATIN.repeat(10)}${CHINESE.repeat(10)}`; |
| 35 | + const size = getChunkCharSize(mixed); |
| 36 | + |
| 37 | + expect(size).toBeGreaterThan(getChunkCharSize(CHINESE.repeat(20))); |
| 38 | + expect(size).toBeLessThan(TEXT_SPLITTER_CHUNK_SIZE); |
| 39 | + }); |
| 40 | + |
| 41 | + it('never returns a size whose chunk would exceed the token budget', () => { |
| 42 | + for (const sample of [LATIN, POLISH, CHINESE, '🚀🚀🚀 ', 'абвгд ']) { |
| 43 | + const text = sample.repeat(60); |
| 44 | + const chunk = text.slice(0, getChunkCharSize(text)); |
| 45 | + expect(estimatePromptTokens(chunk)).toBeLessThanOrEqual( |
| 46 | + EMBEDDING_CHUNK_TOKEN_BUDGET |
| 47 | + ); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('falls back to the full size for empty text', () => { |
| 52 | + expect(getChunkCharSize('')).toBe(TEXT_SPLITTER_CHUNK_SIZE); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('getChunkCharOverlap', () => { |
| 57 | + it('keeps the splitter overlap proportional to the chunk size', () => { |
| 58 | + expect(getChunkCharOverlap(TEXT_SPLITTER_CHUNK_SIZE)).toBe(200); |
| 59 | + expect(getChunkCharOverlap(250)).toBe(50); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('capChunksToTokenBudget', () => { |
| 64 | + it('leaves chunks that already fit untouched', () => { |
| 65 | + const chunks = [LATIN, POLISH]; |
| 66 | + expect(capChunksToTokenBudget(chunks)).toEqual(chunks); |
| 67 | + }); |
| 68 | + |
| 69 | + it('splits a dense passage hiding inside an otherwise Latin document', () => { |
| 70 | + const oversized = CHINESE.repeat(30); |
| 71 | + |
| 72 | + const capped = capChunksToTokenBudget([LATIN, oversized]); |
| 73 | + |
| 74 | + expect(capped.length).toBeGreaterThan(2); |
| 75 | + for (const chunk of capped) { |
| 76 | + expect(estimatePromptTokens(chunk)).toBeLessThanOrEqual( |
| 77 | + EMBEDDING_CHUNK_TOKEN_BUDGET |
| 78 | + ); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('preserves the text across the split', () => { |
| 83 | + const oversized = CHINESE.repeat(30); |
| 84 | + expect(capChunksToTokenBudget([oversized]).join('')).toBe(oversized); |
| 85 | + }); |
| 86 | + |
| 87 | + it('does not drop a character that alone exceeds the budget', () => { |
| 88 | + expect(capChunksToTokenBudget(['字'], 0.5)).toEqual(['字']); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('truncateToTokenBudget', () => { |
| 93 | + it('returns short text unchanged', () => { |
| 94 | + expect(truncateToTokenBudget('Kto jest dyrektorem finansowym?')).toBe( |
| 95 | + 'Kto jest dyrektorem finansowym?' |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('trims an over-long query to the budget', () => { |
| 100 | + const query = CHINESE.repeat(40); |
| 101 | + |
| 102 | + const truncated = truncateToTokenBudget(query); |
| 103 | + |
| 104 | + expect(truncated.length).toBeLessThan(query.length); |
| 105 | + expect(query.startsWith(truncated)).toBe(true); |
| 106 | + expect(estimatePromptTokens(truncated)).toBeLessThanOrEqual( |
| 107 | + EMBEDDING_CHUNK_TOKEN_BUDGET |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('handles empty text', () => { |
| 112 | + expect(truncateToTokenBudget('')).toBe(''); |
| 113 | + }); |
| 114 | +}); |
0 commit comments