@@ -1140,3 +1140,158 @@ describe('add_visual_selection API', function()
11401140 vim .notify = original_notify
11411141 end )
11421142end )
1143+
1144+ describe (' build_inline_selection_text' , function ()
1145+ local context
1146+ local BaseContext
1147+ local util
1148+
1149+ before_each (function ()
1150+ context = require (' opencode.context' )
1151+ BaseContext = require (' opencode.context.base_context' )
1152+ util = require (' opencode.util' )
1153+ end )
1154+
1155+ it (' should return formatted inline text for a visual selection' , function ()
1156+ local original_is_buf_a_file = util .is_buf_a_file
1157+ local original_get_current_selection = BaseContext .get_current_selection
1158+ local original_get_current_file_for_selection = BaseContext .get_current_file_for_selection
1159+ local original_get_current_buf = vim .api .nvim_get_current_buf
1160+
1161+ util .is_buf_a_file = function ()
1162+ return true
1163+ end
1164+ BaseContext .get_current_selection = function ()
1165+ return { text = ' function foo()\n return 42\n end' , lines = ' 10, 12' }
1166+ end
1167+ BaseContext .get_current_file_for_selection = function ()
1168+ return { path = ' /tmp/test.lua' , name = ' test.lua' , extension = ' lua' }
1169+ end
1170+ vim .api .nvim_get_current_buf = function ()
1171+ return 5
1172+ end
1173+
1174+ -- Mock vim.bo to return a filetype
1175+ local original_bo = vim .bo
1176+ vim .bo = setmetatable ({}, {
1177+ __index = function (_ , buf )
1178+ return { filetype = ' lua' }
1179+ end ,
1180+ })
1181+
1182+ local text = context .build_inline_selection_text ()
1183+
1184+ assert .is_not_nil (text )
1185+ assert .is_not_nil (text :match (' Here is some code from /tmp/test%.lua:' ))
1186+ assert .is_not_nil (text :match (' ```lua' ))
1187+ assert .is_not_nil (text :match (' function foo%(%)' ))
1188+ assert .is_not_nil (text :match (' ```$' ))
1189+
1190+ util .is_buf_a_file = original_is_buf_a_file
1191+ BaseContext .get_current_selection = original_get_current_selection
1192+ BaseContext .get_current_file_for_selection = original_get_current_file_for_selection
1193+ vim .api .nvim_get_current_buf = original_get_current_buf
1194+ vim .bo = original_bo
1195+ end )
1196+
1197+ it (' should return nil and notify when not a file buffer' , function ()
1198+ local original_is_buf_a_file = util .is_buf_a_file
1199+ local original_get_current_buf = vim .api .nvim_get_current_buf
1200+
1201+ util .is_buf_a_file = function ()
1202+ return false
1203+ end
1204+ vim .api .nvim_get_current_buf = function ()
1205+ return 10
1206+ end
1207+
1208+ local original_notify = vim .notify
1209+ local notifications = {}
1210+ vim .notify = function (msg , level )
1211+ table.insert (notifications , { msg = msg , level = level })
1212+ end
1213+
1214+ local text = context .build_inline_selection_text ()
1215+
1216+ assert .is_nil (text )
1217+ assert .equal (1 , # notifications )
1218+ assert .equal (' Cannot add selection: not a file buffer' , notifications [1 ].msg )
1219+ assert .equal (vim .log .levels .WARN , notifications [1 ].level )
1220+
1221+ util .is_buf_a_file = original_is_buf_a_file
1222+ vim .api .nvim_get_current_buf = original_get_current_buf
1223+ vim .notify = original_notify
1224+ end )
1225+
1226+ it (' should return nil and notify when no visual selection found' , function ()
1227+ local original_is_buf_a_file = util .is_buf_a_file
1228+ local original_get_current_selection = BaseContext .get_current_selection
1229+ local original_get_current_buf = vim .api .nvim_get_current_buf
1230+
1231+ util .is_buf_a_file = function ()
1232+ return true
1233+ end
1234+ BaseContext .get_current_selection = function ()
1235+ return nil
1236+ end
1237+ vim .api .nvim_get_current_buf = function ()
1238+ return 11
1239+ end
1240+
1241+ local original_notify = vim .notify
1242+ local notifications = {}
1243+ vim .notify = function (msg , level )
1244+ table.insert (notifications , { msg = msg , level = level })
1245+ end
1246+
1247+ local text = context .build_inline_selection_text ()
1248+
1249+ assert .is_nil (text )
1250+ assert .equal (1 , # notifications )
1251+ assert .equal (' No visual selection found' , notifications [1 ].msg )
1252+ assert .equal (vim .log .levels .WARN , notifications [1 ].level )
1253+
1254+ util .is_buf_a_file = original_is_buf_a_file
1255+ BaseContext .get_current_selection = original_get_current_selection
1256+ vim .api .nvim_get_current_buf = original_get_current_buf
1257+ vim .notify = original_notify
1258+ end )
1259+
1260+ it (' should include the filetype in the code fence' , function ()
1261+ local original_is_buf_a_file = util .is_buf_a_file
1262+ local original_get_current_selection = BaseContext .get_current_selection
1263+ local original_get_current_file_for_selection = BaseContext .get_current_file_for_selection
1264+ local original_get_current_buf = vim .api .nvim_get_current_buf
1265+
1266+ util .is_buf_a_file = function ()
1267+ return true
1268+ end
1269+ BaseContext .get_current_selection = function ()
1270+ return { text = ' const x = 1' , lines = ' 1, 1' }
1271+ end
1272+ BaseContext .get_current_file_for_selection = function ()
1273+ return { path = ' /tmp/app.ts' , name = ' app.ts' , extension = ' ts' }
1274+ end
1275+ vim .api .nvim_get_current_buf = function ()
1276+ return 6
1277+ end
1278+
1279+ local original_bo = vim .bo
1280+ vim .bo = setmetatable ({}, {
1281+ __index = function (_ , buf )
1282+ return { filetype = ' typescript' }
1283+ end ,
1284+ })
1285+
1286+ local text = context .build_inline_selection_text ()
1287+
1288+ assert .is_not_nil (text )
1289+ assert .is_not_nil (text :match (' ```typescript' ))
1290+
1291+ util .is_buf_a_file = original_is_buf_a_file
1292+ BaseContext .get_current_selection = original_get_current_selection
1293+ BaseContext .get_current_file_for_selection = original_get_current_file_for_selection
1294+ vim .api .nvim_get_current_buf = original_get_current_buf
1295+ vim .bo = original_bo
1296+ end )
1297+ end )
0 commit comments