github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/serializer/aria2_test.go (about) 1 package serializer 2 3 import ( 4 "testing" 5 6 model "github.com/cloudreve/Cloudreve/v3/models" 7 "github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc" 8 "github.com/cloudreve/Cloudreve/v3/pkg/cache" 9 "github.com/jinzhu/gorm" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestBuildFinishedListResponse(t *testing.T) { 14 asserts := assert.New(t) 15 tasks := []model.Download{ 16 { 17 StatusInfo: rpc.StatusInfo{ 18 Files: []rpc.FileInfo{ 19 { 20 Path: "/file/name.txt", 21 }, 22 }, 23 }, 24 Task: &model.Task{ 25 Model: gorm.Model{}, 26 Error: "error", 27 }, 28 }, 29 { 30 StatusInfo: rpc.StatusInfo{ 31 Files: []rpc.FileInfo{ 32 { 33 Path: "/file/name1.txt", 34 }, 35 { 36 Path: "/file/name2.txt", 37 }, 38 }, 39 }, 40 }, 41 } 42 tasks[1].StatusInfo.BitTorrent.Info.Name = "name.txt" 43 res := BuildFinishedListResponse(tasks).Data.([]FinishedListResponse) 44 asserts.Len(res, 2) 45 asserts.Equal("name.txt", res[1].Name) 46 asserts.Equal("name.txt", res[0].Name) 47 asserts.Equal("name.txt", res[0].Files[0].Path) 48 asserts.Equal("name1.txt", res[1].Files[0].Path) 49 asserts.Equal("name2.txt", res[1].Files[1].Path) 50 asserts.EqualValues(0, res[0].TaskStatus) 51 asserts.Equal("error", res[0].TaskError) 52 } 53 54 func TestBuildDownloadingResponse(t *testing.T) { 55 asserts := assert.New(t) 56 cache.Set("setting_aria2_interval", "10", 0) 57 tasks := []model.Download{ 58 { 59 StatusInfo: rpc.StatusInfo{ 60 Files: []rpc.FileInfo{ 61 { 62 Path: "/file/name.txt", 63 }, 64 }, 65 }, 66 Task: &model.Task{ 67 Model: gorm.Model{}, 68 Error: "error", 69 }, 70 }, 71 { 72 StatusInfo: rpc.StatusInfo{ 73 Files: []rpc.FileInfo{ 74 { 75 Path: "/file/name1.txt", 76 }, 77 { 78 Path: "/file/name2.txt", 79 }, 80 }, 81 }, 82 }, 83 } 84 tasks[1].StatusInfo.BitTorrent.Info.Name = "name.txt" 85 tasks[1].ID = 1 86 87 res := BuildDownloadingResponse(tasks, map[uint]int{1: 5}).Data.([]DownloadListResponse) 88 asserts.Len(res, 2) 89 asserts.Equal("name1.txt", res[1].Name) 90 asserts.Equal(5, res[1].UpdateInterval) 91 asserts.Equal("name.txt", res[0].Name) 92 asserts.Equal("name.txt", res[0].Info.Files[0].Path) 93 asserts.Equal("name1.txt", res[1].Info.Files[0].Path) 94 asserts.Equal("name2.txt", res[1].Info.Files[1].Path) 95 }