✨ 初始提交:月读第三方书源插件集合

每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-16 12:08:03 +08:00
commit 730b2d6d9f
27 files changed
+17665

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
--[[-- QQ阅读进度多阶段取消与换号竞态。 @module tests.moonsource_qqreader.async_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local cookie, detail_delay, push_delay, late = "111", false, false, nil
local cancelled, pushed = {}, 0
local toc = { { uid = "1", title = "one" }, { uid = "2", title = "two" } }
package.preload["source.toc"] = function() return { index = function() return 2 end } end
package.preload["moonsource_qqreader.client"] = function()
return {
cookie = function() return cookie end,
detailAsync = function(_id, cb)
local function done() cb({ title = "fixture", inShelf = 1, lastReadChapterId = 2 }) end
if detail_delay then late = done else done() end
return { cancel = function() cancelled.detail = true end }
end,
updateReadAsync = function(_id, _chapter, cb)
pushed = pushed + 1
if push_delay then late = function() cb(true) end else cb(true) end
return { cancel = function() cancelled.push = true end }
end,
bookCommentsAsync = function(_id, cb)
cb({ commentCount = 32, comments = {
{ commentId = "c1", content = "fixture", user = { nickname = "author" } },
} })
return { cancel = function() end }
end,
}
end
local source = require("moonsource_qqreader.source").new()
local identity = { source_id = "ext.qqreader", stable_id = "9" }
do
source.loadTocAsync = function(_self, _identity, cb)
late = function() cb(toc) end
return { cancel = function() cancelled.toc = true end }
end
local called = false
local job = source:getProgressAsync(identity, function() called = true end)
job.cancel()
Assert.is_true(cancelled.toc)
Assert.is_nil(cancelled.detail, "同步详情不能覆盖下一阶段句柄")
late()
Assert.is_false(called)
end
for _idx, stage in ipairs({ "detail", "push" }) do
cancelled, detail_delay, push_delay = {}, stage == "detail", stage == "push"
source.loadTocAsync = function(_self, _identity, cb)
cb(toc)
return { cancel = function() cancelled.toc = true end }
end
local called = false
local job = source:putProgressAsync(identity, { chapter_idx = 2 }, function() called = true end)
job.cancel()
Assert.is_true(cancelled[stage])
Assert.is_nil(cancelled.toc)
late()
Assert.is_false(called)
end
do
cookie, detail_delay, pushed = "111", true, 0
local result, err
source:putProgressAsync(identity, { chapter_idx = 2 }, function(value, reason) result, err = value, reason end)
cookie = "222"
late()
Assert.is_nil(result)
Assert.matches(err, "账号已变化")
Assert.eq(pushed, 0)
end
do
local page
source:bookReviewsAsync(identity, function(value) page = value end)
Assert.len(page.items, 1)
Assert.eq(page.total, 32)
Assert.is_true(page.more)
Assert.matches(page.title, "预览 1/32")
end
+85
View File
@@ -0,0 +1,85 @@
--[[-- QQ阅读动态分类、筛选、旧节点、多页和取消回归。 @module tests.moonsource_qqreader.browse_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Stubs = require("support.stubs")
local requests, cancelled, delayed, late = {}, {}, false, nil
local KEYS = { "categoryId2", "categoryId3", "finishStatus", "priceStatus", "words", "updateTime", "orderBy", "pageNo" }
package.preload["moonsource_qqreader.client"] = function()
return { categoryAsync = function(path, cb)
requests[#requests + 1] = path
local idx = #requests
local params, position = {}, 0
for value in path:gmatch("[^-]+") do position = position + 1; params[KEYS[position]] = tonumber(value) end
local data = { total = 0, params = params, books = {}, categoryList = {
{ name = "男生", subCategories = { { value = 20001, text = "玄幻" } } },
{ name = "女生", subCategories = { { value = 30013, text = "古言" } } },
{ name = "出版", subCategories = { { value = 13100, text = "小说" } } },
} }
for _idx, key in ipairs({ "categoryId3", "finishStatus", "priceStatus", "words", "updateTime", "orderBy" }) do
data[key] = { title = key == "priceStatus" and "属性" or key, items = {
{ value = 0, text = "全部" }, { value = 3, text = "付费" },
} }
end
cb(data)
return { cancel = function() cancelled[idx] = true end }
end,
listAsync = function(url, cb)
local path = url:match("^/book%-cate/(.+)$")
requests[#requests + 1] = path
local idx, page = #requests, tonumber(path:match("(%d+)$"))
local books = {}
for item = 1, 20 do books[item] = { source_id = "ext.qqreader", stable_id = tostring((page - 1) * 20 + item) } end
if delayed and page == 2 then late = function() cb(books) end else cb(books) end
return { cancel = function() cancelled[idx] = true end }
end }
end
local Library = require("moonsource_qqreader.browse")
do
local result, err
Library.run({ node = "cate:20001", limit = 60 }, function(value, reason) result, err = value, reason end)
Assert.is_nil(err)
Assert.len(result.data, 60)
Assert.len(requests, 4)
Assert.eq(requests[1], "20001-0-0-0-0-0-0-1")
Assert.eq(requests[4], "20001-0-0-0-0-0-0-3")
Assert.eq(result.groups[1].title, "男生分类")
Assert.eq(result.groups[2].title, "女生分类")
Assert.eq(result.groups[3].title, "出版分类")
Assert.eq(result.groups[4].title, "筛选")
local found = false
for _idx, group in ipairs(result.groups) do
if group.title == "属性" then
for _i, chip in ipairs(group.chips) do
if chip.title == "付费" then
found = true
Assert.eq(chip.id, "cate:20001-0-0-3-0-0-0-1")
end
end
end
end
Assert.is_true(found, "属性筛选项单独成组,标题不再拼选中标记")
end
do
requests, cancelled, delayed = {}, {}, true
local called = false
local job = Library.run({ node = "cate:20001-20002-1-3-2-1-2-1" }, function() called = true end)
Assert.eq(requests[1], "20001-20002-1-3-2-1-2-1")
job.cancel()
Assert.is_true(cancelled[3])
late()
Assert.is_false(called)
Assert.len(requests, 3)
end
do
requests = {}
local result, err
Library.run({ node = "cate:20001--0" }, function(value, reason) result, err = value, reason end)
Stubs.flush()
Assert.is_nil(result)
Assert.matches(err, "分类条件无效")
Assert.len(requests, 0)
end
+207
View File
@@ -0,0 +1,207 @@
--[[-- QQ阅读正文取消、身份快照、权限复查与坏响应回归。 @module tests.moonsource_qqreader.client_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local CookieOf = require("support.cookie")
local JSON = require("support.json_stub")
local cfg, requests, replies, late, cancelled = {}, {}, {}, nil, {}
local fock_delay, fock_called, fock_cancelled, fock_uid = false, 0, false, nil
local expired
package.preload["json"] = function() return JSON end
package.preload["utils.settings"] = function() return { getSource = function() return cfg end } end
package.preload["source.auth"] = function()
return { ok = function() end, expire = function(_id, msg) expired = msg; return msg end }
end
package.preload["http.request"] = function()
return { get = function(url, opts, cb)
local idx = #requests + 1
requests[idx] = { url = url, opts = opts }
local reply = table.remove(replies, 1)
local function done()
if reply.before then reply.before() end
cb(reply.body, reply.err, reply.res)
end
if reply.delay then late = done else done() end
return { cancel = function() cancelled[idx] = true end }
end }
end
package.preload["moonsource_qqreader.fock"] = function()
return { chapterAsync = function(_info, _font, _chapter, _pool, _version, uid, cb)
fock_called, fock_uid = fock_called + 1, uid
local function done() cb("decoded fixture") end
if fock_delay then late = done else done() end
return { cancel = function() fock_cancelled = true end }
end }
end
local Client = require("moonsource_qqreader.client")
local Mapper = require("moonsource_qqreader.mapper")
local encrypted = '{"code":0,"data":{"content":"encrypted","encrypt":true,"fontEncrypt":true,'
.. '"chapterVipFlag":1,"authStatus":1,"fontResponse":{"fixedFontTtf":"https://example.invalid/font.ttf"}}}'
local piracy = '{"code":0,"data":{"fkp":"d2luZG93Lm9ua2V5Zm9jdXMoImtleXBvb2wiLCAyKQ==","fuid":1234567890123456}}'
local function reset()
cfg.cookie = "ywguid=111"
requests, replies, late, cancelled = {}, {}, nil, {}
fock_delay, fock_called, fock_cancelled, fock_uid, expired = false, 0, false, nil, nil
end
for stage = 1, 5 do
reset()
replies = { { body = encrypted }, { body = piracy }, { body = encrypted }, { body = "font fixture" } }
if stage <= 4 then replies[stage].delay = true else fock_delay = true end
local called = false
local job = Client.contentAsync("9", "1", function() called = true end)
local count = #requests
Assert.eq(count, math.min(stage, 4))
job.cancel()
if stage <= 4 then Assert.is_true(cancelled[stage]) else Assert.is_true(fock_cancelled) end
late()
Assert.is_false(called, "取消后迟到回调不能返回正文")
Assert.eq(#requests, count, "取消后不能启动下一阶段")
end
do
reset()
replies = {
{ body = encrypted, before = function() cfg.cookie = "ywguid=222" end },
{ body = piracy }, { body = encrypted }, { body = "font fixture" },
}
local text, err
Client.contentAsync("9", "1", function(value, reason) text, err = value, reason end)
Assert.eq(text, "decoded fixture")
Assert.is_nil(err)
for idx = 1, 3 do Assert.eq(CookieOf(requests[idx]), "ywguid=111") end
Assert.matches(requests[3].url, "fuid=1234567890123456")
Assert.eq(fock_uid, "1234567890123456")
Assert.is_nil(requests[4].opts.headers, "字体请求不带账号 Cookie")
end
do
reset()
replies = { { body = encrypted }, { body = piracy },
{ body = '{"code":0,"data":{"content":"preview","chapterVipFlag":1,"authStatus":0}}' } }
local text, err, kind
Client.contentAsync("9", "1", function(value, reason, result) text, err, kind = value, reason, result end)
Assert.is_nil(text)
Assert.eq(kind, "unpaid")
Assert.matches(err, "未购买")
Assert.eq(fock_called, 0)
Assert.is_true(Mapper.locked({ chapterVipFlag = 1, authStatus = 3 }))
end
do
reset()
replies = { { body = '{"code":0}' } }
local text, err
Client.contentAsync("9", "1", function(value, reason) text, err = value, reason end)
Assert.is_nil(text)
Assert.matches(err, "章节内容为空")
end
for _idx, body in ipairs({
'{"code":0}', '{"code":0,"data":{}}',
'{"code":0,"data":{"bookList":[false]}}',
'{"code":0,"data":{"bookList":[],"hasNext":true}}',
}) do
reset()
replies = { { body = body } }
local rows, err
Client.shelfAsync(cfg.cookie, function(value, reason) rows, err = value, reason end)
Assert.is_nil(rows, "坏书架不能当合法空快照 reconcile")
Assert.is_true(type(err) == "string")
end
do
reset()
replies = {
{ body = '{"code":0,"data":{"bookList":[{"bid":1}],"hasNext":true}}' },
{ body = '{"code":0,"data":{"bookList":[{"bid":2}],"hasNext":false}}', delay = true },
}
local called = false
local job = Client.shelfAsync(cfg.cookie, function() called = true end)
job.cancel()
Assert.is_true(cancelled[2], "同步第一页不能覆盖第二页取消句柄")
late()
Assert.is_false(called)
end
do
reset()
local ok, err, account
replies = { { body = '{"code":0,"data":{"nickname":"fixture"}}' } }
Client.loginCheckAsync("uid=123; usid=fixture", function(value, reason, id) ok, err, account = value, reason, id end)
Assert.is_true(ok)
Assert.eq(account.id, "123")
replies = { { body = '{"code":0,"data":{"nickname":"fixture"}}' } }
Client.loginCheckAsync("ywguid=123; uid=456", function(value, reason) ok, err = value, reason end)
Assert.is_nil(ok)
Assert.matches(err, "账号标识缺失")
replies = { { err = "redirect", res = { code = 302 } } }
Client.loginCheckAsync(cfg.cookie, function(value, reason) ok, err = value, reason end)
Assert.is_nil(ok)
Assert.eq(expired, err)
expired = nil
replies = { { err = "offline" } }
Client.loginCheckAsync(cfg.cookie, function(value, reason) ok, err = value, reason end)
Assert.eq(err, "offline")
Assert.is_nil(expired, "网络失败不能注销有效账号")
end
do
reset()
replies = { { body = '{"code":0,"data":{"bookList":[{"bid":9,"title":"fixture"}],"hasNext":true}}' } }
local rows, err, more
Client.searchAsync("fixture", function(value, reason, next_page) rows, err, more = value, reason, next_page end, 2)
Assert.len(rows, 1)
Assert.is_nil(err)
Assert.is_true(more)
Assert.matches(requests[1].url, "keyWord=fixture")
Assert.matches(requests[1].url, "pageNo=2")
Assert.is_nil(CookieOf(requests[1]))
Assert.is_nil(requests[1].opts.headers.Origin, "同源 GET 不发送 Origin")
replies = { { body = '{"code":0,"data":{}}' } }
Client.searchAsync("fixture", function(value, reason) rows, err = value, reason end)
Assert.is_nil(rows)
Assert.matches(err, "搜索响应无效")
replies = { { body = '<html id="__nuxt">challenge</html>' } }
Client.listAsync("/book-rank/male-sell/cycle-1", function(value, reason) rows, err = value, reason end)
Assert.is_nil(rows)
Assert.matches(err, "列表响应无效")
end
do
reset()
local filters = "20001-0-0-0-0-0-0-1"
replies = { { body = '{"code":0,"data":{"categoryList":[],"books":[],"total":0,"params":'
.. '{"categoryId2":20001,"categoryId3":0,"finishStatus":0,"priceStatus":0,"words":0,"updateTime":0,"orderBy":0,"pageNo":1}}}' } }
local data, err
Client.categoryAsync(filters, function(value, reason) data, err = value, reason end)
Assert.is_nil(err)
Assert.is_true(type(data) == "table")
Assert.eq(requests[1].opts.headers.Referer, "https://book.qq.com/book-cate/" .. filters)
Assert.is_nil(requests[1].opts.headers.Origin)
replies = { { body = '<div class="book-cate main"><a title="fixture" href="/book-detail/9">'
.. '<p class="intro">fixture</p></a></div></div>' } }
Client.listAsync("/book-cate/" .. filters, function(value, reason) data, err = value, reason end)
Assert.is_nil(err)
Assert.len(data, 1, "JSON 假空不代表官网 SSR 分类没有书")
end
do
reset()
replies = { { body = '{"code":0,"data":{"commentCount":32,"cursor":"","comments":'
.. '[{"commentId":"c1","title":"title","content":"fixture","user":{"nickname":"author"},"createTime":1620000000000}]}}' } }
local data
Client.bookCommentsAsync("9", function(value) data = value end)
local items = Mapper.bookComments(data.comments)
Assert.eq(items[1].author, "author")
Assert.eq(items[1].text, "title\nfixture")
Assert.eq(items[1].time, 1620000000)
Assert.eq(data.commentCount, 32)
Assert.is_nil(CookieOf(requests[1]))
replies = { { body = '{"code":0,"data":{"commentCount":1,"comments":[false]}}' } }
local err
Client.bookCommentsAsync("9", function(value, reason) data, err = value, reason end)
Assert.is_nil(data)
Assert.matches(err, "书评响应无效")
end
+31
View File
@@ -0,0 +1,31 @@
--[[--
QQ阅读已购 VIP 章节解密(Fock 转译版)与乱序还原离线用例。
@module tests.source.qqreader_fock_spec
--]]
---@diagnostic disable: undefined-field
local Assert = require("support.assert")
local Sample = require("support.qqreader_fock_sample")
local Fock = require("moonsource_qqreader.fock")
local Scramble = require("moonsource_qqreader.yuewen_fonts.scramble")
local plain, err = Fock.decrypt(Sample.content, Sample.chapter_id, Sample.keypool, Sample.version, Sample.fuid)
Assert.is_nil(err)
Assert.eq(#plain, 20816)
Assert.eq(plain:sub(1, 15), '<p class="p0"><')
-- 乱序还原:order 重排、first-letter 藏字 + ::after / attr() 补字、sy-0 占位丢弃、其余码位按字形表替换
local css = "yb{order:1;}ya{order:2;}yc{order:3;}"
.. ".p1 ya::first-letter{font-size:0}.p1 ya::after{content:'乙'}"
.. ".p1 yc::after{content:attr(k)}"
local html = '<p class="p1"><ya>X</ya><yc k="丙"></yc><yb>甲</yb></p>'
.. '<p>  <y class="sy-0">占</y>正<y class="sy-1">䤉</y>文䨀</p>'
local paragraphs = Scramble.paragraphs(html, css)
Assert.eq(paragraphs[1], "甲乙丙")
local rare, han = Scramble.codes(paragraphs)
Assert.eq(#rare, 2)
Assert.eq(#han, 5)
-- randomFont:认得的换、认不出的 □;fixedFont:认得的换、认不出的 □、字体里没有的码位(nil)原样留
Assert.eq(Scramble.text(paragraphs, { [0x4909] = "来" }, { [0x6B63] = "对", [0x6587] = false }),
"甲乙丙\n  对来□□")
+237
View File
@@ -0,0 +1,237 @@
--[[--
QQ阅读书架分页、目录 / 正文映射与 Cookie 解析离线用例。
@module tests.source.qqreader_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local CookieOf = require("support.cookie")
local JSONStub = require("support.json_stub")
package.preload["json"] = function()
return { decode = JSONStub.decode, encode = JSONStub.encode }
end
local cfg = {}
package.preload["utils.settings"] = function()
return { getSource = function() return cfg end }
end
local expired
package.preload["source.auth"] = function()
return { expire = function(_, msg) expired = msg; return msg end, ok = function() 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 }
local reply = table.remove(replies, 1)
cb(reply.body, reply.err, reply.res)
return { cancel = function() end }
end,
post = function(url, body, opts, cb)
requests[#requests + 1] = { method = "POST", url = url, body = body, opts = opts }
local reply = table.remove(replies, 1)
cb(reply.body, reply.err, reply.res)
return { cancel = function() end }
end,
}
end
package.loaded["moonsource_qqreader.client"] = nil
package.loaded["moonsource_qqreader.mapper"] = nil
package.loaded["moonsource_qqreader.setting"] = nil
local Client = require("moonsource_qqreader.client")
local Mapper = require("moonsource_qqreader.mapper")
local Setting = require("moonsource_qqreader.setting")
do
local book = Mapper.book({
bid = 54425396, title = "大生意人", author = "赵之羽",
category2Name = "小说", category3Name = "影视小说",
totalWords = 452453, intro = "生意人", cover = "",
})
Assert.eq(book.stable_id, "54425396")
Assert.eq(book.category, "影视小说")
Assert.eq(book.cover, "https://wfqqreader-1252317822.image.myqcloud.com/cover/396/54425396/b_54425396.jpg")
Assert.eq(book.meta.words, 452453)
Assert.eq(Mapper.book({ bid = 1, title = "甲", category2Name = "小说" }).category, "小说")
end
do
local books = Mapper.shelfList({
{ bid = 1, title = "短", info = { bid = 1, title = "全名", author = "甲", totalWords = 3 } },
{ bid = 2, title = "截断", local_book = { stable_id = "2", title = "本地", cover = "" } },
{ bid = 3, title = "兜底", author = "乙" },
})
Assert.eq(books[1].title, "全名")
Assert.eq(books[2].cover, "https://wfqqreader-1252317822.image.myqcloud.com/cover/2/2/b_2.jpg")
Assert.eq(books[3].title, "兜底")
Assert.eq(books[3].authors, "乙")
end
do
local toc = Mapper.toc({
{ cid = 1, chapterName = "第1章", free = 1 },
{ cid = 5, chapterName = "第5章", free = 0 },
})
Assert.len(toc, 2)
Assert.eq(toc[1].uid, "1")
Assert.is_false(toc[1].vip)
Assert.eq(toc[2].idx, 2)
Assert.is_true(toc[2].vip)
Assert.is_nil(Mapper.readChapter({ lastReadChapterId = 0 }))
Assert.eq(Mapper.readChapter({ lastReadChapterId = 5 }).uid, "5")
Assert.is_false(Mapper.onShelf({ inShelf = 0 }))
Assert.is_true(Mapper.onShelf({ inShelf = 1 }))
end
do
local preview = { chapterVipFlag = 1, authStatus = 0, content = "预览", encrypt = false, fontEncrypt = false }
Assert.is_true(Mapper.locked(preview))
Assert.is_false(Mapper.encrypted(preview))
Assert.is_false(Mapper.locked({ chapterVipFlag = 1, authStatus = 1, content = "已购" }))
Assert.is_false(Mapper.locked({ chapterVipFlag = 1, authStatus = 2, content = "会员" }))
Assert.is_false(Mapper.locked({ chapterVipFlag = 0, authStatus = 1, content = "免费" }))
Assert.is_true(Mapper.encrypted({ encrypt = true, content = "乱码" }))
Assert.is_true(Mapper.encrypted({ fontEncrypt = 1 }))
end
do
Assert.eq(Setting.parseCookie("ywguid=g; ywTicket=t; bad name=1"), "ywTicket=t; ywguid=g")
Assert.eq(Setting.browser.domain, "book.qq.com")
Assert.eq(Setting.browser.login:match("^https://([^/]+)"), Setting.browser.domain)
Assert.eq(Setting.browser.check:match("^https://([^/]+)"), Setting.browser.domain)
Assert.eq(Setting.browser.login:match("^https://"), "https://")
Assert.eq(Setting.fromPayload({ cookies = {
{ domain = ".qq.com", name = "ywguid", value = "g" },
{ domain = "book.qq.com", name = "ywTicket", value = "t" },
{ domain = "yuewen.com", name = "x", value = "y" },
} }), "ywTicket=t; ywguid=g")
end
-- 置顶和普通列表拼在一起,翻页直到 hasNext 为假。同号去重。
do
cfg.cookie = "ywguid=g"
requests, replies = {}, {
{ body = '{"code":0,"data":{"topBookList":[{"bid":1,"title":"甲"}],"bookList":[{"bid":2,"title":"乙"}],"hasNext":true}}' },
{ body = '{"code":0,"data":{"topBookList":[],"bookList":[{"bid":2,"title":"乙"},{"bid":3,"title":"丙"}],"hasNext":false}}' },
}
local rows, err
Client.shelfAsync(cfg.cookie, function(v, e) rows, err = v, e end)
Assert.is_nil(err)
Assert.len(rows, 3)
Assert.eq(rows[3].bid, 3)
Assert.matches(requests[1].url, "pageNo=1")
Assert.matches(requests[1].url, "pageSize=100")
Assert.eq(CookieOf(requests[1]), "ywguid=g")
Assert.matches(requests[2].url, "pageNo=2")
end
-- 分页没有页数上限:超过 50 页照样翻到底
do
requests, replies = {}, {}
for i = 1, 60 do
replies[#replies + 1] = { body = ('{"code":0,"data":{"topBookList":[],"bookList":[{"bid":%d,"title":"书"}],"hasNext":%s}}')
:format(i, i < 60 and "true" or "false") }
end
local rows, err
Client.shelfAsync(cfg.cookie, function(v, e) rows, err = v, e end)
Assert.is_nil(err)
Assert.len(rows, 60)
Assert.len(requests, 60)
end
-- 已保存的 Cookie 收到 -4 才标失效。校验新 Cookie 不污染旧登录态。
do
expired = nil
cfg.cookie = "ywguid=old"
requests, replies = {}, { { body = '{"code":-4,"msg":"登陆鉴权失败"}' } }
local ok, err
Client.loginCheckAsync("ywguid=123", function(v, e) ok, err = v, e end)
Assert.is_nil(ok)
Assert.matches(err, "扫一扫")
Assert.is_nil(expired)
requests, replies, expired = {}, { { body = '{"code":-4,"msg":"登陆鉴权失败"}' } }, nil
Client.loginCheckAsync(cfg.cookie, function(v, e) ok, err = v, e end)
Assert.matches(err, "扫一扫")
Assert.eq(expired, err)
requests, replies = {}, { { body = '{"code":0,"data":{"nickname":"甲"}}' } }
Client.loginCheckAsync("ywguid=123", function(v, e) ok, err = v, e end)
Assert.is_true(ok)
end
-- 加入 / 删除 / 进度都是 JSON。进度不传章内偏移。
do
cfg.cookie = "ywguid=g"
requests, replies = {}, { { body = '{"code":0}' } }
local ok
Client.addAsync("9", function(v) ok = v end)
Assert.is_true(ok)
Assert.eq(requests[1].url, "https://book.qq.com/api/bookshelf/add")
Assert.eq(JSONStub.decode(requests[1].body).bid, 9)
Assert.eq(requests[1].opts.content_type, "application/json")
requests, replies = {}, { { body = '{"code":0}' } }
Client.removeAsync("9", function(v) ok = v end)
Assert.is_true(ok)
Assert.eq(JSONStub.decode(requests[1].body).bids[1], 9)
requests, replies = {}, { { body = '{"code":0}' } }
Client.updateReadAsync("9", "11", function(v) ok = v end)
Assert.is_true(ok)
local body = JSONStub.decode(requests[1].body)
Assert.eq(body.bid, 9)
Assert.eq(body.lastReadChapterId, 11)
Assert.is_nil(body.lastReadOffset)
end
-- 付费预览和字体加密都不当正文,哪怕 content 非空。
do
cfg.cookie = "ywguid=g"
requests, replies = {}, {
{ body = '{"code":0,"data":{"content":"预览两百字","chapterVipFlag":1,"authStatus":0,"encrypt":false,"fontEncrypt":false}}' },
}
local text, err
Client.contentAsync("9", "5", function(v, e) text, err = v, e end)
Assert.is_nil(text)
Assert.eq(err, "QQ阅读 VIP 章节未购买")
-- 加密章:先读(发现加密)→ piracy 取 fuid / fkp → 带 fuid 重读;fkp 无效就明确失败
local enc = '{"code":0,"data":{"content":"乱码","encrypt":true,"fontEncrypt":false,"chapterVipFlag":0,"authStatus":1}}'
requests, replies = {}, {
{ body = enc },
{ body = '{"code":0,"data":{"fkp":"","fuid":"u1"}}' },
{ body = enc },
}
Client.contentAsync("9", "1", function(v, e) text, err = v, e end)
Assert.is_nil(text)
Assert.eq(err, "QQ阅读章节解密失败")
Assert.matches(requests[2].url, "/user/index/piracy")
Assert.matches(requests[3].url, "fuid=u1")
requests, replies = {}, {
{ body = '{"code":0,"data":{"content":"  甲\\r\\n  乙","chapterVipFlag":0,"authStatus":1,"encrypt":false}}' },
}
Client.contentAsync("9", "1", function(v, e) text, err = v, e end)
Assert.eq(text, "  甲\r\n  乙")
Assert.is_nil(err)
end
do
local item = '<a title="%s" href="//book.qq.com/book-detail/%s" class="wrap"><p class="intro">简&amp;介</p>'
.. '<a href="//book.qq.com/book-writer/1"><span>%s</span></a></a></div>'
local books = Mapper.searchHtml(item:format("甲", "11", "乙") .. item:format("丙", "12", "丁...")
.. item:format("甲", "11", "乙"))
Assert.len(books, 2)
Assert.eq(books[1].stable_id, "11")
Assert.eq(books[1].authors, "乙")
Assert.eq(books[1].intro, "简&介")
Assert.is_nil(books[2].authors)
Assert.eq(books[1].cover, "https://wfqqreader-1252317822.image.myqcloud.com/cover/11/11/b_11.jpg")
end
+51
View File
@@ -0,0 +1,51 @@
--[[-- QQ阅读:新账号直接替换,不查旧登录、不弹换号确认。 @module tests.moonsource_qqreader.setting_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Theme = require("support.theme_stubs")
Theme.install()
local cfg, prepared = {}, 0
package.preload["utils.settings"] = function()
return { getSource = function() return cfg end, saveSource = function() end }
end
package.preload["moonsource_qqreader.client"] = function()
return {
cookie = function() return cfg.cookie end,
loginCheckAsync = function(cookie, cb)
Assert.is_true(cookie ~= "old")
cb(true, nil, { id = cookie == "same" and "111" or "222" })
return { cancel = function() end }
end,
}
end
package.preload["source.registry"] = function()
return { reload = function() end, invalidate = function() end,
resolve = function() return { capabilities = function() return { shelf_add = true } end } end }
end
package.preload["reader.session"] = function() return { current = function() return nil end } end
package.preload["book.books_db"] = function()
return { prepareAccountSync = function(id, shelf_add)
Assert.eq(id, "ext.qqreader")
Assert.is_true(shelf_add)
Assert.eq(cfg.cookie, "old")
prepared = prepared + 1
return true
end }
end
require("source.cookie_login").fromPayload = function(payload) return payload.cookie end
local Setting = require("moonsource_qqreader.setting")
local function apply(cookie)
local ok
Setting.browser.apply({ cookie = cookie }, function(result) ok = result end)
Assert.is_true(ok)
end
cfg = { cookie = "old", account_id = "111" }
apply("same")
Assert.eq(prepared, 0)
cfg = { cookie = "old", account_id = "111" }
apply("new")
Assert.eq(prepared, 1)
Assert.eq(cfg.account_id, "222")
Assert.is_nil(Theme.last("confirm"))
cfg = { cookie = "old" }
apply("same")
Assert.eq(prepared, 2)