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
148 lines
3.9 KiB
TypeScript
148 lines
3.9 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { fetchWithCache } from '../../src/cache';
|
|
import { WebhookProvider } from '../../src/providers/webhook';
|
|
|
|
vi.mock('../../src/cache');
|
|
|
|
describe('WebhookProvider', () => {
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
describe('constructor', () => {
|
|
it('should create instance with url', () => {
|
|
const provider = new WebhookProvider('http://test.com');
|
|
expect(provider.webhookUrl).toBe('http://test.com');
|
|
});
|
|
|
|
it('should create instance with url and options', () => {
|
|
const provider = new WebhookProvider('http://test.com', {
|
|
id: 'test-id',
|
|
config: { foo: 'bar' },
|
|
});
|
|
expect(provider.webhookUrl).toBe('http://test.com');
|
|
expect(provider.config).toEqual({ foo: 'bar' });
|
|
expect(provider.id()).toBe('test-id');
|
|
});
|
|
});
|
|
|
|
describe('id', () => {
|
|
it('should return webhook url id', () => {
|
|
const provider = new WebhookProvider('http://test.com');
|
|
expect(provider.id()).toBe('webhook:http://test.com');
|
|
});
|
|
});
|
|
|
|
describe('toString', () => {
|
|
it('should return string representation', () => {
|
|
const provider = new WebhookProvider('http://test.com');
|
|
expect(provider.toString()).toBe('[Webhook Provider http://test.com]');
|
|
});
|
|
});
|
|
|
|
describe('callApi', () => {
|
|
it('should call webhook and return output', async () => {
|
|
const mockFetchResponse = {
|
|
data: {
|
|
output: 'test response',
|
|
},
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValue(mockFetchResponse);
|
|
|
|
const provider = new WebhookProvider('http://test.com');
|
|
const result = await provider.callApi('test prompt');
|
|
|
|
expect(fetchWithCache).toHaveBeenCalledWith(
|
|
'http://test.com',
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
prompt: 'test prompt',
|
|
}),
|
|
},
|
|
300000,
|
|
'json',
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
output: 'test response',
|
|
cached: false,
|
|
latencyMs: undefined,
|
|
});
|
|
});
|
|
|
|
it('should include config in request if provided', async () => {
|
|
const mockFetchResponse = {
|
|
data: {
|
|
output: 'test response',
|
|
},
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValue(mockFetchResponse);
|
|
|
|
const provider = new WebhookProvider('http://test.com', {
|
|
config: { foo: 'bar' },
|
|
});
|
|
|
|
await provider.callApi('test prompt');
|
|
|
|
expect(fetchWithCache).toHaveBeenCalledWith(
|
|
'http://test.com',
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
prompt: 'test prompt',
|
|
config: { foo: 'bar' },
|
|
}),
|
|
},
|
|
300000,
|
|
'json',
|
|
);
|
|
});
|
|
|
|
it('should handle fetch errors', async () => {
|
|
vi.mocked(fetchWithCache).mockRejectedValue(new Error('Network error'));
|
|
|
|
const provider = new WebhookProvider('http://test.com');
|
|
const result = await provider.callApi('test prompt');
|
|
|
|
expect(result).toEqual({
|
|
error: 'Webhook call error: Error: Network error',
|
|
});
|
|
});
|
|
|
|
it('should handle invalid response format', async () => {
|
|
const mockFetchResponse = {
|
|
data: {
|
|
foo: 'bar',
|
|
},
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValue(mockFetchResponse);
|
|
|
|
const provider = new WebhookProvider('http://test.com');
|
|
const result = await provider.callApi('test prompt');
|
|
|
|
expect(result).toEqual({
|
|
error: 'Webhook response error: Unexpected response format: {"foo":"bar"}',
|
|
});
|
|
});
|
|
});
|
|
});
|