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

utils.test.ts 3.7 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
112
113
114
115
116
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import { processFileReference, getFinalTest } from '../../src/assertions/utils';
  4. import cliState from '../../src/cliState';
  5. import type { TestCase, Assertion, ApiProvider } from '../../src/types';
  6. jest.mock('fs');
  7. jest.mock('path');
  8. jest.mock('../../src/cliState');
  9. describe('processFileReference', () => {
  10. beforeEach(() => {
  11. jest.resetAllMocks();
  12. cliState.basePath = '/base/path';
  13. });
  14. it('should process JSON files correctly', () => {
  15. const jsonContent = JSON.stringify({ key: 'value' });
  16. jest.mocked(fs.readFileSync).mockReturnValue(jsonContent);
  17. jest.mocked(path.resolve).mockReturnValue('/base/path/test.json');
  18. jest.mocked(path.extname).mockReturnValue('.json');
  19. const result = processFileReference('file://test.json');
  20. expect(result).toEqual({ key: 'value' });
  21. expect(fs.readFileSync).toHaveBeenCalledWith('/base/path/test.json', 'utf8');
  22. });
  23. it('should process YAML files correctly', () => {
  24. const yamlContent = 'key: value';
  25. jest.mocked(fs.readFileSync).mockReturnValue(yamlContent);
  26. jest.mocked(path.resolve).mockReturnValue('/base/path/test.yaml');
  27. jest.mocked(path.extname).mockReturnValue('.yaml');
  28. const result = processFileReference('file://test.yaml');
  29. expect(result).toEqual({ key: 'value' });
  30. expect(fs.readFileSync).toHaveBeenCalledWith('/base/path/test.yaml', 'utf8');
  31. });
  32. it('should process TXT files correctly', () => {
  33. const txtContent = 'plain text content\n';
  34. jest.mocked(fs.readFileSync).mockReturnValue(txtContent);
  35. jest.mocked(path.resolve).mockReturnValue('/base/path/test.txt');
  36. jest.mocked(path.extname).mockReturnValue('.txt');
  37. const result = processFileReference('file://test.txt');
  38. expect(result).toBe('plain text content');
  39. expect(fs.readFileSync).toHaveBeenCalledWith('/base/path/test.txt', 'utf8');
  40. });
  41. it('should throw an error for unsupported file types', () => {
  42. jest.mocked(path.resolve).mockReturnValue('/base/path/test.unsupported');
  43. jest.mocked(path.extname).mockReturnValue('.unsupported');
  44. expect(() => processFileReference('file://test.unsupported')).toThrow('Unsupported file type');
  45. });
  46. });
  47. describe('getFinalTest', () => {
  48. beforeEach(() => {
  49. jest.clearAllMocks();
  50. });
  51. it('should correctly merge test and assertion data', () => {
  52. const mockApiProvider: ApiProvider = {
  53. id: () => 'mockProvider',
  54. callApi: jest.fn(),
  55. };
  56. const testCase: TestCase = {
  57. vars: { var1: 'value1' },
  58. options: {
  59. provider: {
  60. id: () => 'testProvider',
  61. callApi: jest.fn(),
  62. },
  63. },
  64. };
  65. const assertion: Assertion = {
  66. type: 'equals',
  67. value: 'expected value',
  68. provider: mockApiProvider,
  69. rubricPrompt: 'custom rubric prompt',
  70. };
  71. const result = getFinalTest(testCase, assertion);
  72. expect(Object.isFrozen(result)).toBe(true);
  73. expect(result.options?.provider).toBe(mockApiProvider);
  74. expect(result.options?.rubricPrompt).toBe('custom rubric prompt');
  75. expect(result.vars).toEqual({ var1: 'value1' });
  76. expect(testCase.options?.provider?.id()).toBe('testProvider');
  77. expect(typeof result.options?.provider?.callApi).toBe('function');
  78. });
  79. it('should use test provider when assertion provider is not provided', () => {
  80. const testCase: TestCase = {
  81. options: {
  82. provider: {
  83. id: () => 'testProvider',
  84. callApi: jest.fn(),
  85. },
  86. },
  87. };
  88. const assertion: Assertion = {
  89. type: 'equals',
  90. value: 'expected value',
  91. };
  92. const result = getFinalTest(testCase, assertion);
  93. expect(result.options?.provider?.id()).toBe('testProvider');
  94. expect(typeof result.options?.provider?.callApi).toBe('function');
  95. });
  96. });
Tip!

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

Comments

Loading...