Skip to content

Commit 3fa8517

Browse files
refactor: move to testable methods, add UTs
1 parent 90915ae commit 3fa8517

File tree

2 files changed

+106
-12
lines changed

2 files changed

+106
-12
lines changed

src/commands/agent/generate/test-spec.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,34 @@ async function promptForTestCase(genAiPlugins: Record<string, string>, genAiFunc
126126
};
127127
}
128128

129-
async function promptForCustomEvaluations(): Promise<NonNullable<TestCase['customEvaluations']>> {
129+
/**
130+
* Creates a custom evaluation object with the provided parameters
131+
*
132+
* @param label - Descriptive label for the evaluation
133+
* @param jsonPath - JSONPath for the actual value
134+
* @param operator - Comparison operator
135+
* @param expectedValue - Expected value to compare against
136+
* @returns Custom evaluation object in the expected format
137+
*/
138+
export function createCustomEvaluation(
139+
label: string,
140+
jsonPath: string,
141+
operator: string,
142+
expectedValue: string
143+
): NonNullable<TestCase['customEvaluations']>[0] {
144+
return {
145+
label,
146+
name:
147+
!isNaN(Number(expectedValue)) && !isNaN(parseFloat(expectedValue)) ? 'numeric_comparison' : 'string_comparison',
148+
parameters: [
149+
{ name: 'operator', value: operator, isReference: false },
150+
{ name: 'actual', value: jsonPath, isReference: true },
151+
{ name: 'expected', value: expectedValue, isReference: false },
152+
],
153+
};
154+
}
155+
156+
export async function promptForCustomEvaluations(): Promise<NonNullable<TestCase['customEvaluations']>> {
130157
const customEvaluations: NonNullable<TestCase['customEvaluations']> = [];
131158
let wantsCustomEvaluation = await confirm({
132159
message: 'Do you want to add a custom evaluation',
@@ -188,17 +215,7 @@ async function promptForCustomEvaluations(): Promise<NonNullable<TestCase['custo
188215
theme,
189216
});
190217

191-
customEvaluations.push({
192-
label,
193-
// Determine if the expected value is numeric or string comparison
194-
name:
195-
!isNaN(Number(expectedValue)) && !isNaN(parseFloat(expectedValue)) ? 'numeric_comparison' : 'string_comparison',
196-
parameters: [
197-
{ name: 'operator', value: operator, isReference: false },
198-
{ name: 'actual', value: jsonPath, isReference: true },
199-
{ name: 'expected', value: expectedValue, isReference: false },
200-
],
201-
});
218+
customEvaluations.push(createCustomEvaluation(label, jsonPath, operator, expectedValue));
202219

203220
// eslint-disable-next-line no-await-in-loop
204221
wantsCustomEvaluation = await confirm({

test/commands/agent/generate/test-spec.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
ensureYamlExtension,
1717
getMetadataFilePaths,
1818
getPluginsAndFunctions,
19+
createCustomEvaluation,
1920
} from '../../../../src/commands/agent/generate/test-spec.js';
2021

2122
describe('AgentGenerateTestSpec Helper Methods', () => {
@@ -378,4 +379,80 @@ describe('AgentGenerateTestSpec Helper Methods', () => {
378379
expect(result).to.not.have.property('*');
379380
});
380381
});
382+
383+
describe('createCustomEvaluation', () => {
384+
it('should create correct structure for string comparison', () => {
385+
const evaluation = createCustomEvaluation('Test Label', '$.response.message', 'equals', 'expected text');
386+
387+
expect(evaluation).to.deep.equal({
388+
label: 'Test Label',
389+
name: 'string_comparison',
390+
parameters: [
391+
{ name: 'operator', value: 'equals', isReference: false },
392+
{ name: 'actual', value: '$.response.message', isReference: true },
393+
{ name: 'expected', value: 'expected text', isReference: false },
394+
],
395+
});
396+
});
397+
398+
it('should create correct structure for numeric comparison', () => {
399+
const evaluation = createCustomEvaluation('Numeric Test', '$.metrics.score', 'greater_than_or_equal', '85');
400+
401+
expect(evaluation).to.deep.equal({
402+
label: 'Numeric Test',
403+
name: 'numeric_comparison',
404+
parameters: [
405+
{ name: 'operator', value: 'greater_than_or_equal', isReference: false },
406+
{ name: 'actual', value: '$.metrics.score', isReference: true },
407+
{ name: 'expected', value: '85', isReference: false },
408+
],
409+
});
410+
});
411+
412+
it('should handle all supported operators', () => {
413+
const operators = ['equals', 'greater_than_or_equal', 'greater_than', 'less_than', 'less_than_or_equal'];
414+
415+
operators.forEach((operator) => {
416+
const evaluation = createCustomEvaluation(`Test ${operator}`, '$.test.value', operator, '100');
417+
418+
expect(evaluation.parameters[0]).to.deep.equal({
419+
name: 'operator',
420+
value: operator,
421+
isReference: false,
422+
});
423+
});
424+
});
425+
426+
it('should always set correct isReference flags', () => {
427+
const evaluation = createCustomEvaluation('Reference Test', '$.actual.path', 'equals', 'expected');
428+
429+
const [operatorParam, actualParam, expectedParam] = evaluation.parameters;
430+
431+
expect(operatorParam.isReference).to.be.false;
432+
expect(actualParam.isReference).to.be.true; // actual is always a reference (JSONPath)
433+
expect(expectedParam.isReference).to.be.false; // expected is always a literal value
434+
});
435+
436+
it('should correctly determine comparison type based on expected value', () => {
437+
const numericEvaluation = createCustomEvaluation('Test', '$.path', 'equals', '42');
438+
expect(numericEvaluation.name).to.equal('numeric_comparison');
439+
440+
const stringEvaluation = createCustomEvaluation('Test', '$.path', 'equals', 'text');
441+
expect(stringEvaluation.name).to.equal('string_comparison');
442+
});
443+
444+
it('should handle complex JSONPaths and values', () => {
445+
const evaluation = createCustomEvaluation(
446+
'Complex Test',
447+
'$.response.data[0].nested["special-key"].value',
448+
'less_than',
449+
'3.14159'
450+
);
451+
452+
expect(evaluation.label).to.equal('Complex Test');
453+
expect(evaluation.name).to.equal('numeric_comparison');
454+
expect(evaluation.parameters[1].value).to.equal('$.response.data[0].nested["special-key"].value');
455+
expect(evaluation.parameters[2].value).to.equal('3.14159');
456+
});
457+
});
381458
});

0 commit comments

Comments
 (0)