wip: next round of implementation, this time tests

This commit is contained in:
2026-02-25 20:38:34 +01:00
parent 20ea499a6f
commit 7808ce74ac
8 changed files with 633 additions and 1 deletions

View File

@@ -114,4 +114,61 @@ describe('ProtocolResponseBuilder', () => {
expect(result.envelope.actions).toHaveLength(0);
expect(result.warnings.some((warning) => warning.includes('openSettings'))).toBe(true);
});
it('extracts declarative actions from UI elements and applies policy defaults', () => {
const builder = new ProtocolResponseBuilder();
const raw = JSON.stringify({
protocolVersion: '2.0',
assistantText: 'Choose an option',
intent: 'propose_action',
needsInput: { required: false, fields: [] },
actions: [],
ui: {
specVersion: '1',
elements: [
{
type: 'card',
title: 'Actions',
body: 'Pick one',
actions: [{ label: 'Delete', action: 'deletePost' }],
},
{
type: 'tabs',
tabs: [
{
id: 'first',
label: 'First',
elements: [{ type: 'action', label: 'Open post', action: 'openPost' }],
},
],
},
],
},
confidence: 0.7,
traceId: 'trace-ui-actions',
});
const result = builder.build({
rawAssistantOutput: raw,
surface: 'tab',
capabilities: {
widgets: ['card', 'tabs', 'action'],
actions: ['deletePost', 'openPost'],
tools: ['search_posts'],
},
});
expect(result.envelope.actions).toHaveLength(2);
expect(result.envelope.actions[0]).toEqual(expect.objectContaining({
action: 'deletePost',
policy: 'danger',
requiresConfirmation: true,
}));
expect(result.envelope.actions[1]).toEqual(expect.objectContaining({
action: 'openPost',
policy: 'silent',
requiresConfirmation: false,
}));
});
});

View File

@@ -0,0 +1,93 @@
import { describe, expect, it } from 'vitest';
import { extractAssistantUiSpec } from '../../../../src/main/agentic/protocol/uiSpecParser';
describe('extractAssistantUiSpec', () => {
it('extracts fenced JSON spec and preserves assistant text around it', () => {
const input = [
'Here is your dashboard.',
'```json',
'{"specVersion":"1","elements":[{"type":"text","text":"Hello"}]}',
'```',
'Anything else?',
].join('\n');
const result = extractAssistantUiSpec(input);
expect(result.ui).not.toBeNull();
expect(result.ui?.specVersion).toBe('1');
expect(result.ui?.elements).toHaveLength(1);
expect(result.assistantText).toContain('Here is your dashboard.');
expect(result.assistantText).toContain('Anything else?');
});
it('normalizes markdown element into canonical text element', () => {
const input = JSON.stringify({
specVersion: '1',
elements: [{ type: 'markdown', content: '## Title' }],
});
const result = extractAssistantUiSpec(input);
const first = result.ui?.elements[0] as { type: string; text?: string };
expect(result.ui).not.toBeNull();
expect(first.type).toBe('text');
expect(first.text).toBe('## Title');
});
it('normalizes chart data datasets into chart series', () => {
const input = JSON.stringify({
specVersion: '1',
elements: [
{
type: 'chart',
chartType: 'bar',
data: {
labels: ['Jan', 'Feb'],
datasets: [{ data: [10, 20] }],
},
},
],
});
const result = extractAssistantUiSpec(input);
const first = result.ui?.elements[0] as { series?: Array<{ label: string; value: number }>; data?: unknown };
expect(result.ui).not.toBeNull();
expect(first.series).toEqual([
{ label: 'Jan', value: 10 },
{ label: 'Feb', value: 20 },
]);
expect(first.data).toBeUndefined();
});
it('normalizes tabs content to nested elements arrays', () => {
const input = JSON.stringify({
type: 'tabs',
tabs: [
{
title: 'Overview',
content: { type: 'text', text: 'Summary' },
},
],
});
const result = extractAssistantUiSpec(input);
const tabs = result.ui?.elements[0] as {
tabs: Array<{ id: string; label: string; elements: Array<{ type: string; text?: string }> }>;
};
expect(result.ui).not.toBeNull();
expect(tabs.tabs).toHaveLength(1);
expect(tabs.tabs[0].id).toBe('tab-1');
expect(tabs.tabs[0].label).toBe('Overview');
expect(tabs.tabs[0].elements[0]).toEqual({ type: 'text', text: 'Summary' });
});
it('returns plain assistant text when malformed JSON is provided', () => {
const input = '{"specVersion":"1","elements":[{"type":"text"}';
const result = extractAssistantUiSpec(input);
expect(result.ui).toBeNull();
expect(result.assistantText).toBe(input);
});
});

View File

@@ -71,4 +71,23 @@ describe('agentic protocol validator', () => {
expect(result.ok).toBe(true);
});
it('rejects invalid request envelope and returns structured protocol error', () => {
const result = validateProtocolRequestEnvelope({
protocolVersion: '2.0',
surface: 'tab',
messages: [{ role: 'invalid-role', content: 'Create a chart' }],
context: { projectId: 'project-1' },
capabilities: {
widgets: ['chart'],
actions: ['openPost'],
tools: ['search_posts'],
},
unknown: true,
});
expect(result.ok).toBe(false);
expect(result.error?.code).toBe('AGUI_PROTOCOL_VALIDATION_ERROR');
expect(result.error?.details?.length).toBeGreaterThan(0);
});
});