Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

151 lines
4.8 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { getActualPrompt, getActualPromptWithFallback } from '../../src/util/providerResponse';
import type { ProviderResponse } from '../../src/types/providers';
describe('getActualPrompt', () => {
it('should return undefined for undefined response', () => {
expect(getActualPrompt(undefined)).toBeUndefined();
});
it('should return undefined for response without prompt', () => {
const response: ProviderResponse = {
output: 'test output',
};
expect(getActualPrompt(response)).toBeUndefined();
});
it('should return string prompt directly', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: 'Hello, world!',
};
expect(getActualPrompt(response)).toBe('Hello, world!');
});
it('should return undefined for empty string prompt', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: '',
};
// Empty string is truthy check but empty, so we return undefined
expect(getActualPrompt(response)).toBeUndefined();
});
it('should stringify chat message array', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: [
{ role: 'system', content: 'You are helpful' },
{ role: 'user', content: 'Hello' },
],
};
expect(getActualPrompt(response)).toBe(
'[{"role":"system","content":"You are helpful"},{"role":"user","content":"Hello"}]',
);
});
it('should format chat message array when formatted option is true', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: [
{ role: 'system', content: 'You are helpful' },
{ role: 'user', content: 'Hello' },
],
};
const result = getActualPrompt(response, { formatted: true });
expect(result).toContain('\n');
expect(result).toContain(' ');
});
it('should return undefined for empty array prompt', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: [],
};
// Empty array is not useful
expect(getActualPrompt(response)).toBeUndefined();
});
it('should fall back to redteamFinalPrompt when prompt is not set', () => {
const response: ProviderResponse = {
output: 'test output',
metadata: {
redteamFinalPrompt: 'fallback prompt',
},
};
expect(getActualPrompt(response)).toBe('fallback prompt');
});
it('should prioritize prompt over redteamFinalPrompt', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: 'provider prompt',
metadata: {
redteamFinalPrompt: 'fallback prompt',
},
};
expect(getActualPrompt(response)).toBe('provider prompt');
});
it('should not fall back to redteamFinalPrompt when prompt is empty string', () => {
// Empty string means provider explicitly set no prompt, so we don't fall back
const response: ProviderResponse = {
output: 'test output',
prompt: '',
metadata: {
redteamFinalPrompt: 'fallback prompt',
},
};
// With empty string prompt explicitly set, we return undefined (not the fallback)
expect(getActualPrompt(response)).toBeUndefined();
});
});
describe('getActualPromptWithFallback', () => {
it('should return provider prompt when available', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: 'provider prompt',
};
expect(getActualPromptWithFallback(response, 'original prompt')).toBe('provider prompt');
});
it('should return redteamFinalPrompt when prompt not available', () => {
const response: ProviderResponse = {
output: 'test output',
metadata: {
redteamFinalPrompt: 'redteam prompt',
},
};
expect(getActualPromptWithFallback(response, 'original prompt')).toBe('redteam prompt');
});
it('should return original prompt when neither prompt nor redteamFinalPrompt available', () => {
const response: ProviderResponse = {
output: 'test output',
};
expect(getActualPromptWithFallback(response, 'original prompt')).toBe('original prompt');
});
it('should return original prompt for undefined response', () => {
expect(getActualPromptWithFallback(undefined, 'original prompt')).toBe('original prompt');
});
it('should return original prompt when prompt is empty string', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: '',
};
expect(getActualPromptWithFallback(response, 'original prompt')).toBe('original prompt');
});
it('should return original prompt when prompt is empty array', () => {
const response: ProviderResponse = {
output: 'test output',
prompt: [],
};
expect(getActualPromptWithFallback(response, 'original prompt')).toBe('original prompt');
});
});