Files
openidea/src/features/agents/composerTesting.ts
T
l0ng-ai 89f00a29f9 feat(agents): reference chips, quote blocks and a richer completion in the composer (#3091)
The agent composer is now a CodeMirror editor: the text stays plain and is what's sent, while @ file references are drawn as chips (icon, name, lines) that move and delete as one piece, open on click and preview on hover; Backspace after a chip opens it as text. Quotes show as blocks, and sent messages get the same chips and quote blocks. The @ completion lists open editors first, highlights matches and shows the folder; the focused editor's file is offered above the text, a pasted project path becomes a reference, and dropping files shows a hint. Lines copied from an editor paste as a reference, terminal output as a terminal block, and code blocks show as cards. The reference trimming regex avoids lookbehind for WebKit before 16.4.
2026-11-13 22:29:59 +08:00

54 lines
1.6 KiB
TypeScript

/** Test helpers for the composer's editor (a CodeMirror view); plain text areas work too. */
import { act, fireEvent } from "@testing-library/react";
import { EditorView } from "@codemirror/view";
function viewOf(box: HTMLElement): EditorView {
const view = EditorView.findFromDOM(box);
if (!view) throw new Error("not a composer editor");
return view;
}
/** Replaces the composer's text as typing would, caret at `caret` (the end by default). */
export function typeIn(box: HTMLElement, value: string, caret = value.length) {
if (box instanceof HTMLTextAreaElement) {
fireEvent.change(box, {
target: { value, selectionStart: caret, selectionEnd: caret },
});
return;
}
const view = viewOf(box);
act(() => {
view.focus();
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: value },
selection: { anchor: caret },
userEvent: "input.type",
});
});
}
/** Types `chars` at the caret, one at a time. */
export function typeChars(box: HTMLElement, chars: string) {
const view = viewOf(box);
act(() => view.focus());
for (const ch of chars)
act(() => {
view.dispatch(view.state.replaceSelection(ch), {
userEvent: "input.type",
});
});
}
export const textOf = (box: HTMLElement) =>
box instanceof HTMLTextAreaElement ? box.value : viewOf(box).state.doc.toString();
export const caretOf = (box: HTMLElement) => viewOf(box).state.selection.main.head;
export function setCaret(box: HTMLElement, anchor: number, head = anchor) {
const view = viewOf(box);
act(() => {
view.focus();
view.dispatch({ selection: { anchor, head } });
});
}