chore: import upstream snapshot with attribution
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

This commit is contained in:
wehub-resource-sync
2026-07-13 13:24:08 +08:00
commit 0d3cb498a3
5438 changed files with 1316560 additions and 0 deletions
@@ -0,0 +1,167 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { HARM_PLUGINS, UNALIGNED_PROVIDER_HARM_PLUGINS } from '../../../../src/redteam/constants';
import { categoryAliases } from '../../../../src/redteam/constants/metadata';
import { AlignedHarmfulPlugin } from '../../../../src/redteam/plugins/harmful/aligned';
import { REDTEAM_MODEL_CATEGORIES } from '../../../../src/redteam/plugins/harmful/constants';
import { createMockProvider, type MockApiProvider } from '../../../factories/provider';
import type { HarmfulCategory } from '../../../../src/redteam/plugins/harmful/constants';
describe('AlignedHarmfulPlugin', () => {
let mockProvider: MockApiProvider;
let plugin: AlignedHarmfulPlugin;
let harmCategory: keyof typeof HARM_PLUGINS;
beforeEach(() => {
mockProvider = createMockProvider();
// Find a harm category that isn't in unaligned providers
harmCategory = Object.keys(HARM_PLUGINS).find(
(key) => !(key in UNALIGNED_PROVIDER_HARM_PLUGINS),
) as keyof typeof HARM_PLUGINS;
const harmfulCategory: HarmfulCategory = {
key: harmCategory as HarmfulCategory['key'],
prompt: 'test prompt template {{ examples }}',
examples: 'test examples',
label: 'Test Label' as HarmfulCategory['label'],
description: 'Test Description',
};
// Mock a valid category in REDTEAM_MODEL_CATEGORIES
vi.spyOn(REDTEAM_MODEL_CATEGORIES, 'find').mockReturnValue(harmfulCategory);
plugin = new AlignedHarmfulPlugin(mockProvider, 'test purpose', 'testVar', harmCategory);
});
afterEach(() => {
vi.clearAllMocks();
});
describe('initialization', () => {
it('should create instance with valid parameters', () => {
expect(plugin).toBeInstanceOf(AlignedHarmfulPlugin);
expect(mockProvider.id()).toBe('test-provider');
});
it('should accept optional config parameter', () => {
const configPlugin = new AlignedHarmfulPlugin(
mockProvider,
'test purpose',
'testVar',
harmCategory,
{ examples: ['test example'] },
);
expect(configPlugin).toBeInstanceOf(AlignedHarmfulPlugin);
});
});
describe('template handling', () => {
it('should retrieve correct template for harm category', async () => {
const template = await plugin['getTemplate']();
expect(template).toBe('test prompt template {{ examples }}');
});
it('should throw when category config is not found', async () => {
vi.spyOn(REDTEAM_MODEL_CATEGORIES, 'find').mockReturnValue(undefined);
await expect(plugin['getTemplate']()).rejects.toThrow(
`Harmful category ${harmCategory} not found`,
);
});
});
describe('assertion generation', () => {
it('should generate harmful assertions for given prompt', () => {
const assertions = plugin['getAssertions']('test prompt');
const expectedMetric = categoryAliases[harmCategory] || harmCategory;
expect(assertions).toEqual(
expect.arrayContaining([
expect.objectContaining({
metric: expectedMetric,
}),
]),
);
});
it('should include harm category in assertions', () => {
const assertions = plugin['getAssertions']('test prompt');
const expectedMetric = categoryAliases[harmCategory] || harmCategory;
assertions.forEach((assertion) => {
expect(assertion).toHaveProperty('metric');
expect(assertion.metric).toBe(expectedMetric);
});
});
});
describe('test case generation', () => {
const mockPrompts = [{ __prompt: 'Test prompt 1' }, { __prompt: 'Test prompt 2' }];
it('should create properly structured test cases', async () => {
const testCases = await plugin['promptsToTestCases'](mockPrompts);
const expectedMetric = categoryAliases[harmCategory] || harmCategory;
expect(testCases).toHaveLength(2);
testCases.forEach((testCase) => {
expect(testCase).toMatchObject({
vars: {
testVar: expect.any(String),
},
metadata: {
harmCategory: HARM_PLUGINS[harmCategory],
},
assert: expect.arrayContaining([
expect.objectContaining({
metric: expectedMetric,
}),
]),
});
});
});
it('should handle empty prompts array', async () => {
const testCases = await plugin['promptsToTestCases']([]);
expect(testCases).toHaveLength(0);
});
});
describe('end-to-end test generation', () => {
beforeEach(() => {
vi.spyOn(mockProvider, 'callApi').mockResolvedValue({
output: 'Prompt: Generated test 1\nPrompt: Generated test 2',
});
});
it('should generate requested number of test cases', async () => {
const testCases = await plugin.generateTests(2);
expect(testCases).toHaveLength(2);
expect(mockProvider.callApi).toHaveBeenCalledTimes(1);
});
it('should handle custom examples in config', async () => {
const customExamples = ['Custom example 1', 'Custom example 2'];
const configuredPlugin = new AlignedHarmfulPlugin(
mockProvider,
'test purpose',
'testVar',
harmCategory,
{ examples: customExamples },
);
await configuredPlugin.generateTests(1);
expect(mockProvider.callApi).toHaveBeenCalledWith(
expect.stringContaining('test prompt template Custom example 1,Custom example 2'),
);
});
it('should handle API errors gracefully', async () => {
vi.spyOn(mockProvider, 'callApi').mockResolvedValue({
output: '',
error: 'API Error',
});
const testCases = await plugin.generateTests(1);
expect(testCases).toHaveLength(0);
});
});
});
+191
View File
@@ -0,0 +1,191 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getEnvBool, getEnvString } from '../../../../src/envars';
import {
HARM_PLUGINS,
LLAMA_GUARD_ENABLED_CATEGORIES,
LLAMA_GUARD_REPLICATE_PROVIDER,
} from '../../../../src/redteam/constants';
import { categoryAliases } from '../../../../src/redteam/constants/metadata';
import { GRADERS } from '../../../../src/redteam/graders';
import {
createTestCase,
getHarmfulAssertions,
} from '../../../../src/redteam/plugins/harmful/common';
vi.mock('../../../../src/envars');
describe('harmful plugin', () => {
beforeEach(() => {
vi.mocked(getEnvBool).mockReset();
vi.mocked(getEnvString).mockReset();
});
describe('getHarmfulAssertions', () => {
beforeEach(() => {
vi.mocked(getEnvBool).mockReset();
vi.mocked(getEnvString).mockReset();
});
it('should use specific subcategory as metric name for privacy category', () => {
const assertions = getHarmfulAssertions('harmful:privacy');
expect(assertions).toEqual([
{
metric: categoryAliases['harmful:privacy'],
type: 'promptfoo:redteam:harmful:privacy',
},
]);
expect(assertions[0].metric).toBe('Privacy violations');
});
it('should use specific subcategory as metric name for intellectual property category', () => {
const assertions = getHarmfulAssertions('harmful:intellectual-property');
expect(assertions).toEqual([
{
metric: categoryAliases['harmful:intellectual-property'],
type: 'promptfoo:redteam:harmful:intellectual-property',
},
]);
expect(assertions[0].metric).toBe('Intellectual Property violation');
});
it('should use specific subcategory as metric name for copyright violations', () => {
const assertions = getHarmfulAssertions('harmful:copyright-violations');
expect(assertions).toEqual([
{
metric: categoryAliases['harmful:copyright-violations'],
type: 'promptfoo:redteam:harmful:copyright-violations',
},
]);
expect(assertions[0].metric).toBe('Copyright Violations - Copyrighted text');
});
it('should return only basic assertions when moderation is disabled', () => {
vi.mocked(getEnvBool).mockImplementation(function (key) {
if (key === 'PROMPTFOO_DISABLE_REDTEAM_MODERATION') {
return true;
}
return false;
});
vi.mocked(getEnvString).mockImplementation(function () {
return 'test-key';
});
const assertions = getHarmfulAssertions('harmful:privacy');
expect(assertions).toHaveLength(1);
expect(assertions[0].metric).toBe('Privacy violations');
expect(assertions).not.toContainEqual(expect.objectContaining({ type: 'moderation' }));
});
it('should prioritize Replicate over OpenAI when both keys are present', () => {
vi.mocked(getEnvString).mockImplementation(function (key) {
if (key === 'REPLICATE_API_KEY') {
return 'replicate-key';
}
if (key === 'OPENAI_API_KEY') {
return 'openai-key';
}
return '';
});
const assertions = getHarmfulAssertions('harmful:privacy');
expect(assertions).toContainEqual({
metric: 'Privacy violations',
type: 'moderation',
value: LLAMA_GUARD_ENABLED_CATEGORIES,
provider: LLAMA_GUARD_REPLICATE_PROVIDER,
});
expect(assertions).toHaveLength(2);
// Verify all assertions use the same metric name
expect(assertions[0].metric).toBe('Privacy violations');
expect(assertions[1].metric).toBe('Privacy violations');
});
it('should handle REPLICATE_API_TOKEN', () => {
vi.mocked(getEnvString).mockImplementation(function (key) {
if (key === 'REPLICATE_API_TOKEN') {
return 'replicate-token';
}
return '';
});
const assertions = getHarmfulAssertions('harmful:privacy');
expect(assertions).toContainEqual({
metric: 'Privacy violations',
type: 'moderation',
value: LLAMA_GUARD_ENABLED_CATEGORIES,
provider: LLAMA_GUARD_REPLICATE_PROVIDER,
});
});
it('should use subcategory metric name for OpenAI moderation assertions', () => {
vi.mocked(getEnvString).mockImplementation(function (key) {
if (key === 'OPENAI_API_KEY') {
return 'openai-key';
}
return '';
});
const assertions = getHarmfulAssertions('harmful:hate');
expect(assertions).toHaveLength(2);
expect(assertions[0].metric).toBe('Hate');
expect(assertions[1].metric).toBe('Hate');
expect(assertions[1].type).toBe('moderation');
});
it('should fallback to harm category when not in categoryAliases', () => {
const unknownCategory = 'harmful:unknown-category' as keyof typeof HARM_PLUGINS;
const assertions = getHarmfulAssertions(unknownCategory);
expect(assertions[0].metric).toBe(unknownCategory);
});
});
describe('createTestCase', () => {
it('should create test case with single line output', () => {
const testCase = createTestCase('inputVar', 'single line output', 'harmful:privacy');
expect(testCase).toEqual({
vars: {
inputVar: 'single line output',
},
metadata: {
harmCategory: HARM_PLUGINS['harmful:privacy'],
pluginId: 'harmful:privacy',
},
assert: expect.any(Array),
});
});
it('should handle multiline output', () => {
const testCase = createTestCase(
'inputVar',
'first line\nsecond line\nthird line',
'harmful:privacy',
);
expect(testCase.vars?.inputVar).toBe('first line\nsecond line\nthird line');
});
it('should handle whitespace in output', () => {
const testCase = createTestCase('inputVar', ' padded output ', 'harmful:privacy');
expect(testCase.vars?.inputVar).toBe('padded output');
});
it('should use harm category as fallback when not in HARM_PLUGINS', () => {
const unknownCategory = 'harmful:unknown' as keyof typeof HARM_PLUGINS;
const testCase = createTestCase('inputVar', 'test output', unknownCategory);
expect(testCase.metadata?.harmCategory).toBe(unknownCategory);
});
});
describe('plugin category validation', () => {
it('should have graders for all harmful categories', () => {
const harmCategories = Object.keys(HARM_PLUGINS);
const harmGraders = Object.keys(GRADERS).filter((key) =>
key.startsWith('promptfoo:redteam:harmful:'),
);
expect(harmGraders.length).toBeGreaterThanOrEqual(harmCategories.length);
});
});
});
@@ -0,0 +1,325 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
CopyrightViolationGrader,
GraphicContentGrader,
HarmfulGrader,
HarmfulPrivacyGrader,
ProfanityGrader,
SelfHarmGrader,
SexualContentGrader,
} from '../../../../src/redteam/plugins/harmful/graders';
import { isBasicRefusal } from '../../../../src/redteam/util';
import { createMockProvider } from '../../../factories/provider';
import type { ApiProvider, AtomicTestCase } from '../../../../src/types/index';
vi.mock('../../../../src/redteam/util');
const createMockTest = (overrides?: Partial<AtomicTestCase>): AtomicTestCase => ({
vars: {
purpose: 'To assist users with general tasks while avoiding harmful content',
harmCategory: 'test harm',
prompt: 'test prompt',
entities: ['entity1', 'entity2'],
...overrides?.vars,
},
...overrides,
});
describe('HarmfulGrader', () => {
let grader: HarmfulGrader;
let mockProvider: Partial<ApiProvider>;
let mockTest: AtomicTestCase;
beforeEach(() => {
grader = new HarmfulGrader();
mockProvider = createMockProvider();
mockTest = createMockTest();
vi.mocked(isBasicRefusal).mockReset();
});
describe('getResult', () => {
it('should pass and return score 1 for basic refusal', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return true;
});
const result = await grader.getResult(
'test prompt',
'I cannot help with that.',
mockTest,
mockProvider as ApiProvider,
);
expect(result.grade).toEqual({
pass: true,
score: 1,
reason: 'The output contains a standard refusal.',
assertion: undefined,
});
expect(result.rubric).toBe(grader.rubric);
});
it('should fallback to superclass getResult for non-refusal', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return false;
});
const superGetResult = vi.spyOn(Object.getPrototypeOf(HarmfulGrader.prototype), 'getResult');
superGetResult.mockResolvedValue({
grade: {
pass: false,
score: 0,
reason: 'test reason',
},
rubric: 'test rubric',
});
const result = await grader.getResult(
'test prompt',
'harmful response',
mockTest,
mockProvider as ApiProvider,
);
expect(superGetResult).toHaveBeenCalledWith(
'test prompt',
'harmful response',
mockTest,
mockProvider as ApiProvider,
undefined,
undefined, // additionalRubric
undefined, // skipRefusalCheck
undefined, // gradingContext
);
expect(result).toEqual({
grade: {
pass: false,
score: 0,
reason: 'test reason',
},
rubric: 'test rubric',
});
superGetResult.mockRestore();
});
});
describe('renderRubric', () => {
it('should render rubric with variables', () => {
const vars = {
purpose: 'test purpose',
harmCategory: 'test harm',
prompt: 'test prompt',
entities: ['entity1', 'entity2'],
};
const renderedRubric = grader.renderRubric(vars);
expect(renderedRubric).toContain('test purpose');
expect(renderedRubric).toContain('test harm');
expect(renderedRubric).toContain('test prompt');
expect(renderedRubric).toContain('entity1');
expect(renderedRubric).toContain('entity2');
});
it('should handle missing entities in rubric', () => {
const vars = {
purpose: 'test purpose',
harmCategory: 'test harm',
prompt: 'test prompt',
};
const renderedRubric = grader.renderRubric(vars);
expect(renderedRubric).toContain('test purpose');
expect(renderedRubric).toContain('test harm');
expect(renderedRubric).not.toContain('AllowedEntities');
});
});
});
describe('HarmfulPrivacyGrader', () => {
let grader: HarmfulPrivacyGrader;
let mockTest: AtomicTestCase;
beforeEach(() => {
grader = new HarmfulPrivacyGrader();
mockTest = createMockTest();
});
it('should have privacy-specific rubric', () => {
expect(grader.rubric).toContain('privacy violation');
});
it('should inherit basic refusal handling from parent', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return true;
});
const result = await grader.getResult(
'test prompt',
'I cannot help with that.',
mockTest,
createMockProvider() as ApiProvider,
);
expect(result.grade.pass).toBe(true);
expect(result.grade.score).toBe(1);
});
});
describe('SelfHarmGrader', () => {
let grader: SelfHarmGrader;
let mockTest: AtomicTestCase;
beforeEach(() => {
grader = new SelfHarmGrader();
mockTest = createMockTest();
});
it('should have self-harm specific rubric', () => {
expect(grader.rubric).toContain('self-harm');
});
it('should inherit basic refusal handling from parent', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return true;
});
const result = await grader.getResult(
'test prompt',
'I cannot help with that.',
mockTest,
createMockProvider() as ApiProvider,
);
expect(result.grade.pass).toBe(true);
expect(result.grade.score).toBe(1);
});
});
describe('GraphicContentGrader', () => {
let grader: GraphicContentGrader;
let mockTest: AtomicTestCase;
beforeEach(() => {
grader = new GraphicContentGrader();
mockTest = createMockTest();
});
it('should have graphic content specific rubric', () => {
expect(grader.rubric).toContain('gratuitously graphic');
expect(grader.rubric).toContain('disturbing content');
});
it('should inherit basic refusal handling from parent', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return true;
});
const result = await grader.getResult(
'test prompt',
'I cannot help with that.',
mockTest,
createMockProvider() as ApiProvider,
);
expect(result.grade.pass).toBe(true);
expect(result.grade.score).toBe(1);
});
});
describe('SexualContentGrader', () => {
let grader: SexualContentGrader;
let mockTest: AtomicTestCase;
beforeEach(() => {
grader = new SexualContentGrader();
mockTest = createMockTest();
});
it('should have sexual content specific rubric', () => {
expect(grader.rubric).toContain('inappropriate sexual content');
expect(grader.rubric).toContain('Sexually suggestive');
});
it('should inherit basic refusal handling from parent', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return true;
});
const result = await grader.getResult(
'test prompt',
'I cannot help with that.',
mockTest,
createMockProvider() as ApiProvider,
);
expect(result.grade.pass).toBe(true);
expect(result.grade.score).toBe(1);
});
});
describe('ProfanityGrader', () => {
let grader: ProfanityGrader;
let mockTest: AtomicTestCase;
beforeEach(() => {
grader = new ProfanityGrader();
mockTest = createMockTest();
});
it('should have profanity specific rubric', () => {
expect(grader.rubric).toContain('profanity');
expect(grader.rubric).toContain('inappropriate language');
});
it('should inherit basic refusal handling from parent', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return true;
});
const result = await grader.getResult(
'test prompt',
'I cannot help with that.',
mockTest,
createMockProvider() as ApiProvider,
);
expect(result.grade.pass).toBe(true);
expect(result.grade.score).toBe(1);
});
});
describe('CopyrightViolationGrader', () => {
let grader: CopyrightViolationGrader;
let mockTest: AtomicTestCase;
beforeEach(() => {
grader = new CopyrightViolationGrader();
mockTest = createMockTest();
});
it('should have copyright specific rubric', () => {
expect(grader.rubric).toContain('copyright violations');
expect(grader.rubric).toContain('copyrighted content');
});
it('should inherit basic refusal handling from parent', async () => {
vi.mocked(isBasicRefusal).mockImplementation(function () {
return true;
});
const result = await grader.getResult(
'test prompt',
'I cannot help with that.',
mockTest,
createMockProvider() as ApiProvider,
);
expect(result.grade.pass).toBe(true);
expect(result.grade.score).toBe(1);
});
});
@@ -0,0 +1,206 @@
import { afterEach, beforeEach, describe, expect, it, MockInstance, vi } from 'vitest';
import { getEnvBool, getEnvString } from '../../../../src/envars';
import { PromptfooHarmfulCompletionProvider } from '../../../../src/providers/promptfoo';
import {
LLAMA_GUARD_REPLICATE_PROVIDER,
UNALIGNED_PROVIDER_HARM_PLUGINS,
} from '../../../../src/redteam/constants';
import { REDTEAM_MODEL_CATEGORIES } from '../../../../src/redteam/plugins/harmful/constants';
import { getHarmfulTests } from '../../../../src/redteam/plugins/harmful/unaligned';
import { createMockProvider, type MockApiProvider } from '../../../factories/provider';
import type { HarmfulCategory } from '../../../../src/redteam/plugins/harmful/constants';
vi.mock('../../../../src/envars');
describe('harmful plugin', () => {
let mockProvider: MockApiProvider;
let mockCallApi: MockInstance;
beforeEach(() => {
vi.clearAllMocks();
mockProvider = createMockProvider();
if (mockCallApi) {
mockCallApi.mockRestore();
}
mockCallApi = vi.spyOn(PromptfooHarmfulCompletionProvider.prototype, 'callApi').mockReset();
vi.mocked(getEnvBool).mockReset();
vi.mocked(getEnvString).mockReset();
});
afterEach(() => {
vi.clearAllMocks();
if (mockCallApi) {
mockCallApi.mockRestore();
}
});
describe('getHarmfulTests', () => {
it('should handle unaligned provider plugins with multiple prompts', async () => {
const unalignedPlugin = Object.keys(UNALIGNED_PROVIDER_HARM_PLUGINS)[0];
mockCallApi.mockResolvedValueOnce({
output: ['Test harmful output', 'Another test output'],
});
const result = await getHarmfulTests(
{
provider: mockProvider,
purpose: 'test purpose',
injectVar: 'testVar',
n: 2,
delayMs: 0,
},
unalignedPlugin as keyof typeof UNALIGNED_PROVIDER_HARM_PLUGINS,
);
expect(mockCallApi).toHaveBeenCalledTimes(1);
expect(result).toHaveLength(2);
const prompts = result.map((r) => r.vars?.testVar);
expect(prompts).toContain('Test harmful output');
expect(prompts).toContain('Another test output');
});
it('should retry when not enough unique prompts are returned', async () => {
const unalignedPlugin = Object.keys(UNALIGNED_PROVIDER_HARM_PLUGINS)[0];
mockCallApi
.mockResolvedValueOnce({ output: ['Test output'] })
.mockResolvedValueOnce({ output: ['Test output'] })
.mockResolvedValueOnce({ output: ['Another test output'] });
const result = await getHarmfulTests(
{
provider: mockProvider,
purpose: 'test purpose',
injectVar: 'testVar',
n: 2,
delayMs: 0,
},
unalignedPlugin as keyof typeof UNALIGNED_PROVIDER_HARM_PLUGINS,
);
expect(mockCallApi).toHaveBeenCalledTimes(3);
expect(result).toHaveLength(2);
const prompts = result.map((r) => r.vars?.testVar);
expect(prompts).toContain('Test output');
expect(prompts).toContain('Another test output');
});
it('should handle empty provider response', async () => {
mockCallApi.mockResolvedValue({ output: [] });
const result = await getHarmfulTests(
{
provider: mockProvider,
purpose: 'test purpose',
injectVar: 'testVar',
n: 1,
delayMs: 0,
},
'harmful:sex-crime',
);
expect(result).toHaveLength(0);
});
it('should respect delay parameter between API calls', async () => {
const unalignedPlugin = Object.keys(UNALIGNED_PROVIDER_HARM_PLUGINS)[0];
mockCallApi
.mockResolvedValueOnce({ output: ['Test output'] })
.mockResolvedValueOnce({ output: ['Another output'] });
vi.useFakeTimers();
try {
const startTime = Date.now();
const testsPromise = getHarmfulTests(
{
provider: mockProvider,
purpose: 'test purpose',
injectVar: 'testVar',
n: 2,
delayMs: 100,
},
unalignedPlugin as keyof typeof UNALIGNED_PROVIDER_HARM_PLUGINS,
);
await vi.runAllTimersAsync();
await testsPromise;
expect(Date.now() - startTime).toBeGreaterThanOrEqual(100);
} finally {
vi.useRealTimers();
}
});
it('should handle moderation assertions with OPENAI_API_KEY', async () => {
vi.mocked(getEnvString).mockImplementation(function (key) {
if (key === 'OPENAI_API_KEY') {
return 'test-key';
}
return '';
});
mockCallApi.mockResolvedValueOnce({ output: ['Test output'] });
const result = await getHarmfulTests(
{
provider: mockProvider,
purpose: 'test purpose',
injectVar: 'testVar',
n: 1,
delayMs: 0,
},
'harmful:sex-crime',
);
expect(result).toHaveLength(1);
expect(result[0].vars?.testVar).toBe('Test output');
expect(result[0].assert).toContainEqual(expect.objectContaining({ type: 'moderation' }));
});
it('should handle moderation assertions with REPLICATE_API_KEY', async () => {
const mockCategory: HarmfulCategory = {
key: 'harmful:privacy',
prompt: 'test prompt',
examples: 'example1',
label: 'Privacy violations',
description: 'test description',
};
vi.spyOn(REDTEAM_MODEL_CATEGORIES, 'find').mockReturnValue(mockCategory);
vi.mocked(getEnvString).mockImplementation(function (key) {
if (key === 'REPLICATE_API_KEY') {
return 'test-key';
}
return '';
});
const mockOutput = 'Test output';
vi.spyOn(PromptfooHarmfulCompletionProvider.prototype, 'callApi').mockResolvedValue({
output: [mockOutput],
});
const result = await getHarmfulTests(
{
provider: mockProvider,
purpose: 'test purpose',
injectVar: 'testVar',
n: 1,
delayMs: 0,
},
'harmful:sex-crime',
);
expect(result).toHaveLength(1);
expect(result[0].vars?.testVar).toBe('Test output');
expect(result[0].assert).toContainEqual(
expect.objectContaining({
type: 'moderation',
provider: LLAMA_GUARD_REPLICATE_PROVIDER,
}),
);
});
});
});