@@ -75,3 +75,104 @@ func TestGetRandomFile(t *testing.T) {
7575 assert .Empty (t , f )
7676 })
7777}
78+
79+ func TestListingCache_Decache (t * testing.T ) {
80+ t .Parallel ()
81+
82+ testCases := map [string ]struct {
83+ path string
84+ cached []string
85+ expected []string
86+ }{
87+ "not in cache" : {
88+ path : "c" ,
89+ cached : []string {"a" , "b" },
90+ expected : []string {"a" , "b" },
91+ },
92+ "in cache" : {
93+ path : "c" ,
94+ cached : []string {"a" , "b" , "c" },
95+ expected : []string {"a" , "b" },
96+ },
97+ }
98+
99+ for name , testCase := range testCases {
100+ t .Run (name , func (t * testing.T ) {
101+ t .Parallel ()
102+
103+ basePath := "/a/b"
104+
105+ fullPath := filepath .Join (basePath , testCase .path )
106+ items := buildItemsFromPaths (basePath , testCase .cached )
107+
108+ lc := ListingCache {
109+ root : basePath ,
110+ cache : map [string ][]cacheEntry {
111+ basePath : items ,
112+ },
113+ }
114+
115+ lc .Decache (fullPath )
116+
117+ expected := buildItemsFromPaths (basePath , testCase .expected )
118+ assert .Equal (t , expected , lc .cache [basePath ])
119+ })
120+ }
121+ }
122+
123+ func buildItemsFromPaths (basePath string , input []string ) []cacheEntry {
124+ var items []cacheEntry
125+ for _ , path := range input {
126+ items = append (items , cacheEntry {
127+ path : filepath .Join (basePath , path ),
128+ isDir : false ,
129+ })
130+ }
131+ return items
132+ }
133+
134+ func TestRemoveByIndex (t * testing.T ) {
135+ t .Parallel ()
136+
137+ testCases := map [string ]struct {
138+ input []string
139+ index int
140+ expected []string
141+ }{
142+ "empty" : {},
143+ "only" : {
144+ input : []string {"one" },
145+ index : 0 ,
146+ expected : []string {},
147+ },
148+ "first" : {
149+ input : []string {"one" , "two" , "three" },
150+ index : 0 ,
151+ expected : []string {"two" , "three" },
152+ },
153+ "middle" : {
154+ input : []string {"one" , "two" , "three" },
155+ index : 1 ,
156+ expected : []string {"one" , "three" },
157+ },
158+ "last" : {
159+ input : []string {"one" , "two" , "three" },
160+ index : 2 ,
161+ expected : []string {"one" , "two" },
162+ },
163+ "missing" : {
164+ input : []string {"one" , "two" , "three" },
165+ index : - 1 ,
166+ expected : []string {"one" , "two" , "three" },
167+ },
168+ }
169+
170+ for name , testCase := range testCases {
171+ t .Run (name , func (t * testing.T ) {
172+ t .Parallel ()
173+
174+ actual := removeByIndex (testCase .input , testCase .index )
175+ assert .Equal (t , testCase .expected , actual )
176+ })
177+ }
178+ }
0 commit comments