Files
promptfoo--promptfoo/test/smoke/features-and-assertions.test.ts
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

310 lines
10 KiB
TypeScript

/**
* Smoke tests for additional assertions, transforms, and feature integration.
*
* Tests provider named functions, test generators, additional assertion types,
* response transforms, and advanced config features.
*
* @see docs/plans/smoke-tests.md for the full checklist
*/
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
// Path to the built CLI binary
const CLI_PATH = path.resolve(__dirname, '../../dist/src/main.js');
const ROOT_DIR = path.resolve(__dirname, '../..');
const FIXTURES_DIR = path.resolve(__dirname, 'fixtures');
const CONFIGS_DIR = path.resolve(FIXTURES_DIR, 'configs');
const OUTPUT_DIR = path.resolve(__dirname, '.temp-output-features');
/**
* Helper to run the CLI and capture output
*/
function runCli(
args: string[],
options: { cwd?: string; env?: NodeJS.ProcessEnv } = {},
): { stdout: string; stderr: string; exitCode: number } {
const result = spawnSync('node', [CLI_PATH, ...args], {
cwd: options.cwd || ROOT_DIR,
encoding: 'utf-8',
env: { ...process.env, ...options.env, NO_COLOR: '1' },
timeout: 60000,
});
return {
stdout: result.stdout || '',
stderr: result.stderr || '',
exitCode: result.status ?? 1,
};
}
describe('Named Function Provider Smoke Tests', () => {
beforeAll(() => {
if (!fs.existsSync(CLI_PATH)) {
throw new Error(`Built CLI not found at ${CLI_PATH}. Run 'npm run build' first.`);
}
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
});
afterAll(() => {
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true, force: true });
}
});
describe('5.1 Additional Assertions', () => {
it('5.1.2 - not-contains assertion', () => {
const configPath = path.join(CONFIGS_DIR, 'not-contains-assertion.yaml');
const outputPath = path.join(OUTPUT_DIR, 'not-contains-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed.results.results[0].success).toBe(true);
});
});
describe('3.4 Python Provider Named Functions', () => {
it('3.4.2 - Python provider with named function', () => {
const configPath = path.join(CONFIGS_DIR, 'provider-python-named.yaml');
const outputPath = path.join(OUTPUT_DIR, 'python-named-output.json');
const { exitCode, stdout, stderr } = runCli(
['eval', '-c', configPath, '-o', outputPath, '--no-cache'],
{ cwd: CONFIGS_DIR },
);
// Python may not be available in all environments
if (exitCode !== 0) {
const output = stdout + stderr;
if (
output.toLowerCase().includes('python') &&
(output.toLowerCase().includes('not found') ||
output.toLowerCase().includes('no such file'))
) {
return;
}
}
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed.results.results[0].success).toBe(true);
expect(parsed.results.results[0].response.output).toContain('Python Custom Echo:');
});
});
});
describe('Test Generator Smoke Tests', () => {
beforeAll(() => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
});
afterAll(() => {
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true, force: true });
}
});
describe('4.2 JavaScript Test Generator', () => {
it('4.2.7 - loads tests from JavaScript generator file', () => {
const configPath = path.join(CONFIGS_DIR, 'js-test-generator.yaml');
const outputPath = path.join(OUTPUT_DIR, 'js-generator-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
// Should have 3 test results from the generator
expect(parsed.results.results.length).toBe(3);
const outputs = parsed.results.results.map(
(r: { response: { output: string } }) => r.response.output,
);
expect(outputs.some((o: string) => o.includes('Generated1'))).toBe(true);
expect(outputs.some((o: string) => o.includes('Generated2'))).toBe(true);
expect(outputs.some((o: string) => o.includes('Generated3'))).toBe(true);
});
});
});
describe('Additional Assertion Smoke Tests', () => {
beforeAll(() => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
});
afterAll(() => {
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true, force: true });
}
});
describe('5.1 Built-in Assertions', () => {
it('5.1.1 - contains assertion', () => {
const configPath = path.join(CONFIGS_DIR, 'contains-assertion.yaml');
const outputPath = path.join(OUTPUT_DIR, 'contains-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed.results.results[0].success).toBe(true);
});
it('5.1.4 - starts-with assertion', () => {
const configPath = path.join(CONFIGS_DIR, 'starts-with-assertion.yaml');
const outputPath = path.join(OUTPUT_DIR, 'starts-with-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed.results.results[0].success).toBe(true);
});
it('5.1.7 - contains-json assertion', () => {
const configPath = path.join(CONFIGS_DIR, 'contains-json-assertion.yaml');
const outputPath = path.join(OUTPUT_DIR, 'contains-json-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed.results.results[0].success).toBe(true);
});
});
});
describe('Transform Smoke Tests', () => {
beforeAll(() => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
});
afterAll(() => {
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true, force: true });
}
});
describe('6.1 Response Transforms', () => {
it('6.1.1 - transform response expression', () => {
const configPath = path.join(CONFIGS_DIR, 'transform-response.yaml');
const outputPath = path.join(OUTPUT_DIR, 'transform-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed.results.results[0].success).toBe(true);
});
});
});
describe('Feature Integration Smoke Tests', () => {
beforeAll(() => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
});
afterAll(() => {
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true, force: true });
}
});
describe('7.1 Provider Config Options', () => {
it('7.1.1 - provider with config options', () => {
const configPath = path.join(CONFIGS_DIR, 'provider-with-config.yaml');
const outputPath = path.join(OUTPUT_DIR, 'provider-config-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed.results.results[0].success).toBe(true);
// Check that the provider label is used
expect(parsed.results.prompts[0].provider).toBe('Custom Echo Provider');
});
});
describe('7.2 DefaultTest', () => {
it('7.2.1 - defaultTest applies assertions to all tests', () => {
const configPath = path.join(CONFIGS_DIR, 'default-test.yaml');
const outputPath = path.join(OUTPUT_DIR, 'default-test-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
// All 3 tests should pass with the defaultTest assertion
expect(parsed.results.results.length).toBe(3);
expect(parsed.results.results.every((r: { success: boolean }) => r.success)).toBe(true);
});
});
describe('7.3 Scenarios', () => {
it('7.3.1 - scenarios feature loads and runs correctly', () => {
const configPath = path.join(CONFIGS_DIR, 'scenarios.yaml');
const outputPath = path.join(OUTPUT_DIR, 'scenarios-output.json');
const { exitCode } = runCli(['eval', '-c', configPath, '-o', outputPath, '--no-cache'], {
cwd: CONFIGS_DIR,
});
expect(exitCode).toBe(0);
const content = fs.readFileSync(outputPath, 'utf-8');
const parsed = JSON.parse(content);
// Should have 3 tests total (2 from US scenario + 1 from EU scenario)
expect(parsed.results.results.length).toBe(3);
expect(parsed.results.results.every((r: { success: boolean }) => r.success)).toBe(true);
// Verify scenarios data
const outputs = parsed.results.results.map(
(r: { response: { output: string } }) => r.response.output,
);
expect(outputs.some((o: string) => o.includes('New York'))).toBe(true);
expect(outputs.some((o: string) => o.includes('California'))).toBe(true);
expect(outputs.some((o: string) => o.includes('Paris'))).toBe(true);
});
});
});