11import type { ResolvedKeybindingsConfig , ScopedThreadRef } from "@t3tools/contracts" ;
22import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings" ;
3+ import * as Cause from "effect/Cause" ;
34import { AsyncResult } from "effect/reactivity" ;
45import { act } from "react" ;
56import { create , type ReactTestRenderer } from "react-test-renderer" ;
@@ -9,6 +10,7 @@ const state = vi.hoisted(() => ({
910 keybindings : [ ] as ResolvedKeybindingsConfig ,
1011 params : { } as Record < string , string > ,
1112 paletteOpen : false ,
13+ workspaceAvailable : true ,
1214 navigate : vi . fn ( ) ,
1315 openPreview : vi . fn ( ) ,
1416 setShortcuts : vi . fn ( async ( ) => undefined ) ,
@@ -36,7 +38,7 @@ vi.mock("../rpc/atomRegistry", () => ({
3638} ) ) ;
3739vi . mock ( "../state/entities" , ( ) => ( {
3840 readThreadShell : ( ) => ( { projectId : "project-1" , worktreePath : null } ) ,
39- readProject : ( ) => ( { workspaceRoot : "/work/project" } ) ,
41+ readProject : ( ) => ( state . workspaceAvailable ? { workspaceRoot : "/work/project" } : null ) ,
4042} ) ) ;
4143vi . mock ( "../composerDraftStore" , ( ) => {
4244 const store = {
@@ -106,6 +108,7 @@ beforeEach(() => {
106108 keybindings : DEFAULT_RESOLVED_KEYBINDINGS ,
107109 params : { } ,
108110 paletteOpen : false ,
111+ workspaceAvailable : true ,
109112 } ) ;
110113 state . navigate . mockResolvedValue ( undefined ) ;
111114 state . openPreview . mockReset ( ) ;
@@ -215,6 +218,65 @@ describe("root reopen shortcut", () => {
215218 expect ( useClosedViewStore . getState ( ) . entries ) . toEqual ( [ ] ) ;
216219 } ) ;
217220
221+ it . each ( [ "files" , "file" , "browser" ] as const ) (
222+ "keeps a failed %s restore retryable without blocking older history" ,
223+ async ( kind ) => {
224+ const store = useClosedViewStore . getState ( ) ;
225+ const older = store . remember ( {
226+ kind : "panel-tab" ,
227+ threadRef : ref ,
228+ surface : { kind : "diff" , id : "diff" } ,
229+ } ) ;
230+ const failed = store . remember (
231+ kind === "browser"
232+ ? { kind : "browser" , threadRef : ref , snapshot }
233+ : {
234+ kind : "panel-tab" ,
235+ threadRef : ref ,
236+ surface :
237+ kind === "files"
238+ ? { kind : "files" , id : "files" }
239+ : {
240+ kind : "file" ,
241+ id : "file:src/app.ts" ,
242+ relativePath : "src/app.ts" ,
243+ revealLine : null ,
244+ revealRequestId : 0 ,
245+ } ,
246+ } ,
247+ ) ;
248+ state . workspaceAvailable = false ;
249+ state . openPreview . mockResolvedValue ( AsyncResult . failure ( Cause . fail ( new Error ( "offline" ) ) ) ) ;
250+ await render ( ) ;
251+ await act ( ( ) => {
252+ press ( ) ;
253+ } ) ;
254+ expect ( useClosedViewStore . getState ( ) . entries . map ( ( entry ) => entry . id ) ) . toEqual ( [
255+ older ,
256+ failed ,
257+ ] ) ;
258+ expect ( state . navigate ) . not . toHaveBeenCalled ( ) ;
259+ await act ( ( ) => {
260+ menuAction ?.( "view.reopenClosed" ) ;
261+ } ) ;
262+ expect (
263+ selectThreadRightPanelState ( useRightPanelStore . getState ( ) . byThreadKey , ref ) . activeSurfaceId ,
264+ ) . toBe ( "diff" ) ;
265+ expect ( useClosedViewStore . getState ( ) . entries . map ( ( entry ) => entry . id ) ) . toEqual ( [ failed ] ) ;
266+ state . workspaceAvailable = true ;
267+ state . openPreview . mockResolvedValue ( AsyncResult . success ( { ...snapshot , tabId : "new-tab" } ) ) ;
268+ await act ( ( ) => {
269+ press ( ) ;
270+ } ) ;
271+ expect (
272+ selectThreadRightPanelState ( useRightPanelStore . getState ( ) . byThreadKey , ref ) . activeSurfaceId ,
273+ ) . toBe (
274+ kind === "browser" ? "browser:new-tab" : kind === "file" ? "file:src/app.ts" : "files" ,
275+ ) ;
276+ expect ( useClosedViewStore . getState ( ) . entries ) . toEqual ( [ ] ) ;
277+ } ,
278+ ) ;
279+
218280 it ( "forwards the chord to a focused desktop browser only while history exists" , async ( ) => {
219281 useClosedViewStore
220282 . getState ( )
0 commit comments