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,167 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { callProviderWithContext } from '../../src/matchers/providers';
|
||||
import { withProviderCallExecutionContext } from '../../src/scheduler/providerCallExecutionContext';
|
||||
import { ProviderGroupedCallQueue } from '../../src/scheduler/providerCallQueue';
|
||||
import { wrapProviderWithRateLimiting } from '../../src/scheduler/providerWrapper';
|
||||
import { createMockProvider } from '../factories/provider';
|
||||
|
||||
import type { RateLimitRegistry } from '../../src/scheduler/rateLimitRegistry';
|
||||
import type {
|
||||
ApiProvider,
|
||||
ProviderResponse,
|
||||
RateLimitRegistryRef,
|
||||
VarValue,
|
||||
} from '../../src/types/index';
|
||||
|
||||
function createProvider(response: ProviderResponse = { output: 'ok' }): ApiProvider {
|
||||
return createMockProvider({ id: 'test-grader', response });
|
||||
}
|
||||
|
||||
function createRegistry(): RateLimitRegistryRef & {
|
||||
executeSpy: ReturnType<typeof vi.fn>;
|
||||
disposeSpy: ReturnType<typeof vi.fn>;
|
||||
} {
|
||||
const executeSpy = vi.fn();
|
||||
const disposeSpy = vi.fn();
|
||||
|
||||
return {
|
||||
async execute(provider, callFn, options) {
|
||||
executeSpy(provider, callFn, options);
|
||||
return callFn();
|
||||
},
|
||||
dispose() {
|
||||
disposeSpy();
|
||||
},
|
||||
executeSpy,
|
||||
disposeSpy,
|
||||
};
|
||||
}
|
||||
|
||||
describe('callProviderWithContext', () => {
|
||||
const vars: Record<string, VarValue> = { question: 'What is two plus two?' };
|
||||
|
||||
afterEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it('calls the provider directly without scheduler execution context', async () => {
|
||||
const response = { output: 'direct response' };
|
||||
const provider = createProvider(response);
|
||||
|
||||
await expect(callProviderWithContext(provider, 'grade this', 'rubric', vars)).resolves.toBe(
|
||||
response,
|
||||
);
|
||||
|
||||
expect(provider.callApi).toHaveBeenCalledWith('grade this', {
|
||||
prompt: { raw: 'grade this', label: 'rubric' },
|
||||
vars,
|
||||
});
|
||||
});
|
||||
|
||||
it('uses the scheduler execution context when available', async () => {
|
||||
const provider = createProvider();
|
||||
const registry = createRegistry();
|
||||
|
||||
await withProviderCallExecutionContext({ rateLimitRegistry: registry }, () =>
|
||||
callProviderWithContext(provider, 'grade this', 'rubric', vars),
|
||||
);
|
||||
|
||||
expect(registry.executeSpy).toHaveBeenCalledWith(
|
||||
provider,
|
||||
expect.any(Function),
|
||||
expect.objectContaining({
|
||||
getHeaders: expect.any(Function),
|
||||
isRateLimited: expect.any(Function),
|
||||
getRetryAfter: expect.any(Function),
|
||||
}),
|
||||
);
|
||||
expect(provider.callApi).toHaveBeenCalledWith('grade this', {
|
||||
prompt: { raw: 'grade this', label: 'rubric' },
|
||||
vars,
|
||||
});
|
||||
});
|
||||
|
||||
it('propagates abort signals from the scheduler execution context', async () => {
|
||||
const provider = createProvider();
|
||||
const registry = createRegistry();
|
||||
const abortController = new AbortController();
|
||||
|
||||
await withProviderCallExecutionContext(
|
||||
{ abortSignal: abortController.signal, rateLimitRegistry: registry },
|
||||
() => callProviderWithContext(provider, 'grade this', 'rubric', vars),
|
||||
);
|
||||
|
||||
expect(provider.callApi).toHaveBeenCalledWith(
|
||||
'grade this',
|
||||
{
|
||||
prompt: { raw: 'grade this', label: 'rubric' },
|
||||
vars,
|
||||
},
|
||||
{ abortSignal: abortController.signal },
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps scheduler execution context scoped to its callback', async () => {
|
||||
const provider = createProvider();
|
||||
const registry = createRegistry();
|
||||
|
||||
await withProviderCallExecutionContext({ rateLimitRegistry: registry }, () =>
|
||||
callProviderWithContext(provider, 'scheduled', 'rubric', vars),
|
||||
);
|
||||
await callProviderWithContext(provider, 'direct', 'rubric', vars);
|
||||
|
||||
expect(registry.executeSpy).toHaveBeenCalledTimes(1);
|
||||
expect(provider.callApi).toHaveBeenCalledTimes(2);
|
||||
expect(provider.callApi).toHaveBeenLastCalledWith('direct', {
|
||||
prompt: { raw: 'direct', label: 'rubric' },
|
||||
vars,
|
||||
});
|
||||
});
|
||||
|
||||
it('does not double schedule providers that are already rate-limit wrapped', async () => {
|
||||
const provider = createProvider();
|
||||
const wrapperRegistry = createRegistry();
|
||||
const contextRegistry = createRegistry();
|
||||
const wrappedProvider = wrapProviderWithRateLimiting(
|
||||
provider,
|
||||
wrapperRegistry as unknown as RateLimitRegistry,
|
||||
);
|
||||
|
||||
await withProviderCallExecutionContext({ rateLimitRegistry: contextRegistry }, () =>
|
||||
callProviderWithContext(wrappedProvider, 'grade this', 'rubric', vars),
|
||||
);
|
||||
|
||||
expect(contextRegistry.executeSpy).not.toHaveBeenCalled();
|
||||
expect(wrapperRegistry.executeSpy).toHaveBeenCalledTimes(1);
|
||||
expect(provider.callApi).toHaveBeenCalledWith(
|
||||
'grade this',
|
||||
{
|
||||
prompt: { raw: 'grade this', label: 'rubric' },
|
||||
vars,
|
||||
},
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it('queues provider calls when a provider call queue is available', async () => {
|
||||
const response = { output: 'queued response' };
|
||||
const provider = createProvider(response);
|
||||
const providerCallQueue = new ProviderGroupedCallQueue();
|
||||
|
||||
const promise = withProviderCallExecutionContext({ providerCallQueue }, () =>
|
||||
callProviderWithContext(provider, 'grade this', 'rubric', vars),
|
||||
);
|
||||
|
||||
expect(provider.callApi).not.toHaveBeenCalled();
|
||||
const group = providerCallQueue.takeNextGroup();
|
||||
expect(group).toHaveLength(1);
|
||||
expect(group[0].providerId).toBe('test-grader');
|
||||
|
||||
await providerCallQueue.run(group[0]);
|
||||
await expect(promise).resolves.toBe(response);
|
||||
expect(provider.callApi).toHaveBeenCalledWith('grade this', {
|
||||
prompt: { raw: 'grade this', label: 'rubric' },
|
||||
vars,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user