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

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

218 lines
9.7 KiB
Lua

--[[--
潇湘数据映射与客户端离线用例:目录、榜单、正文、书架、设置、分页。
@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