110 lines
3.9 KiB
Lua
Executable File
110 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 = [[
|
|
[17 Jun 2025, 13:57:41] -Warning ** [/dev/cdc-wdm0] requested auto mode but no MBIM QMUX support available
|
|
[/dev/cdc-wdm0] Successfully read information from the UIM:
|
|
Card result:
|
|
SW1: '0x90'
|
|
SW2: '0x00'
|
|
Read result:
|
|
98:10:14:30:72:52:03:97:70:78
|
|
]]
|
|
|
|
-- 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("89014103272530790787", 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) |