|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { normalizeFinishReason } from './do-stream-step.js'; |
| 3 | + |
| 4 | +describe('normalizeFinishReason', () => { |
| 5 | + describe('string finish reasons', () => { |
| 6 | + it('should pass through "stop"', () => { |
| 7 | + expect(normalizeFinishReason('stop')).toBe('stop'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should pass through "tool-calls"', () => { |
| 11 | + expect(normalizeFinishReason('tool-calls')).toBe('tool-calls'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should pass through "length"', () => { |
| 15 | + expect(normalizeFinishReason('length')).toBe('length'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should pass through "content-filter"', () => { |
| 19 | + expect(normalizeFinishReason('content-filter')).toBe('content-filter'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should pass through "error"', () => { |
| 23 | + expect(normalizeFinishReason('error')).toBe('error'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should pass through "other"', () => { |
| 27 | + expect(normalizeFinishReason('other')).toBe('other'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should pass through "unknown"', () => { |
| 31 | + expect(normalizeFinishReason('unknown')).toBe('unknown'); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('object finish reasons', () => { |
| 36 | + it('should extract "stop" from object', () => { |
| 37 | + expect(normalizeFinishReason({ type: 'stop' })).toBe('stop'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should extract "tool-calls" from object', () => { |
| 41 | + expect(normalizeFinishReason({ type: 'tool-calls' })).toBe('tool-calls'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should extract "length" from object', () => { |
| 45 | + expect(normalizeFinishReason({ type: 'length' })).toBe('length'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should extract "content-filter" from object', () => { |
| 49 | + expect(normalizeFinishReason({ type: 'content-filter' })).toBe( |
| 50 | + 'content-filter' |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should extract "error" from object', () => { |
| 55 | + expect(normalizeFinishReason({ type: 'error' })).toBe('error'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should extract "other" from object', () => { |
| 59 | + expect(normalizeFinishReason({ type: 'other' })).toBe('other'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should extract "unknown" from object', () => { |
| 63 | + expect(normalizeFinishReason({ type: 'unknown' })).toBe('unknown'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return "unknown" for object without type property', () => { |
| 67 | + expect(normalizeFinishReason({})).toBe('unknown'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return "unknown" for object with null type', () => { |
| 71 | + expect(normalizeFinishReason({ type: null })).toBe('unknown'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return "unknown" for object with undefined type', () => { |
| 75 | + expect(normalizeFinishReason({ type: undefined })).toBe('unknown'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle object with additional properties', () => { |
| 79 | + expect( |
| 80 | + normalizeFinishReason({ |
| 81 | + type: 'stop', |
| 82 | + reason: 'end_turn', |
| 83 | + metadata: { foo: 'bar' }, |
| 84 | + }) |
| 85 | + ).toBe('stop'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('edge cases', () => { |
| 90 | + it('should return "unknown" for undefined', () => { |
| 91 | + expect(normalizeFinishReason(undefined)).toBe('unknown'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return "unknown" for null', () => { |
| 95 | + expect(normalizeFinishReason(null)).toBe('unknown'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return "unknown" for number', () => { |
| 99 | + expect(normalizeFinishReason(42)).toBe('unknown'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return "unknown" for boolean', () => { |
| 103 | + expect(normalizeFinishReason(true)).toBe('unknown'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should return "unknown" for array', () => { |
| 107 | + expect(normalizeFinishReason(['stop'])).toBe('unknown'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should handle empty string', () => { |
| 111 | + expect(normalizeFinishReason('')).toBe(''); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('bug reproduction', () => { |
| 116 | + it('should handle object format that caused [object Object] error', () => { |
| 117 | + const normalized = normalizeFinishReason({ type: 'stop' }); |
| 118 | + expect(normalized).toBe('stop'); |
| 119 | + expect(typeof normalized).toBe('string'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should handle tool-calls object format', () => { |
| 123 | + const normalized = normalizeFinishReason({ type: 'tool-calls' }); |
| 124 | + expect(normalized).toBe('tool-calls'); |
| 125 | + expect(typeof normalized).toBe('string'); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments