Files
MosisService/test-apps/com.mosis.sandbox-test/app.lua
omigamedev 02db0d849c add --screenshot-after option and fix test app document access
- Add --screenshot-after CLI option to capture screenshot after playback
- Fix sandbox test app to cache document reference for onclick handlers
- Add getDocument() helper that works with RmlUi Lua proxy objects

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-19 10:36:14 +01:00

250 lines
5.9 KiB
Lua

-- Sandbox Test App
-- Tests: timers, JSON, crypto, storage
local results = {}
local logCounter = 0
local currentDocument = nil -- Cache the document reference
-- Helper to get document (try multiple methods)
local function getDocument()
-- First try the cached document
if currentDocument then
return currentDocument
end
-- Try the global document
if document then
currentDocument = document
return document
end
-- Try to get from RmlUi context
if rmlui and rmlui.contexts and rmlui.contexts.main then
local ctx = rmlui.contexts.main
-- documents is a proxy, iterate to get first doc
if ctx.documents then
for i, doc in ipairs(ctx.documents) do
if doc then
currentDocument = doc
return currentDocument
end
end
end
end
return nil
end
local function log(msg)
logCounter = logCounter + 1
table.insert(results, string.format("[%03d] %s", logCounter, msg))
local doc = getDocument()
if doc then
local el = doc:GetElementById("results")
if el then
el.inner_rml = table.concat(results, "\n")
end
end
end
-- Navigation helper
function goBack()
if navigation and navigation.back then
navigation.back()
else
log("Navigation not available")
end
end
local function setStatus(id, status, success)
local doc = getDocument()
if not doc then
print("[LUA] ERROR: document not available!")
return
end
local el = doc:GetElementById(id)
if el then
if success then
el.inner_rml = "&#x2713; " .. status
else
el.inner_rml = "&#x2717; " .. status
end
else
print("[LUA] ERROR: element not found: " .. id)
end
end
-- Timer test
function testTimer()
print("[LUA] testTimer called!")
setStatus("timer-status", "Running...", true)
log("Starting timer test...")
local count = 0
local timerId = nil
timerId = setInterval(function()
count = count + 1
log("Timer tick: " .. count)
if count >= 3 then
clearInterval(timerId)
setStatus("timer-status", "Passed (3 ticks)", true)
log("Timer test complete!")
end
end, 1000)
log("Timer started with ID: " .. tostring(timerId))
end
-- JSON test
function testJSON()
log("Starting JSON test...")
local success = true
local msg = ""
-- Test encode
local data = {
name = "test",
value = 42,
nested = { a = 1, b = 2 }
}
local encoded = json.encode(data)
if encoded then
log("Encoded: " .. encoded)
else
success = false
msg = "encode failed"
end
-- Test decode
if success then
local decoded = json.decode(encoded)
if decoded and decoded.name == "test" and decoded.value == 42 then
log("Decoded successfully, name=" .. decoded.name)
else
success = false
msg = "decode failed"
end
end
if success then
setStatus("json-status", "Passed", true)
log("JSON test complete!")
else
setStatus("json-status", "Failed: " .. msg, false)
end
end
-- Crypto test
function testCrypto()
log("Starting crypto test...")
local success = true
local msg = ""
-- Test random bytes
local bytes = crypto.randomBytes(16)
if bytes and #bytes == 16 then
log("Random bytes (hex): " .. bytes:gsub(".", function(c)
return string.format("%02x", c:byte())
end))
else
success = false
msg = "randomBytes failed"
end
-- Test SHA256
if success then
local hash = crypto.sha256("hello world")
if hash then
log("SHA256: " .. hash:sub(1, 32) .. "...")
else
success = false
msg = "sha256 failed"
end
end
-- Test HMAC
if success then
local hmac = crypto.hmac("sha256", "secret", "message")
if hmac then
log("HMAC: " .. hmac:sub(1, 32) .. "...")
else
success = false
msg = "hmac failed"
end
end
if success then
setStatus("crypto-status", "Passed", true)
log("Crypto test complete!")
else
setStatus("crypto-status", "Failed: " .. msg, false)
end
end
-- Storage test
function testStorage()
log("Starting storage test...")
local success = true
local msg = ""
-- Test write (VirtualFS requires /data/, /cache/, /temp/, or /shared/ prefix)
local writeOk = fs.write("/data/test.txt", "Hello from sandbox!")
if writeOk then
log("Write successful")
else
success = false
msg = "write failed"
end
-- Test read
if success then
local content = fs.read("/data/test.txt")
if content == "Hello from sandbox!" then
log("Read successful: " .. content)
else
success = false
msg = "read mismatch"
end
end
-- Test list
if success then
local files = fs.list("/data")
if files then
log("Files in /data: " .. #files)
for _, f in ipairs(files) do
log(" - " .. f)
end
end
end
-- Test delete
if success then
local deleteOk = fs.delete("/data/test.txt")
if deleteOk then
log("Delete successful")
else
success = false
msg = "delete failed"
end
end
if success then
setStatus("storage-status", "Passed", true)
log("Storage test complete!")
else
setStatus("storage-status", "Failed: " .. msg, false)
end
end
-- Initialize: cache the document when script loads
if document then
currentDocument = document
print("[LUA] Document cached on load")
end
log("Sandbox Test App loaded")
log("Lua version: " .. (_VERSION or "unknown"))