Files
promptfoo--promptfoo/test/integration/function-provider-grading.test.ts
T
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

100 lines
3.4 KiB
TypeScript

/**
* Integration test to verify function providers work end-to-end in grading scenarios
* This tests the exact issue from #3784: function providers in defaultTest.options.provider
*/
import { describe, expect, it, vi } from 'vitest';
import { getGradingProvider } from '../../src/matchers/providers';
import { createMockProvider } from '../factories/provider';
import type { ApiProvider, ProviderType } from '../../src/types/providers';
describe('Function Provider Integration - Issue #3784', () => {
it('should work with getGradingProvider when passed as ApiProvider object', async () => {
// Create a mock function provider (simulating what resolveProvider returns)
const mockFunctionProvider: any = vi.fn(async (prompt: string) => {
return { output: `Graded: ${prompt}` };
});
mockFunctionProvider.label = 'test-grader';
// This is what resolveProvider returns for function providers.
// Use a plain object here because the test asserts callApi identity.
const resolvedProvider: ApiProvider = {
id: () => mockFunctionProvider.label,
callApi: mockFunctionProvider,
};
// Now pass it through getGradingProvider (this is what matcher provider helpers do)
const gradingProvider = await getGradingProvider(
'text' as ProviderType,
resolvedProvider,
null,
);
expect(gradingProvider).toBeDefined();
expect(gradingProvider).toBe(resolvedProvider); // Should return same object
expect(typeof gradingProvider!.id).toBe('function');
expect(gradingProvider!.id()).toBe('test-grader');
expect(gradingProvider!.callApi).toBe(mockFunctionProvider);
});
it('should actually call the function provider', async () => {
const mockFunctionProvider: any = vi.fn(async (prompt: string) => {
return { output: `Response for: ${prompt}` };
});
const resolvedProvider = createMockProvider({
id: 'custom-grader',
callApi: mockFunctionProvider,
});
const gradingProvider = await getGradingProvider(
'text' as ProviderType,
resolvedProvider,
null,
);
// Actually call the provider
const result = await gradingProvider!.callApi('test prompt');
expect(mockFunctionProvider).toHaveBeenCalledWith('test prompt');
expect(result.output).toBe('Response for: test prompt');
});
it('should handle function provider without label', async () => {
const mockFunctionProvider: any = vi.fn(async (prompt: string) => {
return { output: `Graded: ${prompt}` };
});
// No label, so resolveProvider uses 'custom-function'
const resolvedProvider = createMockProvider({
id: 'custom-function',
callApi: mockFunctionProvider,
});
const gradingProvider = await getGradingProvider(
'text' as ProviderType,
resolvedProvider,
null,
);
expect(gradingProvider).toBeDefined();
expect(gradingProvider!.id()).toBe('custom-function');
});
it('should correctly identify as ApiProvider based on type check', async () => {
const mockFunctionProvider: any = vi.fn(async () => ({ output: 'test' }));
const resolvedProvider = createMockProvider({
id: 'test',
callApi: mockFunctionProvider,
});
// This is the exact check from getGradingProvider line 120
const isApiProviderCheck =
typeof resolvedProvider === 'object' &&
typeof (resolvedProvider as ApiProvider).id === 'function';
expect(isApiProviderCheck).toBe(true);
});
});