0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
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) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
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
383 lines
9.6 KiB
TypeScript
383 lines
9.6 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { ResultFailureReason } from '../../../src/types/index';
|
|
import { getHeaderForTable } from '../../../src/util/exportToFile/getHeaderForTable';
|
|
import {
|
|
convertEvalResultToTableCell,
|
|
convertTestResultsToTableRow,
|
|
} from '../../../src/util/exportToFile/index';
|
|
|
|
import type Eval from '../../../src/models/eval';
|
|
import type EvalResult from '../../../src/models/evalResult';
|
|
|
|
describe('exportToFile utils', () => {
|
|
describe('getHeaderForTable', () => {
|
|
it('should extract vars from defaultTest', () => {
|
|
const eval_: Partial<Eval> = {
|
|
id: 'test-id',
|
|
createdAt: Date.now(),
|
|
config: {
|
|
defaultTest: {
|
|
vars: {
|
|
var1: 'value1',
|
|
var2: 'value2',
|
|
},
|
|
},
|
|
},
|
|
prompts: [],
|
|
results: [],
|
|
persisted: false,
|
|
};
|
|
|
|
const result = getHeaderForTable(eval_ as Eval);
|
|
expect(result.vars).toEqual(['var1', 'var2']);
|
|
});
|
|
|
|
it('should extract vars from tests array', () => {
|
|
const eval_: Partial<Eval> = {
|
|
id: 'test-id',
|
|
createdAt: Date.now(),
|
|
config: {
|
|
tests: [
|
|
{
|
|
vars: {
|
|
var3: 'value3',
|
|
var4: 'value4',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
prompts: [],
|
|
results: [],
|
|
persisted: false,
|
|
};
|
|
|
|
const result = getHeaderForTable(eval_ as Eval);
|
|
expect(result.vars).toEqual(['var3', 'var4']);
|
|
});
|
|
|
|
it('should extract vars from scenarios', () => {
|
|
const eval_: Partial<Eval> = {
|
|
id: 'test-id',
|
|
createdAt: Date.now(),
|
|
config: {
|
|
scenarios: [
|
|
{
|
|
config: [
|
|
{
|
|
vars: {
|
|
var5: 'value5',
|
|
},
|
|
},
|
|
],
|
|
tests: [
|
|
{
|
|
vars: {
|
|
var6: 'value6',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
prompts: [],
|
|
results: [],
|
|
persisted: false,
|
|
};
|
|
|
|
const result = getHeaderForTable(eval_ as Eval);
|
|
expect(result.vars).toEqual(['var5', 'var6']);
|
|
});
|
|
|
|
it('should handle empty config', () => {
|
|
const eval_: Partial<Eval> = {
|
|
id: 'test-id',
|
|
createdAt: Date.now(),
|
|
config: {},
|
|
prompts: [],
|
|
results: [],
|
|
persisted: false,
|
|
};
|
|
|
|
const result = getHeaderForTable(eval_ as Eval);
|
|
expect(result.vars).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('convertEvalResultToTableCell', () => {
|
|
it('should handle successful assertion result', () => {
|
|
const result: Partial<EvalResult> = {
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
testCase: {
|
|
assert: [
|
|
{
|
|
type: 'contains',
|
|
value: 'test',
|
|
},
|
|
],
|
|
},
|
|
success: true,
|
|
response: {
|
|
output: 'test output',
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.NONE,
|
|
};
|
|
|
|
const output = convertEvalResultToTableCell(result as EvalResult);
|
|
expect(output.text).toBe('test output');
|
|
expect(output.pass).toBe(true);
|
|
});
|
|
|
|
it('should handle failed assertion result', () => {
|
|
const result: Partial<EvalResult> = {
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
testCase: {
|
|
assert: [
|
|
{
|
|
type: 'contains',
|
|
value: 'test',
|
|
},
|
|
],
|
|
},
|
|
success: false,
|
|
error: 'test error',
|
|
response: {
|
|
output: 'test output',
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
gradingResult: {
|
|
pass: false,
|
|
score: 0,
|
|
reason: 'test failure',
|
|
componentResults: [
|
|
{
|
|
pass: false,
|
|
score: 0,
|
|
reason: 'test failure',
|
|
},
|
|
],
|
|
},
|
|
failureReason: ResultFailureReason.ASSERT,
|
|
};
|
|
|
|
const output = convertEvalResultToTableCell(result as EvalResult);
|
|
expect(output.text).toBe('test output');
|
|
expect(output.pass).toBe(false);
|
|
});
|
|
|
|
it('should handle error without assertion', () => {
|
|
const result: Partial<EvalResult> = {
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
testCase: {},
|
|
error: 'test error',
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.ERROR,
|
|
};
|
|
|
|
const output = convertEvalResultToTableCell(result as EvalResult);
|
|
expect(output.text).toBe('test error');
|
|
});
|
|
|
|
it('should preserve falsy outputs like 0 and false', () => {
|
|
const numericResult: Partial<EvalResult> = {
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
testCase: {},
|
|
response: {
|
|
output: 0,
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.NONE,
|
|
};
|
|
|
|
const booleanResult: Partial<EvalResult> = {
|
|
id: 'test-2',
|
|
evalId: 'eval-1',
|
|
testCase: {},
|
|
response: {
|
|
output: false,
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.NONE,
|
|
};
|
|
|
|
const numericOutput = convertEvalResultToTableCell(numericResult as EvalResult);
|
|
const booleanOutput = convertEvalResultToTableCell(booleanResult as EvalResult);
|
|
|
|
expect(numericOutput.text).toBe('0');
|
|
expect(booleanOutput.text).toBe('false');
|
|
});
|
|
|
|
it('should handle null output by falling back to error', () => {
|
|
const resultWithError: Partial<EvalResult> = {
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
testCase: {},
|
|
response: {
|
|
output: null,
|
|
},
|
|
error: 'Provider returned null',
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.ERROR,
|
|
};
|
|
|
|
const resultWithoutError: Partial<EvalResult> = {
|
|
id: 'test-2',
|
|
evalId: 'eval-1',
|
|
testCase: {},
|
|
response: {
|
|
output: null,
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.NONE,
|
|
};
|
|
|
|
const outputWithError = convertEvalResultToTableCell(resultWithError as EvalResult);
|
|
const outputWithoutError = convertEvalResultToTableCell(resultWithoutError as EvalResult);
|
|
|
|
expect(outputWithError.text).toBe('Provider returned null');
|
|
expect(outputWithoutError.text).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('convertTestResultsToTableRow', () => {
|
|
it('should convert results to table row', () => {
|
|
const results: Partial<EvalResult>[] = [
|
|
{
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
description: 'test description',
|
|
promptIdx: 0,
|
|
testCase: {
|
|
vars: {
|
|
var1: 'value1',
|
|
var2: 'value2',
|
|
},
|
|
},
|
|
response: {
|
|
output: 'test output',
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.NONE,
|
|
},
|
|
];
|
|
|
|
const varsForHeader = ['var1', 'var2'];
|
|
|
|
const row = convertTestResultsToTableRow(results as EvalResult[], varsForHeader);
|
|
|
|
expect(row.description).toBe('test description');
|
|
expect(row.vars).toEqual(['value1', 'value2']);
|
|
expect(row.outputs[0].text).toBe('test output');
|
|
});
|
|
|
|
it('should handle complex var values', () => {
|
|
const results: Partial<EvalResult>[] = [
|
|
{
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
promptIdx: 0,
|
|
testCase: {
|
|
vars: {
|
|
var1: { complex: 'object' },
|
|
},
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.NONE,
|
|
},
|
|
];
|
|
|
|
const varsForHeader = ['var1'];
|
|
|
|
const row = convertTestResultsToTableRow(results as EvalResult[], varsForHeader);
|
|
expect(row.vars).toEqual(['{"complex":"object"}']);
|
|
});
|
|
|
|
it('should preserve falsy var values like 0 and false', () => {
|
|
const results: Partial<EvalResult>[] = [
|
|
{
|
|
id: 'test-1',
|
|
evalId: 'eval-1',
|
|
promptIdx: 0,
|
|
testCase: {
|
|
vars: {
|
|
var1: 0,
|
|
var2: false,
|
|
},
|
|
},
|
|
prompt: {
|
|
raw: 'test prompt',
|
|
label: 'test',
|
|
},
|
|
provider: {
|
|
id: 'test-provider',
|
|
},
|
|
failureReason: ResultFailureReason.NONE,
|
|
},
|
|
];
|
|
|
|
const varsForHeader = ['var1', 'var2'];
|
|
|
|
const row = convertTestResultsToTableRow(results as EvalResult[], varsForHeader);
|
|
expect(row.vars).toEqual(['0', 'false']);
|
|
});
|
|
});
|
|
});
|