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
|
- import * as fs from 'fs';
- import { processJsonlFile } from '../src/prompts/processors/jsonl';
- jest.mock('fs');
- describe('processJsonlFile', () => {
- const mockReadFileSync = jest.mocked(fs.readFileSync);
- beforeEach(() => {
- jest.clearAllMocks();
- });
- it('should process a valid JSONL file without a label', () => {
- const filePath = 'file.jsonl';
- const fileContent = '[{"key1": "value1"}]\n[{"key2": "value2"}]';
- mockReadFileSync.mockReturnValue(fileContent);
- expect(processJsonlFile(filePath, {})).toEqual([
- {
- raw: '[{"key1": "value1"}]',
- label: 'file.jsonl: [{"key1": "value1"}]',
- },
- {
- raw: '[{"key2": "value2"}]',
- label: 'file.jsonl: [{"key2": "value2"}]',
- },
- ]);
- expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
- });
- it('should process a valid JSONL file with a single record without a label', () => {
- const filePath = 'file.jsonl';
- const fileContent = '[{"key1": "value1"}, {"key2": "value2"}]';
- mockReadFileSync.mockReturnValue(fileContent);
- expect(processJsonlFile(filePath, {})).toEqual([
- {
- raw: '[{"key1": "value1"}, {"key2": "value2"}]',
- label: `file.jsonl`,
- },
- ]);
- expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
- });
- it('should process a valid JSONL file with a single record and a label', () => {
- const filePath = 'file.jsonl';
- const fileContent = '[{"key1": "value1"}, {"key2": "value2"}]';
- mockReadFileSync.mockReturnValue(fileContent);
- expect(processJsonlFile(filePath, { label: 'Label' })).toEqual([
- {
- raw: '[{"key1": "value1"}, {"key2": "value2"}]',
- label: `Label`,
- },
- ]);
- expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
- });
- it('should process a valid JSONL file with multiple records and a label', () => {
- const filePath = 'file.jsonl';
- const fileContent = '[{"key1": "value1"}]\n[{"key2": "value2"}]';
- mockReadFileSync.mockReturnValue(fileContent);
- expect(processJsonlFile(filePath, { label: 'Label' })).toEqual([
- {
- raw: '[{"key1": "value1"}]',
- label: `Label: [{"key1": "value1"}]`,
- },
- {
- raw: '[{"key2": "value2"}]',
- label: `Label: [{"key2": "value2"}]`,
- },
- ]);
- expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
- });
- it('should throw an error if the file cannot be read', () => {
- const filePath = 'nonexistent.jsonl';
- mockReadFileSync.mockImplementation(() => {
- throw new Error('File not found');
- });
- expect(() => processJsonlFile(filePath, {})).toThrow('File not found');
- expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
- });
- });
|