diff --git a/web/app/components/evaluation/components/batch-test-panel/input-fields/__tests__/input-fields-utils.spec.ts b/web/app/components/evaluation/components/batch-test-panel/input-fields/__tests__/input-fields-utils.spec.ts new file mode 100644 index 0000000000..67ee548738 --- /dev/null +++ b/web/app/components/evaluation/components/batch-test-panel/input-fields/__tests__/input-fields-utils.spec.ts @@ -0,0 +1,26 @@ +import { buildTemplateCsvContent } from '../input-fields-utils' + +describe('input fields utils', () => { + describe('buildTemplateCsvContent', () => { + it('should append expected_output as the last CSV column', () => { + expect(buildTemplateCsvContent([ + { name: 'query', type: 'string' }, + { name: 'context', type: 'string' }, + ])).toBe('query,context,expected_output\n') + }) + + it('should not duplicate expected_output when it already exists', () => { + expect(buildTemplateCsvContent([ + { name: 'query', type: 'string' }, + { name: 'expected_output', type: 'string' }, + ])).toBe('query,expected_output\n') + }) + + it('should escape CSV column names before appending expected_output', () => { + expect(buildTemplateCsvContent([ + { name: 'query,text', type: 'string' }, + { name: 'answer "draft"', type: 'string' }, + ])).toBe('"query,text","answer ""draft""",expected_output\n') + }) + }) +}) diff --git a/web/app/components/evaluation/components/batch-test-panel/input-fields/input-fields-utils.ts b/web/app/components/evaluation/components/batch-test-panel/input-fields/input-fields-utils.ts index ce21045e13..6daae07a84 100644 --- a/web/app/components/evaluation/components/batch-test-panel/input-fields/input-fields-utils.ts +++ b/web/app/components/evaluation/components/batch-test-panel/input-fields/input-fields-utils.ts @@ -10,6 +10,8 @@ export type InputField = { type: string } +export const EXPECTED_OUTPUT_FIELD_NAME = 'expected_output' + export const getGraphNodes = (graph?: Record) => { return Array.isArray(graph?.nodes) ? graph.nodes as Node[] : [] } @@ -63,7 +65,12 @@ const escapeCsvCell = (value: string) => { } export const buildTemplateCsvContent = (inputFields: InputField[]) => { - return `${inputFields.map(field => escapeCsvCell(field.name)).join(',')}\n` + const fieldNames = inputFields.map(field => field.name) + const templateFieldNames = fieldNames.includes(EXPECTED_OUTPUT_FIELD_NAME) + ? fieldNames + : [...fieldNames, EXPECTED_OUTPUT_FIELD_NAME] + + return `${templateFieldNames.map(escapeCsvCell).join(',')}\n` } export const getFileExtension = (fileName: string) => {