Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(assistant-ui): handle missing composer sync test file

Steven Enamakel54d9f109a79c
diff --git a/app/src/features/conversations/components/AssistantUiChat.composerSync.test.tsx b/app/src/features/conversations/components/AssistantUiChat.composerSync.test.tsx
index edcc92f548..c7835054ef 100644
--- a/app/src/features/conversations/components/AssistantUiChat.composerSync.test.tsx
+++ b/app/src/features/conversations/components/AssistantUiChat.composerSync.test.tsx
@@ -2,111 +2,97 @@
* Composer ↔ host draft sync under fast typing.
*
* `ComposerTextBridge` mirrored every keystroke into the host with a sync
- * `setState` inside an effect. A burst of keystrokes — key-repeat, fast
- * typing, an automated driver — chained those synchronous updates past React's
- * nested-update limit ("Maximum update depth exceeded") and the chat surface
- * fell to the error boundary. Reproduced in Brave against the dev build by
- * typing a one-line prompt at driver speed.
+ * `setState` inside an effect. Keystrokes are discrete events, so each one's
+ * effect — and the update it schedules — ran synchronously, and a burst of them
+ * (key-repeat, fast typing, an automated driver) chained those updates past
+ * React's nested-update limit: "Maximum update depth exceeded", and the chat
+ * surface fell to the error boundary. Reproduced in Brave against the dev build
+ * by typing a one-line prompt at driver speed.
+ *
+ * `flushSync` per character is the jsdom stand-in for that burst: each is a
+ * synchronous composer update, exactly what a discrete keystroke produces.
*/
-import { combineReducers, configureStore } from '@reduxjs/toolkit';
-import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
-import { useState } from 'react';
-import { Provider } from 'react-redux';
-import { describe, expect, it, vi } from 'vitest';
+import {
+ AssistantRuntimeProvider,
+ useAui,
+ useAuiState,
+ useExternalStoreRuntime,
+} from '@assistant-ui/react';
+import { act, render, waitFor } from '@testing-library/react';
+import { type ReactNode, useState } from 'react';
+import { flushSync } from 'react-dom';
+import { describe, expect, it } from 'vitest';
-import chatRuntimeReducer from '../../../store/chatRuntimeSlice';
-import mascotReducer from '../../../store/mascotSlice';
-import threadReducer from '../../../store/threadSlice';
-import { AssistantUiChat } from './AssistantUiChat';
+import { ComposerTextBridge } from './AssistantUiChat';
-const THREAD_ID = 't-composer-sync';
+type Handles = {
+ type: (text: string) => void;
+ hostSet: (value: string) => void;
+ hostValue: () => string;
+ composerText: () => string;
+};
-function buildStore() {
- return configureStore({
- reducer: combineReducers({
- thread: threadReducer,
- chatRuntime: chatRuntimeReducer,
- mascot: mascotReducer,
- }),
- preloadedState: {
- thread: {
- threads: [],
- selectedThreadId: THREAD_ID,
- activeThreadIds: {},
- welcomeThreadId: null,
- messagesByThreadId: { [THREAD_ID]: [] },
- messages: [],
- isLoadingThreads: false,
- isLoadingMessages: false,
- messagesError: null,
- },
- } as never,
- });
+function Runtime({ children }: { children: ReactNode }) {
+ const runtime = useExternalStoreRuntime({ messages: [], onNew: async () => {} });
+ return <AssistantRuntimeProvider runtime={runtime}>{children}</AssistantRuntimeProvider>;
}
-let hostSet: (value: string) => void = () => {};
-let hostValue = '';
-
-/** A host that owns the draft, like `Conversations` does. */
-function Host() {
+function Harness({ handles }: { handles: Partial<Handles> }) {
const [value, setValue] = useState('');
- hostSet = setValue;
- hostValue = value;
- return (
- <AssistantUiChat
- model={null}
- onModelChange={vi.fn()}
- inputValue={value}
- onInputValueChange={setValue}
- attachments={[]}
- onAttachFiles={vi.fn()}
- onRemoveAttachment={vi.fn()}
- maxAttachments={5}
- attachmentsEnabled={false}
- attachmentInteractionBlocked={false}
- onAttachmentOnlySend={vi.fn()}
- />
- );
+ const aui = useAui();
+ const composerText = useAuiState(s => s.composer.text);
+ handles.hostSet = setValue;
+ handles.hostValue = () => value;
+ handles.composerText = () => composerText;
+ handles.type = text => {
+ for (let n = 1; n <= text.length; n += 1) {
+ flushSync(() => aui.composer.setText(text.slice(0, n)));
+ }
+ };
+ return <ComposerTextBridge value={value} onChange={setValue} />;
}
-function composer(): HTMLTextAreaElement {
- return screen.getByRole('textbox') as HTMLTextAreaElement;
+function mount(): Handles {
+ const handles: Partial<Handles> = {};
+ render(
+ <Runtime>
+ <Harness handles={handles} />
+ </Runtime>
+ );
+ return handles as Handles;
}
+const PROMPT = 'Check the config, then search for the setting, then explain it.';
+
describe('ComposerTextBridge', () => {
- it('survives a burst of keystrokes and the host ends on what was typed', async () => {
- render(
- <Provider store={buildStore()}>
- <Host />
- </Provider>
- );
- const text = 'Check the config, then search for the setting, then explain it.';
- // One discrete input event per character, back to back, as key-repeat or a
- // fast typist produces them.
- for (let n = 1; n <= text.length; n += 1) {
- fireEvent.change(composer(), { target: { value: text.slice(0, n) } });
- }
+ it('survives a burst of synchronous keystrokes and the host ends on what was typed', async () => {
+ const h = mount();
+ act(() => h.type(PROMPT));
- expect(screen.queryByText(/Something went wrong/)).toBeNull();
- expect(composer().value).toBe(text);
- await waitFor(() => expect(hostValue).toBe(text));
- // The lagging echoes never overwrote what was typed after them.
- expect(composer().value).toBe(text);
+ await waitFor(() => expect(h.hostValue()).toBe(PROMPT));
+ // The host's lagging echoes never overwrote what was typed after them.
+ expect(h.composerText()).toBe(PROMPT);
});
- it('still lets a host-side write (clear, restore, dictation) win', async () => {
- render(
- <Provider store={buildStore()}>
- <Host />
- </Provider>
- );
- fireEvent.change(composer(), { target: { value: 'draft' } });
- await waitFor(() => expect(hostValue).toBe('draft'));
+ it('lets a host-side write (restore, dictation, clear) win', async () => {
+ const h = mount();
+ act(() => h.type('draft'));
+ await waitFor(() => expect(h.hostValue()).toBe('draft'));
+
+ act(() => h.hostSet('restored from history'));
+ await waitFor(() => expect(h.composerText()).toBe('restored from history'));
+
+ act(() => h.hostSet(''));
+ await waitFor(() => expect(h.composerText()).toBe(''));
+ });
- act(() => hostSet('restored from history'));
- await waitFor(() => expect(composer().value).toBe('restored from history'));
+ it('keeps typing that lands after a host write', async () => {
+ const h = mount();
+ act(() => h.hostSet('Hello'));
+ await waitFor(() => expect(h.composerText()).toBe('Hello'));
- act(() => hostSet(''));
- await waitFor(() => expect(composer().value).toBe(''));
+ act(() => h.type('Hello, world'));
+ await waitFor(() => expect(h.hostValue()).toBe('Hello, world'));
+ expect(h.composerText()).toBe('Hello, world');
});
});