Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

finishReason.test.ts 2.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  1. import { describe, expect, it } from '@jest/globals';
  2. import { normalizeFinishReason } from '../../src/util/finishReason';
  3. describe('normalizeFinishReason', () => {
  4. describe('OpenAI mappings', () => {
  5. it('should pass through OpenAI standard reasons unchanged', () => {
  6. expect(normalizeFinishReason('stop')).toBe('stop');
  7. expect(normalizeFinishReason('length')).toBe('length');
  8. expect(normalizeFinishReason('content_filter')).toBe('content_filter');
  9. expect(normalizeFinishReason('tool_calls')).toBe('tool_calls');
  10. });
  11. it('should map function_call to tool_calls', () => {
  12. expect(normalizeFinishReason('function_call')).toBe('tool_calls');
  13. });
  14. });
  15. describe('Anthropic mappings', () => {
  16. it('should normalize Anthropic reasons to standard values', () => {
  17. expect(normalizeFinishReason('end_turn')).toBe('stop');
  18. expect(normalizeFinishReason('stop_sequence')).toBe('stop');
  19. expect(normalizeFinishReason('max_tokens')).toBe('length');
  20. expect(normalizeFinishReason('tool_use')).toBe('tool_calls');
  21. });
  22. });
  23. describe('case normalization', () => {
  24. it('should handle uppercase input', () => {
  25. expect(normalizeFinishReason('STOP')).toBe('stop');
  26. expect(normalizeFinishReason('LENGTH')).toBe('length');
  27. expect(normalizeFinishReason('END_TURN')).toBe('stop');
  28. });
  29. it('should handle mixed case input', () => {
  30. expect(normalizeFinishReason('Stop')).toBe('stop');
  31. expect(normalizeFinishReason('Length')).toBe('length');
  32. expect(normalizeFinishReason('End_Turn')).toBe('stop');
  33. });
  34. });
  35. describe('edge cases', () => {
  36. it('should handle null and undefined', () => {
  37. expect(normalizeFinishReason(null)).toBeUndefined();
  38. expect(normalizeFinishReason(undefined)).toBeUndefined();
  39. });
  40. it('should handle empty and whitespace strings', () => {
  41. expect(normalizeFinishReason('')).toBeUndefined();
  42. expect(normalizeFinishReason(' ')).toBeUndefined();
  43. expect(normalizeFinishReason('\t\n')).toBeUndefined();
  44. });
  45. it('should handle non-string input', () => {
  46. expect(normalizeFinishReason(123 as any)).toBeUndefined();
  47. expect(normalizeFinishReason({} as any)).toBeUndefined();
  48. expect(normalizeFinishReason([] as any)).toBeUndefined();
  49. });
  50. it('should trim whitespace', () => {
  51. expect(normalizeFinishReason(' stop ')).toBe('stop');
  52. expect(normalizeFinishReason('\tlength\n')).toBe('length');
  53. });
  54. });
  55. describe('unmapped reasons', () => {
  56. it('should pass through unknown reasons unchanged', () => {
  57. expect(normalizeFinishReason('unknown_reason')).toBe('unknown_reason');
  58. expect(normalizeFinishReason('custom_stop')).toBe('custom_stop');
  59. });
  60. it('should preserve case for unknown reasons after normalization', () => {
  61. expect(normalizeFinishReason('CUSTOM_REASON')).toBe('custom_reason');
  62. });
  63. });
  64. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...