@@ -257,11 +257,67 @@ describe("Workspace Commands", () => {
257257 expect ( readResult . ok ) . toBe ( true ) ;
258258 expect ( readResult . data ) . toMatchObject ( {
259259 workspaceId,
260- sessionId : undefined ,
261260 } ) ;
261+ expect ( readResult . data ) . not . toHaveProperty ( "sessionId" ) ;
262262 expect ( ( readResult . data as { updatedAt : number } ) . updatedAt ) . toEqual ( expect . any ( Number ) ) ;
263263 } ) ;
264264
265+ it ( "preserves a session id that belongs to the workspace" , async ( ) => {
266+ const dir = join ( tmpdir ( ) , `workspace-target-session-test-${ Date . now ( ) } ` ) ;
267+ await mkdir ( dir ) ;
268+
269+ const openResult = await dispatch (
270+ {
271+ kind : "command" ,
272+ id : "open-workspace-target-session" ,
273+ op : "workspace.open" ,
274+ args : { path : dir } ,
275+ } ,
276+ ctx
277+ ) ;
278+
279+ expect ( openResult . ok ) . toBe ( true ) ;
280+ const workspaceId = ( openResult . data as { id : string } ) . id ;
281+ ctx . sessionMgr . get = vi . fn ( ( sessionId : string ) =>
282+ sessionId === "sess-123" ? { id : "sess-123" , workspaceId } : undefined
283+ ) as never ;
284+
285+ const writeResult = await dispatch (
286+ {
287+ kind : "command" ,
288+ id : "set-last-viewed-target-session" ,
289+ op : "workspace.lastViewedTarget.set" ,
290+ args : {
291+ workspaceId,
292+ sessionId : "sess-123" ,
293+ } ,
294+ } ,
295+ ctx
296+ ) ;
297+
298+ expect ( writeResult . ok ) . toBe ( true ) ;
299+ expect ( writeResult . data ) . toMatchObject ( {
300+ workspaceId,
301+ sessionId : "sess-123" ,
302+ } ) ;
303+
304+ const readResult = await dispatch (
305+ {
306+ kind : "command" ,
307+ id : "get-last-viewed-target-session" ,
308+ op : "workspace.lastViewedTarget.get" ,
309+ args : { } ,
310+ } ,
311+ ctx
312+ ) ;
313+
314+ expect ( readResult . ok ) . toBe ( true ) ;
315+ expect ( readResult . data ) . toMatchObject ( {
316+ workspaceId,
317+ sessionId : "sess-123" ,
318+ } ) ;
319+ } ) ;
320+
265321 it ( "drops an out-of-workspace session id while preserving the workspace target" , async ( ) => {
266322 const dir = join ( tmpdir ( ) , `workspace-target-mismatch-${ Date . now ( ) } ` ) ;
267323 await mkdir ( dir ) ;
@@ -315,5 +371,25 @@ describe("Workspace Commands", () => {
315371 expect ( result . ok ) . toBe ( false ) ;
316372 expect ( result . error ?. code ) . toBe ( "workspace_not_found" ) ;
317373 } ) ;
374+
375+ it ( "returns null when the stored last-viewed target is malformed" , async ( ) => {
376+ db . prepare ( "INSERT INTO user_settings (key, value) VALUES (?, ?)" ) . run (
377+ "workspace.lastViewedTarget" ,
378+ "{not-json"
379+ ) ;
380+
381+ const result = await dispatch (
382+ {
383+ kind : "command" ,
384+ id : "get-last-viewed-target-malformed" ,
385+ op : "workspace.lastViewedTarget.get" ,
386+ args : { } ,
387+ } ,
388+ ctx
389+ ) ;
390+
391+ expect ( result . ok ) . toBe ( true ) ;
392+ expect ( result . data ) . toBeNull ( ) ;
393+ } ) ;
318394 } ) ;
319395} ) ;
0 commit comments