1+ local helpers = require (" tests.helpers.setup" )
2+ local integrations = require (" claudecode.integrations" )
3+
4+ describe (" snacks.explorer integration" , function ()
5+ before_each (function ()
6+ helpers .setup ()
7+ end )
8+
9+ after_each (function ()
10+ helpers .cleanup ()
11+ end )
12+
13+ describe (" _get_snacks_explorer_selection" , function ()
14+ it (" should return error when snacks.nvim is not available" , function ()
15+ -- Mock require to fail for snacks
16+ local original_require = _G .require
17+ _G .require = function (module )
18+ if module == " snacks" then
19+ error (" Module not found" )
20+ end
21+ return original_require (module )
22+ end
23+
24+ local files , err = integrations ._get_snacks_explorer_selection ()
25+ assert .are .same ({}, files )
26+ assert .equals (" snacks.nvim not available" , err )
27+
28+ -- Restore original require
29+ _G .require = original_require
30+ end )
31+
32+ it (" should return error when no explorer picker is active" , function ()
33+ -- Mock snacks module
34+ local mock_snacks = {
35+ picker = {
36+ get = function ()
37+ return {}
38+ end ,
39+ },
40+ }
41+
42+ package.loaded [" snacks" ] = mock_snacks
43+
44+ local files , err = integrations ._get_snacks_explorer_selection ()
45+ assert .are .same ({}, files )
46+ assert .equals (" No active snacks.explorer found" , err )
47+
48+ package.loaded [" snacks" ] = nil
49+ end )
50+
51+ it (" should return selected files from snacks.explorer" , function ()
52+ -- Mock snacks module with explorer picker
53+ local mock_explorer = {
54+ selected = function (self , opts )
55+ return {
56+ { file = " /path/to/file1.lua" },
57+ { file = " /path/to/file2.lua" },
58+ }
59+ end ,
60+ current = function (self , opts )
61+ return { file = " /path/to/current.lua" }
62+ end ,
63+ }
64+
65+ local mock_snacks = {
66+ picker = {
67+ get = function (opts )
68+ if opts .source == " explorer" then
69+ return { mock_explorer }
70+ end
71+ return {}
72+ end ,
73+ },
74+ }
75+
76+ package.loaded [" snacks" ] = mock_snacks
77+
78+ local files , err = integrations ._get_snacks_explorer_selection ()
79+ assert .is_nil (err )
80+ assert .are .same ({ " /path/to/file1.lua" , " /path/to/file2.lua" }, files )
81+
82+ package.loaded [" snacks" ] = nil
83+ end )
84+
85+ it (" should fall back to current file when no selection" , function ()
86+ -- Mock snacks module with explorer picker
87+ local mock_explorer = {
88+ selected = function (self , opts )
89+ return {}
90+ end ,
91+ current = function (self , opts )
92+ return { file = " /path/to/current.lua" }
93+ end ,
94+ }
95+
96+ local mock_snacks = {
97+ picker = {
98+ get = function (opts )
99+ if opts .source == " explorer" then
100+ return { mock_explorer }
101+ end
102+ return {}
103+ end ,
104+ },
105+ }
106+
107+ package.loaded [" snacks" ] = mock_snacks
108+
109+ local files , err = integrations ._get_snacks_explorer_selection ()
110+ assert .is_nil (err )
111+ assert .are .same ({ " /path/to/current.lua" }, files )
112+
113+ package.loaded [" snacks" ] = nil
114+ end )
115+
116+ it (" should handle empty file paths" , function ()
117+ -- Mock snacks module with empty file paths
118+ local mock_explorer = {
119+ selected = function (self , opts )
120+ return {
121+ { file = " " },
122+ { file = " /valid/path.lua" },
123+ { file = nil },
124+ }
125+ end ,
126+ current = function (self , opts )
127+ return { file = " " }
128+ end ,
129+ }
130+
131+ local mock_snacks = {
132+ picker = {
133+ get = function (opts )
134+ if opts .source == " explorer" then
135+ return { mock_explorer }
136+ end
137+ return {}
138+ end ,
139+ },
140+ }
141+
142+ package.loaded [" snacks" ] = mock_snacks
143+
144+ local files , err = integrations ._get_snacks_explorer_selection ()
145+ assert .is_nil (err )
146+ assert .are .same ({ " /valid/path.lua" }, files )
147+
148+ package.loaded [" snacks" ] = nil
149+ end )
150+
151+ it (" should try alternative fields for file path" , function ()
152+ -- Mock snacks module with different field names
153+ local mock_explorer = {
154+ selected = function (self , opts )
155+ return {
156+ { path = " /path/from/path.lua" },
157+ { item = { file = " /path/from/item.file.lua" } },
158+ { item = { path = " /path/from/item.path.lua" } },
159+ }
160+ end ,
161+ current = function (self , opts )
162+ return { path = " /current/from/path.lua" }
163+ end ,
164+ }
165+
166+ local mock_snacks = {
167+ picker = {
168+ get = function (opts )
169+ if opts .source == " explorer" then
170+ return { mock_explorer }
171+ end
172+ return {}
173+ end ,
174+ },
175+ }
176+
177+ package.loaded [" snacks" ] = mock_snacks
178+
179+ local files , err = integrations ._get_snacks_explorer_selection ()
180+ assert .is_nil (err )
181+ assert .are .same ({
182+ " /path/from/path.lua" ,
183+ " /path/from/item.file.lua" ,
184+ " /path/from/item.path.lua" ,
185+ }, files )
186+
187+ package.loaded [" snacks" ] = nil
188+ end )
189+ end )
190+
191+ describe (" get_selected_files_from_tree" , function ()
192+ it (" should detect snacks_picker_list filetype" , function ()
193+ vim .bo .filetype = " snacks_picker_list"
194+
195+ -- Mock snacks module
196+ local mock_explorer = {
197+ selected = function (self , opts )
198+ return {}
199+ end ,
200+ current = function (self , opts )
201+ return { file = " /test/file.lua" }
202+ end ,
203+ }
204+
205+ local mock_snacks = {
206+ picker = {
207+ get = function (opts )
208+ if opts .source == " explorer" then
209+ return { mock_explorer }
210+ end
211+ return {}
212+ end ,
213+ },
214+ }
215+
216+ package.loaded [" snacks" ] = mock_snacks
217+
218+ local files , err = integrations .get_selected_files_from_tree ()
219+ assert .is_nil (err )
220+ assert .are .same ({ " /test/file.lua" }, files )
221+
222+ package.loaded [" snacks" ] = nil
223+ end )
224+ end )
225+ end )
0 commit comments