@@ -31,11 +31,33 @@ type TestWebKitWindow = Window & {
3131type MacosTitlebarControlsState = HTMLElement & {
3232 navCollapsed : boolean ;
3333 historyOnly : boolean ;
34+ newSessionDisabledReason ?: string ;
3435 onOpenPalette ?: ( ) => void ;
3536 onOpenNewSession ?: ( ) => void ;
3637 updateComplete : Promise < boolean > ;
3738} ;
3839
40+ function nativeSessionContext (
41+ navigate : ReturnType < typeof vi . fn > ,
42+ selectedId : string ,
43+ options : { methods ?: string [ ] ; scopes ?: string [ ] } = { } ,
44+ ) : ApplicationContext {
45+ return {
46+ navigate,
47+ agentSelection : { state : { selectedId } } ,
48+ gateway : {
49+ snapshot : {
50+ client : { } ,
51+ phase : "connected" ,
52+ hello : {
53+ auth : { role : "operator" , scopes : options . scopes ?? [ "operator.write" ] } ,
54+ features : { methods : options . methods ?? [ "sessions.create" ] } ,
55+ } ,
56+ } ,
57+ } ,
58+ } as unknown as ApplicationContext ;
59+ }
60+
3961afterEach ( ( ) => {
4062 resetAppHostTestGlobals ( ) ;
4163} ) ;
@@ -114,10 +136,7 @@ describe("OpenClaw native shell", () => {
114136 value : { openPalette, togglePalette } ,
115137 } ) ;
116138 shell . runtime = {
117- context : {
118- navigate,
119- agentSelection : { state : { selectedId : "agent/a" } } ,
120- } as unknown as ApplicationContext ,
139+ context : nativeSessionContext ( navigate , "agent/a" ) ,
121140 } ;
122141 shell . handleNativeOpenSearch ( ) ;
123142 const toggleEvent = new CustomEvent ( "openclaw:native-toggle-search" , { cancelable : true } ) ;
@@ -156,23 +175,57 @@ describe("OpenClaw native shell", () => {
156175 controls . remove ( ) ;
157176 } ) ;
158177
178+ it ( "disables the native titlebar new-session control with its access reason" , async ( ) => {
179+ const onOpenNewSession = vi . fn ( ) ;
180+ const controls = document . createElement (
181+ "openclaw-macos-titlebar-controls" ,
182+ ) as unknown as MacosTitlebarControlsState ;
183+ controls . navCollapsed = true ;
184+ controls . newSessionDisabledReason = "Operator write access is required." ;
185+ controls . onOpenNewSession = onOpenNewSession ;
186+ document . body . append ( controls ) ;
187+ await controls . updateComplete ;
188+
189+ const button = controls . querySelector < HTMLButtonElement > (
190+ ".macos-titlebar-controls__new-session" ,
191+ ) ;
192+ expect ( button ?. disabled ) . toBe ( true ) ;
193+ button ?. click ( ) ;
194+ expect ( onOpenNewSession ) . not . toHaveBeenCalled ( ) ;
195+ controls . remove ( ) ;
196+ } ) ;
197+
159198 it ( "retains a native new-session request until a context exists" , ( ) => {
160199 const navigate = vi . fn ( ) ;
161200 const shell = document . createElement ( "openclaw-app-shell" ) as unknown as ShellNavigationState ;
162201
163202 shell . handleNativeNewSession ( ) ;
164203
165204 shell . runtime = {
166- context : {
167- navigate,
168- agentSelection : { state : { selectedId : "main" } } ,
169- } as unknown as ApplicationContext ,
205+ context : nativeSessionContext ( navigate , "main" ) ,
170206 } ;
171207 shell . handleNativeNewSession ( ) ;
172208
173209 expect ( navigate ) . toHaveBeenCalledExactlyOnceWith ( "new-session" , { search : "?agent=main" } ) ;
174210 } ) ;
175211
212+ it ( "does not start a native session without exact sessions.create access" , ( ) => {
213+ for ( const options of [
214+ { methods : [ "sessions.list" ] , scopes : [ "operator.write" ] } ,
215+ { methods : [ "sessions.create" ] , scopes : [ "operator.read" ] } ,
216+ ] ) {
217+ const navigate = vi . fn ( ) ;
218+ const shell = document . createElement ( "openclaw-app-shell" ) as unknown as ShellNavigationState ;
219+ shell . runtime = {
220+ context : nativeSessionContext ( navigate , "main" , options ) ,
221+ } ;
222+
223+ shell . handleNativeNewSession ( ) ;
224+
225+ expect ( navigate ) . not . toHaveBeenCalled ( ) ;
226+ }
227+ } ) ;
228+
176229 it ( "navigates valid native Dashboard paths and acknowledges them" , ( ) => {
177230 const navigate = vi . fn ( ) ;
178231 const shell = document . createElement ( "openclaw-app-shell" ) as unknown as ShellNavigationState ;
0 commit comments