Files
moonsource-iread/tests/iread/iread_spec.lua
T
2026-11-27 08:08:01 +08:00

206 lines
8.0 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
iRead:个人 Token 请求形状、回包解码、空书架验登录态、表单摊平与映射离线用例。
@module tests.moonsource_iread.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")
local json_null = function() end
package.preload["json"] = function()
return {
decode = setmetatable({ simple = {} }, { __call = function(_decoder, raw) return JSONStub.decode(raw) end }),
encode = JSONStub.encode,
util = { null = json_null },
}
end
local cfg = { token = "a1b2" }
package.preload["utils.settings"] = function()
return { getSource = function() return cfg end }
end
local requests, replies = {}, {}
package.preload["http.request"] = function()
return {
post = function(url, body, opts, cb)
requests[#requests + 1] = { url = url, body = body, opts = opts }
local reply = table.remove(replies, 1)
if type(reply) == "table" then cb(reply.body, nil, reply) else cb(reply) end
return { cancel = function() end }
end,
get = function(url, opts, cb)
requests[#requests + 1] = { url = url, opts = opts }
local reply = table.remove(replies, 1)
if type(reply) == "table" then cb(reply.body, nil, reply) else cb(reply) end
return { cancel = function() end }
end,
header = function(res) return res and res.cookie end,
headerAll = function(res) return res and res.cookie and { res.cookie } or {} end,
}
end
for _idx, name in ipairs({ "moonsource_iread.client", "moonsource_iread.mapper", "source.auth" }) do
package.loaded[name] = nil
end
local Client = require("moonsource_iread.client")
local Mapper = require("moonsource_iread.mapper")
local Auth = require("source.auth")
-- 书架:POST 表单 + Bearer Token;有书直接返回。
do
replies = { '{"code":200,"data":[{"name":"书"}]}' }
local rows
Client.shelfAsync(function(v) rows = v end)
Assert.len(rows, 1)
Assert.len(requests, 1)
Assert.eq(requests[1].url, "https://iread.ankio.net/book/list")
Assert.eq(requests[1].opts.headers.Authorization, "Bearer a1b2")
Assert.eq(requests[1].opts.content_type, "application/x-www-form-urlencoded")
end
-- 空书架:服务端对无效 Token 也回 200 + 空数组,必须再验登录态,不能当成「书架被清空」。
do
requests = {}
replies = { '{"code":200,"data":[]}', '{"code":200,"data":{"nickname":"我"}}' }
local rows
Client.shelfAsync(function(v) rows = v end)
Assert.len(rows, 0)
Assert.eq(requests[2].url, "https://iread.ankio.net/user/home")
Assert.not_nil(requests[2].body)
replies = { '{"code":200,"data":[]}', '{"code":401,"data":"x"}' }
local err
rows = nil
Client.shelfAsync(function(v, e) rows, err = v, e end)
Assert.is_nil(rows)
Assert.matches(err, "iread%.ankio%.net/#/system/token")
Assert.is_true(Auth.expired("iread"))
end
-- 非 200:带出 msg;游客也可读取接口,但不能修改远端书架或进度。
do
requests = {}
replies = { '{"code":400,"msg":"书籍数据错误"}' }
local err
Client.saveProgressAsync({ id = 1, name = "书" }, { index = 0, offset = 0 }, function(_ok, e) err = e end)
Assert.eq(err, "书籍数据错误")
cfg.token = nil
requests = {}
Client.saveProgressAsync({ id = 1, name = "书" }, { index = 0, offset = 0 }, function(_ok, e) err = e end)
Assert.len(requests, 0)
cfg.token = "a1b2"
end
-- Cookie 登录(浏览器扩展):只发 Cookie,不带 Bearer;Token 优先。
do
cfg.token, cfg.cookie = nil, "NovaSession=abc"
requests = {}
replies = { '{"code":200,"data":[{"name":"书"}]}' }
Client.shelfAsync(function() end)
Assert.eq(requests[1].opts.headers.Cookie, "NovaSession=abc")
Assert.is_nil(requests[1].opts.headers.Authorization)
cfg.token = "a1b2"
requests = {}
replies = { '{"code":200,"data":[{"name":"书"}]}' }
Client.shelfAsync(function() end)
Assert.eq(requests[1].opts.headers.Authorization, "Bearer a1b2")
cfg.cookie = nil
local Setting = require("moonsource_iread.setting")
Assert.eq(Setting.fromPayload({ cookies = {
{ domain = ".ankio.net", name = "b", value = "2" },
{ domain = "iread.ankio.net", name = "a", value = "1" },
{ domain = "evil.com", name = "x", value = "9" },
{ domain = "iread.ankio.net", name = "bad", value = "v;w" },
} }), "a=1; b=2")
end
-- 进度:只传 id + 位置字段;不再发整行。
do
requests = {}
replies = {
'{"code":200,"data":{}}',
'{"code":200,"data":[{"name":"书","durChapterIndex":4,"durChapterPos":12}]}',
}
local ok
Client.saveProgressAsync({ id = 42, name = "书" },
{ index = 4, title = "第5章", offset = 12 }, function(v) ok = v end)
Assert.is_true(ok)
local body = requests[1].body
Assert.matches(requests[1].url, "/book/process$")
Assert.matches(body, "id=42")
Assert.matches(body, "durChapterIndex=4")
Assert.matches(body, "durChapterPos=12")
Assert.matches(body, "durChapterTitle=%%E7%%AC%%AC5%%E7%%AB%%A0")
-- 不再发原行的其他字段(sourceId、variable 等)
Assert.is_nil(body:find("sourceId", 1, true))
Assert.is_nil(body:find("variable", 1, true))
end
-- 正文:只传 id 和 index;空正文报错。
do
requests = {}
replies = { '{"code":200,"data":{"title":"一","content":"第一段\\n第二段"}}' }
local text
Client.contentAsync({ id = 10 }, 3, function(v) text = v end)
Assert.eq(text, "第一段\n第二段")
Assert.matches(requests[1].url, "/book/chapter$")
Assert.matches(requests[1].body, "id=10")
Assert.matches(requests[1].body, "index=3")
replies = { '{"code":200,"data":{"content":""}}' }
local err
Client.contentAsync({ id = 10 }, 0, function(_, e) err = e end)
Assert.eq(err, "iRead 章节内容为空")
end
-- 映射:书名是身份,自定义封面优先、// 补协议;没书名的行丢弃。
do
local books = Mapper.shelfList({
{ name = "书", author = "作者", coverUrl = "//img/a.jpg", kind = "玄幻" },
{ name = "二", customCoverUrl = "http://img/b.jpg", coverUrl = "x" },
{ author = "无名" },
})
Assert.len(books, 2)
Assert.eq(books[1].stable_id, "书")
Assert.eq(books[1].cover, "https://img/a.jpg")
Assert.eq(books[2].cover, "http://img/b.jpg")
end
-- 阅读位置:第 0 章 0 偏移且没时间戳算没读过;目录 uid 是 0 基序号,不存章节原 JSON(开章不用)。
do
Assert.is_nil(Mapper.readPos({ durChapterIndex = 0, durChapterPos = 0, durChapterTime = 0 }))
Assert.eq(Mapper.readPos({ durChapterIndex = 0, durChapterPos = 0, durChapterTime = 5 }).index, 0)
Assert.eq(Mapper.readPos({ durChapterIndex = 2, durChapterTitle = "第3章" }).title, "第3章")
local toc = Mapper.toc({ { title = "序", url = "a" }, { title = "一", url = "b" } })
Assert.eq(toc[2].idx, 2)
Assert.eq(toc[2].uid, "1")
Assert.is_nil(toc[2].raw)
end
-- 正文带 HTML 标签时剥掉,纯文本原样。
do
Assert.eq(Mapper.text("<p>甲</p><p>乙</p>"), "甲\n乙")
Assert.eq(Mapper.text("1 < 2"), "1 < 2")
end
-- 搜索:提交与轮询共用服务端会话 Cookie,否则取结果 500。
do
requests = {}
replies = {
{ body = '{"code":200,"data":{"task":"search/1/x","total":2}}', cookie = "sid=abc; path=/; HttpOnly" },
'{"code":200,"data":{"remaining":0,"result":[{"name":"书"}]}}',
}
local rows
Client.searchAsync("斗罗", function(v) rows = v end)
Assert.len(rows, 1)
Assert.eq(requests[1].url, "https://iread.ankio.net/book/search")
Assert.matches(requests[1].body, "bookname=")
Assert.eq(requests[2].url, "https://iread.ankio.net/book/search/result")
Assert.eq(requests[2].opts.headers.Cookie, "sid=abc")
end