@@ -40,6 +40,7 @@ describe('image_handler', function()
4040 wsl_distro = 0 ,
4141 added_files = {},
4242 notifications = {},
43+ existing_files = {},
4344 }
4445
4546 vim .fn = setmetatable ({
@@ -50,8 +51,13 @@ describe('image_handler', function()
5051 return mocks .temp_dir
5152 end ,
5253 mkdir = function () end ,
53- getfsize = function (_ )
54- return 100 -- Simulating non-empty file
54+ getfsize = function (path )
55+ for _ , f in ipairs (mocks .existing_files ) do
56+ if f == path then
57+ return 100
58+ end
59+ end
60+ return 0
5561 end ,
5662 has = function (feature )
5763 if feature == ' win32' then
@@ -124,6 +130,7 @@ describe('image_handler', function()
124130 it (' handles Darwin clipboard with osascript' , function ()
125131 mocks .os_name = ' Darwin'
126132 mocks .executable [' osascript' ] = 1
133+ table.insert (mocks .existing_files , ' /tmp/test_dir/pasted_image_20240101_120000.png' )
127134
128135 local success = image_handler .paste_image_from_clipboard ()
129136
@@ -139,6 +146,7 @@ describe('image_handler', function()
139146 mocks .os_name = ' Linux'
140147 mocks .executable [' wl-paste' ] = 1
141148 mocks .executable [' xclip' ] = 0
149+ table.insert (mocks .existing_files , ' /tmp/test_dir/pasted_image_20240101_120000.png' )
142150
143151 local success = image_handler .paste_image_from_clipboard ()
144152
@@ -151,6 +159,7 @@ describe('image_handler', function()
151159 mocks .os_name = ' Linux'
152160 mocks .executable [' wl-paste' ] = 0
153161 mocks .executable [' xclip' ] = 1
162+ table.insert (mocks .existing_files , ' /tmp/test_dir/pasted_image_20240101_120000.png' )
154163
155164 local success = image_handler .paste_image_from_clipboard ()
156165
@@ -162,6 +171,7 @@ describe('image_handler', function()
162171 it (' handles Windows clipboard' , function ()
163172 mocks .os_name = ' Windows_NT'
164173 mocks .executable [' powershell.exe' ] = 1
174+ table.insert (mocks .existing_files , ' /tmp/test_dir/pasted_image_20240101_120000.png' )
165175
166176 local success = image_handler .paste_image_from_clipboard ()
167177
@@ -176,6 +186,7 @@ describe('image_handler', function()
176186 mocks .os_name = ' Linux'
177187 mocks .wsl_distro = 1
178188 mocks .executable [' powershell.exe' ] = 1
189+ table.insert (mocks .existing_files , ' /tmp/test_dir/pasted_image_20240101_120000.png' )
179190
180191 local success = image_handler .paste_image_from_clipboard ()
181192
@@ -195,6 +206,7 @@ describe('image_handler', function()
195206 mocks .os_name = ' Darwin'
196207 mocks .executable [' osascript' ] = 0 -- Force failure of system tool
197208 mocks .clipboard_content = ' data:image/png;base64,fakebasedata'
209+ table.insert (mocks .existing_files , ' /tmp/test_dir/pasted_image_20240101_120000.png' )
198210
199211 local success = image_handler .paste_image_from_clipboard ()
200212
@@ -228,4 +240,38 @@ describe('image_handler', function()
228240 assert .is_false (success )
229241 assert .equals (0 , # mocks .added_files )
230242 end )
243+
244+ it (' restores image path when file exists and name is valid' , function ()
245+ mocks .os_name = ' Darwin'
246+ mocks .executable [' osascript' ] = 1
247+ -- Initialize cached_temp_dir
248+ image_handler .paste_image_from_clipboard ()
249+
250+ local img_name = ' pasted_image_test.png'
251+ local expected_path = mocks .temp_dir .. ' /' .. img_name
252+ table.insert (mocks .existing_files , expected_path )
253+
254+ local restored_path = image_handler .restore_img_path (img_name )
255+ assert .equals (expected_path , restored_path )
256+ end )
257+
258+ it (' returns nil when restoring image path with invalid name' , function ()
259+ mocks .os_name = ' Darwin'
260+ mocks .executable [' osascript' ] = 1
261+ image_handler .paste_image_from_clipboard ()
262+
263+ local restored_path = image_handler .restore_img_path (' not_a_pasted_image.png' )
264+ assert .is_nil (restored_path )
265+ end )
266+
267+ it (' returns nil when restoring image path and file does not exist' , function ()
268+ mocks .os_name = ' Darwin'
269+ mocks .executable [' osascript' ] = 1
270+ image_handler .paste_image_from_clipboard ()
271+
272+ local img_name = ' pasted_image_missing.png'
273+
274+ local restored_path = image_handler .restore_img_path (img_name )
275+ assert .is_nil (restored_path )
276+ end )
231277end )
0 commit comments