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
170 lines
5.4 KiB
TypeScript
170 lines
5.4 KiB
TypeScript
import { SingleBar } from 'cli-progress';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { fetchWithCache } from '../../../src/cache';
|
|
import { redteamProviderManager } from '../../../src/redteam/providers/shared';
|
|
import * as remoteGeneration from '../../../src/redteam/remoteGeneration';
|
|
import {
|
|
addMathPrompt,
|
|
DEFAULT_MATH_CONCEPTS,
|
|
EXAMPLES,
|
|
encodeMathPrompt,
|
|
generateMathPrompt,
|
|
} from '../../../src/redteam/strategies/mathPrompt';
|
|
import { createMockProvider, createProviderResponse } from '../../factories/provider';
|
|
|
|
vi.mock('cli-progress');
|
|
vi.mock('../../../src/redteam/providers/shared');
|
|
vi.mock('../../../src/cache');
|
|
vi.mock('../../../src/redteam/remoteGeneration');
|
|
|
|
describe('mathPrompt', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('generateMathPrompt', () => {
|
|
it('should generate math prompts remotely', async () => {
|
|
const mockProgressBar = {
|
|
start: vi.fn(),
|
|
increment: vi.fn(),
|
|
stop: vi.fn(),
|
|
render: vi.fn(),
|
|
update: vi.fn(),
|
|
isActive: vi.fn(),
|
|
getProgress: vi.fn(),
|
|
} as unknown as SingleBar;
|
|
|
|
(SingleBar as any).mockImplementation(function () {
|
|
return mockProgressBar;
|
|
});
|
|
|
|
const mockTestCases = [{ vars: { prompt: 'test1' } }, { vars: { prompt: 'test2' } }];
|
|
const mockResult = [{ vars: { prompt: 'encoded1' } }, { vars: { prompt: 'encoded2' } }];
|
|
|
|
const mockResponse = {
|
|
data: {
|
|
result: mockResult,
|
|
},
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
headers: {},
|
|
deleteFromCache: async () => {},
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValue(mockResponse as any);
|
|
const result = await generateMathPrompt(mockTestCases as any, 'prompt', {
|
|
targetId: 'cloud-target-123',
|
|
});
|
|
|
|
expect(result).toEqual(mockResult);
|
|
expect(mockProgressBar.start).toHaveBeenCalledWith(1, 0);
|
|
expect(mockProgressBar.increment).toHaveBeenCalledWith(1);
|
|
expect(mockProgressBar.stop).toHaveBeenCalledWith();
|
|
const requestBody = vi.mocked(fetchWithCache).mock.calls[0]?.[1]?.body;
|
|
expect(requestBody).toBeTypeOf('string');
|
|
expect(JSON.parse(requestBody as string)).toMatchObject({
|
|
targetId: 'cloud-target-123',
|
|
});
|
|
});
|
|
|
|
it('should handle errors gracefully', async () => {
|
|
vi.mocked(fetchWithCache).mockRejectedValue(new Error('Network error'));
|
|
(SingleBar as any).mockImplementation(function () {
|
|
return {
|
|
start: vi.fn(),
|
|
increment: vi.fn(),
|
|
stop: vi.fn(),
|
|
} as unknown as SingleBar;
|
|
});
|
|
|
|
const result = await generateMathPrompt([{ vars: { prompt: 'test' } }] as any, 'prompt', {});
|
|
expect(result).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('encodeMathPrompt', () => {
|
|
it('should encode text using math concepts', async () => {
|
|
const mockProvider = createMockProvider({
|
|
id: 'mock',
|
|
response: createProviderResponse({
|
|
output: JSON.stringify({ encodedPrompt: 'encoded math text' }),
|
|
}),
|
|
});
|
|
|
|
vi.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider);
|
|
|
|
const result = await encodeMathPrompt('test text', 'set theory');
|
|
|
|
expect(result).toContain('encoded math text');
|
|
expect(result).toContain('Also provide a complete translation');
|
|
});
|
|
|
|
it('should handle JSON parsing errors', async () => {
|
|
const mockProvider = createMockProvider({
|
|
id: 'mock',
|
|
response: createProviderResponse({ output: 'invalid json' }),
|
|
});
|
|
|
|
vi.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider);
|
|
|
|
await expect(encodeMathPrompt('test text', 'set theory')).rejects.toThrow(
|
|
'Expected a JSON object',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('addMathPrompt', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should use custom math concepts when provided', async () => {
|
|
vi.mocked(remoteGeneration.shouldGenerateRemote).mockImplementation(function () {
|
|
return false;
|
|
});
|
|
const customConcepts = ['topology', 'calculus'];
|
|
|
|
const mockProvider = createMockProvider({
|
|
id: 'mock',
|
|
response: createProviderResponse({
|
|
output: JSON.stringify({ encodedPrompt: 'encoded' }),
|
|
}),
|
|
});
|
|
|
|
vi.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider);
|
|
(SingleBar as any).mockImplementation(function () {
|
|
return {
|
|
start: vi.fn(),
|
|
increment: vi.fn(),
|
|
stop: vi.fn(),
|
|
} as unknown as SingleBar;
|
|
});
|
|
|
|
const result = await addMathPrompt([{ vars: { prompt: 'test' } }] as any, 'prompt', {
|
|
mathConcepts: customConcepts,
|
|
});
|
|
|
|
expect(result).toHaveLength(customConcepts.length);
|
|
});
|
|
|
|
it('should validate mathConcepts config', async () => {
|
|
await expect(addMathPrompt([], 'prompt', { mathConcepts: 'invalid' })).rejects.toThrow(
|
|
'MathPrompt strategy: `mathConcepts` must be an array of strings',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('constants', () => {
|
|
it('should expose DEFAULT_MATH_CONCEPTS', () => {
|
|
expect(DEFAULT_MATH_CONCEPTS).toEqual(['set theory', 'group theory', 'abstract algebra']);
|
|
});
|
|
|
|
it('should expose EXAMPLES', () => {
|
|
expect(EXAMPLES).toHaveLength(3);
|
|
expect(EXAMPLES[0]).toContain('Let A represent a set');
|
|
});
|
|
});
|
|
});
|