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

prompts.processors.jsonl.test.ts 2.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
  1. import * as fs from 'fs';
  2. import { processJsonlFile } from '../src/prompts/processors/jsonl';
  3. jest.mock('fs');
  4. describe('processJsonlFile', () => {
  5. const mockReadFileSync = jest.mocked(fs.readFileSync);
  6. beforeEach(() => {
  7. jest.clearAllMocks();
  8. });
  9. it('should process a valid JSONL file without a label', () => {
  10. const filePath = 'file.jsonl';
  11. const fileContent = '[{"key1": "value1"}]\n[{"key2": "value2"}]';
  12. mockReadFileSync.mockReturnValue(fileContent);
  13. expect(processJsonlFile(filePath, {})).toEqual([
  14. {
  15. raw: '[{"key1": "value1"}]',
  16. label: 'file.jsonl: [{"key1": "value1"}]',
  17. },
  18. {
  19. raw: '[{"key2": "value2"}]',
  20. label: 'file.jsonl: [{"key2": "value2"}]',
  21. },
  22. ]);
  23. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  24. });
  25. it('should process a valid JSONL file with a single record without a label', () => {
  26. const filePath = 'file.jsonl';
  27. const fileContent = '[{"key1": "value1"}, {"key2": "value2"}]';
  28. mockReadFileSync.mockReturnValue(fileContent);
  29. expect(processJsonlFile(filePath, {})).toEqual([
  30. {
  31. raw: '[{"key1": "value1"}, {"key2": "value2"}]',
  32. label: `file.jsonl`,
  33. },
  34. ]);
  35. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  36. });
  37. it('should process a valid JSONL file with a single record and a label', () => {
  38. const filePath = 'file.jsonl';
  39. const fileContent = '[{"key1": "value1"}, {"key2": "value2"}]';
  40. mockReadFileSync.mockReturnValue(fileContent);
  41. expect(processJsonlFile(filePath, { label: 'Label' })).toEqual([
  42. {
  43. raw: '[{"key1": "value1"}, {"key2": "value2"}]',
  44. label: `Label`,
  45. },
  46. ]);
  47. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  48. });
  49. it('should process a valid JSONL file with multiple records and a label', () => {
  50. const filePath = 'file.jsonl';
  51. const fileContent = '[{"key1": "value1"}]\n[{"key2": "value2"}]';
  52. mockReadFileSync.mockReturnValue(fileContent);
  53. expect(processJsonlFile(filePath, { label: 'Label' })).toEqual([
  54. {
  55. raw: '[{"key1": "value1"}]',
  56. label: `Label: [{"key1": "value1"}]`,
  57. },
  58. {
  59. raw: '[{"key2": "value2"}]',
  60. label: `Label: [{"key2": "value2"}]`,
  61. },
  62. ]);
  63. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  64. });
  65. it('should throw an error if the file cannot be read', () => {
  66. const filePath = 'nonexistent.jsonl';
  67. mockReadFileSync.mockImplementation(() => {
  68. throw new Error('File not found');
  69. });
  70. expect(() => processJsonlFile(filePath, {})).toThrow('File not found');
  71. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  72. });
  73. });
Tip!

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

Comments

Loading...