Files
skyweave/spec/utisl_spec.lua
T
wes 5db7a9a864
Skyweave Test, Lint, and Deploy Ritual / Test and Lint Code (push) Failing after 31s
Skyweave Test, Lint, and Deploy Ritual / Deploy to Roof Unit (push) Has been skipped
Cleaning up some comments, adding unit test for get_iccid()
2025-06-17 13:48:37 -04:00

105 lines
3.9 KiB
Lua
Executable File

-- This test script will verify the functions within skyweave/utils.lua
package.path = './skyweave/?.lua;' .. package.path
local utils = require("utils")
describe("Skyweave Utility Functions Module", function()
-- A nested 'describe' for the specific function we are testing.
describe("parse_query_string", function()
-- 'it' blocks contain the actual test cases.
it("should correctly parse a single key-value pair", function()
local query = "get_config=network_config"
local expected = { get_config = "network_config" }
assert.are.same(expected, utils.parse_query_string(query))
end)
it("should handle multiple parameters correctly", function()
local query = "ssid=MyWifi&encryption=psk2&channel=11"
local expected = { ssid = "MyWifi", encryption = "psk2", channel = "11" }
assert.are.same(expected, utils.parse_query_string(query))
end)
it("should handle URL-encoded characters", function()
local query = "ssid=My%20Awesome%20Wifi"
local expected = { ssid = "My Awesome Wifi" }
assert.are.same(expected, utils.parse_query_string(query))
end)
it("should return an empty table for a nil or empty query string", function()
assert.are.same({}, utils.parse_query_string(""))
assert.are.same({}, utils.parse_query_string(nil))
end)
end)
describe("Skyweave ICCID Retrieval", function()
-- This block runs before each test ('it' block).
-- It's used to set up our mock environment.
before_each(function()
-- Create a backup of the original command executor.
-- This ensures we do not permanently damage the original scripture.
_G.original_execute_command = utils.execute_command
end)
-- This block runs after each test.
-- It restores the original function, ensuring our tests do not interfere with each other.
after_each(function()
utils.execute_command = _G.original_execute_command
_G.original_execute_command = nil
end)
it("should correctly decode a valid qmicli output", function()
-- 1. Define the simulated raw output from a successful qmicli invocation.
local mock_qmicli_output = "QMI-Client: Synchronous call... \n" ..
"[/dev/cdc-wdm0] UIM transparent read result: \n" ..
"Read result:\n" ..
"\t'89:01:41:03:27:25:30:79:07:87:f9'\n"
-- 2. Mock the command executor to return our simulated data.
utils.execute_command = function(command)
assert.are.equal("qmicli -d /dev/cdc-wdm0 --uim-read-transparent=0x3F00,0x2FE2", command)
return mock_qmicli_output
end
-- 3. Execute the function under test.
local iccid, err = utils.get_sim_iccid()
-- 4. Assert the expected outcome.
assert.is_nil(err)
assert.are.equal("981014307252039770789f", iccid)
end)
it("should return an error on nil or empty command output", function()
-- 1. Mock the command executor to return nil.
utils.execute_command = function(command)
return nil
end
-- 2. Execute and assert.
local iccid, err = utils.get_sim_iccid()
assert.is_nil(iccid)
assert.are.equal("No output from qmicli command", err)
end)
it("should return an error if the raw data cannot be parsed", function()
-- 1. Mock a response that lacks the 'Read result:' scripture.
local malformed_output = "QMI-Client: Call failed: QMI protocol error"
utils.execute_command = function(command)
return malformed_output
end
-- 2. Execute and assert.
local iccid, err = utils.get_sim_iccid()
assert.is_nil(iccid)
assert.are.equal("Failed to parse raw ICCID data from qmicli output", err)
end)
end)
-- more 'describe' blocks here to test other functions in utils.lua
end)