|
| 1 | +import { convertXaiResponsesUsage } from './convert-xai-responses-usage'; |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | + |
| 4 | +describe('convertXaiResponsesUsage', () => { |
| 5 | + it('should convert basic usage without caching or reasoning', () => { |
| 6 | + const result = convertXaiResponsesUsage({ |
| 7 | + input_tokens: 100, |
| 8 | + output_tokens: 50, |
| 9 | + }); |
| 10 | + |
| 11 | + expect(result).toMatchInlineSnapshot(` |
| 12 | + { |
| 13 | + "inputTokens": { |
| 14 | + "cacheRead": 0, |
| 15 | + "cacheWrite": undefined, |
| 16 | + "noCache": 100, |
| 17 | + "total": 100, |
| 18 | + }, |
| 19 | + "outputTokens": { |
| 20 | + "reasoning": 0, |
| 21 | + "text": 50, |
| 22 | + "total": 50, |
| 23 | + }, |
| 24 | + "raw": { |
| 25 | + "input_tokens": 100, |
| 26 | + "output_tokens": 50, |
| 27 | + }, |
| 28 | + } |
| 29 | + `); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should convert usage with reasoning tokens', () => { |
| 33 | + const result = convertXaiResponsesUsage({ |
| 34 | + input_tokens: 1941, |
| 35 | + output_tokens: 583, |
| 36 | + total_tokens: 2524, |
| 37 | + output_tokens_details: { |
| 38 | + reasoning_tokens: 380, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(result.outputTokens).toMatchInlineSnapshot(` |
| 43 | + { |
| 44 | + "reasoning": 380, |
| 45 | + "text": 203, |
| 46 | + "total": 583, |
| 47 | + } |
| 48 | + `); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should convert usage with cached input tokens', () => { |
| 52 | + const result = convertXaiResponsesUsage({ |
| 53 | + input_tokens: 200, |
| 54 | + output_tokens: 50, |
| 55 | + input_tokens_details: { |
| 56 | + cached_tokens: 150, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(result.inputTokens).toMatchInlineSnapshot(` |
| 61 | + { |
| 62 | + "cacheRead": 150, |
| 63 | + "cacheWrite": undefined, |
| 64 | + "noCache": 50, |
| 65 | + "total": 200, |
| 66 | + } |
| 67 | + `); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle cached_tokens exceeding input_tokens (non-inclusive reporting)', () => { |
| 71 | + const result = convertXaiResponsesUsage({ |
| 72 | + input_tokens: 4142, |
| 73 | + output_tokens: 254, |
| 74 | + input_tokens_details: { |
| 75 | + cached_tokens: 4328, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result.inputTokens).toMatchInlineSnapshot(` |
| 80 | + { |
| 81 | + "cacheRead": 4328, |
| 82 | + "cacheWrite": undefined, |
| 83 | + "noCache": 4142, |
| 84 | + "total": 8470, |
| 85 | + } |
| 86 | + `); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should convert usage with both cached input and reasoning', () => { |
| 90 | + const result = convertXaiResponsesUsage({ |
| 91 | + input_tokens: 200, |
| 92 | + output_tokens: 583, |
| 93 | + input_tokens_details: { |
| 94 | + cached_tokens: 150, |
| 95 | + }, |
| 96 | + output_tokens_details: { |
| 97 | + reasoning_tokens: 380, |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(result).toMatchInlineSnapshot(` |
| 102 | + { |
| 103 | + "inputTokens": { |
| 104 | + "cacheRead": 150, |
| 105 | + "cacheWrite": undefined, |
| 106 | + "noCache": 50, |
| 107 | + "total": 200, |
| 108 | + }, |
| 109 | + "outputTokens": { |
| 110 | + "reasoning": 380, |
| 111 | + "text": 203, |
| 112 | + "total": 583, |
| 113 | + }, |
| 114 | + "raw": { |
| 115 | + "input_tokens": 200, |
| 116 | + "input_tokens_details": { |
| 117 | + "cached_tokens": 150, |
| 118 | + }, |
| 119 | + "output_tokens": 583, |
| 120 | + "output_tokens_details": { |
| 121 | + "reasoning_tokens": 380, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | + `); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should preserve raw usage data', () => { |
| 129 | + const rawUsage = { |
| 130 | + input_tokens: 12, |
| 131 | + output_tokens: 319, |
| 132 | + total_tokens: 331, |
| 133 | + input_tokens_details: { |
| 134 | + cached_tokens: 2, |
| 135 | + }, |
| 136 | + output_tokens_details: { |
| 137 | + reasoning_tokens: 317, |
| 138 | + }, |
| 139 | + }; |
| 140 | + |
| 141 | + const result = convertXaiResponsesUsage(rawUsage); |
| 142 | + |
| 143 | + expect(result.raw).toEqual(rawUsage); |
| 144 | + }); |
| 145 | +}); |
0 commit comments