0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
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) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
CI / Shell Format Check (push) Waiting to run
CI / Check Ruby (3.4) (push) Waiting to run
CI / CI Config (push) Waiting to run
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Blocked by required conditions
CI / Build on Node ${{ matrix.node }} (push) Blocked by required conditions
CI / Style Check (push) Waiting to run
CI / Generate Assets (push) Waiting to run
CI / Check Python (3.14) (push) Waiting to run
CI / Check Python (3.9) (push) Waiting to run
CI / Build Docs (push) Waiting to run
CI / Code Scan Action (push) Waiting to run
CI / Site tests (push) Waiting to run
CI / webui tests (push) Waiting to run
CI / Run Integration Tests (push) Waiting to run
CI / Run Smoke Tests (push) Waiting to run
CI / Go Tests (push) Waiting to run
CI / Share Test (push) Waiting to run
CI / Redteam (Production API) (push) Waiting to run
CI / Redteam (Staging API) (push) Waiting to run
CI / GitHub Actions Lint (push) Waiting to run
CI / Check Ruby (3.0) (push) Waiting to run
release-please / release-please (push) Waiting to run
release-please / build (push) Blocked by required conditions
release-please / publish-npm (push) Blocked by required conditions
release-please / publish-npm-backfill (push) Waiting to run
release-please / docker (push) Blocked by required conditions
release-please / publish-code-scan-action (push) Blocked by required conditions
release-please / attest-code-scan-action (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
174 lines
5.1 KiB
TypeScript
174 lines
5.1 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { fetchWithCache } from '../src/cache';
|
|
import { getUserEmail } from '../src/globalConfig/accounts';
|
|
import { getRequestTimeoutMs } from '../src/providers/shared';
|
|
import {
|
|
getRemoteGenerationHeaders,
|
|
getRemoteGenerationUrl,
|
|
} from '../src/redteam/remoteGeneration';
|
|
import { doRemoteGrading } from '../src/remoteGrading';
|
|
|
|
const mockLoggerDebug = vi.hoisted(() => vi.fn());
|
|
|
|
function containsString(value: unknown, needle: string): boolean {
|
|
if (typeof value === 'string') {
|
|
return value.includes(needle);
|
|
}
|
|
if (Array.isArray(value)) {
|
|
return value.some((item) => containsString(item, needle));
|
|
}
|
|
if (value && typeof value === 'object') {
|
|
return Object.values(value).some((item) => containsString(item, needle));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
vi.mock('../src/cache', () => ({
|
|
fetchWithCache: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/globalConfig/accounts', () => ({
|
|
getUserEmail: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/providers/shared', () => ({
|
|
getRequestTimeoutMs: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/redteam/remoteGeneration', () => ({
|
|
getRemoteGenerationHeaders: vi.fn(),
|
|
getRemoteGenerationUrl: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/logger', () => ({
|
|
default: {
|
|
debug: mockLoggerDebug,
|
|
},
|
|
}));
|
|
|
|
describe('doRemoteGrading', () => {
|
|
afterEach(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
it('preserves grader error metadata from remote grading results', async () => {
|
|
vi.mocked(getUserEmail).mockReturnValue('user@example.com');
|
|
vi.mocked(getRemoteGenerationUrl).mockReturnValue('https://api.promptfoo.test/task');
|
|
vi.mocked(getRemoteGenerationHeaders).mockReturnValue({ authorization: 'Bearer test' });
|
|
vi.mocked(getRequestTimeoutMs).mockReturnValue(1234);
|
|
vi.mocked(fetchWithCache).mockResolvedValueOnce({
|
|
data: {
|
|
result: {
|
|
pass: false,
|
|
score: 0,
|
|
reason: 'API error: 429 Too Many Requests',
|
|
metadata: { graderError: true },
|
|
},
|
|
},
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
} as any);
|
|
|
|
const result = await doRemoteGrading({
|
|
task: 'llm-rubric',
|
|
rubric: 'Only pass if the response is correct.',
|
|
output: 'Example output',
|
|
vars: {},
|
|
});
|
|
|
|
expect(result).toMatchObject({
|
|
pass: false,
|
|
score: 0,
|
|
reason: 'API error: 429 Too Many Requests',
|
|
metadata: { graderError: true },
|
|
});
|
|
expect(fetchWithCache).toHaveBeenCalledWith(
|
|
'https://api.promptfoo.test/task',
|
|
expect.objectContaining({
|
|
method: 'POST',
|
|
headers: { authorization: 'Bearer test' },
|
|
}),
|
|
1234,
|
|
);
|
|
});
|
|
|
|
it('does not add grader error metadata when remote grading succeeds without metadata', async () => {
|
|
vi.mocked(getUserEmail).mockReturnValue('user@example.com');
|
|
vi.mocked(getRemoteGenerationUrl).mockReturnValue('https://api.promptfoo.test/task');
|
|
vi.mocked(getRemoteGenerationHeaders).mockReturnValue({ authorization: 'Bearer test' });
|
|
vi.mocked(getRequestTimeoutMs).mockReturnValue(1234);
|
|
vi.mocked(fetchWithCache).mockResolvedValueOnce({
|
|
data: {
|
|
result: {
|
|
pass: true,
|
|
score: 1,
|
|
reason: 'ok',
|
|
},
|
|
},
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
} as any);
|
|
|
|
const result = await doRemoteGrading({
|
|
task: 'llm-rubric',
|
|
rubric: 'Only pass if the response is correct.',
|
|
output: 'Example output',
|
|
vars: {},
|
|
});
|
|
|
|
expect(result).toMatchObject({
|
|
pass: true,
|
|
score: 1,
|
|
reason: 'ok',
|
|
});
|
|
expect(result.metadata?.graderError).toBeUndefined();
|
|
});
|
|
|
|
it('redacts inline image data from remote grading debug logs', async () => {
|
|
vi.mocked(getUserEmail).mockReturnValue('user@example.com');
|
|
vi.mocked(getRemoteGenerationUrl).mockReturnValue('https://api.promptfoo.test/task');
|
|
vi.mocked(getRemoteGenerationHeaders).mockReturnValue({ authorization: 'Bearer test' });
|
|
vi.mocked(getRequestTimeoutMs).mockReturnValue(1234);
|
|
vi.mocked(fetchWithCache).mockResolvedValueOnce({
|
|
data: {
|
|
result: {
|
|
pass: true,
|
|
score: 1,
|
|
reason: 'ok',
|
|
},
|
|
},
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
} as any);
|
|
|
|
await doRemoteGrading({
|
|
task: 'llm-rubric',
|
|
rubric: 'Only pass if the response is correct.',
|
|
output: 'Example output',
|
|
vars: {},
|
|
images: [{ data: 'data:image/png;base64,abc123', mimeType: 'image/png' }],
|
|
});
|
|
|
|
expect(mockLoggerDebug).toHaveBeenCalledWith('Performing remote grading', {
|
|
body: expect.objectContaining({
|
|
images: [{ data: '[REDACTED_IMAGE_DATA]', mimeType: 'image/png' }],
|
|
}),
|
|
});
|
|
const firstDebugPayload = mockLoggerDebug.mock.calls[0][1] as {
|
|
body: { images: Array<{ data: string; mimeType: string }> };
|
|
};
|
|
expect(firstDebugPayload.body.images[0].data).toBe('[REDACTED_IMAGE_DATA]');
|
|
expect(containsString(mockLoggerDebug.mock.calls, 'abc123')).toBe(false);
|
|
expect(fetchWithCache).toHaveBeenCalledWith(
|
|
'https://api.promptfoo.test/task',
|
|
expect.objectContaining({
|
|
body: expect.stringContaining('abc123'),
|
|
}),
|
|
1234,
|
|
);
|
|
});
|
|
});
|