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

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

477 lines
17 KiB
Lua

--[[--
source.fanqie 门面离线用例。
@module tests.source.fanqie_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 queue = {}
package.preload["ui/uimanager"] = function()
return {
nextTick = function(_, f) queue[#queue + 1] = f end,
}
end
local function drain()
while #queue > 0 do table.remove(queue, 1)() end
end
package.loaded["source.base"] = nil
local progress_wire = { data = {} }
local put_args, put_wire = nil, { code = 0 }
local shelf_actions, pending_adds = {}, {}
local detail_error
local detail_writable = true
local search_wire = { code = 0, data = { ret_data = {
{ book_id = "111", title = "搜到的书", author = "某人", abstract = "简介", category = "玄幻",
thumb_url = "https://p6-tt.bytecdn.cn/novel-pic/abc~tplv-shrink:640:0.image" },
{ title = "没有书号" },
} } }
local source_cfg = {}
package.preload["utils.settings"] = function()
return {
getSource = function() return source_cfg end,
saveSource = function(_, values)
if values and values ~= source_cfg then
for k in pairs(source_cfg) do source_cfg[k] = nil end
for k, v in pairs(values) do source_cfg[k] = v end
end
end,
}
end
package.preload["moonsource_fanqie.settings"] = function()
return {
new = function()
return {
is_cookie_configured = function() return true end,
get = function(_, key, default)
local v = source_cfg[key]
if v == nil then return default end
return v
end,
set = function(_, key, value) source_cfg[key] = value end,
flush = function() end,
}
end,
}
end
package.preload["moonsource_fanqie.client"] = function()
return {
new = function()
return {
fetchBookDetailAsync = function(_self, book_id, cb)
require("ui/uimanager"):nextTick(function()
if detail_error then cb(nil, detail_error); return end
cb({
book_id = book_id, book_name = "详情书名",
author_name = "详情作者", abstract = "<p>详情简介</p>",
thumb_url = "https://p6-novel.byteimg.com/novel-pic/x~tplv-shrink:320:0.image",
})
end)
return { cancel = function() end }
end,
fetchShelfDetailAsync = function(_, _force, cb)
require("ui/uimanager"):nextTick(function()
cb({
data = {
detail_list = {
{
book_id = "1234567890123456789",
book_name = "测试",
author_name = "作者",
thumb_url = "https://p6-novel.byteimg.com/novel-pic/x~tplv-shrink:320:0.image",
abstract = "简介",
_group_name = "追更",
last_operate_time = 1793017940,
add_shelf_time = 1789912130,
},
{
book_id = "9876543210987654321",
book_name = "未分组书",
author_name = "作者乙",
thumb_url = "https://p6-novel.byteimg.com/novel-pic/y~tplv-shrink:320:0.image",
abstract = "简介乙",
last_operate_time = 1788754903,
add_shelf_time = 1788754903000,
},
},
},
})
end)
return { cancel = function() end }
end,
fetchChapterDirectoryAsync = function(_, _id, cb)
require("ui/uimanager"):nextTick(function()
cb({
data = {
chapterList = {
{ itemId = "9876543210987654321", title = "第一章" },
},
},
})
end)
return { cancel = function() end }
end,
officialGetContentAsync = function(_, _bid, _iid, cb)
require("ui/uimanager"):nextTick(function()
cb({ title = "第一章", content = "<p>测试正文</p>" })
end)
return { cancel = function() end }
end,
shelfActionAsync = function(_, action, book_id, cb)
shelf_actions[#shelf_actions + 1] = action .. ":" .. book_id
require("ui/uimanager"):nextTick(function() cb({ code = 0 }) end)
return { cancel = function() end }
end,
searchAsync = function(_, _keyword, cb)
require("ui/uimanager"):nextTick(function() cb(search_wire) end)
return { cancel = function() end }
end,
fetchReadProgressAsync = function(_, cb)
require("ui/uimanager"):nextTick(function()
cb(progress_wire)
end)
return { cancel = function() end }
end,
updateReadProgressAsync = function(_, book_id, item_id, index, progress, cb)
put_args = { book_id = book_id, item_id = item_id, index = index, progress = progress }
require("ui/uimanager"):nextTick(function()
cb(put_wire)
end)
return { cancel = function() end }
end,
}
end,
}
end
package.preload["libs/libkoreader-lfs"] = function()
return { attributes = function() return nil end }
end
package.preload["utils.paths"] = function()
return {
imageDir = function() return "missing" end,
coverPath = function() return "missing/cover.jpg" end,
ensureLayout = function() end,
bookWorkDir = function() return "work" end,
ensureBookWork = function() end,
}
end
local stored
package.preload["book.books_db"] = function()
return {
reconcile = function(id, books)
Assert.eq(id, "ext.fanqie")
stored = books
return true, 0
end,
pendingDeleteIds = function() return {} end,
pendingShelfAddIds = function() return pending_adds end,
markSynced = function() return true end,
upsertRemote = function(book)
if not detail_writable then return false end
stored = { book }
return true
end,
get = function(_source_id, stable_id)
return stored and stored[1] and stored[1].stable_id == stable_id and stored[1] or nil
end,
}
end
local toc
local toc_writable = true
package.preload["source.toc"] = function()
return {
read = function() return toc end,
index = function(_source_id, _stable_id, uid)
for _idx, ch in ipairs(toc or {}) do
if ch.uid == uid then return ch.idx end
end
end,
put = function(_, _, v)
if not toc_writable then return false end
toc = v
return true
end,
}
end
package.preload["source.chapter"] = function()
return {
openAsync = function(_, id, book, opts, ops, cb)
ops.loadToc(id, function(chapters)
Assert.eq(chapters[1].uid, "9876543210987654321")
cb("ready.html")
end)
end,
openWithUi = function()
error("chapter transition must not open a progress dialog")
end,
prefetchAsync = function(_, _, _, _, count, ops, cb)
Assert.eq(count, 3)
Assert.eq(ops.interval_seconds, 6)
cb()
end,
cacheAllAsync = function(source, identity, fetchContent, on_progress, cb, interval)
Assert.eq(source.id, "ext.fanqie")
Assert.eq(interval, 6)
on_progress(1, 1)
fetchContent(identity, { idx = 1, uid = "9876543210987654321", title = "第一章" }, function(payload)
Assert.eq(payload.html, "<p>测试正文</p>")
cb(true, 1, nil, 1, 0)
end)
return { cancel = function() end }
end,
}
end
package.loaded["moonsource_fanqie.source"] = nil
local src = require("moonsource_fanqie.source").new()
local settings = src.settings
Assert.is_true(src:configured())
Assert.eq(src.settings, settings)
local count = 0
src:syncBooksAsync({}, function(r, e)
Assert.not_nil(r)
Assert.is_nil(e)
count = r.pulled
end)
drain()
Assert.eq(count, 2)
Assert.eq(stored[1].stable_id, "1234567890123456789")
Assert.eq(stored[1].authors, "作者")
Assert.eq(stored[1].title, "测试")
Assert.eq(stored[1].category, "追更", "默认开:进组书分类用分组名")
Assert.is_true(stored[1].category_authoritative, "书架快照声明分类口径")
Assert.eq(stored[1].read_at, 1793017940, "最后打开时间进最近阅读")
Assert.eq(stored[1].inserted_at, 1789912130, "加入书架时间进最近添加")
Assert.matches(stored[1].cover, "p6%-novel%.byteimg%.com")
Assert.eq(stored[2].stable_id, "9876543210987654321")
Assert.is_nil(stored[2].category, "没进组不写分类")
Assert.is_true(stored[2].category_authoritative)
Assert.eq(stored[2].read_at, 1788754903)
Assert.eq(stored[2].inserted_at, 1788754903, "毫秒加入时间折成秒")
-- 关掉「使用自定义分组代替分类」后再同步:番茄书架不带题材,分类清空。
do
source_cfg.category_as_group = false
local again = 0
src:syncBooksAsync({ force = true }, function(r, e)
Assert.not_nil(r)
Assert.is_nil(e)
again = r.pulled
end)
drain()
Assert.eq(again, 2)
Assert.is_nil(stored[1].category, "关:进组也不写分组名")
Assert.is_nil(stored[2].category, "关:没进组不写分类")
source_cfg.category_as_group = nil
end
local ref = { source_id = "ext.fanqie", stable_id = "1234567890123456789" }
src:openBookAsync(ref, { chapter_idx = 1 }, function(path)
Assert.eq(path, "ready.html")
count = count + 1
end)
drain()
Assert.eq(count, 3)
local cancelled = src:syncBooksAsync({}, function()
error("cancelled callback delivered")
end)
cancelled.cancel()
drain()
src:prefetchChaptersAsync(ref, {}, 1, 3, function()
count = count + 1
end)
Assert.eq(count, 4)
-- 整本下载:走公共 cacheAllAsync,正文用番茄接口,间隔与预取一致(风控)
do
local progressed, ok, cached
src:cacheAllChaptersAsync(ref, function(done, total) progressed = { done, total } end,
function(success, n) ok, cached = success, n end)
drain()
Assert.eq(progressed[1], 1)
Assert.is_true(ok)
Assert.eq(cached, 1)
end
-- 漫画章节归档已就绪:不联网直接回文件;返回的句柄取消后回调不再投递(之前句柄丢了,取消是空操作)
do
local Chapter = package.loaded["source.chapter"]
local real_cache_all = Chapter.cacheAllAsync
local fetchContent
Chapter.cacheAllAsync = function(_source, _identity, fetcher) fetchContent = fetcher; return { cancel = function() end } end
src:cacheAllChaptersAsync(ref, function() end, function() end)
Chapter.cacheAllAsync = real_cache_all
local Cbz = package.loaded["source.cbz"]
local real_ready = Cbz.ready
Cbz.ready = function(path) return path == "work/777.cbz" end
local chapter = { idx = 1, uid = "777", title = "漫画一" }
local got
fetchContent(ref, chapter, function(payload) got = payload end)
drain()
Assert.eq(got.file, "work/777.cbz")
got = nil
local handle = fetchContent(ref, chapter, function(payload) got = payload end)
handle.cancel()
drain()
Assert.is_nil(got, "取消后不能再回调")
Cbz.ready = real_ready
end
do
toc = nil
toc_writable = false
local chapters, err
src:loadTocAsync(ref, function(value, reason)
chapters, err = value, reason
end)
drain()
Assert.is_nil(chapters)
Assert.eq(err, "章节目录保存失败")
Assert.is_nil(toc)
toc_writable = true
src:loadTocAsync(ref, function(value) chapters = value end)
drain()
Assert.eq(chapters[1].uid, "9876543210987654321")
Assert.eq(chapters[1].depth, 1)
local called = false
local cached = src:loadTocAsync(ref, function() called = true end)
cached:cancel()
drain()
Assert.is_false(called)
end
do
local result
src:syncBooksAsync({ dirty_only = true }, function(r) result = r end)
Assert.is_nil(result)
drain()
Assert.eq(result.pushed, 0)
Assert.len(shelf_actions, 0)
-- 本地新加架的书经网页接口上行
pending_adds = { "555" }
src:syncBooksAsync({ dirty_only = true }, function(r) result = r end)
drain()
Assert.eq(result.pushed, 1)
Assert.eq(shelf_actions[1], "add:555")
pending_adds = {}
end
-- 书城搜索:只留有书号的行,封面归一到手机 CDN
do
local res
src:searchAsync("关键字", function(r) res = r end)
drain()
Assert.len(res.data, 1)
Assert.eq(res.data[1].stable_id, "111")
Assert.eq(res.data[1].authors, "某人")
Assert.eq(res.data[1].category, "玄幻")
Assert.matches(res.data[1].cover, "p6%-novel%.byteimg%.com")
end
-- 封面走基类:查书库 cover(同步书架时写入的手机 CDN 地址)
Assert.matches(src:coverRequest(ref).url, "p6%-novel%.byteimg%.com")
-- 官方网页进度只有章节;read_progress 不是章内比例。
toc = {
{ idx = 1, uid = "a" }, { idx = 2, uid = "b" }, { idx = 3, uid = "c" }, { idx = 4, uid = "d" },
}
do
progress_wire = { code = 0, data = {
{ book_id = "other", item_id = "a" },
{ book_id = ref.stable_id, item_id = "c", read_progress = 5000 },
} }
local pos, err
src:getProgressAsync(ref, function(p, e) pos, err = p, e end)
drain()
Assert.is_nil(err)
Assert.eq(pos.chapter_idx, 3)
Assert.is_nil(pos.chapter_fraction, "不能把章节 order 当成万分比")
Assert.eq(pos.fraction, 0.5)
Assert.eq(pos.extra.chapter_uid, "c")
-- 章节不在目录 / 云端没这本书:空进度,不是错误
progress_wire.data[2].item_id = "gone"
local meta
src:getProgressAsync(ref, function(p, e, m) pos, err, meta = p, e, m end)
drain()
Assert.is_nil(pos)
Assert.is_nil(err)
Assert.is_true(meta.empty)
progress_wire = { code = 0, data = {} }
meta = nil
src:getProgressAsync(ref, function(p, e, m) pos, err, meta = p, e, m end)
drain()
Assert.is_true(meta.empty)
end
-- 进度上传:章节序号 → uid + 0 起 index,不伪造网页不支持的章内位置。
do
local ok, err
src:putProgressAsync(ref, { chapter_idx = 2, chapter_fraction = 1.5 }, function(r, e) ok, err = r, e end)
drain()
Assert.is_true(ok)
Assert.is_nil(err)
Assert.eq(put_args.book_id, ref.stable_id)
Assert.eq(put_args.item_id, "b")
Assert.eq(put_args.index, 1)
Assert.eq(put_args.progress, 0)
-- 没有章节位置:不发请求,直接报错
put_args = nil
src:putProgressAsync(ref, { fraction = 0.3 }, function(r, e) ok, err = r, e end)
drain()
Assert.is_nil(ok)
Assert.eq(err, "缺少番茄章节位置")
Assert.is_nil(put_args)
-- 业务码非 0:上传失败
put_wire = { code = 1001 }
src:putProgressAsync(ref, { chapter_idx = 1 }, function(r, e) ok, err = r, e end)
drain()
Assert.is_nil(ok)
Assert.eq(err, "番茄进度上传失败")
end
do
local detail, err
stored[1].deleted = 1
src:getDetailAsync({ source_id = src.id, stable_id = ref.stable_id, book = { intro = "" } },
function(book, reason) detail, err = book, reason end)
Assert.is_nil(detail, "详情不能同步返回旧的本地行")
drain()
Assert.is_nil(err)
Assert.eq(detail.authors, "详情作者")
Assert.eq(detail.intro, "详情简介")
Assert.eq(detail.deleted, 1, "补详情不能复活墓碑")
detail_error = "network failed"
src:getDetailAsync(ref, function(book, reason) detail, err = book, reason end)
drain()
Assert.is_nil(detail)
Assert.eq(err, "network failed")
Assert.eq(stored[1].intro, "详情简介")
detail_error = nil
detail_writable = false
src:getDetailAsync(ref, function(book, reason) detail, err = book, reason end)
drain()
Assert.is_nil(detail)
Assert.eq(err, "书籍信息保存失败")
detail_writable = true
local previous = stored[1]
local job = src:getDetailAsync(ref, function() error("cancelled detail callback delivered") end)
job:cancel()
drain()
Assert.eq(stored[1], previous, "关闭详情后到达的回包不能写库")
end