11import { scopeThreadRef } from "@t3tools/client-runtime/environment" ;
2+ import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime" ;
23import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell" ;
34import {
45 EnvironmentId ,
@@ -9,13 +10,45 @@ import {
910} from "@t3tools/contracts" ;
1011import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings" ;
1112import { createModelSelection } from "@t3tools/shared/model" ;
13+ import * as Cause from "effect/Cause" ;
14+ import { AsyncResult , Atom } from "effect/reactivity" ;
1215import { expect , it , vi } from "vite-plus/test" ;
1316import { page } from "vite-plus/test/browser" ;
1417
18+ const mocks = vi . hoisted ( ( ) => ( {
19+ setModelSelection : null as ( ( value : unknown ) => Promise < AtomCommandResult < void , unknown > > ) | null ,
20+ } ) ) ;
21+
22+ vi . mock ( import ( "../../state/use-orchestration-command" ) , async ( importOriginal ) => ( {
23+ ...( await importOriginal ( ) ) ,
24+ useOrchestrationCommand : ( ( ) => ( value : unknown ) => mocks . setModelSelection ?.( value ) ) as never ,
25+ } ) ) ;
26+
27+ vi . mock ( import ( "../../state/threads" ) , async ( original ) => {
28+ const actual = await original ( ) ;
29+ return {
30+ ...actual ,
31+ environmentThreadShells : {
32+ ...actual . environmentThreadShells ,
33+ threadShellAtom : ( ( ref : { threadId : string } ) => shellAtom ( ref . threadId ) ) as never ,
34+ } ,
35+ } ;
36+ } ) ;
37+
1538import { render } from "../../../test/render" ;
1639import { deriveEffectiveComposerModelState , useComposerDraftStore } from "../../composerDraftStore" ;
40+ import { AppAtomRegistryProvider , appAtomRegistry } from "../../rpc/atomRegistry" ;
1741import { useComposerSelection } from "./useComposerSelection" ;
1842
43+ // Kept alive so a shell set before the hook mounts is still there when it saves a pick.
44+ const shellAtom = Atom . family ( ( _threadId : string ) => Atom . make < unknown > ( null ) . pipe ( Atom . keepAlive ) ) ;
45+
46+ function setup ( ) {
47+ const setModelSelection = vi . fn < ( value : unknown ) => Promise < AtomCommandResult < void , unknown > > > ( ) ;
48+ mocks . setModelSelection = setModelSelection ;
49+ return { setModelSelection } ;
50+ }
51+
1952const environmentId = EnvironmentId . make ( "environment-local" ) ;
2053const cursor = ProviderInstanceId . make ( "cursor" ) ;
2154const stored = createModelSelection ( cursor , "claude-opus-5-5" , [ { id : "effort" , value : "medium" } ] ) ;
@@ -44,6 +77,7 @@ function cursorThread(id: string) {
4477function Picker ( props : {
4578 thread : EnvironmentThreadShell ;
4679 runtime : Parameters < typeof useComposerSelection > [ 0 ] [ "activeRuntime" ] ;
80+ model ?: string ;
4781} ) {
4882 const { onProviderModelSelectStable } = useComposerSelection ( {
4983 environmentId,
@@ -65,16 +99,19 @@ function Picker(props: {
6599 scheduleComposerFocus : ( ) => { } ,
66100 } ) ;
67101 return (
68- < button type = "button" onClick = { ( ) => onProviderModelSelectStable ( cursor , stored . model ) } >
69- Pick the stored model
102+ < button
103+ type = "button"
104+ onClick = { ( ) => onProviderModelSelectStable ( cursor , props . model ?? stored . model ) }
105+ >
106+ Pick the model
70107 </ button >
71108 ) ;
72109}
73110
74111it ( "keeps a Cursor thread's stored model and options when the user re-picks its unavailable row" , async ( ) => {
75112 const thread = cursorThread ( "thread-cursor" ) ;
76113 await using _screen = await render ( < Picker thread = { thread } runtime = { null } /> ) ;
77- await page . getByRole ( "button" , { name : "Pick the stored model" } ) . click ( ) ;
114+ await page . getByRole ( "button" , { name : "Pick the model" } ) . click ( ) ;
78115
79116 await vi . waitFor ( ( ) => {
80117 const draft = useComposerDraftStore
@@ -90,7 +127,7 @@ it("switches back to the thread's stored model while the session runs on another
90127 typeof useComposerSelection
91128 > [ 0 ] [ "activeRuntime" ] ;
92129 await using _screen = await render ( < Picker thread = { thread } runtime = { runtime } /> ) ;
93- await page . getByRole ( "button" , { name : "Pick the stored model" } ) . click ( ) ;
130+ await page . getByRole ( "button" , { name : "Pick the model" } ) . click ( ) ;
94131
95132 await vi . waitFor ( ( ) => {
96133 const draft = useComposerDraftStore
@@ -110,3 +147,29 @@ it("switches back to the thread's stored model while the session runs on another
110147 ) . toEqual ( { selectedModel : stored . model , modelOptions : { [ cursor ] : stored . options } } ) ;
111148 } ) ;
112149} ) ;
150+
151+ it ( "saves a re-picked model again when its first save failed" , async ( ) => {
152+ const { setModelSelection } = setup ( ) ;
153+ setModelSelection
154+ . mockResolvedValueOnce ( AsyncResult . failure ( Cause . fail ( new Error ( "offline" ) ) ) )
155+ . mockResolvedValue ( AsyncResult . success ( undefined ) ) ;
156+ const thread = cursorThread ( "thread-cursor-retry" ) ;
157+ appAtomRegistry . set ( shellAtom ( thread . id ) , thread ) ;
158+ await using _screen = await render (
159+ < AppAtomRegistryProvider >
160+ < Picker thread = { thread } runtime = { null } model = "auto-smart" />
161+ </ AppAtomRegistryProvider > ,
162+ ) ;
163+ const pick = page . getByRole ( "button" , { name : "Pick the model" } ) ;
164+ await pick . click ( ) ;
165+ await vi . waitFor ( ( ) => expect ( setModelSelection ) . toHaveBeenCalledTimes ( 1 ) ) ;
166+ await pick . click ( ) ;
167+
168+ const autoSave = expect . objectContaining ( {
169+ input : {
170+ threadId : thread . id ,
171+ modelSelection : expect . objectContaining ( { instanceId : cursor , model : "auto-smart" } ) ,
172+ } ,
173+ } ) ;
174+ await vi . waitFor ( ( ) => expect ( setModelSelection . mock . calls ) . toEqual ( [ [ autoSave ] , [ autoSave ] ] ) ) ;
175+ } ) ;
0 commit comments