@@ -275,11 +275,9 @@ project:
275275 assert . strictEqual ( result2 , 'notebook-2' ) ;
276276 } ) ;
277277
278- test ( 'should prioritize active notebook editor over stored selection' , ( ) => {
279- // Store a selection for the project
278+ test ( 'should prioritize stored selection over active editor' , ( ) => {
280279 manager . selectNotebookForProject ( 'project-123' , 'stored-notebook' ) ;
281280
282- // Mock the active notebook editor to return a different notebook
283281 const mockActiveNotebook = {
284282 notebookType : 'deepnote' ,
285283 metadata : {
@@ -294,14 +292,30 @@ project:
294292
295293 const result = serializer . findCurrentNotebookId ( 'project-123' ) ;
296294
297- // Should return the active editor's notebook, not the stored one
295+ assert . strictEqual ( result , 'stored-notebook' ) ;
296+ } ) ;
297+
298+ test ( 'should return active editor notebook when no stored selection exists' , ( ) => {
299+ const mockActiveNotebook = {
300+ notebookType : 'deepnote' ,
301+ metadata : {
302+ deepnoteProjectId : 'project-123' ,
303+ deepnoteNotebookId : 'active-editor-notebook'
304+ }
305+ } ;
306+
307+ when ( mockedVSCodeNamespaces . window . activeNotebookEditor ) . thenReturn ( {
308+ notebook : mockActiveNotebook
309+ } as any ) ;
310+
311+ const result = serializer . findCurrentNotebookId ( 'project-123' ) ;
312+
298313 assert . strictEqual ( result , 'active-editor-notebook' ) ;
299314 } ) ;
300315
301316 test ( 'should ignore active editor when project ID does not match' , ( ) => {
302317 manager . selectNotebookForProject ( 'project-123' , 'stored-notebook' ) ;
303318
304- // Mock active editor with a different project
305319 const mockActiveNotebook = {
306320 notebookType : 'deepnote' ,
307321 metadata : {
@@ -316,14 +330,12 @@ project:
316330
317331 const result = serializer . findCurrentNotebookId ( 'project-123' ) ;
318332
319- // Should fall back to stored selection since active editor is for different project
320333 assert . strictEqual ( result , 'stored-notebook' ) ;
321334 } ) ;
322335
323336 test ( 'should ignore active editor when notebook type is not deepnote' , ( ) => {
324337 manager . selectNotebookForProject ( 'project-123' , 'stored-notebook' ) ;
325338
326- // Mock active editor with non-deepnote notebook type
327339 const mockActiveNotebook = {
328340 notebookType : 'jupyter-notebook' ,
329341 metadata : {
@@ -338,19 +350,16 @@ project:
338350
339351 const result = serializer . findCurrentNotebookId ( 'project-123' ) ;
340352
341- // Should fall back to stored selection since active editor is not a deepnote notebook
342353 assert . strictEqual ( result , 'stored-notebook' ) ;
343354 } ) ;
344355
345356 test ( 'should ignore active editor when notebook ID is missing' , ( ) => {
346357 manager . selectNotebookForProject ( 'project-123' , 'stored-notebook' ) ;
347358
348- // Mock active editor without notebook ID in metadata
349359 const mockActiveNotebook = {
350360 notebookType : 'deepnote' ,
351361 metadata : {
352362 deepnoteProjectId : 'project-123'
353- // Missing deepnoteNotebookId
354363 }
355364 } ;
356365
@@ -360,9 +369,57 @@ project:
360369
361370 const result = serializer . findCurrentNotebookId ( 'project-123' ) ;
362371
363- // Should fall back to stored selection since active editor has no notebook ID
364372 assert . strictEqual ( result , 'stored-notebook' ) ;
365373 } ) ;
374+
375+ test ( 'switching notebooks: selecting a different notebook while one is open should return the new selection' , ( ) => {
376+ manager . selectNotebookForProject ( 'project-123' , 'notebook-A' ) ;
377+
378+ const mockActiveNotebook = {
379+ notebookType : 'deepnote' ,
380+ metadata : {
381+ deepnoteProjectId : 'project-123' ,
382+ deepnoteNotebookId : 'notebook-A'
383+ }
384+ } ;
385+
386+ when ( mockedVSCodeNamespaces . window . activeNotebookEditor ) . thenReturn ( {
387+ notebook : mockActiveNotebook
388+ } as any ) ;
389+
390+ assert . strictEqual ( serializer . findCurrentNotebookId ( 'project-123' ) , 'notebook-A' ) ;
391+
392+ manager . selectNotebookForProject ( 'project-123' , 'notebook-B' ) ;
393+
394+ assert . strictEqual (
395+ serializer . findCurrentNotebookId ( 'project-123' ) ,
396+ 'notebook-B' ,
397+ 'Should return the newly selected notebook, not the one currently in the active editor'
398+ ) ;
399+ } ) ;
400+
401+ test ( 'switching notebooks: rapidly switching between three notebooks should always return the latest selection' , ( ) => {
402+ const mockActiveNotebook = {
403+ notebookType : 'deepnote' ,
404+ metadata : {
405+ deepnoteProjectId : 'project-123' ,
406+ deepnoteNotebookId : 'notebook-1'
407+ }
408+ } ;
409+
410+ when ( mockedVSCodeNamespaces . window . activeNotebookEditor ) . thenReturn ( {
411+ notebook : mockActiveNotebook
412+ } as any ) ;
413+
414+ manager . selectNotebookForProject ( 'project-123' , 'notebook-1' ) ;
415+ assert . strictEqual ( serializer . findCurrentNotebookId ( 'project-123' ) , 'notebook-1' ) ;
416+
417+ manager . selectNotebookForProject ( 'project-123' , 'notebook-2' ) ;
418+ assert . strictEqual ( serializer . findCurrentNotebookId ( 'project-123' ) , 'notebook-2' ) ;
419+
420+ manager . selectNotebookForProject ( 'project-123' , 'notebook-3' ) ;
421+ assert . strictEqual ( serializer . findCurrentNotebookId ( 'project-123' ) , 'notebook-3' ) ;
422+ } ) ;
366423 } ) ;
367424
368425 suite ( 'component integration' , ( ) => {
0 commit comments