0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
108 lines
3.6 KiB
TypeScript
108 lines
3.6 KiB
TypeScript
import * as fs from 'fs';
|
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { PROMPT_DELIMITER } from '../../../src/prompts/constants';
|
|
import { processTxtFile } from '../../../src/prompts/processors/text';
|
|
|
|
vi.mock('fs');
|
|
|
|
describe('processTxtFile', () => {
|
|
const mockReadFileSync = vi.mocked(fs.readFileSync);
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should process a text file with single prompt and no label', () => {
|
|
const filePath = 'file.txt';
|
|
const fileContent = 'This is a prompt';
|
|
mockReadFileSync.mockReturnValue(fileContent);
|
|
expect(processTxtFile(filePath, {})).toEqual([
|
|
{
|
|
raw: 'This is a prompt',
|
|
label: 'file.txt: This is a prompt',
|
|
},
|
|
]);
|
|
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
|
|
});
|
|
|
|
it('should process a text file with single prompt and a label', () => {
|
|
const filePath = 'file.txt';
|
|
const fileContent = 'This is a prompt';
|
|
mockReadFileSync.mockReturnValue(fileContent);
|
|
expect(processTxtFile(filePath, { label: 'prompt 1' })).toEqual([
|
|
{
|
|
raw: 'This is a prompt',
|
|
label: 'prompt 1: file.txt: This is a prompt',
|
|
},
|
|
]);
|
|
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
|
|
});
|
|
|
|
it('should process a text file with multiple prompts and a label', () => {
|
|
const fileContent = `Prompt 1\n${PROMPT_DELIMITER}\nPrompt 2\n${PROMPT_DELIMITER}\nPrompt 3`;
|
|
mockReadFileSync.mockReturnValue(fileContent);
|
|
expect(processTxtFile('file.txt', { label: 'Label' })).toEqual([
|
|
{
|
|
raw: 'Prompt 1',
|
|
label: `Label: file.txt: Prompt 1`,
|
|
},
|
|
{
|
|
raw: 'Prompt 2',
|
|
label: `Label: file.txt: Prompt 2`,
|
|
},
|
|
{
|
|
raw: 'Prompt 3',
|
|
label: `Label: file.txt: Prompt 3`,
|
|
},
|
|
]);
|
|
expect(mockReadFileSync).toHaveBeenCalledWith('file.txt', 'utf-8');
|
|
});
|
|
|
|
it('should handle text file with leading and trailing delimiters', () => {
|
|
const filePath = 'file.txt';
|
|
const fileContent = `${PROMPT_DELIMITER}\nPrompt 1\n${PROMPT_DELIMITER}\nPrompt 2\n${PROMPT_DELIMITER}`;
|
|
mockReadFileSync.mockReturnValue(fileContent);
|
|
expect(processTxtFile(filePath, {})).toEqual([
|
|
{
|
|
raw: 'Prompt 1',
|
|
label: `${filePath}: Prompt 1`,
|
|
},
|
|
{
|
|
raw: 'Prompt 2',
|
|
label: `${filePath}: Prompt 2`,
|
|
},
|
|
]);
|
|
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
|
|
});
|
|
|
|
it('should return an empty array for a file with only delimiters', () => {
|
|
const filePath = 'file.txt';
|
|
const fileContent = `${PROMPT_DELIMITER}\n${PROMPT_DELIMITER}`;
|
|
mockReadFileSync.mockReturnValue(fileContent);
|
|
expect(processTxtFile(filePath, {})).toEqual([]);
|
|
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
|
|
});
|
|
|
|
it('should return an empty array for an empty file', () => {
|
|
const filePath = 'file.txt';
|
|
const fileContent = '';
|
|
mockReadFileSync.mockReturnValue(fileContent);
|
|
expect(processTxtFile(filePath, {})).toEqual([]);
|
|
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
|
|
});
|
|
|
|
it('should not split on lines that contain repeated hyphens', () => {
|
|
const filePath = 'file.txt';
|
|
const fileContent = 'Line 1\n-----------------------------------\nLine 2';
|
|
mockReadFileSync.mockReturnValue(fileContent);
|
|
expect(processTxtFile(filePath, {})).toEqual([
|
|
{
|
|
raw: 'Line 1\n-----------------------------------\nLine 2',
|
|
label: `${filePath}: Line 1\n-----------------------------------\nLine 2`,
|
|
},
|
|
]);
|
|
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, 'utf-8');
|
|
});
|
|
});
|