@@ -9,11 +9,20 @@ import {
99} from "@t3tools/contracts" ;
1010import { useImperativeHandle , type RefObject } from "react" ;
1111import { describe , expect , it , vi } from "vite-plus/test" ;
12- import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences" ;
12+ import {
13+ ATTACHMENT_ONLY_BOOTSTRAP_PROMPT ,
14+ threadContextRecord ,
15+ } from "@t3tools/shared/composerContextReferences" ;
1316import { toastManager } from "../ui/toast" ;
1417
1518import { act , render } from "../../../test/render" ;
1619import { useComposerDraftStore , useComposerThreadDraft } from "../../composerDraftStore" ;
20+ import { formatInlineContextReference } from "../../lib/composerContextReferences" ;
21+ import {
22+ environmentContextRecord ,
23+ environmentContextReference ,
24+ threadContextReference ,
25+ } from "../../lib/composerContextRecords" ;
1726import { deriveComposerSendState } from "../ChatView.logic" ;
1827import { ComposerPrimaryActions } from "./ComposerPrimaryActions" ;
1928import { AppAtomRegistryProvider , appAtomRegistry } from "../../rpc/atomRegistry" ;
@@ -74,8 +83,8 @@ const RUN = RunId.make("run-1");
7483
7584type HookResult = ReturnType < typeof useQueuedRunEdit > ;
7685
77- function projection ( runs : ReadonlyArray < RunStatus > ) {
78- return { runs, messages : [ ] } as unknown as ThreadStructure ;
86+ function projection ( runs : ReadonlyArray < RunStatus > , messages : ReadonlyArray < unknown > ) {
87+ return { runs, messages } as unknown as ThreadStructure ;
7988}
8089
8190function setRuns ( ref : ScopedThreadRef , status : string ) {
@@ -86,13 +95,14 @@ function Harness(props: {
8695 readonly threadRef : ScopedThreadRef ;
8796 readonly runStatus : string ;
8897 readonly hookRef : RefObject < HookResult | null > ;
98+ readonly messages : ReadonlyArray < unknown > ;
8999} ) {
90100 const result = useQueuedRunEdit ( {
91101 environmentId : ENV ,
92102 routeThreadRef : props . threadRef ,
93103 threadDraftTarget : props . threadRef ,
94104 activeThreadId : props . threadRef . threadId ,
95- serverProjection : projection ( [ { id : RUN , status : props . runStatus } ] ) ,
105+ serverProjection : projection ( [ { id : RUN , status : props . runStatus } ] , props . messages ) ,
96106 promptRef : { current : "" } ,
97107 composerRef : { current : null } ,
98108 } ) ;
@@ -124,10 +134,15 @@ function Harness(props: {
124134 ) : null ;
125135}
126136
127- function view ( threadRef : ScopedThreadRef , runStatus : string , hook : { current : HookResult | null } ) {
137+ function view (
138+ threadRef : ScopedThreadRef ,
139+ runStatus : string ,
140+ hook : { current : HookResult | null } ,
141+ messages : ReadonlyArray < unknown > = [ ] ,
142+ ) {
128143 return (
129144 < AppAtomRegistryProvider >
130- < Harness threadRef = { threadRef } runStatus = { runStatus } hookRef = { hook } />
145+ < Harness threadRef = { threadRef } runStatus = { runStatus } hookRef = { hook } messages = { messages } />
131146 </ AppAtomRegistryProvider >
132147 ) ;
133148}
@@ -262,6 +277,56 @@ describe("useQueuedRunEdit", () => {
262277 ) ;
263278 } ) ;
264279
280+ it ( "opens an edit with only the thread and machine records its text still mentions" , async ( ) => {
281+ const { hook } = setup ( ) ;
282+ const thread = threadContextRecord ( THREAD_B , "Support Any File Uploads" ) ;
283+ const machine = environmentContextRecord ( ENV , "Build box" ) ;
284+ const unreferenced = threadContextRecord ( THREAD_A , "Removed earlier" ) ;
285+ const messageId = MessageId . make ( "message-1" ) ;
286+ const text = `wait for ${ formatInlineContextReference ( threadContextReference ( thread ) ) } on ${ formatInlineContextReference ( environmentContextReference ( machine ) ) } ` ;
287+ const messages = [
288+ { id : messageId , text, context : { version : 1 , records : [ thread , machine , unreferenced ] } } ,
289+ ] ;
290+ await using _screen = await render ( view ( THREAD_A , "queued" , hook , messages ) ) ;
291+
292+ await act ( ( ) => requireHook ( hook ) . beginEdit ( { runId : RUN , messageId, text, attachments : [ ] } ) ) ;
293+
294+ const draft = useComposerDraftStore . getState ( ) . getComposerDraft ( requireHook ( hook ) . draftTarget ) ;
295+ expect ( draft ?. prompt ) . toBe ( text ) ;
296+ expect ( draft ?. threadContexts ) . toEqual ( [ thread ] ) ;
297+ expect ( draft ?. environmentContexts ) . toEqual ( [ machine ] ) ;
298+ } ) ;
299+
300+ it ( "saves only the thread mentions still in the edited message" , async ( ) => {
301+ const { hook } = setup ( ) ;
302+ mocks . editQueuedRun . mockResolvedValue ( { _tag : "Success" } ) ;
303+ const kept = threadContextRecord ( THREAD_B , "Kept" ) ;
304+ const removed = threadContextRecord ( THREAD_A , "Removed" ) ;
305+ const messageId = MessageId . make ( "message-1" ) ;
306+ const text = [ kept , removed ]
307+ . map ( ( record ) => formatInlineContextReference ( threadContextReference ( record ) ) )
308+ . join ( " and " ) ;
309+ const messages = [ { id : messageId , text, context : { version : 1 , records : [ kept , removed ] } } ] ;
310+ await using _screen = await render ( view ( THREAD_A , "queued" , hook , messages ) ) ;
311+ await act ( ( ) => requireHook ( hook ) . beginEdit ( { runId : RUN , messageId, text, attachments : [ ] } ) ) ;
312+ const target = requireHook ( hook ) . draftTarget ;
313+ await act ( ( ) => useComposerDraftStore . getState ( ) . setThreadContexts ( target , [ kept ] ) ) ;
314+
315+ const draft = useComposerDraftStore . getState ( ) . getComposerDraft ( target ) ;
316+ await requireHook ( hook ) . saveEdit (
317+ { ...savedContent ( draft ?. prompt ?? "" ) , threadContexts : draft ?. threadContexts ?? [ ] } ,
318+ ( ) => { } ,
319+ ) ;
320+
321+ expect ( mocks . editQueuedRun ) . toHaveBeenCalledWith (
322+ expect . objectContaining ( {
323+ input : expect . objectContaining ( {
324+ edit : expect . objectContaining ( { context : { version : 1 , records : [ kept ] } } ) ,
325+ } ) ,
326+ } ) ,
327+ ) ;
328+ } ) ;
329+
265330 it ( "keeps an open edit when the user leaves the thread and comes back" , async ( ) => {
266331 const { hook } = setup ( ) ;
267332 await using screen = await render ( view ( THREAD_A , "queued" , hook ) ) ;
0 commit comments