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

markdown.test.ts 2.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
  1. import * as fs from 'fs';
  2. import { processMarkdownFile } from '../../../src/prompts/processors/markdown';
  3. jest.mock('fs');
  4. describe('processMarkdownFile', () => {
  5. const mockReadFileSync = jest.mocked(fs.readFileSync);
  6. beforeEach(() => {
  7. jest.clearAllMocks();
  8. });
  9. it('should process a valid Markdown file without a label', () => {
  10. const filePath = 'file.md';
  11. const fileContent = '# Heading\n\nThis is some markdown content.';
  12. mockReadFileSync.mockReturnValue(fileContent);
  13. expect(processMarkdownFile(filePath, {})).toEqual([
  14. {
  15. raw: fileContent,
  16. label: `${filePath}: # Heading\n\nThis is some markdown content....`,
  17. },
  18. ]);
  19. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
  20. });
  21. it('should process a valid Markdown file with a label', () => {
  22. const filePath = 'file.md';
  23. const fileContent = '# Heading\n\nThis is some markdown content.';
  24. mockReadFileSync.mockReturnValue(fileContent);
  25. expect(processMarkdownFile(filePath, { label: 'Custom Label' })).toEqual([
  26. {
  27. raw: fileContent,
  28. label: 'Custom Label',
  29. },
  30. ]);
  31. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
  32. });
  33. it('should truncate the label for long Markdown files', () => {
  34. const filePath = 'file.md';
  35. const fileContent = '# ' + 'A'.repeat(100);
  36. mockReadFileSync.mockReturnValue(fileContent);
  37. expect(processMarkdownFile(filePath, {})).toEqual([
  38. {
  39. raw: fileContent,
  40. label: `${filePath}: # ${'A'.repeat(48)}...`,
  41. },
  42. ]);
  43. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
  44. });
  45. it('should throw an error if the file cannot be read', () => {
  46. const filePath = 'nonexistent.md';
  47. mockReadFileSync.mockImplementation(() => {
  48. throw new Error('File not found');
  49. });
  50. expect(() => processMarkdownFile(filePath, {})).toThrow('File not found');
  51. expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
  52. });
  53. });
Tip!

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

Comments

Loading...