char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
521a64cd0
char/aliceai-t5-moe
1// Guards the Enter-key contract of the chat form against the 2// fenced-code-block flow: while the caret sits inside a fenced 3// block region - closed, or still OPEN while the user is typing 4// one - plain Enter adds a line instead of submitting the message. 5// The textarea path is covered here end-to-end (the ChatFormInputRich 6// consumes the same case locally; see chat-form-input-rich). 7 8import ChatFormTestWrapper from './components/ChatFormTestWrapper.svelte' ; 9import { SETTINGS_KEYS } from '$lib/constants' ; 10import { settingsStore } from '$lib/stores/settings/index.svelte' ; 11import { tick } from 'svelte' ; 12import { beforeEach , describe , expect , it , vi } from 'vitest' ; 13import { userEvent } from 'vitest/browser' ; 14import { render } from 'vitest-browser-svelte' ; 15 16function textareaIn ( container :HTMLElement ) :HTMLTextAreaElement { 17const el = container . querySelector ( 'textarea' ); 18 19if ( ! ( el instanceof HTMLTextAreaElement )) throw new Error ( 'textarea not rendered' ); 20 21return el ; 22} 23 24describe ( 'ChatForm Enter in code blocks' , () => { 25beforeEach (() => { 26settingsStore . updateConfig ( SETTINGS_KEYS . SEND_ON_ENTER , true ); 27}); 28 29it ( 'adds a line after a still-open fence instead of submitting' , async () => { 30const onSubmit = vi . fn (); 31const { container} = render ( ChatFormTestWrapper , { onSubmit}); 32 33await tick (); 34 35const textarea = textareaIn ( container ); 36 37await userEvent . click ( textarea ); 38await userEvent . keyboard ( '```' ); 39await tick (); 40 41await userEvent . keyboard ( '{Enter}' ); 42await tick (); 43 44expect ( onSubmit ). not . toHaveBeenCalled (); 45expect ( textarea . value ). toBe ( '```\n' ); 46}); 47 48it ( 'keeps adding lines while the block stays open' , async () => { 49const onSubmit = vi . fn (); 50const { container} = render ( ChatFormTestWrapper , { onSubmit}); 51 52await tick (); 53 54const textarea = textareaIn ( container ); 55 56await userEvent . click ( textarea ); 57await userEvent . keyboard ( '```js' ); 58await tick (); 59 60await userEvent . keyboard ( '{Enter}' ); 61await userEvent . keyboard ( 'const a = 1;' ); 62await userEvent . keyboard ( '{Enter}' ); 63await tick (); 64 65expect ( onSubmit ). not . toHaveBeenCalled (); 66expect ( textarea . value ). toBe ( '```js\nconst a = 1;\n' ); 67}); 68 69it ( 'submits when the caret is before the opening fence' , async () => { 70const onSubmit = vi . fn (); 71const { container} = render ( ChatFormTestWrapper , { onSubmit}); 72 73await tick (); 74 75const textarea = textareaIn ( container ); 76 77await userEvent . click ( textarea ); 78await userEvent . keyboard ( '```' ); 79await tick (); 80 81textarea . setSelectionRange ( 0 , 0 ); 82 83await userEvent . keyboard ( '{Enter}' ); 84await tick (); 85 86expect ( onSubmit ). toHaveBeenCalledTimes ( 1 ); 87}); 88 89it ( 'submits on Enter outside a code block' , async () => { 90const onSubmit = vi . fn (); 91const { container} = render ( ChatFormTestWrapper , { onSubmit}); 92 93await tick (); 94 95const textarea = textareaIn ( container ); 96 97await userEvent . click ( textarea ); 98await userEvent . keyboard ( 'hello' ); 99await tick (); 100 101await userEvent . keyboard ( '{Enter}' ); 102await tick (); 103 104expect ( onSubmit ). toHaveBeenCalledTimes ( 1 ); 105}); 106 107it ( 'submits on Ctrl+Enter even inside a code block' , async () => { 108const onSubmit = vi . fn (); 109const { container} = render ( ChatFormTestWrapper , { onSubmit}); 110 111await tick (); 112 113const textarea = textareaIn ( container ); 114 115await userEvent . click ( textarea ); 116await userEvent . keyboard ( '```' ); 117await tick (); 118 119await userEvent . keyboard ( '{Control>}{Enter}{/Control}' ); 120await tick (); 121 122expect ( onSubmit ). toHaveBeenCalledTimes ( 1 ); 123}); 124});