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
323 lines
10 KiB
TypeScript
323 lines
10 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { handleContextRecall } from '../../src/assertions/contextRecall';
|
|
import * as contextUtils from '../../src/assertions/contextUtils';
|
|
import * as matchers from '../../src/matchers/rag';
|
|
import { createMockProvider } from '../factories/provider';
|
|
|
|
import type { AssertionParams, ProviderResponse } from '../../src/types/index';
|
|
|
|
vi.mock('../../src/matchers/rag');
|
|
vi.mock('../../src/assertions/contextUtils');
|
|
|
|
describe('handleContextRecall', () => {
|
|
const mockMatchesContextRecall = vi.spyOn(matchers, 'matchesContextRecall');
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should pass when context recall is above threshold', async () => {
|
|
const mockResult = {
|
|
pass: true,
|
|
score: 0.9,
|
|
reason: 'Context contains expected information',
|
|
metadata: {
|
|
sentenceAttributions: [
|
|
{ sentence: 'Test sentence 1', attributed: true },
|
|
{ sentence: 'Test sentence 2', attributed: false },
|
|
],
|
|
totalSentences: 2,
|
|
attributedSentences: 1,
|
|
score: 0.9,
|
|
},
|
|
};
|
|
mockMatchesContextRecall.mockResolvedValue(mockResult);
|
|
vi.mocked(contextUtils.resolveContext).mockResolvedValue('test context');
|
|
|
|
const mockProvider = createMockProvider({ response: {} });
|
|
|
|
const params: AssertionParams = {
|
|
assertion: { type: 'context-recall', threshold: 0.8 },
|
|
renderedValue: 'Expected fact',
|
|
prompt: 'test prompt',
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
baseType: 'context-recall',
|
|
assertionValueContext: {
|
|
prompt: 'test prompt',
|
|
vars: { context: 'test context' },
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
logProbs: undefined,
|
|
provider: mockProvider,
|
|
providerResponse: undefined,
|
|
},
|
|
inverse: false,
|
|
output: 'test output',
|
|
outputString: 'test output',
|
|
provider: mockProvider,
|
|
providerResponse: {} as ProviderResponse,
|
|
};
|
|
|
|
const result = await handleContextRecall(params);
|
|
|
|
expect(result.pass).toBe(true);
|
|
expect(result.score).toBe(0.9);
|
|
expect(result.reason).toBe('Context contains expected information');
|
|
expect(result.metadata).toBeDefined();
|
|
expect(result.metadata?.context).toBe('test context');
|
|
// Verify metadata from matcher is preserved
|
|
expect(result.metadata?.sentenceAttributions).toEqual([
|
|
{ sentence: 'Test sentence 1', attributed: true },
|
|
{ sentence: 'Test sentence 2', attributed: false },
|
|
]);
|
|
expect(result.metadata?.totalSentences).toBe(2);
|
|
expect(result.metadata?.attributedSentences).toBe(1);
|
|
expect(result.metadata?.score).toBe(0.9);
|
|
expect(mockMatchesContextRecall).toHaveBeenCalledWith(
|
|
'test context',
|
|
'Expected fact',
|
|
0.8,
|
|
{},
|
|
{ context: 'test context' },
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
it('should fail when context recall is below threshold', async () => {
|
|
const mockResult = { pass: false, score: 0.3, reason: 'Context missing expected information' };
|
|
mockMatchesContextRecall.mockResolvedValue(mockResult);
|
|
vi.mocked(contextUtils.resolveContext).mockResolvedValue('test context');
|
|
|
|
const mockProvider = createMockProvider({ response: {} });
|
|
|
|
const params: AssertionParams = {
|
|
assertion: { type: 'context-recall', threshold: 0.7 },
|
|
renderedValue: 'Missing fact',
|
|
prompt: 'test prompt',
|
|
test: { vars: { context: 'incomplete context' }, options: {} },
|
|
baseType: 'context-recall',
|
|
assertionValueContext: {
|
|
prompt: 'test prompt',
|
|
vars: { context: 'incomplete context' },
|
|
test: { vars: { context: 'incomplete context' }, options: {} },
|
|
logProbs: undefined,
|
|
provider: mockProvider,
|
|
providerResponse: undefined,
|
|
},
|
|
inverse: false,
|
|
output: 'test output',
|
|
outputString: 'test output',
|
|
provider: mockProvider,
|
|
providerResponse: {} as ProviderResponse,
|
|
};
|
|
|
|
const result = await handleContextRecall(params);
|
|
|
|
expect(result.pass).toBe(false);
|
|
expect(result.score).toBe(0.3);
|
|
expect(result.reason).toBe('Context missing expected information');
|
|
expect(result.metadata).toBeDefined();
|
|
expect(result.metadata?.context).toBe('test context');
|
|
expect(mockMatchesContextRecall).toHaveBeenCalledWith(
|
|
'test context',
|
|
'Missing fact',
|
|
0.7,
|
|
{},
|
|
{ context: 'incomplete context' },
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
it('should use default threshold of 0 when not provided', async () => {
|
|
const mockResult = { pass: true, score: 1, reason: 'Perfect match' };
|
|
mockMatchesContextRecall.mockResolvedValue(mockResult);
|
|
vi.mocked(contextUtils.resolveContext).mockResolvedValue('test context');
|
|
|
|
const mockProvider = createMockProvider({ response: {} });
|
|
|
|
const params: AssertionParams = {
|
|
assertion: { type: 'context-recall' },
|
|
renderedValue: 'test value',
|
|
prompt: 'test prompt',
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
baseType: 'context-recall',
|
|
assertionValueContext: {
|
|
prompt: 'test prompt',
|
|
vars: { context: 'test context' },
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
logProbs: undefined,
|
|
provider: mockProvider,
|
|
providerResponse: undefined,
|
|
},
|
|
inverse: false,
|
|
output: 'test output',
|
|
outputString: 'test output',
|
|
provider: mockProvider,
|
|
providerResponse: {} as ProviderResponse,
|
|
};
|
|
|
|
const result = await handleContextRecall(params);
|
|
|
|
expect(result.metadata).toBeDefined();
|
|
expect(result.metadata?.context).toBe('test context');
|
|
expect(mockMatchesContextRecall).toHaveBeenCalledWith(
|
|
'test context',
|
|
'test value',
|
|
0,
|
|
{},
|
|
{ context: 'test context' },
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
it('should fall back to prompt when no context variable', async () => {
|
|
const mockResult = { pass: true, score: 1, reason: 'ok' };
|
|
mockMatchesContextRecall.mockResolvedValue(mockResult);
|
|
vi.mocked(contextUtils.resolveContext).mockResolvedValue('test prompt');
|
|
|
|
const mockProvider = createMockProvider({ response: {} });
|
|
|
|
const params: AssertionParams = {
|
|
assertion: { type: 'context-recall' },
|
|
renderedValue: 'test output',
|
|
prompt: 'test prompt',
|
|
test: { vars: {}, options: {} },
|
|
baseType: 'context-recall',
|
|
assertionValueContext: {
|
|
prompt: 'test prompt',
|
|
vars: {},
|
|
test: { vars: {}, options: {} },
|
|
logProbs: undefined,
|
|
provider: mockProvider,
|
|
providerResponse: undefined,
|
|
},
|
|
inverse: false,
|
|
output: 'test output',
|
|
outputString: 'test output',
|
|
provider: mockProvider,
|
|
providerResponse: {} as ProviderResponse,
|
|
};
|
|
|
|
const result = await handleContextRecall(params);
|
|
|
|
expect(result.metadata).toBeDefined();
|
|
expect(result.metadata?.context).toBe('test prompt');
|
|
expect(contextUtils.resolveContext).toHaveBeenCalledWith(
|
|
params.assertion,
|
|
params.test,
|
|
params.output,
|
|
'test prompt',
|
|
'test prompt',
|
|
{},
|
|
);
|
|
expect(mockMatchesContextRecall).toHaveBeenCalledWith(
|
|
'test prompt',
|
|
'test output',
|
|
0,
|
|
{},
|
|
{},
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
it('should use contextTransform when provided', async () => {
|
|
const mockResult = { pass: true, score: 1, reason: 'ok' };
|
|
mockMatchesContextRecall.mockResolvedValue(mockResult);
|
|
vi.mocked(contextUtils.resolveContext).mockResolvedValue('ctx');
|
|
|
|
const mockProvider = createMockProvider({ id: 'p', response: {} });
|
|
|
|
const params: AssertionParams = {
|
|
assertion: { type: 'context-recall', contextTransform: 'expr' },
|
|
renderedValue: 'val',
|
|
prompt: 'prompt',
|
|
test: { vars: {}, options: {} },
|
|
baseType: 'context-recall',
|
|
assertionValueContext: {
|
|
prompt: 'prompt',
|
|
vars: {},
|
|
test: { vars: {}, options: {} },
|
|
logProbs: undefined,
|
|
provider: mockProvider,
|
|
providerResponse: undefined,
|
|
},
|
|
inverse: false,
|
|
output: { context: 'hello' } as any,
|
|
outputString: 'str',
|
|
provider: mockProvider,
|
|
providerResponse: {} as ProviderResponse,
|
|
};
|
|
|
|
const result = await handleContextRecall(params);
|
|
|
|
expect(result.metadata).toBeDefined();
|
|
expect(result.metadata?.context).toBe('ctx');
|
|
expect(contextUtils.resolveContext).toHaveBeenCalledWith(
|
|
params.assertion,
|
|
params.test,
|
|
params.output,
|
|
'prompt',
|
|
'prompt',
|
|
{},
|
|
);
|
|
expect(mockMatchesContextRecall).toHaveBeenCalledWith('ctx', 'val', 0, {}, {}, undefined);
|
|
});
|
|
|
|
it('should throw error when renderedValue is not a string', async () => {
|
|
const mockProvider = createMockProvider({ response: {} });
|
|
|
|
const params: AssertionParams = {
|
|
assertion: { type: 'context-recall' },
|
|
renderedValue: 123 as any,
|
|
prompt: 'test prompt',
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
baseType: 'context-recall',
|
|
assertionValueContext: {
|
|
prompt: 'test prompt',
|
|
vars: { context: 'test context' },
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
logProbs: undefined,
|
|
provider: mockProvider,
|
|
providerResponse: undefined,
|
|
},
|
|
inverse: false,
|
|
output: 'test output',
|
|
outputString: 'test output',
|
|
provider: mockProvider,
|
|
providerResponse: {} as ProviderResponse,
|
|
};
|
|
|
|
await expect(handleContextRecall(params)).rejects.toThrow(
|
|
'context-recall assertion requires a string value (expected answer or fact to verify)',
|
|
);
|
|
});
|
|
|
|
it('should throw error when prompt is missing', async () => {
|
|
const mockProvider = createMockProvider({ response: {} });
|
|
|
|
const params: AssertionParams = {
|
|
assertion: { type: 'context-recall' },
|
|
renderedValue: 'test value',
|
|
prompt: undefined,
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
baseType: 'context-recall',
|
|
assertionValueContext: {
|
|
prompt: undefined,
|
|
vars: { context: 'test context' },
|
|
test: { vars: { context: 'test context' }, options: {} },
|
|
logProbs: undefined,
|
|
provider: mockProvider,
|
|
providerResponse: undefined,
|
|
},
|
|
inverse: false,
|
|
output: 'test output',
|
|
outputString: 'test output',
|
|
provider: mockProvider,
|
|
providerResponse: {} as ProviderResponse,
|
|
};
|
|
|
|
await expect(handleContextRecall(params)).rejects.toThrow(
|
|
'context-recall assertion requires a prompt',
|
|
);
|
|
});
|
|
});
|