@@ -11,6 +11,7 @@ import { filesystemEnvironment } from "../state/filesystem";
1111import { useRightPanelStore } from "../rightPanelStore" ;
1212import { setMarkdownTaskChecked } from "./files/filePreviewMode" ;
1313import { HostImageUrlContext } from "./chat/hostImageUrl" ;
14+ import type { ChatShellRun } from "./chat/chatShellRun" ;
1415
1516const { workspaceSearch, filesystemMetadata } = vi . hoisted ( ( ) => ( {
1617 workspaceSearch : vi . fn ( ) ,
@@ -44,6 +45,13 @@ vi.mock(import("../state/server"), async (original) => {
4445 serverEnvironment : { ...actual . serverEnvironment , configValueAtom : ( ( ) => noConfig ) as never } ,
4546 } ;
4647} ) ;
48+ vi . mock ( import ( "./ThreadTerminalDrawer" ) , async ( original ) => ( {
49+ ...( await original ( ) ) ,
50+ TerminalViewport : ( { terminalId } : { terminalId : string } ) => (
51+ < div data-testid = "run-terminal" > { terminalId } </ div >
52+ ) ,
53+ } ) ) ;
54+
4755vi . mock ( import ( "../state/session" ) , async ( original ) => {
4856 const { AuthStandardClientScopes } = await import ( "@t3tools/contracts" ) ;
4957 const granted = new Set < string > ( AuthStandardClientScopes ) ;
@@ -59,6 +67,26 @@ vi.mock(import("../remoteOpen"), async (original) => ({
5967 ...( await original ( ) ) ,
6068 useRemoteOpenResolution : ( ) => ( { state : { mode : "local-exec" } , isResolved : true } ) as never ,
6169} ) ) ;
70+ const terminalOutput = vi . hoisted ( ( ) => ( { text : "" } ) ) ;
71+ const sessionAtoms = vi . hoisted ( ( ) => new Map < string , unknown > ( ) ) ;
72+ vi . mock ( import ( "../state/terminalSessions" ) , async ( original ) => {
73+ const { Atom } = await import ( "effect/reactivity" ) ;
74+ const attachedTerminalSessionAtom = ( _environmentId : unknown , input : { terminalId : string } ) => {
75+ const cached = sessionAtoms . get ( input . terminalId ) ;
76+ if ( cached ) return cached ;
77+ const atom = Atom . make ( {
78+ output : { chunks : [ { data : terminalOutput . text } ] } ,
79+ error : null ,
80+ status : "running" ,
81+ } ) ;
82+ sessionAtoms . set ( input . terminalId , atom ) ;
83+ return atom ;
84+ } ;
85+ return {
86+ ...( await original ( ) ) ,
87+ attachedTerminalSessionAtom : attachedTerminalSessionAtom as never ,
88+ } ;
89+ } ) ;
6290const { renderDiagram } = vi . hoisted ( ( ) => ( {
6391 renderDiagram : vi . fn < ( source : string ) => Promise < string > | undefined > ( ) ,
6492} ) ) ;
@@ -112,6 +140,7 @@ it("keeps compact audio playback loaded across signed URL renewals", async () =>
112140
113141function setup ( ) {
114142 parsedSources . length = 0 ;
143+ sessionAtoms . clear ( ) ;
115144 settingsOverrides . mathRenderingEnabled = false ;
116145 renderDiagram . mockReset ( ) ;
117146 workspaceSearch . mockReset ( ) . mockResolvedValue ( AsyncResult . success ( { entries : [ ] } ) ) ;
@@ -180,6 +209,60 @@ it("reuses the parse of a remounted message that has stable extra plugins", asyn
180209 expect ( parsed ) . toHaveBeenCalledTimes ( 1 ) ;
181210} ) ;
182211
212+ describe ( "ChatMarkdown inline run output" , ( ) => {
213+ const runId = "run1" ;
214+ const run : ChatShellRun = {
215+ id : runId ,
216+ environmentId : EnvironmentId . make ( "env-test" ) ,
217+ threadId : ThreadId . make ( "thread-test" ) ,
218+ terminalId : "terminal-test" ,
219+ cwd : "/tmp/project" ,
220+ worktreePath : null ,
221+ } ;
222+ const runAndWait = async ( container : HTMLElement ) => {
223+ await act ( async ( ) =>
224+ buttonsLabelled ( container , "Run in terminal and send output to Claude" ) [ 0 ] ! . click ( ) ,
225+ ) ;
226+ } ;
227+
228+ it ( "shows the output and exit status under the code block" , async ( ) => {
229+ setup ( ) ;
230+ terminalOutput . text = `\u001b]7717;T3S;${ runId } \u0007hello\n\u001b]7717;T3E;${ runId } ;0\u0007` ;
231+ const onRunShellCommand = vi . fn ( ( _command : string , onStarted : ( run : ChatShellRun ) => void ) =>
232+ onStarted ( run ) ,
233+ ) ;
234+ await using screen = await render (
235+ < ChatMarkdown
236+ cwd = "/tmp/project"
237+ text = { "```bash\necho hello\n```" }
238+ onRunShellCommand = { onRunShellCommand }
239+ /> ,
240+ ) ;
241+ await runAndWait ( screen . container ) ;
242+ await expect . element ( screen . getByText ( "Exited with code 0" ) ) . toBeVisible ( ) ;
243+ await expect . element ( screen . getByText ( "hello" , { exact : true } ) ) . toBeVisible ( ) ;
244+ expect ( onRunShellCommand ) . toHaveBeenCalledExactlyOnceWith (
245+ "echo hello" ,
246+ expect . any ( Function ) ,
247+ undefined ,
248+ ) ;
249+ } ) ;
250+
251+ it ( "keeps a run without its end marker as running" , async ( ) => {
252+ setup ( ) ;
253+ terminalOutput . text = `\u001b]7717;T3S;${ runId } \u0007server starting\n` ;
254+ await using screen = await render (
255+ < ChatMarkdown
256+ cwd = "/tmp/project"
257+ text = { "```bash\nnpm run dev\n```" }
258+ onRunShellCommand = { ( _command , onStarted ) => onStarted ( run ) }
259+ /> ,
260+ ) ;
261+ await runAndWait ( screen . container ) ;
262+ await expect . element ( screen . getByText ( "Running…" ) ) . toBeVisible ( ) ;
263+ } ) ;
264+ } ) ;
265+
183266describe ( "ChatMarkdown favicon privacy" , ( ) => {
184267 it ( "suppresses private link images while preserving public links across updates" , async ( ) => {
185268 setup ( ) ;
@@ -202,7 +285,7 @@ describe("ChatMarkdown favicon privacy", () => {
202285} ) ;
203286
204287describe ( "ChatMarkdown streaming" , ( ) => {
205- it ( "runs only a complete single-line shell block after a click" , async ( ) => {
288+ it ( "runs only a complete shell block after a click" , async ( ) => {
206289 setup ( ) ;
207290 const onRunShellCommand = vi . fn ( ) ;
208291 const message = ( text : string , isStreaming = false ) => (
@@ -215,12 +298,17 @@ describe("ChatMarkdown streaming", () => {
215298 ) ;
216299 await using screen = await render ( message ( "```bash\necho hello\n```" , true ) ) ;
217300 const { container, rerender } = screen ;
218- const runButtons = ( ) => buttonsLabelled ( container , "Run in terminal" ) ;
301+ const runButtons = ( ) =>
302+ buttonsLabelled ( container , "Run in terminal and send output to Claude" ) ;
219303 expect ( runButtons ( ) ) . toHaveLength ( 0 ) ;
220304
221305 await rerender ( message ( "```bash\necho hello\n```" ) ) ;
222306 await act ( async ( ) => runButtons ( ) [ 0 ] ! . click ( ) ) ;
223- expect ( onRunShellCommand ) . toHaveBeenCalledExactlyOnceWith ( "echo hello" ) ;
307+ expect ( onRunShellCommand ) . toHaveBeenCalledExactlyOnceWith (
308+ "echo hello" ,
309+ expect . any ( Function ) ,
310+ undefined ,
311+ ) ;
224312
225313 for ( const text of [
226314 "~~~bash\necho tilde\n~~~" ,
@@ -234,10 +322,8 @@ describe("ChatMarkdown streaming", () => {
234322 }
235323
236324 for ( const text of [
237- "```bash\necho one\necho two\n```" ,
238325 "```typescript\necho hello\n```" ,
239326 "```bash\n\n```" ,
240- "```bash\necho hello\n\n```" ,
241327 "```bash\necho hello\\\n```" ,
242328 "```bash\necho safe \u202e#\n```" ,
243329 "```bash\necho incomplete" ,
0 commit comments