[ST Edge] Unable to use ST libs outside of hub or integration test suite

This isn’t really a bug, but would be great to address for developers. I have some unit tests that I need to run outside of an actual integration test suite. These are for utility libraries which include some of the ST libraries. I am unable to use the ST libs because they are looking for _envlibrequire which doesn’t exist.
This is one example from socket/init.lua

local socket = _envlibrequire("socket")

This function is defined in integration_test/init.lua

local envlibs = {                  
  datastore = datastore,                 
  devices = mock_devices_api,     
  log = log,                                          
  socket = socket,                
  --ssl = socket.ssl, -- unused
  timer = timer,               
  --util.call_rpc_method = call_rpc_method.lua, -- unused
  --util.stringify_and_join = stringify_and_join, -- unused
  json = dkjson,                                                                                            
}    
                                                     
function _envlibrequire(module)                            
  return envlibs[module]                                   
end  

It would be great if the libs could be included in standalone scripts. I wasn’t even trying to run the code outside of ST. I just have a lib file of my own that is requiring cosock for one part and it won’t load it. I get this error.

lua: ...ocal/lib/smartthings/lua_libs-api_v0/socket/init.lua:12: attempt to call global '_envlibrequire' (a nil value)
stack traceback:
	...ocal/lib/smartthings/lua_libs-api_v0/socket/init.lua:12: in main chunk
	[C]: in function 'require'
	...al/lib/smartthings/lua_libs-api_v0/cosock/socket.lua:14: in main chunk
	[C]: in function 'require'
	...usr/local/lib/smartthings/lua_libs-api_v0/cosock.lua:1: in main chunk
	[C]: in function 'require'
	test.lua:1: in main chunk
	[C]: ?

Edit:

Here is my workaround in my own lib:

local socket
if _envlibrequire ~= nil then
    socket = require 'cosock.socket'
else
    socket = require 'socket'
end

You’re referring to using a luasocket, right?
Those are only available to use in the Hub (see this note). Using your workaround you can make the test successfully?

Yes. Workaround works for me. It just has to be in each place my libraries require a ST lib. I have to pick the correct variant of the file. To be clear, I wasn’t trying to actually run cosock outside of the hub, just require it in a package.

Can you provide more details about the use case, please?
I think I understand that you want to test each part individually (unit test) without having the complete structure of a driver, is that correct?

Sure. I wrote some lua libraries that I am using in my drivers. I am running unit tests on those libraries, not the driver. The libraries require cosock which in turn needs that _envlibrequire to be defined. That is the root of my issue. I probably just need to keep the workaround I have above to support both hub and local execution. This looks like it is just the cost of doing business both on and off the hub.

Is this somewhat related to your post here?

No. That was a request to grab libraries that are being used by the driver and package them. This is a request to be able to include the ST libraries in standalone scripts.

Hi!
Following up on this situation, the team mentioned that you could call cosock.run in your script and use cosock.socket.
Also, there’s cosock.asyncify which will transform the require "socket" into require "cosock.socket".

Have you tried those approaches?

I’ll give this a shot. This sounds like it would work.