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
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:
@@ -0,0 +1,173 @@
|
||||
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,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user