local webview = require("@neyuki/webview")
local fs = require("@neyuki/fs")
local string = require("@neyuki/string")
function expectError(f: any, needle: string, message: string, ...: any)
local ok, err = try(f, ...)
assert(not ok and string.find(tostring(err), needle, 1, true) != nil, message .. ": " .. tostring(err))
end
-- ARGUMENTS (no display needed) --------------------------------------------
assert(type(webview.isAvailable()) == "boolean", "isAvailable reports a boolean")
expectError(webview.open, "title must be a string", "title is checked", { title = 1 })
expectError(webview.open, "url must be a string", "url is checked", { url = 1 })
expectError(webview.open, "width and height must be positive", "sizes are checked", { width = 0 })
expectError(webview.open, "assets.dir must be provided", "assets need a directory", { assets = {} })
expectError(webview.open, "options must be a table", "options are checked", "x")
local base = webview.assetUrl("app", "index.html")
assert(string.find(base, "localhost", 1, true) != nil, "asset addresses point at localhost")
assert(string.sub(base, -11) == "/index.html", "asset addresses end in the path")
assert(webview.assetUrl() == webview.assetUrl("app", "/"), "the scheme defaults to app")
-- LIVE VIEW (opt in with an empty tests/webview.local file) -----------------
--
-- Needs a display, the system web engine and, on macOS, the main thread.
if (not fs.exists("tests/webview.local")) then
print("webview: skipping the live checks (no tests/webview.local)")
return
end
assert(webview.isAvailable(), "tests/webview.local is present but no web engine is available")
local ui = require("@neyuki/ui")
local view = webview.open({
title = "Neyuki test",
visible = false,
html = "<script>window.neyuki.postMessage('hello')</script>",
})
assert(view:isOpen())
local got = nil
local deadline = os.clock() + 20
while (got == nil and os.clock() < deadline) do
for _, event in ui.poll(0.1) do
if (event.kind == "webview" and event.type == "message") then got = event.data end
end
end
assert(got == "hello", "the page's message arrives, got " .. tostring(got))
view:close()
assert(not view:isOpen())
expectError(view.eval, "webview is closed", "a closed view is unusable", view, "1")
print("webview: live checks passed")