@@ -4,12 +4,14 @@ import (
44 "fmt"
55 "os"
66 "path/filepath"
7+ "runtime"
78 "testing"
89 "time"
910
1011 tea "github.com/charmbracelet/bubbletea"
1112 "github.com/stretchr/testify/assert"
1213 "github.com/stretchr/testify/require"
14+ variable "github.com/yorukot/superfile/src/config"
1315 "github.com/yorukot/superfile/src/internal/common"
1416 "github.com/yorukot/superfile/src/internal/ui/notify"
1517 "github.com/yorukot/superfile/src/internal/utils"
@@ -187,3 +189,104 @@ func TestFileRename(t *testing.T) {
187189 actualTest (true )
188190 })
189191}
192+
193+ func createDirectories (dirs ... string ) error {
194+ for _ , dir := range dirs {
195+ if err := os .MkdirAll (dir , 0755 ); err != nil {
196+ return fmt .Errorf ("failed to create directory %s: %w" , dir , err )
197+ }
198+ }
199+ return nil
200+ }
201+
202+ func initTrash () error {
203+ return createDirectories (
204+ variable .CustomTrashDirectory ,
205+ variable .CustomTrashDirectoryFiles ,
206+ variable .CustomTrashDirectoryInfo ,
207+ )
208+ }
209+
210+ func getTrashPath (src string ) string {
211+ home , _ := os .UserHomeDir ()
212+ src = filepath .Base (src )
213+ switch runtime .GOOS {
214+ case utils .OsDarwin :
215+ return filepath .Join (variable .DarwinTrashDirectory , src )
216+ default :
217+ return filepath .Join (home , ".local" , "share" , "Trash" , "files" , src )
218+ }
219+ }
220+
221+ func TestFileDelete (t * testing.T ) {
222+ curTestDir := t .TempDir ()
223+ file1 := filepath .Join (curTestDir , "file1.txt" )
224+ file2 := filepath .Join (curTestDir , "file2.txt" )
225+
226+ setupFilesWithData (t , []byte ("f1" ), file1 )
227+ setupFilesWithData (t , []byte ("f2" ), file2 )
228+
229+ t .Run ("move to trash" , func (t * testing.T ) {
230+ m := defaultTestModel (curTestDir )
231+ err := initTrash ()
232+ if err == nil {
233+ m .hasTrash = true
234+ } else {
235+ fmt .Println ("Unable to create trash directories." )
236+ }
237+ p := NewTestTeaProgWithEventLoop (t , m )
238+ idx := findItemIndexInPanelByLocation (m .getFocusedFilePanel (), file1 )
239+ require .NotEqual (t , - 1 , idx , "%s should be found in panel" , file1 )
240+ m .getFocusedFilePanel ().cursor = idx
241+
242+ p .SendKey (common .Hotkeys .DeleteItems [0 ])
243+
244+ assert .Eventually (t , func () bool {
245+ return m .notifyModel .IsOpen ()
246+ }, time .Second , 10 * time .Microsecond , "Notify model never opened" )
247+
248+ p .Send (tea.KeyMsg {Type : tea .KeyRight })
249+
250+ assert .Eventually (t , func () bool {
251+ _ , err1 := os .Stat (file1 )
252+ trashFile := getTrashPath (file1 )
253+ _ , errTrash := os .Stat (trashFile )
254+ if runtime .GOOS == utils .OsWindows {
255+ return err1 != nil && os .IsNotExist (err1 )
256+ }
257+ return err1 != nil && os .IsNotExist (err1 ) && errTrash == nil
258+ }, time .Second , 10 * time .Millisecond , "File never moved to trash." )
259+ })
260+
261+ t .Run ("move to trash" , func (t * testing.T ) {
262+ m := defaultTestModel (curTestDir )
263+ err := initTrash ()
264+ if err == nil {
265+ m .hasTrash = true
266+ } else {
267+ fmt .Println ("Unable to create trash directories." )
268+ }
269+ p := NewTestTeaProgWithEventLoop (t , m )
270+ idx := findItemIndexInPanelByLocation (m .getFocusedFilePanel (), file2 )
271+ require .NotEqual (t , - 1 , idx , "%s should be found in panel" , file2 )
272+ m .getFocusedFilePanel ().cursor = idx
273+
274+ p .SendKey (common .Hotkeys .PermanentlyDeleteItems [0 ])
275+
276+ assert .Eventually (t , func () bool {
277+ return m .notifyModel .IsOpen ()
278+ }, time .Second , 10 * time .Microsecond , "Notify model never opened" )
279+
280+ p .Send (tea.KeyMsg {Type : tea .KeyRight })
281+
282+ assert .Eventually (t , func () bool {
283+ _ , err1 := os .Stat (file2 )
284+ trashFile := getTrashPath (file2 )
285+ _ , errTrash := os .Stat (trashFile )
286+ if runtime .GOOS == utils .OsWindows {
287+ return err1 != nil && os .IsNotExist (err1 )
288+ }
289+ return err1 != nil && os .IsNotExist (err1 ) && errTrash != nil && os .IsNotExist (err1 )
290+ }, time .Second , 10 * time .Millisecond , "File never moved to trash." )
291+ })
292+ }
0 commit comments