✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。 Co-authored-by: Copilot <[email protected]>
This commit is contained in:
commit
0907702861
27 files changed
+18687
No files matched your search
@@ -0,0 +1,82 @@
|
||||
--[[--
|
||||
潇湘动态分类、标签、完整分页及取消。
|
||||
|
||||
@module tests.moonsource_xiaoxiang.browse_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
local A = require("support.assert")
|
||||
local null = function() end
|
||||
local JSON = require("support.json_stub")
|
||||
package.loaded["json"] = { decode = function(wire)
|
||||
if wire == "null" then return null end
|
||||
return JSON.decode(wire)
|
||||
end }
|
||||
local calls, waiting, stopped, mode = {}, nil, false, "sync"
|
||||
---@type {id: integer, name: string, labelClassList: {name?: string, tagInfoList?: (fun())|string|{tagId: integer, tagName: string}[]}[]}[]
|
||||
local categories = {
|
||||
{ id = 0, name = "全部", labelClassList = {} },
|
||||
{ id = 30013, name = "古代言情", labelClassList = {
|
||||
{ name = "全部标签", tagInfoList = null },
|
||||
{ tagInfoList = { { tagId = 61000007, tagName = "女强" }, { tagId = 61000007, tagName = "女强" } } },
|
||||
} },
|
||||
{ id = 30020, name = "现代言情", labelClassList = {} },
|
||||
}
|
||||
local function reply(index, cb)
|
||||
if mode == "error" and index == 2 then cb(nil, "网络失败"); return end
|
||||
local rows = {}
|
||||
for i = 1, 20 do rows[i] = { cbId = tostring((index - 1) * 20 + i), bookName = "书" } end
|
||||
cb({ data = { categoryList = categories, bookList = rows, total = 100 } })
|
||||
end
|
||||
package.loaded["moonsource_xiaoxiang.client"] = {
|
||||
libraryAsync = function(params, index, cb)
|
||||
calls[#calls + 1] = { params = params, index = index }
|
||||
if mode == "waiting" and index == 2 then waiting = cb else reply(index, cb) end
|
||||
return { cancel = function() stopped = true end }
|
||||
end,
|
||||
}
|
||||
package.loaded["moonsource_xiaoxiang.browse"] = nil
|
||||
local Library = require("moonsource_xiaoxiang.browse")
|
||||
local result, err
|
||||
Library.run({}, function(data, reason) result, err = data, reason end)
|
||||
A.is_nil(err)
|
||||
A.len(result.data, 60)
|
||||
A.len(calls, 3)
|
||||
A.eq(result.data[60].stable_id, "60")
|
||||
A.eq(result.groups[1].title, "分类")
|
||||
local chosen = result.groups[1].chips[2].id
|
||||
A.eq(chosen, "cate:-1:-1:-1:-1:10:30013:-1")
|
||||
Library.run({ node = chosen }, function(data) result = data end)
|
||||
A.eq(result.groups[2].title, "标签")
|
||||
A.len(result.groups[2].chips, 2, "重复标签不重复显示")
|
||||
local tagged = result.groups[2].chips[2].id
|
||||
Library.run({ node = tagged, limit = 20 }, function(data) result = data end)
|
||||
A.eq(calls[#calls].params.tag, 61000007)
|
||||
A.eq(result.groups[1].chips[3].id, "cate:-1:-1:-1:-1:10:30020:-1", "换分类清标签")
|
||||
Library.run({ node = "cate:1:0:2:25:6:30013:-1", limit = 20 }, function(data) result = data end)
|
||||
A.eq(calls[#calls].params.state, 1)
|
||||
A.eq(calls[#calls].params.charge, 0)
|
||||
A.eq(calls[#calls].params.words, 25)
|
||||
A.len(result.data, 20)
|
||||
for _idx, invalid in ipairs({ "invalid", function() end }) do
|
||||
categories[2].labelClassList[1].tagInfoList = invalid
|
||||
Library.run({ node = chosen }, function(data, reason) result, err = data, reason end)
|
||||
A.is_nil(result, "未知函数和坏类型不能冒充 JSON null")
|
||||
A.eq(err, "响应无效")
|
||||
end
|
||||
categories[2].labelClassList[1].tagInfoList = null
|
||||
mode = "error"
|
||||
Library.run({}, function(data, reason) result, err = data, reason end)
|
||||
A.is_nil(result, "中间页失败不能伪装完整列表")
|
||||
A.eq(err, "网络失败")
|
||||
mode, stopped, result = "waiting", false, nil
|
||||
local handle = Library.run({}, function(data) result = data end)
|
||||
handle.cancel()
|
||||
A.is_true(stopped, "同步第一页不能覆盖第二页取消句柄")
|
||||
reply(2, waiting)
|
||||
A.is_nil(result)
|
||||
mode = "sync"
|
||||
Library.run({ node = "cate:-1:-1:-1:-1:99:0:-1" }, function(data, reason) result, err = data, reason end)
|
||||
require("support.stubs").flush()
|
||||
A.is_nil(result)
|
||||
A.eq(err, "潇湘分类条件无效")
|
||||
@@ -0,0 +1,541 @@
|
||||
--[[--
|
||||
长佩 / 潇湘数据映射离线用例:搜索、目录、正文解密与未购识别。
|
||||
|
||||
@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>来日&有信</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("ext.xiaoxiang")
|
||||
cfg.cookie = "csrfToken=C1; ywKey=K"
|
||||
Settings.saveSource("ext.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("ext.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("ext.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("ext.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("ext.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("ext.changpei")
|
||||
Auth.reset()
|
||||
cfg.cookie = nil
|
||||
Settings.saveSource("ext.changpei", cfg)
|
||||
Assert.eq(#CpSetting.rows(), 4, "未登录:账号 + 浏览器 + 粘贴 + 章评")
|
||||
cfg.cookie = "PHPSESSID=T1"
|
||||
Settings.saveSource("ext.changpei", cfg)
|
||||
Assert.eq(#CpSetting.rows(), 5, "已登录:账号 + 浏览器 + 粘贴 + 退出 + 章评")
|
||||
Auth.expire("ext.changpei", "x")
|
||||
Assert.eq(#CpSetting.rows(), 5, "失效后仍保留浏览器与粘贴入口")
|
||||
cfg.cookie = nil
|
||||
Settings.saveSource("ext.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("ext.changpei")
|
||||
cfg.cookie = "PHPSESSID=T1"
|
||||
Settings.saveSource("ext.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("ext.changpei"))
|
||||
cfg.cookie = nil
|
||||
Settings.saveSource("ext.changpei", cfg)
|
||||
Auth.reset()
|
||||
Client.searchAsync("x", function() end)
|
||||
Assert.is_nil(seen.cookie)
|
||||
Assert.is_true(not Auth.expired("ext.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("ext.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("ext.changpei").cookie, "moon_token=NEW1")
|
||||
Assert.matches(settings.getSource("ext.changpei").cookie, "moon_refresh=RF")
|
||||
Assert.is_false(Auth.expired("ext.changpei"))
|
||||
|
||||
settings.saveSource("ext.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("ext.changpei"))
|
||||
|
||||
settings.saveSource("ext.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("ext.changpei"))
|
||||
settings.saveSource("ext.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("ext.xiaoxiang")
|
||||
cfg.cookie = "csrfToken=C1; ywKey=K"
|
||||
Settings.saveSource("ext.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("ext.xiaoxiang", cfg)
|
||||
package.loaded["moonsource_xiaoxiang.client"] = nil
|
||||
package.preload["http.request"] = nil
|
||||
end
|
||||
@@ -0,0 +1,91 @@
|
||||
--[[--
|
||||
潇湘账号失效、换号竞态、写后确认和多阶段取消。
|
||||
|
||||
@module tests.moonsource_xiaoxiang.client_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
local A = require("support.assert")
|
||||
package.loaded["json"] = require("support.json_stub")
|
||||
local cfg = { cookie = "csrfToken=x%26; ywguid=42" }
|
||||
package.loaded["utils.settings"] = { getSource = function() return cfg end }
|
||||
local network, pending, posts, cancelled, shelf = "sync", nil, 0, false, {}
|
||||
local function json(value) return require("json").encode(value) end
|
||||
package.loaded["http.request"] = {
|
||||
get = function(url, opts, cb)
|
||||
A.eq(opts.cache_ttl, 0)
|
||||
if url:find("/user/auth/personal", 1, true) then
|
||||
if network == "wait" then pending = cb
|
||||
elseif network == "failure" then cb(nil, "timeout")
|
||||
elseif network == "expired" then cb('{"code":0,"data":{"isLogin":0}}')
|
||||
else cb('{"code":0,"data":{"isLogin":1,"user":{"guid":42}}}') end
|
||||
else
|
||||
if network == "shelf-wait" then pending = cb
|
||||
else cb(json({ code = 0, data = { data = shelf, pageCount = 1 } })) end
|
||||
end
|
||||
return { cancel = function() cancelled = true end }
|
||||
end,
|
||||
post = function(url, _body, opts, cb)
|
||||
posts = posts + 1
|
||||
A.matches(url, "_csrfToken=x%%2526")
|
||||
A.eq(opts.cache_ttl, 0)
|
||||
cb('{"code":0,"data":null}')
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
package.loaded["moonsource_xiaoxiang.client"] = nil
|
||||
local C = require("moonsource_xiaoxiang.client")
|
||||
local Auth = require("source.auth")
|
||||
Auth.reset()
|
||||
local info, err, result
|
||||
C.userAsync(cfg.cookie, function(value) info = value end)
|
||||
A.eq(info.id, "42")
|
||||
network = "failure"
|
||||
C.shelfAsync(function(value, reason) result, err = value, reason end)
|
||||
A.is_nil(result)
|
||||
A.eq(err, "timeout")
|
||||
A.is_false(Auth.expired("ext.xiaoxiang"), "暂时网络失败保留登录")
|
||||
network = "expired"
|
||||
C.shelfAsync(function(value, reason) result, err = value, reason end)
|
||||
A.is_true(Auth.expired("ext.xiaoxiang"))
|
||||
A.eq(posts, 0)
|
||||
Auth.reset()
|
||||
network = "wait"
|
||||
C.shelfAsync(function(value, reason) result, err = value, reason end)
|
||||
cfg.cookie = "csrfToken=new; ywguid=43"
|
||||
pending('{"code":0,"data":{"isLogin":0}}')
|
||||
A.is_false(Auth.expired("ext.xiaoxiang"), "旧请求不能注销新账号")
|
||||
A.eq(err, "账号已变化,请重新登录")
|
||||
cfg.cookie = "csrfToken=x%26; ywguid=42"
|
||||
network, cancelled, info = "wait", false, nil
|
||||
local user = C.userAsync(cfg.cookie, function(value) info = value end)
|
||||
user.cancel()
|
||||
pending('{"code":0,"data":{"isLogin":1,"user":{"guid":42}}}')
|
||||
A.is_nil(info)
|
||||
A.is_true(cancelled)
|
||||
network = "sync"
|
||||
shelf = { { bookid = "123", ccid = "77", restype = 1, groupId = -100 } }
|
||||
C.shelfAddAsync("123", function(value, reason) result, err = value, reason end)
|
||||
A.is_true(result ~= nil)
|
||||
C.shelfRemoveAsync("123", function(value, reason) result, err = value, reason end)
|
||||
A.is_nil(result)
|
||||
A.eq(err, "潇湘远端状态未更新,请稍后重试")
|
||||
C.progressAsync("123", "77", function(value) result = value end)
|
||||
A.is_true(result ~= nil)
|
||||
C.progressAsync("123", "78", function(value, reason) result, err = value, reason end)
|
||||
A.is_nil(result)
|
||||
A.eq(err, "潇湘远端状态未更新,请稍后重试")
|
||||
shelf = {}
|
||||
C.shelfRemoveAsync("123", function(value) result = value end)
|
||||
A.is_true(result ~= nil)
|
||||
network, cancelled, result = "shelf-wait", false, nil
|
||||
local writing = C.shelfRemoveAsync("123", function(value) result = value end)
|
||||
writing.cancel()
|
||||
A.is_true(cancelled, "同步 POST 后取消实际回读请求")
|
||||
pending('{"code":0,"data":{"data":[],"pageCount":1}}')
|
||||
A.is_nil(result)
|
||||
network = "sync"
|
||||
shelf = { { restype = 1, groupId = -100, title = "缺身份" } }
|
||||
C.shelfAsync(function(value, reason) result, err = value, reason end)
|
||||
A.is_nil(result)
|
||||
A.eq(err, "响应无效", "坏书架行不能变成身份为 nil 的书")
|
||||
@@ -0,0 +1,57 @@
|
||||
--[[--
|
||||
正文归属、扫码 Cookie 大小写与取消后的迟到响应。
|
||||
|
||||
@module tests.moonsource_xiaoxiang.content_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
local A = require("support.assert")
|
||||
local JSON = require("support.json_stub")
|
||||
package.loaded["json"] = JSON
|
||||
local cfg, mode, post_cb, decrypt_cb, stopped = { cookie = "ywguid=42" }, "sync", nil, nil, false
|
||||
package.loaded["utils.settings"] = { getSource = function() return cfg end }
|
||||
local chapter = { ccid = "123", chapterPrice = 20, auth = true, encrypt = true, font = false,
|
||||
chapterContent = "encrypted", fkp = require("utils.text").base64Encode('window.onkeyfocus("pool", 3)') }
|
||||
package.loaded["http.request"] = {
|
||||
post = function(_url, _body, _opts, cb)
|
||||
post_cb = cb
|
||||
if mode == "sync" then cb(JSON.encode({ code = 0, data = { chapterContents = { chapter } } })) end
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
package.loaded["moonsource_xiaoxiang.fock"] = {
|
||||
chapterAsync = function(_chapter, _font, _id, _pool, _version, uid, cb)
|
||||
A.eq(uid, "42", "官网同时接受 ywGuid 与扫码生成的小写 ywguid")
|
||||
decrypt_cb = cb
|
||||
local handle = {}
|
||||
function handle:cancel() A.eq(self, handle); stopped = true end
|
||||
return handle
|
||||
end,
|
||||
}
|
||||
package.loaded["moonsource_xiaoxiang.client"] = nil
|
||||
local C = require("moonsource_xiaoxiang.client")
|
||||
local result, err
|
||||
local handle = C.contentAsync("book", "123", function(text, reason) result, err = text, reason end)
|
||||
handle.cancel()
|
||||
A.is_true(stopped, "同步正文请求不能覆盖解密句柄")
|
||||
decrypt_cb("迟到正文")
|
||||
A.is_nil(result)
|
||||
mode, result = "wait", nil
|
||||
handle = C.contentAsync("book", "123", function(text) result = text end)
|
||||
handle.cancel()
|
||||
post_cb(JSON.encode({ code = 0, data = { chapterContents = { chapter } } }))
|
||||
A.is_nil(result)
|
||||
C.contentAsync("book", "123", function(text, reason) result, err = text, reason end)
|
||||
cfg.cookie = "ywguid=43"
|
||||
post_cb(JSON.encode({ code = 0, data = { chapterContents = { chapter } } }))
|
||||
A.is_nil(result)
|
||||
A.eq(err, "账号已变化,请重新登录")
|
||||
mode, cfg.cookie = "sync", "ywguid=42"
|
||||
chapter.ccid = "456"
|
||||
C.contentAsync("book", "123", function(text, reason) result, err = text, reason end)
|
||||
A.is_nil(result)
|
||||
A.eq(err, "响应无效", "错误章节不能落入请求章节缓存")
|
||||
chapter.ccid, chapter.auth = "123", false
|
||||
C.contentAsync("book", "123", function(text, reason, kind) result, err = text, kind or reason end)
|
||||
A.is_nil(result)
|
||||
A.eq(err, "unpaid")
|
||||
@@ -0,0 +1,54 @@
|
||||
--[[--
|
||||
潇湘新站结构、长整数身份及旧目录兼容。
|
||||
|
||||
@module tests.moonsource_xiaoxiang.mapper_spec
|
||||
--]]
|
||||
|
||||
local A = require("support.assert")
|
||||
package.loaded["json"] = require("support.json_stub")
|
||||
local M = require("moonsource_xiaoxiang.mapper")
|
||||
|
||||
---@type {cbId: string, ccid: string, ratio: number, exp: number, text: string}|nil
|
||||
local decoded = M.decode('{"cbId" : 27760376302677004, "ccid":35554414603847008,"ratio":1.5,"exp":12345678901234567e2,'
|
||||
.. '"text":"{\\"cbId\\":27760376302677004}"}')
|
||||
A.eq(decoded.cbId, "27760376302677004")
|
||||
A.eq(decoded.ccid, "35554414603847008")
|
||||
A.eq(decoded.ratio, 1.5)
|
||||
A.eq(decoded.exp, 12345678901234567e2)
|
||||
A.eq(decoded.text, '{"cbId":27760376302677004}')
|
||||
A.is_nil(M.decode("not JSON"))
|
||||
|
||||
local function page(data)
|
||||
return '<script id="vite-plugin-ssr_pageContext" type="application/json">'
|
||||
.. '{"pageContext":{"pageProps":{"pageData":' .. data .. '}}}</script>'
|
||||
end
|
||||
local book = M.detail(page('{"detailData":{"cbid":"27760376302677004","title":" 灯花笑 ","author":"千山茶客",'
|
||||
.. '"intro":"简介","categoryShortName":"古代言情","totalWords":1200}}'))
|
||||
A.eq(book.stable_id, "27760376302677004")
|
||||
A.eq(book.title, "灯花笑")
|
||||
A.eq(book.category, "古代言情")
|
||||
A.eq(book.words, 1200)
|
||||
A.is_nil(M.detail("<html>维护</html>"))
|
||||
A.is_nil(M.books({ { title = "缺身份" } }))
|
||||
A.len(M.books({}), 0)
|
||||
local books = M.books({ { cbId = "123", bookName = "书", authorName = "作者", cate2Name = "分类" } })
|
||||
A.eq(books[1].authors, "作者")
|
||||
|
||||
local toc = M.toc(page('{"chapters":[{"type":0,"title":"正文卷"},'
|
||||
.. '{"type":1,"ccid":35554414603847008,"title":"第1章","price":0},'
|
||||
.. '{"type":1,"ccid":"35554414603847009","title":"第2章","price":25}]}'))
|
||||
A.len(toc, 2)
|
||||
A.eq(toc[1].uid, "35554414603847008")
|
||||
A.eq(toc[2].idx, 2)
|
||||
A.is_nil(toc[1].vip)
|
||||
A.is_true(toc[2].vip)
|
||||
A.eq(toc[1].toc_version, 1)
|
||||
A.is_nil(M.toc(page('{"chapters":[{"type":1,"title":"缺身份"}]}')))
|
||||
local legacy = M.toc('x"chapters":[{"type":0},{"type":1,"ccid":"42","title":"旧章","price":0}]')
|
||||
A.eq(legacy[1].uid, "42")
|
||||
A.len(M.toc(page('{"chapters":[]}')), 0)
|
||||
local ranks = page('[{"columnInfo":{"name":"潇湘票榜"},"bookList":[]},'
|
||||
.. '{"columnInfo":{"name":"收藏榜"},"bookList":[{"cbid":"123","title":"收藏书"}]}]')
|
||||
A.len(M.rank(ranks, "潇湘票榜"), 0, "真实空榜单与坏页面分别处理")
|
||||
A.eq(M.rank(ranks, "收藏榜")[1].title, "收藏书")
|
||||
A.is_nil(M.rank(ranks, "不存在的榜单"))
|
||||
@@ -0,0 +1,58 @@
|
||||
--[[--
|
||||
进度编排保护同步取消句柄与目录返回前的账号变更。
|
||||
|
||||
@module tests.moonsource_xiaoxiang.progress_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
|
||||
|
||||
local A = require("support.assert")
|
||||
local cookie, delayed, toc_cb, write_cb, stopped, writes = "old", false, nil, nil, false, 0
|
||||
local toc = { { uid = "77", title = "第一章" }, { uid = "78", title = "第二章" } }
|
||||
package.loaded["moonsource_xiaoxiang.client"] = {
|
||||
cookie = function() return cookie end,
|
||||
shelfAsync = function(cb)
|
||||
cb({ { bookid = "123", ccid = "78" } })
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
progressAsync = function(_book, _chapter, cb)
|
||||
writes = writes + 1
|
||||
write_cb = cb
|
||||
return { cancel = function() stopped = true end }
|
||||
end,
|
||||
}
|
||||
package.loaded["source.toc"] = { index = function() return 2 end }
|
||||
package.loaded["moonsource_xiaoxiang.source"] = nil
|
||||
local source = require("moonsource_xiaoxiang.source").new()
|
||||
function source:loadTocAsync(_identity, cb)
|
||||
toc_cb = cb
|
||||
if not delayed then cb(toc) end
|
||||
return { cancel = function() stopped = true end }
|
||||
end
|
||||
local identity = { source_id = "ext.xiaoxiang", stable_id = "123" }
|
||||
local result, err
|
||||
source:getProgressAsync(identity, function(data) result = data end)
|
||||
A.eq(result.chapter_idx, 2)
|
||||
A.eq(result.fraction, 0.5)
|
||||
delayed, stopped, result = true, false, nil
|
||||
local handle = source:getProgressAsync(identity, function(data) result = data end)
|
||||
handle.cancel()
|
||||
A.is_true(stopped)
|
||||
toc_cb(toc)
|
||||
A.is_nil(result)
|
||||
source:getProgressAsync(identity, function(data, reason) result, err = data, reason end)
|
||||
cookie = "new"
|
||||
toc_cb(toc)
|
||||
A.is_nil(result)
|
||||
A.eq(err, "账号已变化,请重新登录")
|
||||
cookie, delayed, stopped = "old", false, false
|
||||
handle = source:putProgressAsync(identity, { chapter_idx = 2 }, function(data) result = data end)
|
||||
handle.cancel()
|
||||
A.is_true(stopped)
|
||||
write_cb({})
|
||||
A.is_nil(result)
|
||||
delayed = true
|
||||
source:putProgressAsync(identity, { chapter_idx = 2 }, function(data, reason) result, err = data, reason end)
|
||||
cookie = "new"
|
||||
toc_cb(toc)
|
||||
A.eq(writes, 1, "不能把旧书进度推到新账号")
|
||||
A.eq(err, "账号已变化,请重新登录")
|
||||
@@ -0,0 +1,51 @@
|
||||
--[[-- 潇湘账号:续期不动同步状态,换号保留本地记录。 @module tests.moonsource_xiaoxiang.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_xiaoxiang.client"] = function()
|
||||
return {
|
||||
cookie = function() return cfg.cookie end,
|
||||
userAsync = function(cookie, cb)
|
||||
Assert.is_true(cookie ~= "session=old")
|
||||
cb({ id = cookie == "session=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.xiaoxiang")
|
||||
Assert.is_true(shelf_add)
|
||||
Assert.eq(cfg.cookie, "session=old")
|
||||
prepared = prepared + 1
|
||||
return true
|
||||
end }
|
||||
end
|
||||
local Setting = require("moonsource_xiaoxiang.setting")
|
||||
local function apply(value)
|
||||
local ok
|
||||
Setting.browser.apply({ cookies = { { name = "session", value = value, domain = ".xxsypro.com" } } },
|
||||
function(result) ok = result end)
|
||||
Assert.is_true(ok)
|
||||
end
|
||||
cfg = { cookie = "session=old", account_id = "111" }
|
||||
apply("same")
|
||||
Assert.eq(prepared, 0)
|
||||
cfg = { cookie = "session=old", account_id = "111" }
|
||||
apply("new")
|
||||
Assert.eq(prepared, 1)
|
||||
Assert.eq(cfg.account_id, "222")
|
||||
Assert.is_nil(Theme.last("confirm"))
|
||||
cfg = { cookie = "session=old" }
|
||||
apply("same")
|
||||
Assert.eq(prepared, 2)
|
||||
Reference in new issue
Block a user