Files
llama.cpp/ui/apps/chat/tests/client/chat-form-input.svelte.test.ts
T
Emanuil Rusev 918076a635 ui: turn the live markdown preview off by default (#32469)
As discussed in #31955, the live preview is now opt-in. With it on, the editor renders blocks as widgets, and moving the cursor down from the first line of a prompt with a few blocks (a heading, a list, a code block) jumps to the last line; with it off, the cursor moves one line at a time. Users with a saved config keep their current value, since the full config is stored in localStorage.

The message input also gets spellcheck, autocorrect and autocapitalize back. CodeMirror turns them off by default, so they were off with the preview on or off; the former textarea and contenteditable inputs had the browser's defaults.
2026-11-17 16:17:46 +01:00

167 lines
5.8 KiB
TypeScript

// Guards the chat form input contract: a markdown editor whose value stays the exact
// markdown source, with mention links shown as badges while their block is edited and,
// with the live preview setting on, every block rendered once the editor loses focus. The form's
// key and paste handlers run first and win when they handle the event.
import { EditorView } from '@codemirror/view';
import ChatFormInput from '#lib/components/app/chat/ChatForm/ChatFormInput/ChatFormInput.svelte';
import { SETTINGS_KEYS } from '#lib/constants/index.ts';
import { settingsStore } from '#lib/stores/settings/index.svelte.ts';
import { tick } from 'svelte';
import { describe, expect, it, vi } from 'vitest';
import { userEvent } from 'vitest/browser';
import { render } from 'vitest-browser-svelte';
const MENTION = '[notes.md](file:///tmp/notes.md)';
function editorIn(container: HTMLElement): EditorView {
const el = container.querySelector<HTMLElement>('.cm-content');
const view = el && EditorView.findFromDOM(el);
if (!view) throw new Error('editor not rendered');
return view;
}
async function settle() {
await tick();
await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));
}
describe('ChatFormInput', () => {
it('keeps the typed markdown byte-exact in the value', async () => {
const screen = render(ChatFormInput, { value: '' });
const editor = editorIn(screen.container);
await userEvent.click(editor.contentDOM);
await userEvent.keyboard('## Title{Shift>}{Enter}{Enter}{/Shift}- **bold** `code`');
await settle();
expect(editor.state.doc.toString()).toBe('## Title\n\n- **bold** `code`');
});
it('shows a mention link as a badge in the block being edited', async () => {
const screen = render(ChatFormInput, { value: `see ${MENTION} ok` });
const editor = editorIn(screen.container);
editor.focus();
editor.dispatch({ selection: { anchor: 0 } });
await settle();
const line = screen.container.querySelector('.cm-line');
expect(line?.textContent).toContain('notes.md');
expect(line?.textContent).not.toContain('file://');
expect(editor.state.doc.toString()).toBe(`see ${MENTION} ok`);
});
it('leaves a mention link in a code block as text', async () => {
const screen = render(ChatFormInput, { value: `~~~\n${MENTION}\n~~~` });
const editor = editorIn(screen.container);
editor.focus();
editor.dispatch({ selection: { anchor: 0 } });
await settle();
expect(screen.container.querySelector('.cm-content')?.textContent).toContain('file://');
});
it('steps over a mention badge as one unit', async () => {
const screen = render(ChatFormInput, { value: `a ${MENTION}` });
const editor = editorIn(screen.container);
editor.focus();
editor.dispatch({ selection: { anchor: editor.state.doc.length } });
await settle();
await userEvent.keyboard('{Backspace}');
await settle();
expect(editor.state.doc.toString()).toBe('a ');
});
it('renders every block like a chat message once blurred, with the live preview on', async () => {
settingsStore.updateConfig(SETTINGS_KEYS.LIVE_MARKDOWN_PREVIEW, true);
try {
const screen = render(ChatFormInput, { value: '# Title\n\nSome **bold** text' });
const editor = editorIn(screen.container);
editor.contentDOM.blur();
await vi.waitFor(() => {
expect(screen.container.querySelector('.cm-md-block h1')?.textContent).toBe('Title');
expect(screen.container.querySelector('.cm-md-block strong')?.textContent).toBe('bold');
});
} finally {
settingsStore.updateConfig(SETTINGS_KEYS.LIVE_MARKDOWN_PREVIEW, false);
}
});
it('shows the plain markdown, with mention badges, by default', async () => {
try {
const screen = render(ChatFormInput, { value: `# Title\n\nsee ${MENTION}` });
const editor = editorIn(screen.container);
editor.contentDOM.blur();
await settle();
const lines = [...screen.container.querySelectorAll('.cm-line')].map((l) => l.textContent);
expect(screen.container.querySelector('.cm-md-block')).toBeNull();
expect(lines[0]).toBe('# Title');
expect(lines.at(-1)).toContain('notes.md');
expect(lines.at(-1)).not.toContain('file://');
settingsStore.updateConfig(SETTINGS_KEYS.LIVE_MARKDOWN_PREVIEW, true);
await vi.waitFor(() =>
expect(screen.container.querySelector('.cm-md-block h1')?.textContent).toBe('Title')
);
} finally {
settingsStore.updateConfig(SETTINGS_KEYS.LIVE_MARKDOWN_PREVIEW, false);
}
});
it('spellchecks, autocorrects and autocapitalizes like a textarea', () => {
const screen = render(ChatFormInput, { value: '' });
const content = editorIn(screen.container).contentDOM;
expect(content.getAttribute('spellcheck')).toBe('true');
expect(content.getAttribute('autocorrect')).toBe('on');
expect(content.getAttribute('autocapitalize')).toBe('on');
});
it('lets the form handle a key first and keeps it from the editor', async () => {
const onKeydown = vi.fn((event: KeyboardEvent) => {
if (event.key === 'Enter') event.preventDefault();
});
const screen = render(ChatFormInput, { onKeydown, value: 'hello' });
const editor = editorIn(screen.container);
editor.focus();
editor.dispatch({ selection: { anchor: 5 } });
await userEvent.keyboard('{Enter}');
await settle();
expect(onKeydown).toHaveBeenCalled();
expect(editor.state.doc.toString()).toBe('hello');
});
it('leaves a paste the form handles out of the editor', async () => {
const onPaste = vi.fn((event: ClipboardEvent) => event.preventDefault());
const screen = render(ChatFormInput, { onPaste, value: '' });
const editor = editorIn(screen.container);
const clipboardData = new DataTransfer();
clipboardData.setData('text/plain', 'pasted');
editor.focus();
editor.contentDOM.dispatchEvent(
new ClipboardEvent('paste', { bubbles: true, cancelable: true, clipboardData })
);
await settle();
expect(onPaste).toHaveBeenCalled();
expect(editor.state.doc.toString()).toBe('');
});
});