|
| 1 | +import { deepSeekMetadataExtractor } from './deepseek-metadata-extractor'; |
| 2 | + |
| 3 | +describe('buildMetadataFromResponse', () => { |
| 4 | + it('should extract metadata from complete response with usage data', () => { |
| 5 | + const response = { |
| 6 | + usage: { |
| 7 | + prompt_cache_hit_tokens: 100, |
| 8 | + prompt_cache_miss_tokens: 50, |
| 9 | + }, |
| 10 | + }; |
| 11 | + |
| 12 | + const metadata = deepSeekMetadataExtractor.extractMetadata({ |
| 13 | + parsedBody: response, |
| 14 | + }); |
| 15 | + |
| 16 | + expect(metadata).toEqual({ |
| 17 | + deepseek: { |
| 18 | + promptCacheHitTokens: 100, |
| 19 | + promptCacheMissTokens: 50, |
| 20 | + }, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should handle missing usage data', () => { |
| 25 | + const response = { |
| 26 | + id: 'test-id', |
| 27 | + choices: [], |
| 28 | + }; |
| 29 | + |
| 30 | + const metadata = deepSeekMetadataExtractor.extractMetadata({ |
| 31 | + parsedBody: response, |
| 32 | + }); |
| 33 | + |
| 34 | + expect(metadata).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should handle invalid response data', () => { |
| 38 | + const response = 'invalid data'; |
| 39 | + |
| 40 | + const metadata = deepSeekMetadataExtractor.extractMetadata({ |
| 41 | + parsedBody: response, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(metadata).toBeUndefined(); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('streaming metadata processor', () => { |
| 49 | + it('should process streaming chunks and build final metadata', () => { |
| 50 | + const processor = deepSeekMetadataExtractor.createStreamExtractor(); |
| 51 | + |
| 52 | + // Process initial chunks without usage data |
| 53 | + processor.processChunk({ |
| 54 | + choices: [{ finish_reason: null }], |
| 55 | + }); |
| 56 | + |
| 57 | + // Process final chunk with usage data |
| 58 | + processor.processChunk({ |
| 59 | + choices: [{ finish_reason: 'stop' }], |
| 60 | + usage: { |
| 61 | + prompt_cache_hit_tokens: 100, |
| 62 | + prompt_cache_miss_tokens: 50, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const finalMetadata = processor.buildMetadata(); |
| 67 | + |
| 68 | + expect(finalMetadata).toEqual({ |
| 69 | + deepseek: { |
| 70 | + promptCacheHitTokens: 100, |
| 71 | + promptCacheMissTokens: 50, |
| 72 | + }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle streaming chunks without usage data', () => { |
| 77 | + const processor = deepSeekMetadataExtractor.createStreamExtractor(); |
| 78 | + |
| 79 | + processor.processChunk({ |
| 80 | + choices: [{ finish_reason: 'stop' }], |
| 81 | + }); |
| 82 | + |
| 83 | + const finalMetadata = processor.buildMetadata(); |
| 84 | + |
| 85 | + expect(finalMetadata).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle invalid streaming chunks', () => { |
| 89 | + const processor = deepSeekMetadataExtractor.createStreamExtractor(); |
| 90 | + |
| 91 | + processor.processChunk('invalid chunk'); |
| 92 | + |
| 93 | + const finalMetadata = processor.buildMetadata(); |
| 94 | + |
| 95 | + expect(finalMetadata).toBeUndefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should only capture usage data from final chunk with stop reason', () => { |
| 99 | + const processor = deepSeekMetadataExtractor.createStreamExtractor(); |
| 100 | + |
| 101 | + // Process chunk with usage but no stop reason |
| 102 | + processor.processChunk({ |
| 103 | + choices: [{ finish_reason: null }], |
| 104 | + usage: { |
| 105 | + prompt_cache_hit_tokens: 50, |
| 106 | + prompt_cache_miss_tokens: 25, |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + // Process final chunk with different usage data |
| 111 | + processor.processChunk({ |
| 112 | + choices: [{ finish_reason: 'stop' }], |
| 113 | + usage: { |
| 114 | + prompt_cache_hit_tokens: 100, |
| 115 | + prompt_cache_miss_tokens: 50, |
| 116 | + }, |
| 117 | + }); |
| 118 | + |
| 119 | + const finalMetadata = processor.buildMetadata(); |
| 120 | + |
| 121 | + expect(finalMetadata).toEqual({ |
| 122 | + deepseek: { |
| 123 | + promptCacheHitTokens: 100, |
| 124 | + promptCacheMissTokens: 50, |
| 125 | + }, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle null values in usage data', () => { |
| 130 | + const processor = deepSeekMetadataExtractor.createStreamExtractor(); |
| 131 | + |
| 132 | + processor.processChunk({ |
| 133 | + choices: [{ finish_reason: 'stop' }], |
| 134 | + usage: { |
| 135 | + prompt_cache_hit_tokens: null, |
| 136 | + prompt_cache_miss_tokens: 50, |
| 137 | + }, |
| 138 | + }); |
| 139 | + |
| 140 | + const finalMetadata = processor.buildMetadata(); |
| 141 | + |
| 142 | + expect(finalMetadata).toEqual({ |
| 143 | + deepseek: { |
| 144 | + promptCacheHitTokens: NaN, |
| 145 | + promptCacheMissTokens: 50, |
| 146 | + }, |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments