--[[--
阅读(Legado)Web 服务:地址归一、回包解码、请求形状与映射离线用例。
@module tests.moonsource_legado.source_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch, cast-type-mismatch, return-type-mismatch, cast-local-type
local Assert = require("support.assert")
local JSONStub = require("support.json_stub")
package.preload["json"] = function()
return { decode = JSONStub.decode, encode = JSONStub.encode }
end
local cfg = { server_address = "192.168.1.8:1122/" }
package.preload["utils.settings"] = function()
return { getSource = function() return cfg end }
end
local requests, replies = {}, {}
package.preload["http.request"] = function()
return {
get = function(url, opts, cb)
requests[#requests + 1] = { method = "GET", url = url, opts = opts }
cb(table.remove(replies, 1))
return { cancel = function() end }
end,
post = function(url, body, opts, cb)
requests[#requests + 1] = { method = "POST", url = url, body = body, opts = opts }
cb(table.remove(replies, 1))
return { cancel = function() end }
end,
}
end
package.loaded["moonsource_legado.client"] = nil
package.loaded["moonsource_legado.mapper"] = nil
local Client = require("moonsource_legado.client")
local Mapper = require("moonsource_legado.mapper")
-- 地址缺协议头补 http、去尾斜杠;没配置为 nil。
do
Assert.eq(Client.base(), "http://192.168.1.8:1122")
cfg.server_address = "https://legado.lan:1122"
Assert.eq(Client.base(), "https://legado.lan:1122")
cfg.server_address = ""
Assert.is_nil(Client.base())
cfg.server_address = "http://h:1122"
end
-- 书架:GET /getBookshelf,isSuccess=false 带出 errorMsg。
do
replies = { '{"isSuccess":true,"errorMsg":"","data":[{"bookUrl":"u1"}]}' }
local rows, err
Client.shelfAsync(function(v, e) rows, err = v, e end)
Assert.is_nil(err)
Assert.len(rows, 1)
Assert.matches(requests[1].url, "^http://h:1122/getBookshelf%?refresh=0$")
replies = { '{"isSuccess":false,"errorMsg":"没有权限","data":null}' }
rows, err = nil, nil
Client.shelfAsync(function(v, e) rows, err = v, e end)
Assert.is_nil(rows)
Assert.eq(err, "没有权限")
replies = { "" }
Client.shelfAsync(function(v, e) rows, err = v, e end)
Assert.eq(err, "响应无效")
end
-- 校验新库使用显式配置:地址与登录凭据不落盘,也不替换当前配置。
do
requests = {}
replies = {
'{"isSuccess":true,"data":{"accessToken":"probe-token"}}',
'{"isSuccess":true,"data":[{"bookUrl":"probe-book"}]}',
}
local rows, err
Client.shelfAsync(function(value, reason) rows, err = value, reason end, {
server_address = "https://probe.test/reader3", username = "probe-user", password = "probe-password",
})
Assert.is_nil(err)
Assert.eq(rows[1].bookUrl, "probe-book")
Assert.matches(requests[1].url, "^https://probe.test/reader3/login$")
Assert.matches(requests[1].body, '"username":"probe%-user"')
Assert.matches(requests[2].url, "^https://probe.test/reader3/getBookshelf%?")
Assert.matches(requests[2].url, "accessToken=probe%-token")
Assert.eq(Client.base(), "http://h:1122", "探测新库时当前客户端仍读旧库配置")
requests, replies = {}, { '{"isSuccess":true,"data":[]}' }
Client.shelfAsync(function() end)
Assert.matches(requests[1].url, "^http://h:1122/getBookshelf%?")
Assert.is_nil(requests[1].url:find("probe%-token"), "探测令牌不能污染旧库请求")
end
-- 正文:index 0 基、url 编码;空正文报错。
do
requests = {}
replies = { '{"isSuccess":true,"data":"第一段\\n第二段"}' }
local text
Client.contentAsync("https://a.b/c?d=1", 3, nil, function(v) text = v end)
Assert.eq(text, "第一段\n第二段")
Assert.matches(requests[1].url, "/getBookContent%?")
Assert.matches(requests[1].url, "index=3")
Assert.matches(requests[1].url, "url=https%%3A%%2F%%2Fa%.b%%2Fc%%3Fd%%3D1")
replies = { '{"isSuccess":true,"data":""}' }
local err
Client.contentAsync("u", 0, nil, function(_, e) err = e end)
Assert.eq(err, "阅读章节内容为空 #1")
end
-- 进度上报:JSON 体带 0 基章序号。
do
requests = {}
replies = { '{"isSuccess":true,"data":""}' }
local ok
Client.saveProgressAsync({ name = "书", author = "作者", bookUrl = "u1" },
{ index = 4, title = "第5章", offset = 12 }, function(v) ok = v end)
Assert.is_true(ok)
Assert.eq(requests[1].method, "POST")
Assert.matches(requests[1].url, "/saveBookProgress$")
Assert.eq(requests[1].opts.content_type, "application/json")
Assert.matches(requests[1].body, '"durChapterIndex":4')
Assert.matches(requests[1].body, '"url":"u1"')
end
-- 正文夹 :真图留成标签、svg 懒加载占位丢弃、src 尾部请求头剥掉,其余文字照旧转义
do
local html = Mapper.chapterHtml("章", 'a\n
\n'
.. '
', function(src) return src end)
Assert.eq(html, '章
\n
a<b>
\n
判空;保留实体文本、转换 SVG、规范化图片属性及 URL。 do local base = "http://h/book-assets/Text/chapter.xhtml" local function imageUrl(src, document_url) return require("utils.text").absoluteUrl(document_url, src) end local html = Mapper.assetHtml("章", '
", base, imageUrl)
Assert.matches(html, 'src="http://h/book%-assets/Images/%%E5%%B0%%81%%20%%E9%%9D%%A2%%20%%E5%%9B%%BE%.png"')
Assert.matches(html, 'src="http://h/book%-assets/Images/a%.png%?a=1&b=2"')
Assert.is_nil(html:find("