🚚 (split): 从月读拆出独立仓库,沿用原书源 id
Release / release (push) Successful in 10s

This commit is contained in:
Garfield committed 2026-11-27 08:08:01 +08:00
commit 28215d4989
25 files changed
+2822

No files matched your search

+237
View File
@@ -0,0 +1,237 @@
--[[--
iRead 同步回调下的取消、搜索轮询、坏响应及登录态隔离。
@module tests.moonsource_iread.async_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local JSON = require("support.json_stub")
local Stubs = require("support.stubs")
local simple = {}
package.loaded.json = {
decode = setmetatable({ simple = simple }, {
__call = function(_decoder, raw, opts)
local data = JSON.decode(raw)
if opts ~= simple and raw:find('"intro":null', 1, true) then
data.data[1].intro = function() end
end
return data
end,
}),
encode = JSON.encode,
util = { null = function() end },
}
local cfg = { token = "old-token" }
package.loaded["utils.settings"] = { getSource = function() return cfg end }
local requests, replies = {}, {}
local queue = {}
package.loaded["http.request"] = {
post = function(url, body, opts, cb)
local request = { url = url, body = body, opts = opts, cb = cb }
requests[#requests + 1] = request
local reply = table.remove(replies, 1)
if reply then cb(JSON.encode({ code = 200, data = reply }), nil, { cookies = { "sid=new; Path=/" } }) end
request.cancel = function() request.cancelled = true end
return request
end,
get = function(url, opts, cb)
local request = { url = url, opts = opts, cb = cb }
requests[#requests + 1] = request
-- 延迟回调,与真实网络行为一致
local reply = table.remove(replies, 1)
local wrapped = function()
if reply then cb(JSON.encode({ code = 200, data = reply }), nil, nil) end
end
queue[#queue + 1] = wrapped
request.cancel = function() request.cancelled = true end
return request
end,
headerAll = function(res) return res and res.cookies or {} end,
}
local Client = require("moonsource_iread.client")
local Auth = require("source.auth")
local result, reason, calls
local function done(value, err) result, reason, calls = value, err, (calls or 0) + 1 end
local function reset()
requests, replies, result, reason, calls = {}, {}, nil, nil, 0
cfg.token, cfg.cookie = "old-token", nil
Stubs.reset()
queue = {}
Auth.reset()
end
local function reply(request, data, code)
request.cb(JSON.encode({ code = code or 200, data = data }))
end
reset()
Client.shelfAsync(done)
requests[1].cb('{"code":200,"data":[{"name":"书","intro":null,"author":null}]}')
Assert.is_nil(result[1].intro, "LuaJSON 的 null 应解成 nil,不能把函数哨兵写进书籍元数据")
reset()
Client.exploreRulesAsync(done)
reply(requests[1], { { id = 1, bookSourceName = "测试书源", exploreUrl = "bad" } })
Assert.is_nil(result)
Assert.eq(reason, "响应无效")
Client.exploreRulesAsync(done)
reply(requests[2], { { id = 1, bookSourceName = "测试书源", exploreUrl = {
{ title = "分组", url = "" }, { title = "↑ *** ↑" }, { title = "分类", url = "https://example.test?page={{page}}" },
} } })
Assert.len(result[1].exploreUrl, 1)
Assert.eq(result[1].exploreUrl[1].title, "分类")
reset()
replies = { {} }
local handle = Client.shelfAsync(done)
local pending = requests[2]
Assert.matches(pending.url, "/user/home$")
handle:cancel()
Assert.is_true(pending.cancelled)
reply(pending, { nickname = "测试" })
Assert.eq(calls, 0)
reply(pending, { nickname = "测试" })
Assert.eq(calls, 0)
reset()
replies = { {} }
Client.shelfAsync(done)
reply(requests[2], {})
Assert.is_nil(result)
Assert.eq(reason, "iRead 账号信息为空")
Assert.eq(calls, 1)
reset()
cfg.token = nil
replies = { { task = "guest-task" }, { result = { { name = "公开书" } }, remaining = 0 } }
Client.searchAsync("公开书", done)
Assert.len(result, 1)
Assert.is_nil(requests[1].opts.headers.Authorization)
Assert.is_nil(requests[1].opts.headers.Cookie)
Assert.eq(requests[1].opts.headers["X-Requested-With"], "XMLHttpRequest")
Client.userAsync({}, done)
reply(requests[3], {}, 401)
Assert.is_true(not Auth.expired("iread"), "游客未登录不能污染已有源的失效提示")
reset()
Client.shelfAsync(done)
cfg.token = "new-token"
reply(requests[1], {})
Assert.len(requests, 1, "旧书架回调不能拿新凭证校验空书架")
Assert.is_nil(result)
Assert.eq(reason, "登录状态已变化,请重试")
reset()
Auth.expire("iread", "expired")
Client.chaptersAsync({ id = 1, name = "书" }, done)
reply(requests[1], { bad = true })
Assert.is_nil(result)
Assert.eq(reason, "响应无效")
Assert.is_true(Auth.expired("iread"), "坏响应不能清除失效标记")
Client.shelfAsync(done)
reply(requests[2], { { author = "缺少书名" } })
Assert.is_nil(result)
Assert.eq(reason, "响应无效")
Client.shelfAsync(done)
requests[3].cb(nil, "network failed")
Assert.eq(reason, "network failed")
Assert.eq(cfg.token, "old-token")
reset()
Client.shelfAsync(done)
cfg.token = "new-token"
reply(requests[1], {}, 401)
Assert.is_true(not Auth.expired("iread"), "旧凭证失败不能注销新凭证")
Assert.eq(reason, "登录状态已变化,请重试")
reset()
replies = { { task = "task-1" } }
handle = Client.searchAsync("书", done)
pending = requests[2]
handle:cancel()
Assert.is_true(pending.cancelled, "同步提交不能覆盖正在轮询的取消句柄")
reply(pending, { result = { { name = "书" } }, remaining = 0 })
Stubs.flush()
Assert.eq(calls, 0)
reset()
cfg.token, cfg.cookie = nil, "sid=old; login=test"
replies = { { task = "task-1" } }
Client.searchAsync("书", done)
Assert.eq(requests[2].opts.headers.Cookie, "login=test; sid=new")
Assert.is_nil(requests[2].opts.headers.Authorization)
reply(requests[2], { result = { { name = "书" } }, remaining = 1 })
Stubs.flush()
Assert.len(requests, 3)
reply(requests[3], { result = { { name = "书" }, { name = "二" } }, remaining = 0 })
Assert.len(result, 2)
Assert.eq(calls, 1)
reset()
replies = { { task = "task-1" }, { result = {}, remaining = 1 } }
handle = Client.searchAsync("书", done)
handle:cancel()
Stubs.flush()
Assert.len(requests, 2, "取消后不再调度下一次轮询")
Assert.eq(calls, 0)
reset()
replies = { { task = "task-1" } }
Client.searchAsync("书", done)
reply(requests[2], { result = {} })
Assert.is_nil(result)
Assert.eq(reason, "响应无效", "缺少 remaining 不能伪装成搜索完成")
reset()
replies = { { task = "task-1" } }
Client.searchAsync("书", done)
reply(requests[2], { result = { { name = "书" } }, remaining = 1 })
Stubs.flush()
requests[3].cb(nil, "network failed")
Assert.is_nil(result)
Assert.eq(reason, "network failed", "中途失败不能返回不完整搜索结果")
Assert.eq(calls, 1)
reset()
replies = { { task = "task-1" } }
for _index = 1, 30 do replies[#replies + 1] = { result = { { name = "书" } }, remaining = 1 } end
Client.searchAsync("书", done)
Stubs.flush()
Assert.is_nil(result)
Assert.eq(reason, "iRead 搜索超时,请重试")
Assert.eq(calls, 1)
Assert.len(requests, 31)
reset()
replies = { { task = "task-1" }, { result = {}, remaining = 1 } }
Client.searchAsync("书", done)
cfg.token = "new-token"
Stubs.flush()
Assert.len(requests, 2, "换登录态后不继续旧搜索任务")
Assert.eq(reason, "登录状态已变化,请重试")
reset()
replies = { {} }
handle = Client.saveProgressAsync({ id = 1, name = "书" }, { index = 2, offset = 0 }, done)
pending = requests[2]
handle:cancel()
Assert.is_true(pending.cancelled)
reply(pending, { { name = "书", durChapterIndex = 2, durChapterPos = 0 } })
Assert.eq(calls, 0)
reset()
replies = { {}, { { name = "书", durChapterIndex = 1, durChapterPos = 0 } } }
Client.saveProgressAsync({ id = 1, name = "书" }, { index = 2, offset = 0 }, done)
Assert.is_nil(result)
Assert.eq(reason, "iRead 阅读进度更新未生效")
Assert.eq(calls, 1)
reset()
Client.chaptersAsync({ id = 7, name = "书" }, done)
Assert.matches(requests[1].body, "page=1")
requests[1].cb('{"code":200,"data":[{"title":"一","index":0}],"hasMore":true}')
Assert.len(requests, 2, "hasMore 为真要继续翻页")
Assert.matches(requests[2].body, "page=2")
requests[2].cb('{"code":200,"data":[{"title":"二","index":1}],"hasMore":false}')
Assert.len(result, 2, "目录要拼全所有页")
Assert.eq(result[2].title, "二")
+205
View File
@@ -0,0 +1,205 @@
--[[--
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
+95
View File
@@ -0,0 +1,95 @@
--[[--
iRead 候选登录的取消与竞态、Cookie 域范围及手动粘贴入口。
@module tests.moonsource_iread.setting_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Themed = require("support.theme_stubs")
Themed.install()
package.loaded["ui.themes.widgets.row"] = { widget = function(spec) return spec end }
local cfg, saved, reloaded = { token = "old-token" }, 0, 0
local cleared = 0
package.loaded["source.logout"] = { row = require("source.logout").row, clear = function(_id, clear)
clear(); cleared = cleared + 1; return true
end }
package.loaded["utils.settings"] = {
getSource = function() return cfg end,
saveSource = function() saved = saved + 1 end,
}
package.loaded["source.registry"] = { reload = function() reloaded = reloaded + 1 end }
local requests = {}
package.loaded["moonsource_iread.client"] = {
credential = function()
if cfg.token then return { token = cfg.token } end
if cfg.cookie then return { cookie = cfg.cookie } end
end,
userAsync = function(cred, cb)
local request = { cred = cred, cb = cb }
requests[#requests + 1] = request
return { cancel = function() request.cancelled = true end }
end,
}
local Setting = require("moonsource_iread.setting")
local calls, result, reason = 0, nil, nil
local function done(user, err) calls, result, reason = calls + 1, user, err end
local handle = Setting.save({ token = "candidate" }, done)
handle:cancel()
Assert.is_true(requests[1].cancelled)
requests[1].cb({ nickname = "测试" })
Assert.eq(calls, 0)
Assert.eq(cfg.token, "old-token")
Assert.eq(saved, 0)
Setting.save({ token = "candidate" }, done)
requests[2].cb(nil, "network failed")
Assert.eq(cfg.token, "old-token")
Assert.eq(reason, "network failed")
Assert.eq(saved, 0)
Setting.save({ token = "candidate" }, done)
cfg.token = "new-token"
requests[3].cb({ nickname = "测试" })
Assert.is_nil(result)
Assert.eq(reason, "登录状态已变化,请重试")
Assert.eq(cfg.token, "new-token")
Assert.eq(saved, 0)
Setting.save({ token = "different-token" }, done)
requests[4].cb({ nickname = "测试" })
Assert.is_true(result ~= nil)
Assert.is_nil(reason)
Assert.eq(cfg.token, "different-token")
Assert.eq(cleared, 1, "凭据变化时直接清旧源")
Assert.is_nil(Themed.last("confirm"))
cfg.token = nil
saved, reloaded = 0, 0
handle = Setting.browser.apply({ cookies = {
{ domain = ".ankio.net", name = "parent", value = "ok" },
{ domain = "iread.ankio.net", name = "site", value = "ok" },
{ domain = "other.ankio.net", name = "sibling", value = "no" },
{ domain = "ankio.net.evil.test", name = "evil", value = "no" },
} }, function(ok, err) result, reason = ok, err end)
Assert.eq(requests[5].cred.cookie, "parent=ok; site=ok")
requests[5].cb({ nickname = "测试" })
Assert.is_true(result)
Assert.is_nil(reason)
Assert.is_nil(cfg.token)
Assert.eq(cfg.cookie, "parent=ok; site=ok")
Assert.eq(saved, 1)
Assert.eq(reloaded, 1)
local rows = Setting.rows()
Assert.eq(rows[2](500).title, "浏览器登录")
local cookie_row = rows[3](500)
Assert.eq(cookie_row.title, "粘贴 Cookie")
cookie_row.callback()
local input = Themed.last("input")
Assert.eq(input.title, "iRead 登录 Cookie")
input.callback("Cookie: sid=manual")
Assert.eq(requests[6].cred.cookie, "sid=manual")
Themed.last("progress_dialog").on_cancel()
Assert.is_true(requests[6].cancelled)
+233
View File
@@ -0,0 +1,233 @@
--[[--
iRead 搜索元数据持久化、重启后加架、写后确认与源编排取消。
@module tests.moonsource_iread.source_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch, inject-field
local Assert = require("support.assert")
local Config = require("support.config")
local Stubs = require("support.stubs")
local sqlite_ok = pcall(require, "lua-ljsqlite3/init")
if not sqlite_ok then Assert.skip("当前离线环境没有 ljsqlite3 Lua 模块") end
package.loaded.json = require("support.json_stub")
local path = Config.dir() .. "/iread_source_spec.sqlite3"
for _index, suffix in ipairs({ "", "-wal", "-shm" }) do os.remove(path .. suffix) end
local Paths = require("utils.paths")
package.loaded["utils.paths"] = setmetatable({ dbPath = function() return path end }, { __index = Paths })
local BookDB = require("book.books_db")
local Store = require("book.store")
local Base = require("source.base")
local row = { id = 9, name = "测试书", bookUrl = "https://example.test/book", sourceId = 7, variable = { key = "value" } }
local requests, shelf_replies = {}, {}
local function pending(kind, cb, data)
local request = { kind = kind, cb = cb, data = data }
requests[#requests + 1] = request
return { cancel = function() request.cancelled = true end }
end
package.loaded["moonsource_iread.client"] = {
credential = function() return { token = "test" } end,
searchAsync = function(_keyword, cb) return Base.defer(cb, { row }) end,
shelfAsync = function(cb)
local job = pending("shelf", cb)
local rows = table.remove(shelf_replies, 1)
if rows then cb(rows) end
return job
end,
favouriteAsync = function(book, cb)
local job = pending("toggle", cb, book)
cb(true)
return job
end,
chaptersAsync = function(book, cb) return pending("toc", cb, book) end,
exploreRulesAsync = function(cb)
local job = pending("rules", cb)
cb({ { id = 7, exploreUrl = { { title = "分类", url = "https://example.test/category" } } } })
return job
end,
exploreAsync = function(_id, _url, cb) return pending("explore", cb) end,
}
local remote
package.loaded["source.shelf"] = {
syncAsync = function(_id, adapter, _mapper, _opts, cb)
remote = adapter
return Base.defer(cb, { skipped = true })
end,
}
local IRead = require("moonsource_iread.source")
local source = IRead.new()
local result, reason, calls = nil, nil, 0
local function done(value, err) result, reason, calls = value, err, calls + 1 end
source:searchAsync("测试", function(res)
Store.rememberMany(res.data)
end)
Stubs.flush()
local stored = BookDB.get("iread", row.name)
Assert.eq(stored.meta.source.iread.bookUrl, row.bookUrl)
Assert.eq(stored.meta.source.iread.variable.key, "value")
Assert.eq(stored.deleted, 1, "保存书城结果不能自动上架")
source = IRead.new()
local identity = { source_id = "iread", stable_id = row.name }
local handle = source:loadTocAsync(identity, done, true)
Assert.len(requests, 1, "重启后目录可使用随书籍持久化的源记录")
Assert.eq(requests[1].kind, "toc")
Assert.eq(requests[1].data.sourceId, 7)
handle:cancel()
Assert.is_true(requests[1].cancelled)
source:syncBooksAsync({}, function() end)
Stubs.flush()
requests, shelf_replies, result, reason, calls = {}, { {} }, nil, nil, 0
handle = remote:addToShelfAsync(row.name, done)
Assert.eq(requests[2].kind, "toggle")
Assert.eq(requests[2].data.bookUrl, row.bookUrl)
Assert.eq(requests[3].kind, "shelf")
Assert.eq(calls, 0, "收藏接口成功不代表书架已改变")
requests[3].cb({ row })
Assert.is_true(result)
Assert.eq(calls, 1)
requests, shelf_replies, result, reason, calls = {}, { {} }, nil, nil, 0
remote:addToShelfAsync(row.name, done)
requests[3].cb({})
Assert.is_nil(result)
Assert.eq(reason, "iRead 书架更新未生效")
requests, shelf_replies, result, reason, calls = {}, { { row } }, nil, nil, 0
remote:addToShelfAsync(row.name, done)
Assert.is_true(result)
Assert.len(requests, 1, "已在架不重复调用开关接口")
requests, shelf_replies, result, reason, calls = {}, { { row } }, nil, nil, 0
handle = remote:removeFromShelfAsync(row.name, done)
Assert.eq(requests[3].kind, "shelf")
handle:cancel()
Assert.is_true(requests[3].cancelled, "同步回调不能覆盖写后确认的句柄")
requests[3].cb({})
Assert.eq(calls, 0)
requests, shelf_replies, result, reason, calls = {}, { { row } }, nil, nil, 0
remote:removeFromShelfAsync(row.name, done)
requests[3].cb({})
Assert.is_true(result)
requests, shelf_replies, result, reason, calls = {}, { {} }, nil, nil, 0
remote:removeFromShelfAsync(row.name, done)
Assert.is_true(result)
Assert.len(requests, 1, "已移出不能调用开关而重新加入")
requests, shelf_replies, result, reason, calls = {}, { { row } }, nil, nil, 0
handle = source:withRow(identity, true, done, function(book)
return require("moonsource_iread.client").chaptersAsync(book, done)
end)
Assert.eq(requests[2].kind, "toc")
handle:cancel()
Assert.is_true(requests[2].cancelled)
requests, shelf_replies, result, reason, calls = {}, {}, nil, nil, 0
source.rows = nil
handle = source:fetchShelf(done)
handle:cancel()
requests[1].cb({ row })
Assert.is_nil(source.rows)
Assert.eq(calls, 0)
requests, result, reason, calls = {}, nil, nil, 0
source:browseAsync({}, done)
Assert.len(result.groups[1].chips, 1)
handle = source:browseAsync({ node = "7:1" }, done)
Assert.eq(requests[2].kind, "explore")
handle:cancel()
local before = calls
requests[2].cb({ row })
Assert.eq(calls, before)
local shelf = package.loaded["source.shelf"]
local capture = shelf.syncAsync
package.loaded["source.shelf"] = nil
shelf.syncAsync = require("source.shelf").syncAsync
Assert.is_true(BookDB.setLibraryMembership("iread", row.name, true))
requests, shelf_replies = {}, { {}, {} }
source:syncBooksAsync({ dirty_only = true }, done)
Stubs.flush()
Assert.eq(result.pushed, 0)
Assert.eq(BookDB.get("iread", row.name).sync_status, 0, "写后回读未确认的新加书必须保留脏状态")
Assert.is_true(BookDB.markDeleted("iread", row.name))
requests, shelf_replies = {}, { { row }, { row } }
source:syncBooksAsync({ dirty_only = true }, done)
Stubs.flush()
stored = BookDB.get("iread", row.name)
Assert.eq(stored.deleted, 1)
Assert.eq(stored.sync_status, 0, "远端仍在架时不能撕墓碑")
shelf.syncAsync = capture
local client = require("moonsource_iread.client")
client.credential = function() return nil end
source = IRead.new()
source.explore_rules = { [7] = {
id = 7, exploreUrl = { { title = "分类", url = "https://example.test/list?page={{page}}" } },
} }
local pages = {}
client.exploreAsync = function(_id, url, cb)
local page = tonumber(url:match("page=(%d+)"))
pages[#pages + 1] = page
local rows = {}
for index = 1, 20 do
rows[index] = { name = "分类书" .. ((page - 1) * 20 + index), bookUrl = "https://example.test/book", sourceId = 7 }
end
return Base.defer(cb, rows)
end
source:browseAsync({ node = "7:1", limit = 45 }, done)
Stubs.flush()
Assert.len(result.data, 45)
Assert.len(pages, 3)
Assert.eq(pages[3], 3)
Assert.eq(result.groups[1].title, "分类")
Assert.eq(result.data[45].stable_id, "分类书45")
pages = {}
client.exploreAsync = function(_id, url, cb)
local page = tonumber(url:match("page=(%d+)"))
pages[#pages + 1] = page
if page == 1 then return Base.defer(cb, { row }) end
return Base.defer(cb, nil, "network failed")
end
source:browseAsync({ node = "7:1", limit = 45 }, done)
Stubs.flush()
Assert.is_nil(result)
Assert.eq(reason, "network failed", "第二页失败不能把第一页当成完整分类列表")
source:searchAsync("测试", done)
Stubs.flush()
Assert.len(result.data, 1, "未登录时仍可使用官网公共搜索")
local empty
source:getProgressAsync(identity, function(value, err, meta)
result, reason, empty = value, err, meta.empty
end)
Stubs.flush()
Assert.is_nil(result)
Assert.is_nil(reason)
Assert.is_true(empty, "游客阅读不应触发云端进度登录提示")
local text
client.contentAsync = function(_book, _index, cb) return Base.defer(cb, text) end
source.found = { [row.name] = row }
local fetch = source:contentFetcher()
text = "测试段落\n第二段"
fetch(identity, { uid = "0", title = "测试章节", raw = "{}" }, done)
Stubs.flush()
Assert.matches(result.html, "测试段落")
text = '<IMG src="https://example.test/image.png">'
fetch(identity, { uid = "0", title = "测试章节", raw = "{}" }, done)
Stubs.flush()
Assert.is_nil(result)
Assert.eq(reason, "iRead 图片正文暂不支持,请使用网页版阅读")
text = "<p></p>"
fetch(identity, { uid = "0", title = "测试章节", raw = "{}" }, done)
Stubs.flush()
Assert.is_nil(result)
Assert.eq(reason, "iRead 章节内容为空", "不能把剥标签后的空正文缓存成成功")
require("utils.db").close()
for _index, suffix in ipairs({ "", "-wal", "-shm" }) do os.remove(path .. suffix) end