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
192 lines
6.4 KiB
TypeScript
192 lines
6.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
createMockProvider,
|
|
createProviderResponse,
|
|
type MockApiProvider,
|
|
} from '../../factories/provider';
|
|
|
|
import type { ApiProvider, CallApiContextParams } from '../../../src/types/index';
|
|
|
|
const mockFetchWithProxy = vi.fn();
|
|
|
|
vi.mock('../../../src/util/fetch/index', () => ({
|
|
fetchWithProxy: (...args: unknown[]) => mockFetchWithProxy(...args),
|
|
}));
|
|
|
|
vi.mock('../../../src/evaluatorHelpers', () => ({
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
renderPrompt: vi
|
|
.fn()
|
|
.mockImplementation((_prompt: any, vars: any) => vars.input || 'rendered prompt'),
|
|
}));
|
|
|
|
vi.mock('../../../src/globalConfig/accounts', () => ({
|
|
getUserEmail: vi.fn().mockReturnValue('test@example.com'),
|
|
}));
|
|
|
|
vi.mock('../../../src/redteam/remoteGeneration', () => ({
|
|
getRemoteGenerationUrl: vi.fn().mockReturnValue('http://test.api/generate'),
|
|
getRemoteGenerationHeaders: vi.fn((extra) => ({ 'Content-Type': 'application/json', ...extra })),
|
|
neverGenerateRemote: vi.fn().mockReturnValue(false),
|
|
}));
|
|
|
|
describe('AuthoritativeMarkupInjectionProvider', () => {
|
|
let AuthoritativeMarkupInjectionProvider: typeof import('../../../src/redteam/providers/authoritativeMarkupInjection').default;
|
|
let mockTargetProvider: MockApiProvider;
|
|
|
|
const createMockContext = (targetProvider: ApiProvider): CallApiContextParams => ({
|
|
originalProvider: targetProvider,
|
|
vars: { input: 'test input' },
|
|
prompt: { raw: 'test prompt', label: 'test' },
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
|
|
// Dynamic import after mocks are set up
|
|
const module = await import('../../../src/redteam/providers/authoritativeMarkupInjection');
|
|
AuthoritativeMarkupInjectionProvider = module.default;
|
|
|
|
mockTargetProvider = createMockProvider({
|
|
response: createProviderResponse({ output: 'target response' }),
|
|
});
|
|
|
|
// Mock successful response from remote API
|
|
mockFetchWithProxy.mockResolvedValue({
|
|
json: async () => ({
|
|
message: { role: 'assistant', content: 'injected content' },
|
|
}),
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should pass abortSignal to fetchWithProxy', async () => {
|
|
const provider = new AuthoritativeMarkupInjectionProvider({
|
|
injectVar: 'input',
|
|
});
|
|
|
|
const abortController = new AbortController();
|
|
const context = createMockContext(mockTargetProvider);
|
|
|
|
await provider.callApi('test prompt', context, { abortSignal: abortController.signal });
|
|
|
|
expect(mockFetchWithProxy).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.any(Object),
|
|
abortController.signal,
|
|
);
|
|
});
|
|
|
|
it('should include target context in remote generation requests', async () => {
|
|
const provider = new AuthoritativeMarkupInjectionProvider({
|
|
injectVar: 'input',
|
|
targetId: 'cloud-target-123',
|
|
});
|
|
|
|
await provider.callApi('test prompt', createMockContext(mockTargetProvider));
|
|
|
|
const request = mockFetchWithProxy.mock.calls[0]?.[1] as { body?: string } | undefined;
|
|
expect(JSON.parse(request?.body ?? '{}')).toMatchObject({
|
|
targetId: 'cloud-target-123',
|
|
task: 'authoritative-markup-injection',
|
|
});
|
|
});
|
|
|
|
it('should pass options to target provider callApi', async () => {
|
|
const provider = new AuthoritativeMarkupInjectionProvider({
|
|
injectVar: 'input',
|
|
});
|
|
|
|
const abortController = new AbortController();
|
|
const context = createMockContext(mockTargetProvider);
|
|
const options = { abortSignal: abortController.signal };
|
|
|
|
await provider.callApi('test prompt', context, options);
|
|
|
|
// The target provider should be called with the options
|
|
expect(mockTargetProvider.callApi).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.any(Object),
|
|
options,
|
|
);
|
|
});
|
|
|
|
describe('Token Usage Tracking', () => {
|
|
it('should accumulate token usage from target provider', async () => {
|
|
mockTargetProvider.callApi.mockResolvedValue({
|
|
output: 'target response',
|
|
tokenUsage: { prompt: 50, completion: 25, total: 75, numRequests: 1 },
|
|
});
|
|
|
|
const provider = new AuthoritativeMarkupInjectionProvider({
|
|
injectVar: 'input',
|
|
});
|
|
|
|
const context = createMockContext(mockTargetProvider);
|
|
const result = await provider.callApi('test prompt', context);
|
|
|
|
expect(result.tokenUsage).toBeDefined();
|
|
expect(result.tokenUsage?.prompt).toBe(50);
|
|
expect(result.tokenUsage?.completion).toBe(25);
|
|
expect(result.tokenUsage?.total).toBe(75);
|
|
expect(result.tokenUsage?.numRequests).toBe(1);
|
|
});
|
|
|
|
it('should return token usage even when target provider returns error', async () => {
|
|
mockTargetProvider.callApi.mockResolvedValue({
|
|
output: '',
|
|
error: 'Target provider error',
|
|
tokenUsage: { prompt: 10, completion: 0, total: 10, numRequests: 1 },
|
|
});
|
|
|
|
const provider = new AuthoritativeMarkupInjectionProvider({
|
|
injectVar: 'input',
|
|
});
|
|
|
|
const context = createMockContext(mockTargetProvider);
|
|
const result = await provider.callApi('test prompt', context);
|
|
|
|
expect(result.error).toBe('Target provider error');
|
|
expect(result.tokenUsage).toBeDefined();
|
|
expect(result.tokenUsage?.numRequests).toBe(1);
|
|
});
|
|
|
|
it('should handle target provider with no token usage', async () => {
|
|
mockTargetProvider.callApi.mockResolvedValue({
|
|
output: 'response without token usage',
|
|
});
|
|
|
|
const provider = new AuthoritativeMarkupInjectionProvider({
|
|
injectVar: 'input',
|
|
});
|
|
|
|
const context = createMockContext(mockTargetProvider);
|
|
const result = await provider.callApi('test prompt', context);
|
|
|
|
// Should still have token usage object with numRequests counted
|
|
expect(result.tokenUsage).toBeDefined();
|
|
expect(result.tokenUsage?.numRequests).toBe(1);
|
|
});
|
|
|
|
it('should include metadata with redteamFinalPrompt', async () => {
|
|
mockTargetProvider.callApi.mockResolvedValue({
|
|
output: 'target response',
|
|
tokenUsage: { prompt: 50, completion: 25, total: 75, numRequests: 1 },
|
|
});
|
|
|
|
const provider = new AuthoritativeMarkupInjectionProvider({
|
|
injectVar: 'input',
|
|
});
|
|
|
|
const context = createMockContext(mockTargetProvider);
|
|
const result = await provider.callApi('test prompt', context);
|
|
|
|
expect(result.metadata).toBeDefined();
|
|
expect(result.metadata?.redteamFinalPrompt).toBeDefined();
|
|
});
|
|
});
|
|
});
|