Files
promptfoo--promptfoo/test/redteam/mcpRemoteMaterialization.test.ts
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

176 lines
5.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fetchWithCache } from '../../src/cache';
import cliState from '../../src/cliState';
import { getEnvBool, getEnvInt, getEnvString } from '../../src/envars';
import { getUserEmail, isLoggedIntoCloud } from '../../src/globalConfig/accounts';
import { materializeMcpToolCallRemote } from '../../src/redteam/extraction/util';
vi.mock('../../src/cache');
vi.mock('../../src/envars');
vi.mock('../../src/globalConfig/accounts');
vi.mock('../../src/globalConfig/cloud', async (importOriginal) => {
return {
...(await importOriginal()),
CloudConfig: class {
isEnabled() {
return false;
}
getApiHost() {
return 'https://api.promptfoo.app';
}
},
};
});
describe('materializeMcpToolCallRemote', () => {
const searchCompaniesTool = {
name: 'search_companies',
description: 'Search sample company records.',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string' },
},
required: ['query'],
},
};
beforeEach(() => {
vi.resetAllMocks();
cliState.remote = undefined;
vi.mocked(getUserEmail).mockReturnValue('test@example.com');
vi.mocked(isLoggedIntoCloud).mockReturnValue(false);
vi.mocked(getEnvInt).mockReturnValue(300_000);
vi.mocked(getEnvString).mockReturnValue('');
vi.mocked(getEnvBool).mockReturnValue(false);
});
it('returns normalized MCP JSON from the remote task server', async () => {
vi.mocked(getEnvString).mockImplementation((key: string) =>
key === 'PROMPTFOO_REMOTE_GENERATION_URL' ? 'https://remote.example.test/task' : '',
);
vi.mocked(fetchWithCache).mockResolvedValue({
data: {
result: {
tool: 'search_companies',
args: { query: 'clean energy' },
},
tokenUsage: {
prompt: 12,
completion: 4,
total: 16,
},
},
status: 200,
statusText: 'OK',
} as Awaited<ReturnType<typeof fetchWithCache>>);
const result = await materializeMcpToolCallRemote({
intentValue: 'Find clean energy companies.',
purpose: 'Search companies',
tools: [searchCompaniesTool],
value: 'Find clean energy companies.',
});
expect(JSON.parse(result?.prompt ?? '')).toEqual({
tool: 'search_companies',
args: { query: 'clean energy' },
});
expect(result?.tokenUsage).toEqual({
prompt: 12,
completion: 4,
total: 16,
});
expect(JSON.parse((vi.mocked(fetchWithCache).mock.calls[0][1] as any).body)).toMatchObject({
email: 'test@example.com',
jsonOnly: true,
mcpMaterializationContext: {
intentValue: 'Find clean energy companies.',
purpose: 'Search companies',
tools: [searchCompaniesTool],
},
preferSmallModel: false,
prompt: 'Find clean energy companies.',
task: 'mcp-materialization',
});
});
it('returns undefined when local credentials are available and remote generation is not selected', async () => {
vi.mocked(getEnvString).mockImplementation((key: string) =>
key === 'OPENAI_API_KEY' ? 'sk-local' : '',
);
await expect(
materializeMcpToolCallRemote({
tools: [searchCompaniesTool],
value: 'Find clean energy companies.',
}),
).resolves.toBeUndefined();
expect(fetchWithCache).not.toHaveBeenCalled();
});
it('returns undefined when remote generation is explicitly disabled', async () => {
vi.mocked(getEnvBool).mockImplementation(
(key: string) => key === 'PROMPTFOO_DISABLE_REMOTE_GENERATION',
);
await expect(
materializeMcpToolCallRemote({
tools: [searchCompaniesTool],
value: 'Find clean energy companies.',
}),
).resolves.toBeUndefined();
expect(fetchWithCache).not.toHaveBeenCalled();
});
it('includes target context in the remote task body', async () => {
vi.mocked(getEnvString).mockImplementation((key: string) =>
key === 'PROMPTFOO_REMOTE_GENERATION_URL' ? 'https://remote.example.test/task' : '',
);
vi.mocked(fetchWithCache).mockResolvedValue({
data: {
result: {
tool: 'search_companies',
args: { query: 'cloud' },
},
},
status: 200,
statusText: 'OK',
} as Awaited<ReturnType<typeof fetchWithCache>>);
await materializeMcpToolCallRemote({
targetId: 'cloud-target-123',
tools: [searchCompaniesTool],
value: 'Find cloud companies.',
});
expect(JSON.parse((vi.mocked(fetchWithCache).mock.calls[0][1] as any).body)).toMatchObject({
targetId: 'cloud-target-123',
});
});
it('rejects invalid remote materialization output', async () => {
vi.mocked(getEnvString).mockImplementation((key: string) =>
key === 'PROMPTFOO_REMOTE_GENERATION_URL' ? 'https://remote.example.test/task' : '',
);
vi.mocked(fetchWithCache).mockResolvedValue({
data: {
result: {
tool: 'unknown_tool',
args: {},
},
},
status: 200,
statusText: 'OK',
} as Awaited<ReturnType<typeof fetchWithCache>>);
await expect(
materializeMcpToolCallRemote({
tools: [searchCompaniesTool],
value: 'Find clean energy companies.',
}),
).rejects.toThrow('Remote MCP materialization failed');
});
});