// Copyright 2026 The Lynx Authors. All rights reserved. // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. import { describe, expect, test } from '@rstest/core'; import { loadBasicCatalog } from '../agent/a2ui/a2ui-catalog.js'; import { A2UI_PROTOCOL_VERSION } from '../agent/a2ui/a2ui-prompt.js'; import { A2UIProtocolMessageStreamParser } from '../agent/a2ui/a2ui-stream-parser.js'; import { validateA2UIOutput } from '../agent/a2ui/a2ui-validator.js'; import { normalizeRendererEvent, validateAction } from '../app/a2ui/_shared.js'; import app from '../src/app.js'; describe('A2UI v1.0 server', () => { test.each(['v0.9', 'v0.9.1', 'v1.1', 'v2.0', undefined])( 'rejects unsupported action version %s on both transports', async (version) => { for (const route of ['/a2ui/action', '/a2ui/action/stream']) { const response = await app.request(route, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-forwarded-for': `migration-${route}-${String(version)}`, }, body: JSON.stringify({ version, surfaceId: 's', action: { name: 'submit', surfaceId: 's' }, }), }); expect(response.status).toBe(400); expect(await response.json()).toMatchObject({ error: 'Only A2UI v1.0 is supported', }); } }, ); test('a non-index deletion does not remove the first array entry', async () => { const catalog = await loadBasicCatalog(); const safe = 'https://images.example.com/safe.png'; const raw = JSON.stringify([ { version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, dataModel: { items: [{ url: safe }, { url: 'https://untrusted.example/image.png', }], }, }, }, { version: '1.0', updateDataModel: { surfaceId: 's', path: '/items/label', value: null }, }, { version: '1.0', updateComponents: { surfaceId: 's', components: [{ id: 'root', component: 'Image', url: { '@path': '/items/0/url' }, }], }, }, ]); expect( validateA2UIOutput(raw, catalog, { isImageSourceAllowed: url => url === safe, }).errors, ).toEqual([]); }); test('loads structured validation function metadata from the v1.0 catalog', async () => { const catalog = await loadBasicCatalog(); expect(catalog.functions?.find(fn => fn.name === 'required')?.returnType) .toBe('validationResult'); }); test('validates inline components and data with the existing catalog', async () => { const catalog = await loadBasicCatalog(); const messages = [{ version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, components: [{ id: 'root', component: 'Text', text: { '@path': '/name' }, }], dataModel: { name: 'Alice' }, }, }]; const result = validateA2UIOutput(JSON.stringify(messages), catalog); expect(result.errors).toEqual([]); expect(result.messages).toEqual(messages); expect(A2UI_PROTOCOL_VERSION).toBe('1.0'); expect( validateA2UIOutput( JSON.stringify([{ version: '1.0', createSurface: { surfaceId: 's', components: [{ id: 'root', component: 'Text', '@catalogId': catalog.id, text: 'Hi', }], }, }]), catalog, ).ok, ).toBe(true); }); test('rejects unknown inline components, themes, missing values and unsafe remote calls', async () => { const catalog = await loadBasicCatalog(); for ( const invalid of [ { version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, components: [{ id: 'root', component: 'Unknown' }], }, }, { version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, theme: {} }, }, { version: '1.0', updateDataModel: { surfaceId: 's' } }, { version: '1.0', callRendererFunction: { functionCallId: 'f', callFunction: { '@call': 'openUrl', catalogId: catalog.id, args: { url: 'https://example.com' }, }, }, }, ] ) { expect( validateA2UIOutput(JSON.stringify([invalid]), catalog, { requireCreateSurface: false, existingSurfaceIds: ['s'], }).ok, ).toBe(false); } }); test('streams inline content through source policies and preserves versions at every chunk boundary', async () => { const catalog = await loadBasicCatalog(); const raw = JSON.stringify([{ version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, components: [{ id: 'root', component: 'Image', url: 'https://untrusted.example/image.png', }], dataModel: { name: 'Alice' }, }, }]); for (let split = 1; split < raw.length; split++) { const parser = new A2UIProtocolMessageStreamParser({ isImageSourceAllowed: () => false, }); const messages = [ ...parser.push(raw.slice(0, split)), ...parser.push(raw.slice(split)), ]; expect(messages.every(message => message.version === '1.0')).toBe(true); expect(JSON.stringify(messages)).not.toContain('untrusted.example'); expect(messages.some(message => 'updateDataModel' in message)).toBe(true); expect( messages.some(message => 'updateComponents' in message && message.updateComponents.components[0]?.component === 'Loading' ), ).toBe(true); } }); test.each(['v0.9', 'v0.9.1', 'v1.1', 'v2.0', undefined])( 'rejects unsupported stream version %s', async (version) => { const catalog = await loadBasicCatalog(); const raw = JSON.stringify([{ version, createSurface: { surfaceId: 's', catalogId: catalog.id }, }]); expect(validateA2UIOutput(raw, catalog).ok).toBe(false); expect(new A2UIProtocolMessageStreamParser().push(raw)).toEqual([]); }, ); test('deleting a surface clears validation state before its ID is reused', async () => { const catalog = await loadBasicCatalog(); const previous = [ { version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, components: [ { id: 'root', component: 'Column', children: ['old'] }, { id: 'old', component: 'Text', text: { '@path': '/old' } }, ], dataModel: { old: 'Deleted' }, }, }, { version: '1.0', deleteSurface: { surfaceId: 's' } }, ]; for ( const [components, expected] of [ [ [{ id: 'orphan', component: 'Text', text: 'New' }], 'no component with id "root"', ], [ [{ id: 'root', component: 'Column', children: ['old'] }], 'missing child "old"', ], [ [{ id: 'root', component: 'Text', text: { '@path': '/old' } }], 'not populated', ], ] as const ) { const result = validateA2UIOutput( JSON.stringify([...previous, { version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, components }, }]), catalog, ); expect(result.ok).toBe(false); expect(result.errors.join(' ')).toContain(expected); } expect( validateA2UIOutput( JSON.stringify([...previous, { version: '1.0', createSurface: { surfaceId: 's', catalogId: catalog.id, components: [{ id: 'root', component: 'Text', text: 'New' }], }, }]), catalog, ).errors, ).toEqual([]); }); test('recreated existing surfaces do not inherit their previous catalog', async () => { const catalog = await loadBasicCatalog(); const result = validateA2UIOutput( JSON.stringify([ { version: '1.0', deleteSurface: { surfaceId: 's' } }, { version: '1.0', createSurface: { surfaceId: 's', components: [{ id: 'root', component: 'Text', text: 'New' }], }, }, ]), catalog, { requireCreateSurface: false, existingSurfaceIds: ['s'] }, ); expect(result.ok).toBe(false); expect(result.errors.join(' ')).toContain('must resolve to catalog'); }); test('never streams data updates with missing values at any chunk boundary', () => { const valid = [false, 0, '', null, { name: 'Alice' }].map(value => ({ version: '1.0', updateDataModel: { surfaceId: 's', value }, })); const raw = JSON.stringify([ { version: '1.0', updateDataModel: { surfaceId: 's' } }, ...valid, ]); for (let split = 1; split < raw.length; split++) { const parser = new A2UIProtocolMessageStreamParser(); expect([ ...parser.push(raw.slice(0, split)), ...parser.push(raw.slice(split)), ]).toEqual(valid); } }); test('preserves resolved action userMessage for the agent', () => { expect(validateAction({ name: 'select', userMessage: 'Selected item 2' })) .toMatchObject({ ok: true, action: { name: 'select', userMessage: 'Selected item 2' }, }); expect( validateAction({ name: 'select', userMessage: { '@path': '/label' } }), ) .toMatchObject({ ok: false }); }); test('maps action surface and data-model metadata into the stateless conversation', () => { const body = normalizeRendererEvent({ version: '1.0', action: { surfaceId: 's', name: 'submit' }, metadata: { a2uiRendererDataModel: { version: '1.0', surfaces: { s: { name: 'Bob' } }, }, }, conversation: { history: [] }, }); expect(body.surfaceId).toBe('s'); expect(body.conversation).toEqual({ history: [], dataModel: { name: 'Bob' }, }); }); test('keeps RPC replies on the streaming endpoint as SSE', async () => { const response = await app.request('/a2ui/action/stream', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-forwarded-for': '203.0.113.202', }, body: JSON.stringify({ version: '1.0', callAgentFunction: { surfaceId: 's', functionCallId: 'stream-f1', callFunction: { '@call': 'unknown' }, }, }), }); expect(response.headers.get('Content-Type')).toContain('text/event-stream'); const text = await response.text(); expect(text).toContain('event: done'); expect(text).toContain('stream-f1'); expect(text).toContain('UNKNOWN_FUNCTION'); }); test('returns a correlated UNKNOWN_FUNCTION without invoking a model', async () => { const response = await app.request('/a2ui/action', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-forwarded-for': '203.0.113.201', }, body: JSON.stringify({ version: '1.0', callAgentFunction: { surfaceId: 's', functionCallId: 'f1', callFunction: { '@call': 'verifyProvider', catalogId: 'test', args: {}, }, }, }), }); expect(response.status).toBe(200); expect(await response.json()).toEqual({ ok: true, messages: [{ version: '1.0', agentFunctionResponse: { functionCallId: 'f1', error: { code: 'UNKNOWN_FUNCTION', message: 'No agent implementation registered for "verifyProvider"', }, }, }], }); }); });