Files
moonsource-changpei/tests/changpei/changpei_spec.lua
T
GarfieldandCopilot cc00c43b4c 🚚 (split): 从 moon-source 拆出独立仓库,更新只管本插件
书源插件自成仓库、独立发版;update.lua 改为只检查并更新本插件(仓库 GarfieldMoon/moonsource-changpei),
打包、测试脚本与发布流程改为单插件,跨书源的测试按插件拆开。公共文件见 moonsource-template。

Co-authored-by: Copilot <[email protected]>
2026-11-18 14:28:56 +08:00

338 lines
14 KiB
Lua
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
长佩数据映射与客户端离线用例:搜索、目录、正文解密与未购识别、书架、登录、榜单、分页。
@module tests.changpei.changpei_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
package.preload["json"] = function() return require("support.json_stub") end
package.loaded["moonsource_changpei.mapper"] = nil
local Aes = require("crypto.aes")
local Assert = require("support.assert")
local Text = require("utils.text")
local Changpei = require("moonsource_changpei.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 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()
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