Files
wehub-resource-sync 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
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
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
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

219 lines
5.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { handleSearchRubric } from '../../src/assertions/searchRubric';
import { matchesSearchRubric } from '../../src/matchers/search';
import { createMockProvider } from '../factories/provider';
import type { Assertion, AssertionParams, GradingResult } from '../../src/types/index';
vi.mock('../../src/matchers/search');
describe('handleSearchRubric', () => {
beforeEach(() => {
vi.resetAllMocks();
});
const mockMatchesSearchRubric = vi.mocked(matchesSearchRubric);
const defaultParams: AssertionParams = {
assertion: {
type: 'search-rubric',
value: 'test rubric',
} as Assertion,
baseType: 'search-rubric',
assertionValueContext: {
prompt: 'test prompt',
vars: {},
test: {
vars: {},
},
logProbs: undefined,
provider: undefined,
providerResponse: undefined,
},
inverse: false,
output: 'test output',
outputString: 'test output string',
test: {
vars: { city: 'Tokyo' },
},
providerResponse: {
output: 'The weather in Tokyo is sunny',
},
};
it('should throw error when renderedValue is undefined', async () => {
const params: AssertionParams = {
...defaultParams,
renderedValue: undefined,
};
await expect(handleSearchRubric(params)).rejects.toThrow(
'search-rubric assertion type must have a string value',
);
});
it('should call matchesSearchRubric with correct parameters', async () => {
const params: AssertionParams = {
...defaultParams,
renderedValue: 'Contains accurate weather for Tokyo',
};
const expectedResult: GradingResult = {
pass: true,
score: 1,
reason: 'The output correctly states the weather in Tokyo',
};
mockMatchesSearchRubric.mockResolvedValue(expectedResult);
const result = await handleSearchRubric(params);
expect(result).toEqual(expectedResult);
expect(mockMatchesSearchRubric).toHaveBeenCalledWith(
'Contains accurate weather for Tokyo',
'The weather in Tokyo is sunny',
params.test.options,
{ city: 'Tokyo' },
params.assertion,
undefined,
undefined, // providerCallContext
);
});
it('should handle passing result', async () => {
const params: AssertionParams = {
...defaultParams,
renderedValue: 'Correctly identifies Satya Nadella as CEO',
};
const expectedResult: GradingResult = {
pass: true,
score: 1,
reason: 'Web search confirmed Satya Nadella is the current CEO of Microsoft',
};
mockMatchesSearchRubric.mockResolvedValue(expectedResult);
const result = await handleSearchRubric(params);
expect(result.pass).toBe(true);
expect(result.score).toBe(1);
});
it('should handle failing result', async () => {
const params: AssertionParams = {
...defaultParams,
renderedValue: 'States correct Bitcoin price within 5%',
};
const expectedResult: GradingResult = {
pass: false,
score: 0,
reason: 'The stated price is off by more than 50%',
};
mockMatchesSearchRubric.mockResolvedValue(expectedResult);
const result = await handleSearchRubric(params);
expect(result.pass).toBe(false);
expect(result.score).toBe(0);
});
it('should handle inverse assertion (not:search-rubric)', async () => {
const params: AssertionParams = {
...defaultParams,
inverse: true,
renderedValue: 'Contains outdated information',
};
const originalResult: GradingResult = {
pass: true,
score: 1,
reason: 'Information is current',
};
mockMatchesSearchRubric.mockResolvedValue(originalResult);
const result = await handleSearchRubric(params);
// Inverse should flip the pass value
expect(result.pass).toBe(false);
expect(result.reason).toContain('requires web search verification');
});
it('should handle inverse assertion when original fails', async () => {
const params: AssertionParams = {
...defaultParams,
inverse: true,
renderedValue: 'Contains outdated information',
};
const originalResult: GradingResult = {
pass: false,
score: 0,
reason: 'Information is outdated',
};
mockMatchesSearchRubric.mockResolvedValue(originalResult);
const result = await handleSearchRubric(params);
// Inverse should flip the pass value
expect(result.pass).toBe(true);
expect(result.reason).toContain('does not require web search verification');
});
it('should pass provider to matchesSearchRubric', async () => {
const mockProvider = createMockProvider();
const params: AssertionParams = {
...defaultParams,
renderedValue: 'test rubric',
provider: mockProvider as any,
};
const expectedResult: GradingResult = {
pass: true,
score: 1,
reason: 'test',
};
mockMatchesSearchRubric.mockResolvedValue(expectedResult);
await handleSearchRubric(params);
// Verify provider is passed as the 6th argument
const calls = mockMatchesSearchRubric.mock.calls;
expect(calls[0][5]).toBe(mockProvider);
});
it('should handle test.options being passed correctly', async () => {
const params: AssertionParams = {
...defaultParams,
renderedValue: 'test rubric',
test: {
vars: { city: 'New York' },
options: {
provider: 'anthropic:messages:claude-opus-4-6',
},
},
};
const expectedResult: GradingResult = {
pass: true,
score: 1,
reason: 'test',
};
mockMatchesSearchRubric.mockResolvedValue(expectedResult);
await handleSearchRubric(params);
// Verify the options and vars are passed correctly
const calls = mockMatchesSearchRubric.mock.calls;
expect(calls[0][2]).toEqual({ provider: 'anthropic:messages:claude-opus-4-6' });
expect(calls[0][3]).toEqual({ city: 'New York' });
});
});