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

171 lines
4.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fetchHuggingFaceDataset } from '../../../src/integrations/huggingfaceDatasets';
import {
AegisGrader,
AegisPlugin,
DATASET_PATH,
fetchDataset,
} from '../../../src/redteam/plugins/aegis';
import { RedteamGraderBase } from '../../../src/redteam/plugins/base';
import { createMockProvider } from '../../factories/provider';
import type { TestCase } from '../../../src/types/index';
vi.mock('../../../src/integrations/huggingfaceDatasets');
describe('AegisPlugin', () => {
let plugin: AegisPlugin;
const mockProvider = createMockProvider();
beforeEach(() => {
plugin = new AegisPlugin(mockProvider, 'test-var', 'test-var');
});
it('should set canGenerateRemote to false', () => {
expect(AegisPlugin.canGenerateRemote).toBe(false);
});
it('should return inject var from getTemplate', async () => {
const template = await plugin.getTemplate();
expect(template).toBe('test-var');
});
it('should return correct assertions', () => {
const assertions = (plugin as any).getAssertions('test prompt');
expect(assertions).toEqual([
{
type: 'promptfoo:redteam:aegis',
metric: 'Aegis',
},
]);
});
it('should generate tests correctly', async () => {
const mockTestCases: TestCase[] = [
{
vars: {
text: 'test text',
text_type: 'user_message',
labels_0: 'Safe',
},
},
];
vi.mocked(fetchHuggingFaceDataset).mockResolvedValue(mockTestCases);
const tests = await plugin.generateTests(1);
expect(tests).toEqual([
{
vars: {
'test-var': 'test text',
},
metadata: {
label: 'Safe',
},
assert: [
{
type: 'promptfoo:redteam:aegis',
metric: 'Aegis',
},
],
},
]);
});
it('should handle empty dataset', async () => {
vi.mocked(fetchHuggingFaceDataset).mockResolvedValue([]);
const tests = await plugin.generateTests(1);
expect(tests).toEqual([]);
});
});
describe('fetchDataset', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('should fetch and filter dataset', async () => {
const mockTestCases: TestCase[] = [
{
vars: { text: 'hello', text_type: 'user_message', labels_0: 'Safe' },
},
{ vars: { text: 'ignore me', text_type: 'llm_response' } },
];
vi.mocked(fetchHuggingFaceDataset).mockResolvedValue(mockTestCases);
const result = await fetchDataset(1);
expect(fetchHuggingFaceDataset).toHaveBeenCalledWith(DATASET_PATH, 5);
expect(result).toHaveLength(1);
expect(result[0].vars?.text).toBe('hello');
expect(result[0].vars?.labels_0).toBe('Safe');
});
it('should handle errors', async () => {
vi.mocked(fetchHuggingFaceDataset).mockRejectedValue(new Error('fail'));
const result = await fetchDataset(2);
expect(result).toEqual([]);
});
it('should handle invalid records', async () => {
const mockTestCases: TestCase[] = [
{ vars: { text: 'valid', text_type: 'wrong_type' } },
{ vars: { text: 'valid', text_type: 'user_message' } },
];
vi.mocked(fetchHuggingFaceDataset).mockResolvedValue(mockTestCases);
const result = await fetchDataset(1);
expect(result).toEqual([
{
vars: {
text: 'valid',
labels_0: '',
},
},
]);
});
});
describe('AegisGrader', () => {
let grader: AegisGrader;
beforeEach(() => {
grader = new AegisGrader();
});
it('should have correct ID', () => {
expect(grader.id).toBe('promptfoo:redteam:aegis');
});
it('should handle basic refusal', async () => {
const output = "I'm sorry, I cannot help with that request";
const result = await grader.getResult('test prompt', output, {}, undefined);
expect(result).toEqual({
grade: {
pass: true,
score: 1,
reason: 'The output contains a standard refusal.',
},
rubric: grader.rubric,
});
});
it('should use super class grading for non-refusal', async () => {
const output = 'Here is a normal response';
const mockSuperResult = {
grade: { pass: true, score: 0.8, reason: 'test' },
rubric: 'test rubric',
};
vi.spyOn(RedteamGraderBase.prototype, 'getResult').mockResolvedValue(mockSuperResult);
const result = await grader.getResult('test prompt', output, {}, undefined);
expect(result).toEqual(mockSuperResult);
});
it('should have correct rubric content', () => {
expect(grader.rubric).toContain('You are a content safety evaluator');
expect(grader.rubric).toContain('content safety guidelines');
expect(grader.rubric).toContain('{ "pass": true }');
expect(grader.rubric).toContain('{ "pass": false }');
});
});