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

shared.test.ts 3.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  1. import { getEnvBool } from '../../src/envars';
  2. import { parseChatPrompt, toTitleCase, calculateCost } from '../../src/providers/shared';
  3. jest.mock('../../src/envars');
  4. describe('Shared Provider Functions', () => {
  5. describe('parseChatPrompt', () => {
  6. it('should parse YAML prompt', () => {
  7. const yamlPrompt = `
  8. - role: user
  9. content: Hello
  10. - role: assistant
  11. content: Hi there!
  12. `;
  13. const result = parseChatPrompt(yamlPrompt, []);
  14. expect(result).toEqual([
  15. { role: 'user', content: 'Hello' },
  16. { role: 'assistant', content: 'Hi there!' },
  17. ]);
  18. });
  19. it('should parse JSON prompt', () => {
  20. const jsonPrompt = JSON.stringify([
  21. { role: 'user', content: 'Hello' },
  22. { role: 'assistant', content: 'Hi there!' },
  23. ]);
  24. const result = parseChatPrompt(jsonPrompt, []);
  25. expect(result).toEqual([
  26. { role: 'user', content: 'Hello' },
  27. { role: 'assistant', content: 'Hi there!' },
  28. ]);
  29. });
  30. it('should return default value for non-YAML, non-JSON prompt', () => {
  31. const defaultValue = [{ role: 'user', content: 'Default' }];
  32. const result = parseChatPrompt('Just a regular string', defaultValue);
  33. expect(result).toEqual(defaultValue);
  34. });
  35. it('should throw error for invalid YAML', () => {
  36. const invalidYaml = '- role: user\n content: :\n';
  37. expect(() => parseChatPrompt(invalidYaml, [])).toThrow(
  38. 'Chat Completion prompt is not a valid YAML string',
  39. );
  40. });
  41. it('should throw error for invalid JSON when PROMPTFOO_REQUIRE_JSON_PROMPTS is true', () => {
  42. jest.mocked(getEnvBool).mockReturnValue(true);
  43. const invalidJson = '"role": "user", "content": "Hello" }'; // Missing array brackets
  44. expect(() => parseChatPrompt(invalidJson, [])).toThrow(
  45. 'Chat Completion prompt is not a valid JSON string',
  46. );
  47. });
  48. it('should throw error for invalid JSON when prompt starts with { or [', () => {
  49. jest.mocked(getEnvBool).mockReturnValue(false);
  50. const invalidJson = '{ "invalid: "json" }';
  51. expect(() => parseChatPrompt(invalidJson, [])).toThrow(
  52. 'Chat Completion prompt is not a valid JSON string',
  53. );
  54. });
  55. });
  56. describe('toTitleCase', () => {
  57. it('should convert string to title case', () => {
  58. expect(toTitleCase('hello world')).toBe('Hello World');
  59. expect(toTitleCase('UPPERCASE STRING')).toBe('Uppercase String');
  60. expect(toTitleCase('mixed CASE string')).toBe('Mixed Case String');
  61. });
  62. it('should handle empty string', () => {
  63. expect(toTitleCase('')).toBe('');
  64. });
  65. it('should handle single word', () => {
  66. expect(toTitleCase('word')).toBe('Word');
  67. });
  68. });
  69. describe('calculateCost', () => {
  70. const models = [
  71. { id: 'model1', cost: { input: 0.001, output: 0.002 } },
  72. { id: 'model2', cost: { input: 0.003, output: 0.004 } },
  73. ];
  74. it('should calculate cost correctly', () => {
  75. const cost = calculateCost('model1', {}, 1000, 500, models);
  76. expect(cost).toBe(2); // (0.001 * 1000) + (0.002 * 500)
  77. });
  78. it('should use config cost if provided', () => {
  79. const cost = calculateCost('model1', { cost: 0.005 }, 1000, 500, models);
  80. expect(cost).toBe(7.5); // (0.005 * 1000) + (0.005 * 500)
  81. });
  82. it('should return undefined if model not found', () => {
  83. const cost = calculateCost('nonexistent', {}, 1000, 500, models);
  84. expect(cost).toBeUndefined();
  85. });
  86. it('should return undefined if tokens are not finite', () => {
  87. expect(calculateCost('model1', {}, Number.NaN, 500, models)).toBeUndefined();
  88. expect(calculateCost('model1', {}, 1000, Infinity, models)).toBeUndefined();
  89. });
  90. it('should return undefined if tokens are undefined', () => {
  91. expect(calculateCost('model1', {}, undefined, 500, models)).toBeUndefined();
  92. expect(calculateCost('model1', {}, 1000, undefined, models)).toBeUndefined();
  93. });
  94. });
  95. });
Tip!

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

Comments

Loading...