11import { scopeThreadRef } from "@t3tools/client-runtime/environment" ;
2- import { type EnvironmentId , ThreadId } from "@t3tools/contracts" ;
2+ import { type EnvironmentId , RunId , ThreadId } from "@t3tools/contracts" ;
33import { beforeEach , describe , expect , it } from "vite-plus/test" ;
44
55import { useClosedViewStore } from "./closedViewStore" ;
@@ -29,6 +29,16 @@ beforeEach(() => {
2929 } ) ;
3030} ) ;
3131
32+ async function reloadRightPanelStore ( ) {
33+ const { storage, name } = useRightPanelStore . persist . getOptions ( ) ;
34+ const persisted = await storage ! . getItem ( name ! ) ;
35+ expect ( persisted ) . not . toBeNull ( ) ;
36+ useRightPanelStore . setState ( useRightPanelStore . getInitialState ( ) ) ;
37+ await storage ! . setItem ( name ! , persisted ! ) ;
38+ await useRightPanelStore . persist . rehydrate ( ) ;
39+ expect ( useRightPanelStore . persist . hasHydrated ( ) ) . toBe ( true ) ;
40+ }
41+
3242describe ( "rightPanelStore" , ( ) => {
3343 it ( "records single and bulk tab closes, newest first" , ( ) => {
3444 const store = useRightPanelStore . getState ( ) ;
@@ -208,7 +218,7 @@ describe("rightPanelStore", () => {
208218 } ,
209219 ) ;
210220
211- const completedDiff = { id : "diff" , kind : "diff" } as const ;
221+ const completedDiff = { kind : "diff" , turnId : RunId . make ( "turn-1" ) } as const ;
212222 const linkedPullRequest = pullRequestSurface ( {
213223 projectId : "project-a" ,
214224 repository : "pingdotgg/t3code" ,
@@ -281,9 +291,15 @@ describe("rightPanelStore", () => {
281291 expect (
282292 store . openProactive ( refA , { id : "pull-requests" , kind : "pull-requests" } , revision ) ,
283293 ) . toBe ( false ) ;
284- expect ( selectThreadRightPanelState ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe (
285- chosen ,
294+ const { isOpen, activeSurfaceId, surfaces } = selectThreadRightPanelState (
295+ useRightPanelStore . getState ( ) . byThreadKey ,
296+ refA ,
286297 ) ;
298+ expect ( { isOpen, activeSurfaceId, surfaces } ) . toEqual ( {
299+ isOpen : chosen . isOpen ,
300+ activeSurfaceId : chosen . activeSurfaceId ,
301+ surfaces : chosen . surfaces ,
302+ } ) ;
287303 } ) ;
288304
289305 it ( "allows automatic panels for a later turn after a manual choice" , ( ) => {
@@ -293,7 +309,127 @@ describe("rightPanelStore", () => {
293309 expect ( store . openProactive ( refA , completedDiff , firstTurnRevision ) ) . toBe ( false ) ;
294310
295311 const nextTurnRevision = store . getUserActionRevision ( refA ) ;
296- expect ( store . openProactive ( refA , completedDiff , nextTurnRevision ) ) . toBe ( true ) ;
312+ expect (
313+ store . openProactive (
314+ refA ,
315+ { ...completedDiff , turnId : RunId . make ( "turn-2" ) } ,
316+ nextTurnRevision ,
317+ ) ,
318+ ) . toBe ( true ) ;
319+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "diff" ) ;
320+ } ) ;
321+
322+ it ( "offers each completed diff once across dismissal, panel changes, and reload" , async ( ) => {
323+ const store = useRightPanelStore . getState ( ) ;
324+ expect ( store . openProactive ( refA , completedDiff , store . getUserActionRevision ( refA ) ) ) . toBe ( true ) ;
325+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "diff" ) ;
326+
327+ store . close ( refA ) ;
328+ await reloadRightPanelStore ( ) ;
329+ expect ( store . openProactive ( refA , completedDiff , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
330+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBeNull ( ) ;
331+
332+ store . open ( refA , "files" ) ;
333+ store . openFile ( refA , "src/app.ts" ) ;
334+ await reloadRightPanelStore ( ) ;
335+ expect ( store . openProactive ( refA , completedDiff , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
336+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "file" ) ;
337+
338+ store . closeAllSurfaces ( refA ) ;
339+ await reloadRightPanelStore ( ) ;
340+ expect ( store . openProactive ( refA , completedDiff , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
341+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBeNull ( ) ;
342+
343+ const otherEnvironment = scopeThreadRef ( "env-2" as EnvironmentId , refA . threadId ) ;
344+ for ( const ref of [ refB , otherEnvironment ] ) {
345+ expect ( store . openProactive ( ref , completedDiff , store . getUserActionRevision ( ref ) ) ) . toBe ( true ) ;
346+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , ref ) ) . toBe ( "diff" ) ;
347+ }
348+
349+ const nextTurn = { ...completedDiff , turnId : RunId . make ( "turn-2" ) } ;
350+ expect ( store . openProactive ( refA , nextTurn , store . getUserActionRevision ( refA ) ) ) . toBe ( true ) ;
351+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "diff" ) ;
352+
353+ const completedWhileOpen = { ...completedDiff , turnId : RunId . make ( "turn-3" ) } ;
354+ const existingDiff = selectActiveRightPanelSurface (
355+ useRightPanelStore . getState ( ) . byThreadKey ,
356+ refA ,
357+ ) ;
358+ expect ( store . openProactive ( refA , completedWhileOpen , store . getUserActionRevision ( refA ) ) ) . toBe (
359+ false ,
360+ ) ;
361+ expect ( selectActiveRightPanelSurface ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toEqual (
362+ existingDiff ,
363+ ) ;
364+ store . closeSurface ( refA , "diff" ) ;
365+ await reloadRightPanelStore ( ) ;
366+ expect ( store . openProactive ( refA , completedWhileOpen , store . getUserActionRevision ( refA ) ) ) . toBe (
367+ false ,
368+ ) ;
369+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBeNull ( ) ;
370+
371+ // A later storage migration must preserve the dismissal, just like an ordinary reload.
372+ const { storage, name, version } = useRightPanelStore . persist . getOptions ( ) ;
373+ const persisted = await storage ! . getItem ( name ! ) ;
374+ await storage ! . setItem ( name ! , { ...persisted ! , version : version ! - 1 } ) ;
375+ await reloadRightPanelStore ( ) ;
376+ expect ( store . openProactive ( refA , completedWhileOpen , store . getUserActionRevision ( refA ) ) ) . toBe (
377+ false ,
378+ ) ;
379+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBeNull ( ) ;
380+
381+ expect (
382+ store . openProactive (
383+ refA ,
384+ { ...completedDiff , turnId : RunId . make ( "turn-4" ) } ,
385+ store . getUserActionRevision ( refA ) ,
386+ ) ,
387+ ) . toBe ( true ) ;
388+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "diff" ) ;
389+ store . closeSurface ( refA , "diff" ) ;
390+ store . open ( refA , "diff" ) ;
391+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "diff" ) ;
392+ } ) ;
393+
394+ it ( "does not retry declined diff offers after manual choices or PR priority clear" , async ( ) => {
395+ const store = useRightPanelStore . getState ( ) ;
396+ const revision = store . getUserActionRevision ( refA ) ;
397+ store . openFile ( refA , "src/app.ts" ) ;
398+ expect ( store . openProactive ( refA , completedDiff , revision ) ) . toBe ( false ) ;
399+ await reloadRightPanelStore ( ) ;
400+ expect ( store . openProactive ( refA , completedDiff , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
401+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "file" ) ;
402+
403+ store . closeAllSurfaces ( refA ) ;
404+ expect ( store . openProactive ( refA , completedDiff , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
405+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBeNull ( ) ;
406+
407+ const nextTurn = { ...completedDiff , turnId : RunId . make ( "turn-2" ) } ;
408+ store . openPullRequest ( refA , linkedPullRequest ) ;
409+ expect ( store . openProactive ( refA , nextTurn , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
410+ expect ( selectActiveRightPanelSurface ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toEqual (
411+ linkedPullRequest ,
412+ ) ;
413+ expect (
414+ selectThreadRightPanelState ( useRightPanelStore . getState ( ) . byThreadKey , refA ) . surfaces ,
415+ ) . toEqual ( [ linkedPullRequest ] ) ;
416+ await reloadRightPanelStore ( ) ;
417+ expect ( store . openProactive ( refA , nextTurn , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
418+ expect ( selectActiveRightPanelSurface ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toEqual (
419+ linkedPullRequest ,
420+ ) ;
421+ store . closeSurface ( refA , linkedPullRequest . id ) ;
422+ await reloadRightPanelStore ( ) ;
423+ expect ( store . openProactive ( refA , nextTurn , store . getUserActionRevision ( refA ) ) ) . toBe ( false ) ;
424+ expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBeNull ( ) ;
425+
426+ expect (
427+ store . openProactive (
428+ refA ,
429+ { ...completedDiff , turnId : RunId . make ( "turn-3" ) } ,
430+ store . getUserActionRevision ( refA ) ,
431+ ) ,
432+ ) . toBe ( true ) ;
297433 expect ( selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , refA ) ) . toBe ( "diff" ) ;
298434 } ) ;
299435
0 commit comments