Files
moonsource-changpei/tests/changpei/qrlogin_spec.lua
T
GarfieldandCopilot 0c6553d8d9 ✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
2026-11-16 12:08:03 +08:00

53 lines
2.1 KiB
Lua

--[[-- 长佩二维码帧、网络错误与关闭后的迟到回调。 @module tests.moonsource_changpei.qrlogin_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Theme = require("support.theme_stubs")
Theme.install()
local late, cancelled, sync = nil, false, true
local polling_timeout
package.preload["http.request"] = function()
return {
get = function(_url, _opts, cb)
if sync then cb("", nil, {}) else late = cb end
return { cancel = function() cancelled = true end }
end,
post = function(url, _body, opts, cb)
if url:match("/qrcode_polling$") then polling_timeout = opts.timeout end
late = cb
return { cancel = function() cancelled = true end }
end,
headerAll = function() return { "PHPSESSID=anonymous; Path=/; HttpOnly" } end,
header = function() return "0" end,
}
end
local QR = require("moonsource_changpei.qrlogin")
local value = ("a"):rep(200)
Assert.eq(QR.decode(QR.frame(value))[1], value, "refresh token 长度用 protobuf varint")
Assert.eq(QR.decode(QR.frame("ok") .. "\128\0\0\0\0")[1], "ok", "不把 gRPC trailer 当 protobuf")
Assert.is_nil(next(QR.decode("\0\0\0\0\9short")), "截断帧不是成功回包")
local called = false
local login = QR:new(function() called = true end)
login:start()
Assert.not_nil(late)
login:cancel()
Assert.is_true(cancelled, "同步匿名会话不能覆盖获取二维码的句柄")
late(QR.frame("token"), nil, {})
Assert.is_nil(Theme.last("qr"))
Assert.is_false(called)
login = QR:new(function() called = true end)
login:_poll(login.generation, "token", os.time())
Assert.is_true(polling_timeout > 30, "官网长轮询必须能等到约 30 秒的待扫码响应")
late(nil, "network unavailable", {})
Assert.eq(Theme.last("confirm").text, "network unavailable", "网络错误不冒充二维码过期")
sync, cancelled = false, false
login = QR:new(function() called = true end)
login:start()
login:cancel()
late("", nil, {})
Assert.is_true(cancelled)
Assert.is_false(called)