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

text.test.ts 3.0 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
  1. import * as fs from 'fs';
  2. import { PROMPT_DELIMITER } from '../../../src/prompts/constants';
  3. import { processTxtFile } from '../../../src/prompts/processors/text';
  4. jest.mock('fs');
  5. describe('processTxtFile', () => {
  6. const mockReadFileSync = jest.mocked(fs.readFileSync);
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. });
  10. it('should process a text file with single prompt and no label', () => {
  11. const filePath = 'file.txt';
  12. const fileContent = 'This is a prompt';
  13. mockReadFileSync.mockReturnValue(fileContent);
  14. expect(processTxtFile(filePath, {})).toEqual([
  15. {
  16. raw: 'This is a prompt',
  17. label: 'file.txt: This is a prompt',
  18. },
  19. ]);
  20. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  21. });
  22. it('should process a text file with single prompt and a label', () => {
  23. const filePath = 'file.txt';
  24. const fileContent = 'This is a prompt';
  25. mockReadFileSync.mockReturnValue(fileContent);
  26. expect(processTxtFile(filePath, { label: 'prompt 1' })).toEqual([
  27. {
  28. raw: 'This is a prompt',
  29. label: 'prompt 1: file.txt: This is a prompt',
  30. },
  31. ]);
  32. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  33. });
  34. it('should process a text file with multiple prompts and a label', () => {
  35. const fileContent = `Prompt 1${PROMPT_DELIMITER}Prompt 2${PROMPT_DELIMITER}Prompt 3`;
  36. mockReadFileSync.mockReturnValue(fileContent);
  37. expect(processTxtFile('file.txt', { label: 'Label' })).toEqual([
  38. {
  39. raw: 'Prompt 1',
  40. label: `Label: file.txt: Prompt 1`,
  41. },
  42. {
  43. raw: 'Prompt 2',
  44. label: `Label: file.txt: Prompt 2`,
  45. },
  46. {
  47. raw: 'Prompt 3',
  48. label: `Label: file.txt: Prompt 3`,
  49. },
  50. ]);
  51. expect(mockReadFileSync).toHaveBeenCalledWith('file.txt', 'utf-8');
  52. });
  53. it('should handle text file with leading and trailing delimiters', () => {
  54. const filePath = 'file.txt';
  55. const fileContent = `${PROMPT_DELIMITER}Prompt 1${PROMPT_DELIMITER}Prompt 2${PROMPT_DELIMITER}`;
  56. mockReadFileSync.mockReturnValue(fileContent);
  57. expect(processTxtFile(filePath, {})).toEqual([
  58. {
  59. raw: 'Prompt 1',
  60. label: `${filePath}: Prompt 1`,
  61. },
  62. {
  63. raw: 'Prompt 2',
  64. label: `${filePath}: Prompt 2`,
  65. },
  66. ]);
  67. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  68. });
  69. it('should return an empty array for a file with only delimiters', () => {
  70. const filePath = 'file.txt';
  71. const fileContent = `${PROMPT_DELIMITER}${PROMPT_DELIMITER}`;
  72. mockReadFileSync.mockReturnValue(fileContent);
  73. expect(processTxtFile(filePath, {})).toEqual([]);
  74. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  75. });
  76. it('should return an empty array for an empty file', () => {
  77. const filePath = 'file.txt';
  78. const fileContent = '';
  79. mockReadFileSync.mockReturnValue(fileContent);
  80. expect(processTxtFile(filePath, {})).toEqual([]);
  81. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
  82. });
  83. });
Tip!

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

Comments

Loading...