🚚 (split): 从 moon-source 拆出独立仓库,更新只管本插件

书源插件自成仓库、独立发版;update.lua 改为只检查并更新本插件(仓库 GarfieldMoon/moonsource-xiaoxiang),
打包、测试脚本与发布流程改为单插件,跨书源的测试按插件拆开。公共文件见 moonsource-template。

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-18 14:28:56 +08:00
1 parent 2ccf2fff37
commit 6b6eef8364
11 files changed
+1324 -651

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
--[[-- 书源插件在线更新:版本比较、只配本插件的包、校验下载地址、设置页追加更新行。本文件由模板仓库同步,勿手改。 @module tests.update.update_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
package.preload["json"] = function() return require("support.json_stub") end
local Assert = require("support.assert")
local Json = require("support.json_stub")
local PKG = "moonsource_xiaoxiang"
local DIR = "moonsource-xiaoxiang.koplugin"
local REPO = "GarfieldMoon/" .. DIR:gsub("%.koplugin$", "")
local cfg = {}
package.preload["utils.settings"] = function()
return { getSource = function() return cfg end, saveSource = function() end }
end
package.preload["utils.text"] = function()
return { trim = function(s) return (s:gsub("^%s+", ""):gsub("%s+$", "")) end }
end
package.preload["http.timeout"] = function() return { net = function() return 5 end } end
local BASE = "https://codeberg.org/" .. REPO .. "/releases/download/v1.2.3/"
local zip = DIR .. "-1.2.3.zip"
local other = "moonsource-other.koplugin-1.2.3.zip"
local release = {
tag_name = "v1.2.3", draft = false, prerelease = false, body = " 更新说明 ",
assets = {
{ name = "SHA256SUMS", size = 10, browser_download_url = BASE .. "SHA256SUMS" },
{ name = zip, size = 100, browser_download_url = BASE .. zip },
{ name = other, size = 200, browser_download_url = "https://evil.example/x.zip" },
},
}
local sums = ("a"):rep(64) .. " " .. zip .. "\n" .. ("b"):rep(64) .. " " .. other .. "\n"
local requested = {}
package.preload["http.request"] = function()
return {
get = function(url, _opts, cb)
requested[#requested + 1] = url
if url:find("/releases/latest", 1, true) then cb(Json.encode(release)) else cb(sums) end
return { cancel = function() end }
end,
}
end
local Update = require(PKG .. ".update")
-- 版本比较
Assert.is_true(Update.newer("0.6.1", "0.6.0"))
Assert.is_false(Update.newer("0.6.0", "0.6.0"))
Assert.is_false(Update.newer("0.5.9", "0.6.0"))
Assert.is_true(Update.newer("1.0.0", "1.0.0-rc.1"))
Assert.is_true(Update.newer("0.0.1", "0.0.0-dev"))
-- 只查自己仓库的 Release,只配自己的包
local got, err
Update.check(function(r, e) got, err = r, e end)
Assert.is_nil(err)
Assert.eq(requested[1], "https://codeberg.org/api/v1/repos/" .. REPO .. "/releases/latest")
Assert.eq(got.version, "1.2.3")
Assert.eq(got.notes, "更新说明")
Assert.is_true(got.available)
Assert.eq(got.target.dir, DIR)
Assert.eq(got.target.sha256, ("a"):rep(64))
Assert.is_true(cfg.update_last_checked_at ~= nil)
-- 下载地址不在本仓库 Release 下的资产被拒
release.assets[2].browser_download_url = "https://evil.example/x.zip"
local rejected
Update.check(function(_r, e) rejected = e end)
Assert.matches(rejected, "no plugin archive")
release.assets[2].browser_download_url = BASE .. zip
-- 没有 SHA256SUMS 的 Release 无效
release.assets[1] = nil
local bad_err
Update.check(function(_r, e) bad_err = e end)
Assert.matches(bad_err, "no plugin checksum")
-- 设置页「书库 → 更新」每个书源登记自己的一项
package.preload["ui.themes.widgets.row"] = function() return { widget = function(spec) return spec end } end
local entries = Update.entries({ updateView = function() end })
Assert.len(entries, 1)
Assert.eq(entries[1].group, "library")
Assert.matches(entries[1].id, "^moonsource_update_")
Assert.len(entries[1].page.sections()[1].rows, 2)
-541
View File
@@ -1,541 +0,0 @@
--[[--
长佩 / 潇湘数据映射离线用例:搜索、目录、正文解密与未购识别。
@module tests.source.changpei_xiaoxiang_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
package.preload["json"] = function() return require("support.json_stub") end
package.loaded["moonsource_xiaoxiang.mapper"] = nil
local Aes = require("crypto.aes")
local Assert = require("support.assert")
local Text = require("utils.text")
local Changpei = require("moonsource_changpei.mapper")
local Xiaoxiang = require("moonsource_xiaoxiang.mapper")
local KEY, IV = "u0LRrbu$Enm84koA", "$h$b3!iGzsYnnshj"
local function seal(plain) return Text.base64Encode(Aes.cbc_encrypt(plain, KEY, IV)) end
do
local books = Changpei.search({ data = { list = {
{ novel_id = 7, novel_name = " 书 ", novel_author = "甲", novel_desc = "简介", novel_type = "架空",
novel_cover = "https://x/a.jpg?x-oss-process=style/small", novel_wordnumber = 1000 },
{ novel_id = 8, novel_name = "乙", novel_author = "丙", novel_desc = "",
novel_cover = "https://x/nocover.jpg?x", novel_type = "" },
} } })
Assert.len(books, 2)
Assert.eq(books[1].stable_id, "7")
Assert.eq(books[1].title, "书")
Assert.eq(books[1].cover, "https://x/a.jpg")
Assert.is_nil(books[2].cover)
Assert.is_nil(books[2].category)
end
do
local toc = Changpei.toc({ data = { list = {
{ type = "item", id = 1, name = "一", price = 0 },
{ type = "item", id = 2, name = "二" },
{ type = "item", id = 3, name = "三", price = 5 },
{ type = "more" },
} } })
Assert.len(toc, 3)
Assert.eq(toc[3].uid, "3")
Assert.is_nil(toc[2].vip)
Assert.is_true(toc[3].vip)
Assert.eq(toc[1].toc_version, Changpei.TOC_VERSION)
end
do
local text = Changpei.content({ data = { chapterInfo = { chapterPrice = 0, content = seal("  第一段\r\n\r\n  第二段\n") } } })
Assert.eq(text, "  第一段\n  第二段")
local _t, _e, kind = Changpei.content({ data = { chapterInfo = { chapterPrice = 9, isSub = false, content = "" } } })
Assert.eq(kind, "unpaid")
local _t0, e0, kind0 = Changpei.content({ code = 200, msg = "操作成功",
data = { chapterInfo = { chapterPrice = "25", isSub = "0", content = "" } } })
Assert.eq(kind0, "unpaid")
Assert.is_nil(e0)
local bought = Changpei.content({ data = { chapterInfo = { chapterPrice = 9, isSub = true, content = seal("正文") } } })
Assert.eq(bought, "正文")
local _t2, _e2, verify = Changpei.content({ data = { check = 2, chapterInfo = { chapterPrice = 0, content = seal("乱码") } } })
Assert.eq(verify, "verify")
local gated, _gated_err, gated_kind = Changpei.content({ data = {
check = 2, chapterInfo = { chapterPrice = 0, content = "not-aes" },
} })
Assert.is_nil(gated, "待验证内容即使形似正文也不能交付")
Assert.eq(gated_kind, "verify", "验证前不应尝试解密不可信载荷")
local none, err = Changpei.content({ msg = "访问异常" })
Assert.is_nil(none)
Assert.eq(err, "访问异常")
local invalid, invalid_err = Changpei.content({ data = {
chapterInfo = { chapterPrice = 0, content = seal("\255\254\128bad") },
} })
Assert.is_nil(invalid)
Assert.eq(invalid_err, "长佩章节解密失败", "解密得到非法 UTF-8 不能当作正文缓存")
local control, control_err = Changpei.content({ data = {
chapterInfo = { chapterPrice = 0, content = seal("前文\0后文") },
} })
Assert.is_nil(control)
Assert.eq(control_err, "长佩章节解密失败")
Assert.eq(Changpei.content({ data = {
chapterInfo = { chapterPrice = 0, content = seal("中文🙂\t后文") },
} }), "中文🙂\t后文", "合法正文保留 emoji 与制表符")
end
do
local html = 'x"chapters":[{"type":0,"isVip":-1,"title":"正文"},'
.. '{"type":1,"ccid":"11","title":"第1章","price":0},'
.. '{"type":1,"ccid":"12","title":"第2章","price":10}],"other":{"a":1}'
local toc = Xiaoxiang.toc(html)
Assert.len(toc, 2)
Assert.eq(toc[2].uid, "12")
Assert.is_nil(toc[1].vip)
Assert.is_true(toc[2].vip)
local missing, err = Xiaoxiang.toc("<html></html>")
Assert.is_nil(missing)
Assert.eq(err, "响应无效")
end
do
local html = '<a href="/book/25866531901067904" style="" class="details-part" data-bookid="25866531901067904" data-v-1>'
.. '<div class="common-img" data-v-1><div><img src="//bookcover.yuewen.com/a/90.webp" alt="x"></div></div>'
.. '<div class="common-info"><p class="book-title" data-v-1>来日&amp;有信</p><p class="book-profile" data-v-1>简介</p>'
.. '<p class="book-module" data-v-1>千山茶客<span class="pipe">·</span>连载中<span class="pipe">·</span>21万字</p></div></a>'
.. '<a href="/book/2" data-bookid="2" data-v-1><div class="common-img"><img src="//c/2.webp"></div>'
.. '<div class="common-info"><p class="book-title">乙</p><p class="book-module">丙<span>·</span>完结</p></div></a>'
local books = Xiaoxiang.rank(html)
Assert.len(books, 2)
Assert.eq(books[1].stable_id, "25866531901067904")
Assert.eq(books[1].title, "来日&有信")
Assert.eq(books[1].authors, "千山茶客")
Assert.eq(books[1].cover, "https://bookcover.yuewen.com/a/90.webp")
Assert.eq(books[2].title, "乙")
Assert.is_nil(Xiaoxiang.rank("<html></html>"))
end
do
local function wire(chapter) return { data = { chapterContents = { chapter } } } end
Assert.eq(Xiaoxiang.content(wire({ chapterPrice = "0", auth = true, chapterContent = "\r\n\227\128\128甲\r\n\227\128\128\r\n\227\128\128乙" })), "甲\n乙")
local free, kind = Xiaoxiang.content(wire({ chapterPrice = "9", auth = false, chapterContent = "预览" }))
Assert.is_nil(free)
Assert.eq(kind, "unpaid")
Assert.eq(Xiaoxiang.content(wire({ chapterPrice = "9", auth = true, chapterContent = "全文" })), "全文")
Assert.is_nil((Xiaoxiang.content({ data = {} })))
local cipher, ckind, chapter = Xiaoxiang.content(wire({ chapterPrice = "20", auth = true, encrypt = true, chapterContent = "9HHP" }))
Assert.is_nil(cipher)
Assert.eq(ckind, "encrypted")
Assert.eq(chapter.chapterContent, "9HHP")
local rows = { { bookid = "35554414603847007", title = " 书 ", author = "甲", ccid = "35554414603847008", restype = 1 },
{ bookid = "2", title = "乙", author = "丙", ccid = "0" } }
local books = Xiaoxiang.shelfList(rows)
Assert.eq(books[1].stable_id, "35554414603847007")
Assert.eq(books[1].title, "书")
Assert.eq(books[1].cover, "https://bookcover.yuewen.com/qdbimg/349573/c_35554414603847007/180")
local progress = Xiaoxiang.progress(rows)
Assert.eq(progress["35554414603847007"], "35554414603847008")
Assert.is_nil(progress["2"])
end
do
local seen = {}
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, opts, cb)
seen.url, seen.headers = url, opts.headers
seen.cookie = opts.jar and opts.jar:header(url)
cb(seen.reply)
return {}
end,
post = function(url, body, opts, cb)
seen.post_url, seen.body, seen.headers = url, body, opts.headers
seen.cookie = opts.jar and opts.jar:header(url)
cb(seen.reply)
return {}
end,
}
end
package.loaded["moonsource_xiaoxiang.client"] = nil
local Settings = require("utils.settings")
local cfg = Settings.getSource("xiaoxiang")
cfg.cookie = "csrfToken=C1; ywKey=K"
Settings.saveSource("xiaoxiang", cfg)
local Client = require("moonsource_xiaoxiang.client")
require("source.auth").reset()
seen.reply = '{"code":0,"data":{"isLogin":0}}'
local err
Client.shelfRemoveAsync("35554414603847007", function(_wire, e) err = e end)
Assert.is_true(require("source.auth").expired("xiaoxiang"))
Assert.is_nil(seen.post_url)
seen.reply = '{"code":0,"data":{"isLogin":1,"user":{"guid":"42"}}}'
Client.userAsync("a=1", function(info) seen.info = info end)
Assert.eq(seen.info.isLogin, 1)
Assert.eq(seen.cookie, "a=1; csrfToken=moon")
Assert.matches(seen.url, "_csrfToken=moon")
local replies = { '{"code":0,"data":{"isLogin":1,"user":{"guid":"42"}}}', '{"code":0,"msg":"ok","data":null}' }
package.loaded["source.auth"].reset()
seen.reply = nil
local n = 0
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, opts, cb)
seen.url = url
if url:find("/bookshelf/pc/page", 1, true) then
cb('{"code":0,"data":{"data":[],"pageCount":0}}')
else
n = n + 1
cb(replies[1])
end
return {}
end,
post = function(url, body, opts, cb) seen.post_url, seen.body = url, body; cb(replies[2]) return {} end,
}
end
package.loaded["moonsource_xiaoxiang.client"] = nil
Client = require("moonsource_xiaoxiang.client")
local ok
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.is_true(ok)
Assert.matches(seen.post_url, "/bookshelf/delete%?_csrfToken=C1")
Assert.matches(seen.body, '"bookId":"35554414603847007"')
Assert.eq(n, 1)
-- 同一 Cookie AUTH_TTL 内不再每次先打个人接口(每章翻页报进度不再双倍请求)
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.is_true(ok)
Assert.eq(n, 1, "校验结果复用")
cfg.cookie = "csrfToken=C2; ywKey=K2"
Settings.saveSource("xiaoxiang", cfg)
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.eq(n, 2, "换了 Cookie 重新校验")
Client.AUTH_TTL = 0
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.eq(n, 4, "过期后写入与回读分别重新校验")
Client.AUTH_TTL = 600
replies[2] = '{"code":0,"data":{"chapterContents":[{"ccid":35554414603847008,"chapterPrice":"0","auth":true,"chapterContent":"甲"}]}}'
local text
Client.contentAsync("35554414603847007", "35554414603847008", function(t) text = t end)
Assert.eq(text, "甲")
Assert.matches(seen.body, '"ccids":%["35554414603847008"%]')
-- 加密章的字体下载回来是 HTML 错误页(CDN 200):不当字体去解,直接报解密失败
local fkp = require("utils.text").base64Encode('window.onkeyfocus("pool", 3)')
replies[2] = '{"code":0,"data":{"chapterContents":[{"ccid":35554414603847008,"chapterPrice":"0","auth":true,'
.. '"encrypt":true,"font":true,"fkp":"' .. fkp .. '","chapterContent":"xx",'
.. '"fontResponse":{"fixedFontTtf":"https://cdn.test/f.ttf"}}]}}'
replies[1] = "<html>Not Found</html>"
cfg.cookie = "csrfToken=C1; ywKey=K; ywGuid=42"
Settings.saveSource("xiaoxiang", cfg)
local fock_called = false
package.loaded["moonsource_xiaoxiang.fock"] = { chapterAsync = function() fock_called = true; return { cancel = function() end } end }
local ferr
text = nil
Client.contentAsync("35554414603847007", "35554414603847008", function(t, e) text, ferr = t, e end)
Assert.is_nil(text)
Assert.eq(ferr, "潇湘章节解密失败")
Assert.is_false(fock_called, "坏字体不进解密")
package.loaded["moonsource_xiaoxiang.fock"] = nil
cfg.cookie = nil
Settings.saveSource("xiaoxiang", cfg)
package.loaded["moonsource_xiaoxiang.client"] = nil
end
do
local CpSetting = require("moonsource_changpei.setting")
Assert.eq(CpSetting.parseCookie("Cookie: PHPSESSID=x; _c_n_=y\n"), "PHPSESSID=x; _c_n_=y")
Assert.eq(CpSetting.parseCookie(" "), "")
local Settings = require("utils.settings")
local Auth = require("source.auth")
local cfg = Settings.getSource("changpei")
Auth.reset()
cfg.cookie = nil
Settings.saveSource("changpei", cfg)
Assert.eq(#CpSetting.rows(), 4, "未登录:账号 + 浏览器 + 粘贴 + 章评")
cfg.cookie = "PHPSESSID=T1"
Settings.saveSource("changpei", cfg)
Assert.eq(#CpSetting.rows(), 5, "已登录:账号 + 浏览器 + 粘贴 + 退出 + 章评")
Auth.expire("changpei", "x")
Assert.eq(#CpSetting.rows(), 5, "失效后仍保留浏览器与粘贴入口")
cfg.cookie = nil
Settings.saveSource("changpei", cfg)
Auth.reset()
local XxSetting = require("moonsource_xiaoxiang.setting")
Assert.eq(XxSetting.parseCookie("Cookie: a=1; b=2\n"), "a=1; b=2")
Assert.eq(XxSetting.parseCookie("乱七八糟"), "")
end
do
local seen, reply = {}, '{"code":9996,"msg":"请先登录","data":[]}'
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return { get = function(_url, opts, cb) seen.headers = opts.headers; seen.cookie = opts.jar and opts.jar:header(_url); cb(reply) return {} end }
end
package.loaded["moonsource_changpei.client"] = nil
local Settings = require("utils.settings")
local cfg = Settings.getSource("changpei")
cfg.cookie = "PHPSESSID=T1"
Settings.saveSource("changpei", cfg)
local Client = require("moonsource_changpei.client")
local Auth = require("source.auth")
Auth.reset()
local err
Client.chaptersAsync("1", function(_wire, e) err = e end)
Assert.eq(seen.cookie, "PHPSESSID=T1")
Assert.eq(seen.headers.Client, "pc")
Assert.matches(err, "扫码登录")
Assert.is_true(Auth.expired("changpei"))
cfg.cookie = nil
Settings.saveSource("changpei", cfg)
Auth.reset()
Client.searchAsync("x", function() end)
Assert.is_nil(seen.cookie)
Assert.is_true(not Auth.expired("changpei"))
package.preload["http.request"] = nil
end
-- 书架 / 阅读历史 / 签名 POST
do
local Cp = require("moonsource_changpei.mapper")
local books = Cp.shelfList({ { id = 7, name = " 书 ", author = "甲", cover = "https://x/a.jpg?x-oss=1" } })
Assert.eq(books[1].stable_id, "7")
Assert.eq(books[1].title, "书")
Assert.eq(books[1].cover, "https://x/a.jpg")
local hist = Cp.history({ data = { list = { { list = {
{ novel_id = 9, latest_read_cid = 123 }, { novel_id = 8, latest_read_cid = 0 },
} } } } })
Assert.eq(hist["9"], "123")
Assert.is_nil(hist["8"])
local pages, posted = {}, {}
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, _opts, cb)
if url:find("system/init", 1, true) then
cb('{"code":200,"data":{"timestamp":1792556196}}')
else
local n = tonumber(url:match("page=(%d+)"))
pages[#pages + 1] = n
cb(n == 1 and '{"code":200,"data":{"list":[{"type":"novel","id":5},{"type":"shelf","id":1}]}}'
or '{"code":200,"data":{"list":[]}}')
end
return {}
end,
post = function(url, body, opts, cb)
posted = { url = url, body = body, headers = opts.headers }
cb('{"code":200,"data":[]}')
return {}
end,
}
end
package.loaded["moonsource_changpei.client"] = nil
local Client = require("moonsource_changpei.client")
local rows
Client.shelfAsync(function(r) rows = r end)
Assert.len(pages, 2)
Assert.eq(pages[2], 2)
Assert.len(rows, 1)
local ok
Client.shelfAddAsync("5", function(wire) ok = wire ~= nil end)
Assert.is_true(ok)
Assert.eq(posted.body, '{"module":"novel","cid":5,"status":1}')
Assert.eq(posted.headers.Timestamp, "1792556196")
Client.shelfAddAsync("2125021", function() end)
Assert.eq(posted.headers["Rand-Str"], "4d427f47b607d1d06ecd0d3a11fbcf7d")
Client.shelfRemoveAsync("5", function() end)
Assert.eq(posted.body, '{"module":"novel","cid":5,"status":0}')
package.preload["http.request"] = nil
end
-- 扫码登录:gRPC-web 帧与 protobuf 顶层字段
local QRLogin = require("moonsource_changpei.qrlogin")
do
Assert.eq(QRLogin.frame("Login"), "\0\0\0\0\7\10\5Login")
end
do
local token = ("a"):rep(32)
local fields = QRLogin.decode("\0\0\0\0\37\10\32" .. token .. "\16\200\1")
Assert.eq(fields[1], token)
Assert.eq(fields[2], 200)
local poll = QRLogin.decode("\0\0\0\0\36\8\4\26\32" .. token)
Assert.eq(poll[1], 4)
Assert.eq(poll[3], token)
Assert.eq(next(QRLogin.decode("")), nil)
-- login_res(2){ status 1, web_data(4){ token(1) } }
local login = QRLogin.fields("\8\1\34\4\10\2tk")
Assert.eq(login[1], 1)
Assert.eq(QRLogin.fields(login[4])[1], "tk")
end
do
local seen
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return { get = function(_url, opts, cb) seen = opts.headers; seen.Cookie = opts.jar:header(_url); cb('{"code":200,"data":{}}') return {} end }
end
package.loaded["moonsource_changpei.client"] = nil
require("moonsource_changpei.client").userAsync("PHPSESSID=a; moon_token=TK", function() end)
Assert.eq(seen.token, "TK")
Assert.eq(seen.Cookie, "PHPSESSID=a")
end
-- token 过期(9996):用 refresh_token 换新 token 写回 Cookie 再重发;换不到才算登录失效
do
local grpc_status, renew_calls, tokens = "0", 0, {}
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
header = function(res, name) return res and res[name] end,
headerAll = function() return {} end,
get = function(_url, opts, cb)
tokens[#tokens + 1] = opts.headers.token
if opts.headers.token == "OLD" then cb('{"code":9996,"msg":"请先登录"}') else cb('{"code":200,"data":{}}') end
return {}
end,
post = function(url, body, opts, cb)
renew_calls = renew_calls + 1
Assert.matches(url, "get_token")
Assert.eq(body, "\0\0\0\0\4\42\2RF")
Assert.eq(opts.headers.imei, "dev")
cb("\0\0\0\0\6\26\4NEW1", nil, { ["grpc-status"] = grpc_status })
return {}
end,
}
end
package.loaded["moonsource_changpei.client"] = nil
local Client = require("moonsource_changpei.client")
local Auth = require("source.auth")
local settings = require("utils.settings")
settings.saveSource("changpei", { cookie = "PHPSESSID=a; _c_n_=dev; moon_token=OLD; moon_refresh=RF" })
Auth.reset()
local wire
Client.historyAsync(function(w) wire = w end)
Assert.not_nil(wire)
Assert.eq(renew_calls, 1)
Assert.eq(tokens[#tokens], "NEW1")
Assert.matches(settings.getSource("changpei").cookie, "moon_token=NEW1")
Assert.matches(settings.getSource("changpei").cookie, "moon_refresh=RF")
Assert.is_false(Auth.expired("changpei"))
settings.saveSource("changpei", { cookie = "PHPSESSID=a; _c_n_=dev; moon_token=OLD; moon_refresh=RF" })
grpc_status = "16"
local failed
Client.historyAsync(function(w, err) failed = { w, err } end)
Assert.is_nil(failed[1])
Assert.matches(failed[2], "moon%-cookie%-storage")
Assert.is_true(Auth.expired("changpei"))
settings.saveSource("changpei", { cookie = "PHPSESSID=a; _c_n_=dev; moon_token=OLD" })
Auth.reset()
renew_calls = 0
Client.historyAsync(function() end)
Assert.eq(renew_calls, 0, "没有 refresh_token 不请求")
Assert.is_true(Auth.expired("changpei"))
settings.saveSource("changpei", {})
Auth.reset()
package.preload["http.request"] = nil
end
-- 书城榜单:rankingNav → 两行 chip,节点取榜单 + 子榜
do
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return { get = function(url, _opts, cb)
if url:find("rankingNav", 1, true) then
cb('{"code":200,"data":[{"name":"畅销榜","rank_id":1,"type":[{"name":"昨日","value":1},{"name":"30天","value":3}]},'
.. '{"name":"精品榜","rank_id":15,"url":"event/x","type":[]},'
.. '{"name":"人气榜","rank_id":3,"type":[{"name":"7天","value":2}]}]}')
else
cb('{"code":200,"data":{"count":1,"list":[{"novel_id":9,"novel_name":"榜书","novel_author":"甲"}]}}')
end
return {}
end }
end
package.loaded["moonsource_changpei.client"] = nil
package.loaded["moonsource_changpei.source"] = nil
local source = require("moonsource_changpei.source").new()
local res
source:browseAsync({ node = "1:3" }, function(r) res = r end)
Assert.eq(res.data[1].stable_id, "9")
Assert.len(res.groups[1].chips, 3)
Assert.eq(res.groups[1].active, "1:1")
Assert.eq(res.groups[2].active, "1:3")
package.preload["http.request"] = nil
end
-- 长佩:分页没有页数上限,超过 50 页照样翻到空页
do
local pages = {}
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, _opts, cb)
if url:find("system/init", 1, true) then
cb('{"code":200,"data":{"timestamp":1792556196}}')
else
local n = tonumber(url:match("page=(%d+)"))
pages[#pages + 1] = n
cb(n > 60 and '{"code":200,"data":{"list":[]}}'
or ('{"code":200,"data":{"list":[{"type":"novel","id":%d}]}}'):format(n))
end
return {}
end,
post = function(_url, _body, _opts, cb) cb('{"code":200,"data":[]}') return {} end,
}
end
package.loaded["moonsource_changpei.client"] = nil
local Deep = require("moonsource_changpei.client")
local rows, err
Deep.shelfAsync(function(r, e) rows, err = r, e end)
Assert.is_nil(err)
Assert.len(rows, 60)
Assert.len(pages, 61)
package.preload["http.request"] = nil
end
-- 潇湘:分页没有页数上限,pageCount 多少就翻多少页
do
local pages = 0
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, _opts, cb)
if url:find("/bookshelf/pc/page", 1, true) then
pages = pages + 1
cb('{"code":0,"data":{"pageCount":60,"data":[{"restype":1,"groupId":-100,"bookid":"1","title":"甲"}]}}')
else
cb('{"code":0,"data":{"isLogin":1,"user":{"guid":"42"}}}')
end
return {}
end,
post = function(_url, _body, _opts, cb) cb('{"code":0,"data":{"isLogin":1}}') return {} end,
}
end
local Settings = require("utils.settings")
local cfg = Settings.getSource("xiaoxiang")
cfg.cookie = "csrfToken=C1; ywKey=K"
Settings.saveSource("xiaoxiang", cfg)
package.loaded["moonsource_xiaoxiang.client"] = nil
local Deep = require("moonsource_xiaoxiang.client")
require("source.auth").reset()
local rows, err
Deep.shelfAsync(function(r, e) rows, err = r, e end)
Assert.is_nil(err)
Assert.len(rows, 60)
Assert.eq(pages, 60)
cfg.cookie = nil
Settings.saveSource("xiaoxiang", cfg)
package.loaded["moonsource_xiaoxiang.client"] = nil
package.preload["http.request"] = nil
end
+217
View File
@@ -0,0 +1,217 @@
--[[--
潇湘数据映射与客户端离线用例:目录、榜单、正文、书架、设置、分页。
@module tests.xiaoxiang.xiaoxiang_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
package.preload["json"] = function() return require("support.json_stub") end
package.loaded["moonsource_xiaoxiang.mapper"] = nil
local Assert = require("support.assert")
local Xiaoxiang = require("moonsource_xiaoxiang.mapper")
do
local html = 'x"chapters":[{"type":0,"isVip":-1,"title":"正文"},'
.. '{"type":1,"ccid":"11","title":"第1章","price":0},'
.. '{"type":1,"ccid":"12","title":"第2章","price":10}],"other":{"a":1}'
local toc = Xiaoxiang.toc(html)
Assert.len(toc, 2)
Assert.eq(toc[2].uid, "12")
Assert.is_nil(toc[1].vip)
Assert.is_true(toc[2].vip)
local missing, err = Xiaoxiang.toc("<html></html>")
Assert.is_nil(missing)
Assert.eq(err, "响应无效")
end
do
local html = '<a href="/book/25866531901067904" style="" class="details-part" data-bookid="25866531901067904" data-v-1>'
.. '<div class="common-img" data-v-1><div><img src="//bookcover.yuewen.com/a/90.webp" alt="x"></div></div>'
.. '<div class="common-info"><p class="book-title" data-v-1>来日&amp;有信</p><p class="book-profile" data-v-1>简介</p>'
.. '<p class="book-module" data-v-1>千山茶客<span class="pipe">·</span>连载中<span class="pipe">·</span>21万字</p></div></a>'
.. '<a href="/book/2" data-bookid="2" data-v-1><div class="common-img"><img src="//c/2.webp"></div>'
.. '<div class="common-info"><p class="book-title">乙</p><p class="book-module">丙<span>·</span>完结</p></div></a>'
local books = Xiaoxiang.rank(html)
Assert.len(books, 2)
Assert.eq(books[1].stable_id, "25866531901067904")
Assert.eq(books[1].title, "来日&有信")
Assert.eq(books[1].authors, "千山茶客")
Assert.eq(books[1].cover, "https://bookcover.yuewen.com/a/90.webp")
Assert.eq(books[2].title, "乙")
Assert.is_nil(Xiaoxiang.rank("<html></html>"))
end
do
local function wire(chapter) return { data = { chapterContents = { chapter } } } end
Assert.eq(Xiaoxiang.content(wire({ chapterPrice = "0", auth = true, chapterContent = "\r\n\227\128\128甲\r\n\227\128\128\r\n\227\128\128乙" })), "甲\n乙")
local free, kind = Xiaoxiang.content(wire({ chapterPrice = "9", auth = false, chapterContent = "预览" }))
Assert.is_nil(free)
Assert.eq(kind, "unpaid")
Assert.eq(Xiaoxiang.content(wire({ chapterPrice = "9", auth = true, chapterContent = "全文" })), "全文")
Assert.is_nil((Xiaoxiang.content({ data = {} })))
local cipher, ckind, chapter = Xiaoxiang.content(wire({ chapterPrice = "20", auth = true, encrypt = true, chapterContent = "9HHP" }))
Assert.is_nil(cipher)
Assert.eq(ckind, "encrypted")
Assert.eq(chapter.chapterContent, "9HHP")
local rows = { { bookid = "35554414603847007", title = " 书 ", author = "甲", ccid = "35554414603847008", restype = 1 },
{ bookid = "2", title = "乙", author = "丙", ccid = "0" } }
local books = Xiaoxiang.shelfList(rows)
Assert.eq(books[1].stable_id, "35554414603847007")
Assert.eq(books[1].title, "书")
Assert.eq(books[1].cover, "https://bookcover.yuewen.com/qdbimg/349573/c_35554414603847007/180")
local progress = Xiaoxiang.progress(rows)
Assert.eq(progress["35554414603847007"], "35554414603847008")
Assert.is_nil(progress["2"])
end
do
local seen = {}
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, opts, cb)
seen.url, seen.headers = url, opts.headers
seen.cookie = opts.jar and opts.jar:header(url)
cb(seen.reply)
return {}
end,
post = function(url, body, opts, cb)
seen.post_url, seen.body, seen.headers = url, body, opts.headers
seen.cookie = opts.jar and opts.jar:header(url)
cb(seen.reply)
return {}
end,
}
end
package.loaded["moonsource_xiaoxiang.client"] = nil
local Settings = require("utils.settings")
local cfg = Settings.getSource("xiaoxiang")
cfg.cookie = "csrfToken=C1; ywKey=K"
Settings.saveSource("xiaoxiang", cfg)
local Client = require("moonsource_xiaoxiang.client")
require("source.auth").reset()
seen.reply = '{"code":0,"data":{"isLogin":0}}'
local err
Client.shelfRemoveAsync("35554414603847007", function(_wire, e) err = e end)
Assert.is_true(require("source.auth").expired("xiaoxiang"))
Assert.is_nil(seen.post_url)
seen.reply = '{"code":0,"data":{"isLogin":1,"user":{"guid":"42"}}}'
Client.userAsync("a=1", function(info) seen.info = info end)
Assert.eq(seen.info.isLogin, 1)
Assert.eq(seen.cookie, "a=1; csrfToken=moon")
Assert.matches(seen.url, "_csrfToken=moon")
local replies = { '{"code":0,"data":{"isLogin":1,"user":{"guid":"42"}}}', '{"code":0,"msg":"ok","data":null}' }
package.loaded["source.auth"].reset()
seen.reply = nil
local n = 0
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, opts, cb)
seen.url = url
if url:find("/bookshelf/pc/page", 1, true) then
cb('{"code":0,"data":{"data":[],"pageCount":0}}')
else
n = n + 1
cb(replies[1])
end
return {}
end,
post = function(url, body, opts, cb) seen.post_url, seen.body = url, body; cb(replies[2]) return {} end,
}
end
package.loaded["moonsource_xiaoxiang.client"] = nil
Client = require("moonsource_xiaoxiang.client")
local ok
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.is_true(ok)
Assert.matches(seen.post_url, "/bookshelf/delete%?_csrfToken=C1")
Assert.matches(seen.body, '"bookId":"35554414603847007"')
Assert.eq(n, 1)
-- 同一 Cookie AUTH_TTL 内不再每次先打个人接口(每章翻页报进度不再双倍请求)
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.is_true(ok)
Assert.eq(n, 1, "校验结果复用")
cfg.cookie = "csrfToken=C2; ywKey=K2"
Settings.saveSource("xiaoxiang", cfg)
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.eq(n, 2, "换了 Cookie 重新校验")
Client.AUTH_TTL = 0
Client.shelfRemoveAsync("35554414603847007", function(wire) ok = wire ~= nil end)
Assert.eq(n, 4, "过期后写入与回读分别重新校验")
Client.AUTH_TTL = 600
replies[2] = '{"code":0,"data":{"chapterContents":[{"ccid":35554414603847008,"chapterPrice":"0","auth":true,"chapterContent":"甲"}]}}'
local text
Client.contentAsync("35554414603847007", "35554414603847008", function(t) text = t end)
Assert.eq(text, "甲")
Assert.matches(seen.body, '"ccids":%["35554414603847008"%]')
-- 加密章的字体下载回来是 HTML 错误页(CDN 200):不当字体去解,直接报解密失败
local fkp = require("utils.text").base64Encode('window.onkeyfocus("pool", 3)')
replies[2] = '{"code":0,"data":{"chapterContents":[{"ccid":35554414603847008,"chapterPrice":"0","auth":true,'
.. '"encrypt":true,"font":true,"fkp":"' .. fkp .. '","chapterContent":"xx",'
.. '"fontResponse":{"fixedFontTtf":"https://cdn.test/f.ttf"}}]}}'
replies[1] = "<html>Not Found</html>"
cfg.cookie = "csrfToken=C1; ywKey=K; ywGuid=42"
Settings.saveSource("xiaoxiang", cfg)
local fock_called = false
package.loaded["moonsource_xiaoxiang.fock"] = { chapterAsync = function() fock_called = true; return { cancel = function() end } end }
local ferr
text = nil
Client.contentAsync("35554414603847007", "35554414603847008", function(t, e) text, ferr = t, e end)
Assert.is_nil(text)
Assert.eq(ferr, "潇湘章节解密失败")
Assert.is_false(fock_called, "坏字体不进解密")
package.loaded["moonsource_xiaoxiang.fock"] = nil
cfg.cookie = nil
Settings.saveSource("xiaoxiang", cfg)
package.loaded["moonsource_xiaoxiang.client"] = nil
end
do
local XxSetting = require("moonsource_xiaoxiang.setting")
Assert.eq(XxSetting.parseCookie("Cookie: a=1; b=2\n"), "a=1; b=2")
Assert.eq(XxSetting.parseCookie("乱七八糟"), "")
end
do
local pages = 0
package.loaded["http.request"] = nil
package.preload["http.request"] = function()
return {
get = function(url, _opts, cb)
if url:find("/bookshelf/pc/page", 1, true) then
pages = pages + 1
cb('{"code":0,"data":{"pageCount":60,"data":[{"restype":1,"groupId":-100,"bookid":"1","title":"甲"}]}}')
else
cb('{"code":0,"data":{"isLogin":1,"user":{"guid":"42"}}}')
end
return {}
end,
post = function(_url, _body, _opts, cb) cb('{"code":0,"data":{"isLogin":1}}') return {} end,
}
end
local Settings = require("utils.settings")
local cfg = Settings.getSource("xiaoxiang")
cfg.cookie = "csrfToken=C1; ywKey=K"
Settings.saveSource("xiaoxiang", cfg)
package.loaded["moonsource_xiaoxiang.client"] = nil
local Deep = require("moonsource_xiaoxiang.client")
require("source.auth").reset()
local rows, err
Deep.shelfAsync(function(r, e) rows, err = r, e end)
Assert.is_nil(err)
Assert.len(rows, 60)
Assert.eq(pages, 60)
cfg.cookie = nil
Settings.saveSource("xiaoxiang", cfg)
package.loaded["moonsource_xiaoxiang.client"] = nil
package.preload["http.request"] = nil
end
+111
View File
@@ -0,0 +1,111 @@
--[[--
阅文三源的设备扫码入口:各用自己的应用参数,先校验再保存。
起点真 csrf 由 loginCheck 合并书架 Set-Cookie,扫码回调不再造假。
@module tests.yuewen_qr_setting_spec(qidian / qqreader / xiaoxiang 仓库各一份,只测本仓库存在的书源)
--]]
local Assert = require("support.assert")
local beginLogin = require("source.auth").beginLogin
local Themed = require("support.theme_stubs")
Themed.install()
package.preload["ui.themes.widgets.row"] = function()
return { widget = function(spec) return spec end }
end
package.preload["ui/network/manager"] = function()
return { runWhenConnected = function(_self, fn) fn() end }
end
package.preload["source.review_setting"] = function()
return { row = function() return function() return {} end end }
end
package.preload["source.category_setting"] = package.preload["source.review_setting"]
local configs, saved_id, authorized, reloads = {}, nil, nil, 0
package.preload["utils.settings"] = function()
return {
getSource = function(id) configs[id] = configs[id] or {}; return configs[id] end,
saveSource = function(id) saved_id = id end,
}
end
package.loaded["source.auth"] = nil
package.preload["source.auth"] = function()
return { beginLogin = beginLogin, ok = function(id) authorized = id end, expired = function() return false end }
end
package.preload["source.registry"] = function()
return { reload = function() reloads = reloads + 1 end }
end
local options, on_cookie, started, checked, reject
for _idx, name in ipairs({ "qqreader", "qidian", "xiaoxiang" }) do
package.preload["moonsource_" .. name .. ".yuewen_qr"] = function()
return { new = function(_self, spec, cb)
options, on_cookie = spec, cb
return { start = function() started = true end }
end }
end
end
for _idx, name in ipairs({ "qqreader", "qidian", "xiaoxiang" }) do
local id = name
package.preload["moonsource_" .. name .. ".client"] = function()
return {
cookie = function() return configs[id] and configs[id].cookie end,
loginCheckAsync = function(cookie, cb)
checked = cookie
cb(not reject, reject and "rejected" or nil, { id = "123" })
end,
userAsync = function(cookie, cb)
checked = cookie
cb(not reject and { id = "123" } or nil, reject and "rejected" or nil)
end,
}
end
end
-- 每个书源一个仓库:只测本仓库里存在的那几家
local SOURCES = {}
for _idx, source in ipairs({
{ id = "qqreader", app_id = "1450000220", area_id = "1" },
{ id = "qidian", app_id = "13", area_id = "31" },
{ id = "xiaoxiang", app_id = "57", area_id = "5" },
}) do
if package.searchpath("moonsource_" .. source.id .. ".setting", package.path) then SOURCES[#SOURCES + 1] = source end
end
for _idx, source in ipairs(SOURCES) do
local Setting = require("moonsource_" .. source.id .. ".setting")
started, checked, saved_id, authorized = nil, nil, nil, nil
local rows = Setting.rows()
rows[1](400).callback()
Assert.is_true(started, "账号行直接启动设备扫码")
Assert.eq(options.app_id, source.app_id)
Assert.eq(options.area_id, source.area_id)
Assert.is_nil(saved_id, "仅获取二维码不能保存登录态")
reject = true
on_cookie("ywguid=guid; ywkey=bad")
Assert.is_nil(saved_id, "各源的账号校验失败时不保存")
Assert.is_nil(authorized)
Assert.eq(Themed.last("toast").text, "rejected")
Assert.is_true(Themed.last("progress_dialog").closed)
reject = false
on_cookie("ywguid=guid; ywkey=key")
Assert.eq(saved_id, source.id)
Assert.eq(authorized, source.id)
Assert.eq(configs[source.id].cookie, checked)
Assert.eq(checked, "ywguid=guid; ywkey=key")
Assert.eq(Themed.last("toast").text, "登录成功")
started = nil
Setting.login()
Assert.is_true(started, "登录失效的直达入口也走设备扫码")
if source.id == "qqreader" then
Assert.eq(Setting.browser.domain, "book.qq.com")
Assert.eq(Setting.browser.login, "https://book.qq.com/book-shelf")
Assert.eq(Setting.browser.check, "https://book.qq.com/book-shelf")
else
local paste
for _idx, factory in ipairs(Setting.rows()) do
local row = factory(400)
if row.title == "粘贴 Cookie" then paste = row; break end
end
Assert.not_nil(paste)
paste.callback()
Assert.eq(Themed.last("input").title, source.id == "qidian" and "起点登录 Cookie" or "潇湘登录 Cookie")
end
end
Assert.eq(reloads, #SOURCES)