forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompts.processors.markdown.test.ts
61 lines (53 loc) · 1.96 KB
/
prompts.processors.markdown.test.ts
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
import * as fs from 'fs';
import { processMarkdownFile } from '../src/prompts/processors/markdown';
jest.mock('fs');
describe('processMarkdownFile', () => {
const mockReadFileSync = jest.mocked(fs.readFileSync);
beforeEach(() => {
jest.clearAllMocks();
});
it('should process a valid Markdown file without a label', () => {
const filePath = 'file.md';
const fileContent = '# Heading\n\nThis is some markdown content.';
mockReadFileSync.mockReturnValue(fileContent);
expect(processMarkdownFile(filePath, {})).toEqual([
{
raw: fileContent,
label: `${filePath}: # Heading\n\nThis is some markdown content....`,
},
]);
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
});
it('should process a valid Markdown file with a label', () => {
const filePath = 'file.md';
const fileContent = '# Heading\n\nThis is some markdown content.';
mockReadFileSync.mockReturnValue(fileContent);
expect(processMarkdownFile(filePath, { label: 'Custom Label' })).toEqual([
{
raw: fileContent,
label: 'Custom Label',
},
]);
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
});
it('should truncate the label for long Markdown files', () => {
const filePath = 'file.md';
const fileContent = '# ' + 'A'.repeat(100);
mockReadFileSync.mockReturnValue(fileContent);
expect(processMarkdownFile(filePath, {})).toEqual([
{
raw: fileContent,
label: `${filePath}: # ${'A'.repeat(48)}...`,
},
]);
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
});
it('should throw an error if the file cannot be read', () => {
const filePath = 'nonexistent.md';
mockReadFileSync.mockImplementation(() => {
throw new Error('File not found');
});
expect(() => processMarkdownFile(filePath, {})).toThrow('File not found');
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf8');
});
});