Files
wehub-resource-sync 0d3cb498a3
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
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

123 lines
3.8 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { createTransformResponse } from '../../../src/providers/a2a/transforms';
describe('A2A createTransformResponse', () => {
const finalResponse = {
events: [{ statusUpdate: { status: { state: 'TASK_STATE_COMPLETED' } } }],
message: {
parts: [{ text: 'hello from message' }],
role: 'ROLE_AGENT',
},
raw: {
message: {
parts: [{ text: 'hello from message' }],
role: 'ROLE_AGENT',
},
},
task: {
id: 'task-1',
status: { state: 'TASK_STATE_COMPLETED' },
},
};
const context = {
events: finalResponse.events,
message: finalResponse.message,
mode: 'stream' as const,
raw: finalResponse.raw,
task: finalResponse.task,
};
it('returns extracted text when no transform is configured', async () => {
const transform = createTransformResponse(undefined);
await expect(transform(finalResponse, 'fallback text', context)).resolves.toEqual({
output: 'fallback text',
});
});
it('passes json, text, and context to function transforms', async () => {
const transform = createTransformResponse((json: any, text: string, transformContext: any) => ({
metadata: {
eventCount: transformContext.events.length,
mode: transformContext.mode,
taskId: json.task.id,
},
output: text.toUpperCase(),
}));
await expect(transform(finalResponse, 'fallback text', context)).resolves.toEqual({
metadata: {
eventCount: 1,
mode: 'stream',
taskId: 'task-1',
},
output: 'FALLBACK TEXT',
});
});
it('wraps primitive function transform results as provider output', async () => {
const transform = createTransformResponse((json: any) => json.task.id);
await expect(transform(finalResponse, 'fallback text', context)).resolves.toEqual({
output: 'task-1',
});
});
it('supports string expressions with json and result aliases', async () => {
const transform = createTransformResponse(
'({ output: `${json.task.id}:${result.message.role}:${text}:${context.mode}` })',
);
await expect(transform(finalResponse, 'fallback text', context)).resolves.toEqual({
output: 'task-1:ROLE_AGENT:fallback text:stream',
});
});
it('supports async string function expressions', async () => {
const transform = createTransformResponse('async (json, text) => `${json.task.id}:${text}`');
await expect(transform(finalResponse, 'fallback text', context)).resolves.toEqual({
output: 'task-1:fallback text',
});
});
it('wraps object string-expression results without provider response fields', async () => {
const transform = createTransformResponse('({ taskId: result.task.id })');
await expect(transform(finalResponse, 'fallback text', context)).resolves.toEqual({
output: { taskId: 'task-1' },
});
});
it('throws if file transforms are not pre-loaded', () => {
expect(() => createTransformResponse('file://parser.js')).toThrow(
/should be pre-loaded before calling createTransformResponse/,
);
});
it('rejects unsupported transform types', () => {
expect(() => createTransformResponse(123 as any)).toThrow(
/Unsupported response transform type: number/,
);
});
it('preserves errors from function transforms', async () => {
const transform = createTransformResponse(() => {
throw new Error('function transform failed');
});
await expect(transform(finalResponse, 'fallback text', context)).rejects.toThrow(
'function transform failed',
);
});
it('wraps errors from string transforms', async () => {
const transform = createTransformResponse('result.missing.value');
await expect(transform(finalResponse, 'fallback text', context)).rejects.toThrow(
/Failed to transform A2A response/,
);
});
});