// 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 { createFallbackMessagesFromPlainText, createTextCardMessages, normalizePayloadToMessages, prepareMessagesForProcessing, } from '../src/store/payloadNormalizer.js'; import type { ServerToClientMessage } from '../src/store/types.js'; describe('payloadNormalizer', () => { test('createFallbackMessagesFromPlainText wraps text in a Text component', () => { const msgs = createFallbackMessagesFromPlainText('hello'); expect(msgs).toHaveLength(2); const update = msgs[1] as { version: '1.0'; updateComponents: { components: unknown[] }; }; expect(update.updateComponents.components[0]).toMatchObject({ component: 'Text', text: 'hello', }); }); test('createTextCardMessages wraps text in a Card with Text child', () => { const msgs = createTextCardMessages('greetings'); expect(msgs).toHaveLength(2); const update = msgs[1] as { version: '1.0'; updateComponents: { components: unknown[] }; }; expect(update.updateComponents.components).toHaveLength(2); const [card, text] = update.updateComponents .components as Record[]; expect(card!['component']).toBe('Card'); expect(text!['component']).toBe('Text'); expect(text!['text']).toBe('greetings'); }); test('normalizePayloadToMessages passes through structured messages', () => { const input = [{ version: '1.0', createSurface: { surfaceId: 's' } }]; expect(normalizePayloadToMessages(input)).toEqual(input); }); test('normalizePayloadToMessages wraps plain text in fallback', () => { const out = normalizePayloadToMessages('hi'); expect(out).toHaveLength(2); expect( (out[0] as { version: '1.0'; createSurface: unknown }).createSurface, ).toBeDefined(); }); test('normalizePayloadToMessages handles { kind: "text", data: "..." }', () => { const out = normalizePayloadToMessages({ kind: 'text', data: 'yo' }); expect(out).toHaveLength(2); const update = out[1] as { version: '1.0'; updateComponents: { components: unknown[] }; }; expect(update.updateComponents.components).toHaveLength(2); }); test.each([null, undefined])( 'preprocessing tolerates createSurface=%s', (createSurface) => { const messages = [{ version: '1.0', createSurface, }] as unknown as ServerToClientMessage[]; const active = new Set(); const result = prepareMessagesForProcessing(messages, 'task', active); expect(result.hasComponentUpdate).toBe(false); expect(result.messages).toEqual([{ version: '1.0', createSurface, messageId: 'task', }]); expect(active.size).toBe(0); }, ); test('prepareMessagesForProcessing tags messageId and leaves duplicate validation to the processor', () => { const messages = [ { version: '1.0', createSurface: { surfaceId: 's1' } }, { version: '1.0', createSurface: { surfaceId: 's1' } }, { version: '1.0', updateComponents: { surfaceId: 's1', components: [{ id: 'root', component: 'Text' }], }, }, ] as ServerToClientMessage[]; const active = new Set(); const result = prepareMessagesForProcessing(messages, 'task_1', active); expect(result.messages).toHaveLength(3); expect(result.hasComponentUpdate).toBe(true); for (const m of result.messages) { expect(m.messageId).toBe('task_1'); } expect(active.has('s1')).toBe(true); }); });