@@ -3,7 +3,7 @@ import * as Cause from "effect/Cause";
33import { afterEach , describe , expect , it , vi } from "vite-plus/test" ;
44
55import { toastManager } from "../components/ui/toast" ;
6- import { showUndoToast } from "./showUndoToast" ;
6+ import { showUndoToast , undoLatestThreadAction } from "./showUndoToast" ;
77import * as ThreadUndo from "./threadUndo" ;
88
99afterEach ( ( ) => vi . restoreAllMocks ( ) ) ;
@@ -93,3 +93,35 @@ describe("showUndoToast", () => {
9393 expect ( add ) . toHaveBeenCalledOnce ( ) ;
9494 } ) ;
9595} ) ;
96+
97+ describe ( "undoLatestThreadAction" , ( ) => {
98+ it ( "runs the newest live Undo once and then reports nothing to undo" , ( ) => {
99+ const { options } = setup ( ) ;
100+ const older = vi . fn ( async ( ) => AsyncResult . success ( undefined ) ) ;
101+ const newer = vi . fn ( async ( ) => AsyncResult . success ( undefined ) ) ;
102+ showUndoToast ( { ...options , undo : older , claim : ThreadUndo . begin ( "settle" , "env/a" ) } ) ;
103+ showUndoToast ( { ...options , undo : newer , claim : ThreadUndo . begin ( "snooze" , "env/b" ) } ) ;
104+ expect ( undoLatestThreadAction ( ) ) . toBe ( true ) ;
105+ expect ( newer ) . toHaveBeenCalledOnce ( ) ;
106+ expect ( older ) . not . toHaveBeenCalled ( ) ;
107+ expect ( undoLatestThreadAction ( ) ) . toBe ( true ) ;
108+ expect ( older ) . toHaveBeenCalledOnce ( ) ;
109+ expect ( undoLatestThreadAction ( ) ) . toBe ( false ) ;
110+ } ) ;
111+
112+ it ( "skips a superseded toast and a closed toast" , ( ) => {
113+ const { add, options } = setup ( ) ;
114+ const superseded = vi . fn ( async ( ) => AsyncResult . success ( undefined ) ) ;
115+ const closed = vi . fn ( async ( ) => AsyncResult . success ( undefined ) ) ;
116+ const live = vi . fn ( async ( ) => AsyncResult . success ( undefined ) ) ;
117+ showUndoToast ( { ...options , undo : live , claim : ThreadUndo . begin ( "archive" , "env/live" ) } ) ;
118+ showUndoToast ( { ...options , undo : closed , claim : ThreadUndo . begin ( "archive" , "env/closed" ) } ) ;
119+ add . mock . calls [ 1 ] ?. [ 0 ] . onClose ?.( ) ;
120+ showUndoToast ( { ...options , undo : superseded , claim : ThreadUndo . begin ( "pin" , "env/stale" ) } ) ;
121+ ThreadUndo . invalidate ( "pin" , "env/stale" ) ;
122+ expect ( undoLatestThreadAction ( ) ) . toBe ( true ) ;
123+ expect ( superseded ) . not . toHaveBeenCalled ( ) ;
124+ expect ( closed ) . not . toHaveBeenCalled ( ) ;
125+ expect ( live ) . toHaveBeenCalledOnce ( ) ;
126+ } ) ;
127+ } ) ;
0 commit comments