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

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

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-16 12:08:03 +08:00
commit 0c6553d8d9
24 files changed
+2756

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
--[[-- 长佩 HTTP 续期竞态、多阶段取消及进度确认。 @module tests.moonsource_changpei.async_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 cfg = { cookie = "PHPSESSID=old; _c_n_=device; moon_refresh=refresh; moon_token=old" }
local late, expired, cancelled, phase, history_uid = nil, false, false, "renew", "123"
local user_data
local chapter_requests = 0
local after_read_uid
package.preload["utils.settings"] = function()
return { getSource = function() return cfg end, saveSource = function() end }
end
package.preload["source.auth"] = function()
return { ok = function() end, expire = function(_id, msg) expired = true; return msg end }
end
package.preload["moonsource_changpei.qrlogin"] = function()
return { frame = function() return "request" end, decode = function() return { [3] = "new-token" } end }
end
package.preload["http.request"] = function()
return {
get = function(url, opts, cb)
Assert.eq(opts.cache_ttl, 0)
if phase == "retry" then
if opts.headers.token == "old" then cb(Json.encode({ code = 9996 }))
else late = function() cb(Json.encode({ code = 200, data = { list = {} } })) end end
elseif phase == "read" then
if url:find("chapterGetInfo", 1, true) then
chapter_requests = chapter_requests + 1
if after_read_uid then history_uid = after_read_uid end
cb(Json.encode({ code = 200, data = { check = 1, chapterInfo = {} } }))
else
cb(Json.encode({ code = 200, data = { list = {
{ list = { { novel_id = 1, latest_read_cid = history_uid } } },
} } }))
end
elseif phase == "user" then
cb(Json.encode({ code = 200, data = user_data }))
elseif phase == "user_error" then
cb(nil, "network")
elseif phase == "post" then
cb(Json.encode({ code = 200, data = { timestamp = 1 } }))
else late = function() cb(Json.encode({ code = 200, data = {} })) end end
return { cancel = function() cancelled = true end }
end,
post = function(_url, _body, _opts, cb)
if phase == "retry" then cb("grpc", nil, {})
else late = cb end
return { cancel = function() cancelled = true end }
end,
header = function() return "0" end,
headerAll = function() return {} end,
}
end
local Client = require("moonsource_changpei.client")
local ok, err
Client.renewAsync(function(value, reason) ok, err = value, reason end)
cfg.cookie = "PHPSESSID=another"
late("grpc", nil, {})
Assert.is_false(ok)
Assert.matches(err, "账号已变化")
Assert.eq(cfg.cookie, "PHPSESSID=another")
cfg.cookie = "PHPSESSID=old; _c_n_=device; moon_refresh=refresh; moon_token=old"
Client.renewAsync(function(value, reason) ok, err = value, reason end)
late(nil, "network", {})
Assert.is_false(ok)
Assert.eq(err, "network")
Assert.is_false(expired, "暂时网络故障不注销账号")
phase, cancelled = "retry", false
local called = false
local handle = Client.chaptersAsync("1", function() called = true end)
handle.cancel()
Assert.is_true(cancelled)
late()
Assert.is_false(called, "同步续期不能覆盖重发请求的句柄")
phase = "read"
Client.markReadAsync("123", function(value, reason) ok, err = value, reason end)
Assert.is_true(ok)
Assert.eq(chapter_requests, 0, "历史已对齐时不再取章触发验证")
history_uid = "999"
Client.markReadAsync("123", function(value, reason) ok, err = value, reason end)
Assert.is_nil(ok)
Assert.eq(err, "长佩阅读进度未确认")
Assert.eq(chapter_requests, 1)
after_read_uid = "123"
Client.markReadAsync("123", function(value, reason) ok, err = value, reason end)
Assert.is_true(ok, "历史不一致时仍取章并回读确认")
Assert.eq(chapter_requests, 2)
phase, cancelled, called = "read-late", false, false
handle = Client.markReadAsync("123", function() called = true end)
late()
handle:cancel()
Assert.is_true(cancelled, "历史返回后,取消应停止后续取章请求")
late()
Assert.is_false(called)
phase, user_data = "user", { info = { show_uid = 111, nick_name = "测试" }, verify = true }
local account
Client.userAsync(cfg.cookie, function(value, reason) account, err = value, reason end)
Assert.eq(account.uid, 111, "旧 JSON 接口的稳定身份在 data.info.show_uid,与官网 gRPC uid 一致")
Assert.is_nil(account.nick_name, "登录边界只保留身份,不传播个人资料")
user_data = { uid = 111, info = { show_uid = 0 } }
Client.userAsync(cfg.cookie, function(value, reason) account, err = value, reason end)
Assert.is_nil(account)
Assert.eq(err, "长佩账号标识缺失", "坏身份仍须拒绝,不能删掉检查绕过")
user_data = {}
Client.userAsync(cfg.cookie, function(value, reason) account, err = value, reason end)
Assert.is_nil(account)
Assert.eq(err, "长佩账号标识缺失")
phase = "user_error"
Client.userAsync(cfg.cookie, function(value, reason) account, err = value, reason end)
Assert.is_nil(account)
Assert.eq(err, "network")
phase = "post"
Client.shelfAddAsync("1", function(value, reason) ok, err = value, reason end)
cfg.cookie = "PHPSESSID=another"
late(Json.encode({ code = 200, data = {} }), nil, {})
Assert.is_nil(ok)
Assert.matches(err, "账号已变化", "旧书架请求不能被新账号当成同步成功")
local source = require("moonsource_changpei.source").new()
Client.historyAsync = function(cb)
late = cb
return { cancel = function() cancelled = true end }
end
called, cancelled = false, false
handle = source:getProgressAsync({ stable_id = "1" }, function() called = true end)
handle.cancel()
Assert.is_true(cancelled, "取消进度拉取必须停止仍在执行的历史请求")
late({ data = { list = {} } })
Assert.is_false(called)
Client.historyAsync = function(cb)
cb({ data = { list = { { list = { { novel_id = 1, latest_read_cid = "123" } } } } } })
return { cancel = function() error("finished history request cancelled") end }
end
source.loadTocAsync = function(_self, _identity, cb)
late = cb
return { cancel = function() cancelled = true end }
end
called, cancelled = false, false
handle = source:getProgressAsync({ stable_id = "1" }, function() called = true end)
handle.cancel()
Assert.is_true(cancelled, "同步历史回调不能覆盖后续目录请求的取消句柄")
late({})
Assert.is_false(called)
+65
View File
@@ -0,0 +1,65 @@
--[[-- 长佩分类、筛选、多页与取消。 @module tests.moonsource_changpei.browse_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local pages, query, late, cancelled = {}, nil, nil, false
local bad_page, delayed = false, false
package.preload["moonsource_changpei.client"] = function()
return {
typesAsync = function(id, cb)
local rows = id and { { id = 1, type_name = "现代" }, { id = 3, type_name = "古代" } }
or { { id = 75, type_name = "纯爱" }, { id = 74, type_name = "言情" } }
cb({ data = { list = rows } })
return { cancel = function() error("finished category request cancelled") end }
end,
libraryAsync = function(params, page, cb)
query = params
pages[#pages + 1] = page
local function done()
if bad_page and page == 2 then cb(nil, "network"); return end
local list = {}
for idx = 1, 10 do
list[idx] = { novel_id = (page - 1) * 10 + idx, novel_name = "书", novel_author = "作者",
novel_info = "分类简介", novel_type = "" }
end
cb({ data = { list = list, count = 50 } })
end
if delayed then late = done else done() end
return { cancel = function() cancelled = true end }
end,
}
end
local Library = require("moonsource_changpei.browse")
local result, err
Library.run({ limit = 30 }, function(value, reason) result, err = value, reason end)
Assert.len(result.data, 30)
Assert.eq(#pages, 3)
Assert.eq(result.data[1].intro, "分类简介")
Assert.eq(result.groups[1].title, "频道")
Assert.eq(query.field, 8)
Assert.is_nil(query.sell)
Library.run({ node = "cate:75:1:2:0:3:4", limit = 10 }, function(value) result = value end)
Assert.eq(query.tid, 1)
Assert.eq(query.process, 2)
Assert.eq(query.sell, 0, "免费不是未设置")
Assert.eq(query.number, 3)
Assert.eq(query.field, 4)
Assert.eq(result.groups[2].active, "cate:75:1:2:0:3:4")
Assert.eq(result.groups[1].active, "cate:75:1:2:0:3:4")
Assert.eq(result.groups[1].chips[2].id, result.groups[1].active, "当前频道保留子分类并显示选中")
local switched = result.groups[1].chips[3].id
Assert.eq(switched, "cate:74:0:2:0:3:4", "换频道清分类,保留通用筛选")
bad_page = true
Library.run({ limit = 30 }, function(value, reason) result, err = value, reason end)
Assert.is_nil(result)
Assert.eq(err, "network", "中间页失败不能伪装成完整结果")
bad_page, delayed = false, true
local called = false
local handle = Library.run({ limit = 30 }, function() called = true end)
handle.cancel()
Assert.is_true(cancelled, "同步导航不能覆盖后续分页的取消句柄")
late()
Assert.is_false(called)
+277
View File
@@ -0,0 +1,277 @@
--[[--
长佩客户端续期与正文权限回归用例。
@module tests.moonsource_changpei.client_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 requests, posts, jobs = {}, {}, {}
local settings = { cookie = "PHPSESSID=s; _c_n_=device; _c_n_n_=device; moon_refresh=refresh; moon_token=old" }
local expired
local check = 3
local read_check_code = 200
local get_cookies, post_cookies
local content_text = "chapter text"
local renewed_token = "new"
local defer_content, delayed_content
local signed
package.preload["ffi/sha2"] = function()
return { md5 = function(value) signed = value; return "signature" end }
end
package.preload["moonsource_changpei.mapper"] = function()
return {
content = function(wire)
local level = wire.data.check
if level > 1 then return content_text, nil, "verify" end
return content_text
end,
}
end
package.preload["utils.settings"] = function()
return {
getSource = function() return settings end,
saveSource = function() end,
}
end
package.preload["source.auth"] = function()
return {
expire = function(_id, message) expired = message; return message end,
ok = function() end,
}
end
package.preload["workers.job"] = function()
return {
run = function(run, options)
jobs[#jobs + 1] = function() options.on_done(run()) end
return { cancel = function() end }
end,
}
end
package.preload["moonsource_changpei.qrlogin"] = function()
return {
frame = function() return "refresh-request" end,
decode = function() return { [3] = renewed_token } end,
}
end
-- 模拟 Request.request 的 opts.jar 接线:发前带 Cookie,回包收 Set-Cookie
local function viaJar(url, options, callback, deliver)
local jar, since = options.jar, options.jar and options.jar.seq
local headers = {}
for key, value in pairs(options.headers or {}) do headers[key] = value end
headers.Cookie = headers.Cookie or (jar and jar:header(url))
return headers, function(body, err, res)
if jar and res then jar:absorb(url, res.headers["Set-Cookie"] or {}, since) end
callback(body, err, res)
end
end
package.preload["http.request"] = function()
return {
get = function(url, options, callback)
local headers
headers, callback = viaJar(url, options, callback)
requests[#requests + 1] = { url = url, headers = headers }
local body = Json.encode({ code = 200, data = url:find("/system/init", 1, true)
and { timestamp = 123456 } or { check = check } })
local cookies = get_cookies
local function respond() callback(body, nil, { headers = { ["Set-Cookie"] = cookies } }) end
if defer_content and url:find("/novel/chapterGetInfo", 1, true) then delayed_content = respond
else respond() end
return { cancel = function() end }
end,
post = function(_url, _body, options, callback)
local headers
headers, callback = viaJar(_url, options, callback)
posts[#posts + 1] = { url = _url, body = _body, headers = headers }
if _url:find("/novel/readCheck", 1, true) then
callback(Json.encode({ code = 200, data = { code = read_check_code } }),
nil, { headers = { ["Set-Cookie"] = post_cookies } })
else
callback("grpc-body", nil, { headers = {} })
end
return { cancel = function() end }
end,
header = function() return "0" end,
headerAll = function(res) return res.headers and res.headers["Set-Cookie"] or {} end,
}
end
local Client = require("moonsource_changpei.client")
local result, message, result_kind
Client.contentAsync("n", "c", function(text, err, kind) result, message, result_kind = text, err, kind end)
Assert.eq(#requests, 1)
Assert.eq(requests[1].headers.token, "old")
jobs[1]()
Assert.is_nil(result)
Assert.eq(#requests, 1, "check > 1 不是 token 过期信号,不应重试章节请求")
Assert.eq(#posts, 0, "不可读章节不应触发 token 续期")
Assert.eq(message, Client.VERIFY_REQUIRED)
Assert.eq(result_kind, "verify")
Assert.is_nil(expired, "check > 1 不应把有效登录标记为失效")
local renewed
Client.renewAsync(function(ok) renewed = ok end)
Assert.is_true(renewed, "refresh token 可单独续期")
Assert.eq(settings.cookie, "PHPSESSID=s; _c_n_=device; _c_n_n_=device; moon_refresh=refresh; moon_token=new")
Assert.eq(#posts, 1, "显式续期请求一次")
Assert.eq(posts[1].headers.token, "old")
Assert.eq(posts[1].headers.Cookie, "PHPSESSID=s; _c_n_=device; _c_n_n_=device")
Assert.eq(posts[1].headers.imei, "device")
Assert.eq(posts[1].headers["_c_n_"], "device")
Assert.eq(posts[1].headers["X-Grpc-Web"], "1")
local verified, verify_err
Client.readCheckAsync('proof"value', function(ok, err) verified, verify_err = ok, err end)
Assert.is_true(verified)
Assert.is_nil(verify_err)
Assert.eq(posts[2].url, "https://www.gongzicp.com/webapi/novel/readCheck")
Assert.eq(Json.decode(posts[2].body).token, 'proof"value')
Assert.eq(posts[2].headers.token, "new", "验证用设备登录态,浏览器不用重新登录")
Assert.eq(posts[2].headers.Timestamp, "123456")
Assert.eq(posts[2].headers["Rand-Str"], "signature")
Assert.eq(signed, 'SIa7xu0LRrbu$En3*I1T3EiO^40cN49D&token=proof"value&123456')
read_check_code = 400
Client.readCheckAsync("invalid-proof", function(ok, err) verified, verify_err = ok, err end)
Assert.is_false(verified, "外层 200 不代表滑块验证成功")
Assert.eq(verify_err, "验证失败,请重新滑动验证")
local verification_done, verification_cancelled
package.preload["moonsource_changpei.verify"] = function()
return {
new = function(cb)
verification_done = cb
return { cancel = function() verification_cancelled = true end }
end,
}
end
-- check == 2 也必须重取,不能把验证前的可疑正文写进缓存。
check, result, message, verification_done = 2, nil, nil, nil
content_text = "untrusted before verification"
local first_job = #jobs + 1
local before = #requests
Client.openContentAsync("n", "c", function(text, err) result, message = text, err end)
jobs[first_job]()
Assert.not_nil(verification_done, "用户开章遇验证才打开扫码页")
Assert.is_nil(message, "等待验证时不提前结束开书")
check, content_text = 1, "chapter text"
verification_done(true)
Assert.eq(#requests, before + 2, "check < 3 验完也重取章节")
jobs[first_job + 1]()
Assert.eq(result, "chapter text", "只能交付验证后重新取得的正文")
Assert.is_nil(message)
-- check >= 3:验完像官网 reload,重取一次;重取降到 1 则成功。
check, result, message, verification_done = 3, nil, nil, nil
first_job = #jobs + 1
Client.openContentAsync("n", "c", function(text, err) result, message = text, err end)
jobs[first_job]()
Assert.not_nil(verification_done)
check = 1
verification_done(true)
jobs[first_job + 1]()
Assert.eq(result, "chapter text", "check >= 3 验证后自动重新取章")
Assert.is_nil(message)
check, result, verification_done, verification_cancelled = 3, nil, nil, nil
first_job = #jobs + 1
local handle = Client.openContentAsync("n", "c", function(text) result = text end)
jobs[first_job]()
handle:cancel()
Assert.is_true(verification_cancelled)
before = #requests
verification_done(true)
Assert.eq(#requests, before, "取消后迟到的验证结果不能重开章节")
Assert.is_nil(result)
verification_done, message = nil, nil
first_job = #jobs + 1
Client.openContentAsync("n", "c", function(_text, err) message = err end)
jobs[first_job]()
verification_done(false, "verification cancelled")
Assert.eq(message, "verification cancelled")
-- check >= 3 且重取仍被门控:即使有正文也不收下,只报错、不反复弹二维码。
check, result, message, verification_done = 3, nil, nil, nil
first_job = #jobs + 1
Client.openContentAsync("n", "c", function(_text, err) message = err end)
jobs[first_job]()
verification_done(true)
verification_done = nil
jobs[first_job + 1]()
Assert.is_nil(verification_done, "验证后仍被拦截只报错,不反复弹二维码")
Assert.eq(message, Client.VERIFY_REQUIRED)
-- 后台预取遇验证不回正文,避免未验证章进缓存。
result, message, result_kind = nil, nil, nil
Client.contentAsync("n", "c", function(text, err, kind) result, message, result_kind = text, err, kind end)
jobs[#jobs]()
Assert.is_nil(result)
Assert.eq(message, Client.VERIFY_REQUIRED)
Assert.eq(result_kind, "verify")
-- 普通登录请求与 readCheck 回包轮换会话;后续每章复用,不要求用户重扫。
check, read_check_code, verification_done = 1, 200, nil
local saved_cookie = settings.cookie
get_cookies = { "PHPSESSID=bootstrap; Path=/; HttpOnly" }
Client.chaptersAsync("n", function() end)
get_cookies = { "PHPSESSID=anonymous-init; Path=/" }
post_cookies = { "PHPSESSID=verified; Path=/; HttpOnly", "reader_pass=ok; Path=/" }
Client.readCheckAsync("proof", function(ok) verified = ok end)
Assert.is_true(verified)
Assert.matches(posts[#posts].headers.Cookie, "PHPSESSID=bootstrap", "验证沿用登录请求建立的会话")
Assert.eq(settings.cookie, saved_cookie, "会话轮换不修改保存的登录凭据")
get_cookies, post_cookies = nil, nil
first_job = #jobs + 1
Client.openContentAsync("n", "next", function(text, err) result, message = text, err end)
Assert.matches(requests[#requests].headers.Cookie, "PHPSESSID=verified")
Assert.matches(requests[#requests].headers.Cookie, "reader_pass=ok")
jobs[first_job]()
Assert.is_nil(verification_done)
Assert.eq(result, "chapter text")
-- 验证前发出的预取回包不能把已验证的新会话覆盖回旧会话。
defer_content = true
get_cookies = { "PHPSESSID=stale-prefetch; Path=/" }
Client.contentAsync("n", "prefetch", function() end)
defer_content, get_cookies = false, nil
post_cookies = { "PHPSESSID=verified-new; Path=/" }
Client.readCheckAsync("new-proof", function(ok) verified = ok end)
post_cookies = nil
delayed_content()
jobs[#jobs]()
Client.contentAsync("n", "next", function() end)
Assert.matches(requests[#requests].headers.Cookie, "PHPSESSID=verified%-new",
"旧请求迟到时不得回滚扫码后建立的会话")
jobs[#jobs]()
get_cookies = { "PHPSESSID=anonymous; Path=/" }
Client.detailAsync("n", function() end)
get_cookies = nil
renewed_token = "newer"
Client.renewAsync(function(ok) renewed = ok end)
Assert.is_true(renewed)
Client.contentAsync("n", "next", function() end)
Assert.matches(requests[#requests].headers.Cookie, "PHPSESSID=verified%-new",
"匿名请求不能覆盖已验证会话,token 续期也要保留它")
Assert.eq(requests[#requests].headers.token, "newer")
jobs[#jobs]()
for _idx, age in ipairs({ "060", "-1", "0" }) do
get_cookies = { "reader_pass=value; Max-Age=" .. age .. "; Path=/" }
Client.contentAsync("n", "next", function() end)
jobs[#jobs]()
get_cookies = nil
Client.contentAsync("n", "next", function() end)
Assert.eq(requests[#requests].headers.Cookie:find("reader_pass=", 1, true) ~= nil, tonumber(age) > 0,
"仅保留正 Max-Age 会话 Cookie")
jobs[#jobs]()
end
settings.cookie = "PHPSESSID=another-account; moon_token=other"
Client.contentAsync("n", "next", function() end)
Assert.eq(requests[#requests].headers.Cookie, "PHPSESSID=another-account", "换号不能继承旧验证 Cookie")
Assert.eq(requests[#requests].headers.token, "other")
jobs[#jobs]()
+52
View File
@@ -0,0 +1,52 @@
--[[-- 长佩二维码帧、网络错误与关闭后的迟到回调。 @module tests.moonsource_changpei.qrlogin_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Theme = require("support.theme_stubs")
Theme.install()
local late, cancelled, sync = nil, false, true
local polling_timeout
package.preload["http.request"] = function()
return {
get = function(_url, _opts, cb)
if sync then cb("", nil, {}) else late = cb end
return { cancel = function() cancelled = true end }
end,
post = function(url, _body, opts, cb)
if url:match("/qrcode_polling$") then polling_timeout = opts.timeout end
late = cb
return { cancel = function() cancelled = true end }
end,
headerAll = function() return { "PHPSESSID=anonymous; Path=/; HttpOnly" } end,
header = function() return "0" end,
}
end
local QR = require("moonsource_changpei.qrlogin")
local value = ("a"):rep(200)
Assert.eq(QR.decode(QR.frame(value))[1], value, "refresh token 长度用 protobuf varint")
Assert.eq(QR.decode(QR.frame("ok") .. "\128\0\0\0\0")[1], "ok", "不把 gRPC trailer 当 protobuf")
Assert.is_nil(next(QR.decode("\0\0\0\0\9short")), "截断帧不是成功回包")
local called = false
local login = QR:new(function() called = true end)
login:start()
Assert.not_nil(late)
login:cancel()
Assert.is_true(cancelled, "同步匿名会话不能覆盖获取二维码的句柄")
late(QR.frame("token"), nil, {})
Assert.is_nil(Theme.last("qr"))
Assert.is_false(called)
login = QR:new(function() called = true end)
login:_poll(login.generation, "token", os.time())
Assert.is_true(polling_timeout > 30, "官网长轮询必须能等到约 30 秒的待扫码响应")
late(nil, "network unavailable", {})
Assert.eq(Theme.last("confirm").text, "network unavailable", "网络错误不冒充二维码过期")
sync, cancelled = false, false
login = QR:new(function() called = true end)
login:start()
login:cancel()
late("", nil, {})
Assert.is_true(cancelled)
Assert.is_false(called)
+60
View File
@@ -0,0 +1,60 @@
--[[-- 长佩书评与真正章评,分页、归属和坏响应。 @module tests.moonsource_changpei.reviews_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Mapper = require("moonsource_changpei.mapper")
local rows = {}
for idx = 1, 10 do
rows[idx] = { replay_id = idx, replay_module = "novel_chapter", replay_cid = 123,
replay_content = "评论", replay_nickname = "读者", child_comment_number = 2 }
end
local page = Mapper.comments({ data = { list = rows, count = 0 } }, "chapter", "123", 1)
Assert.len(page.items, 10)
Assert.is_true(page.more, "官网 count=0 不代表没有评论")
Assert.eq(page.items[1].replies, 2)
Assert.eq(page.max_idx, 1)
local ending = Mapper.comments({ data = { list = { rows[1] }, count = 0 } }, "chapter", "123", 2)
Assert.eq(ending.total, 11)
Assert.is_false(ending.more)
Assert.len(Mapper.comments({ data = { list = {}, count = 0 } }, "chapter", "123", 1).items, 0)
Assert.is_nil((Mapper.comments({ data = {} }, "chapter", "123", 1)))
Assert.is_nil((Mapper.comments({ data = { list = rows } }, "chapter", "124", 1)), "不把旧章评论挂到新章")
Assert.is_nil((Mapper.comments({ data = { list = rows } }, "novel", "123", 1)), "章评不能冒充书评")
local mixed = { replay_id = 9, replay_module = "novel_chapter", replay_cid = 123, novel_id = 7,
novel_chapter_name = "第二章", replay_content = "章末讨论" }
local book_page = Mapper.comments({ data = { list = { mixed }, count = 0 } }, "novel", "7", 1)
Assert.eq(book_page.items[1].reply_to, "第二章", "全书列表汇总章评时保留章节归属")
Assert.is_nil((Mapper.comments({ data = { list = { mixed } } }, "novel", "8", 1)))
local late, cancelled
local WithUid = require("source.with_chapter_uid")
local called = false
local job = WithUid.run({ loadTocAsync = function(_self, _identity, cb)
cb({ { uid = "123" } })
return { cancel = function() error("stale toc cancelled") end }
end }, { chapter_idx = 1 }, "missing", function(uid, cb)
Assert.eq(uid, "123")
late = cb
return { cancel = function() cancelled = true end }
end, function() called = true end)
job.cancel()
Assert.is_true(cancelled)
late({})
Assert.is_false(called)
local detail = Mapper.detail({ data = { novel_id = 1, novel_name = "书", author_nickname = "作者",
novel_info = "长简介", novel_desc = "短简介", type_names = "纯爱" } })
Assert.eq(detail.intro, "长简介")
Assert.eq(detail.authors, "作者")
local toc = Mapper.toc({ data = { list = {
{ type = "volume", name = "上卷" }, { type = "item", id = 1, name = "第一章", price = 0 },
{ type = "volume", name = "下卷" }, { type = "item", id = 2, name = "第二章", price = 2, is_sub = true },
} } })
Assert.eq(toc[2].uid, "2", "跨卷仍按官网顺序连续编号")
Assert.is_true(toc[2].paid)
Assert.is_nil((Mapper.toc({ data = {} })))
local history = Mapper.history({ data = { list = {
{ list = { { novel_id = 1, latest_read_cid = 20 } } },
{ list = { { novel_id = 1, latest_read_cid = 10 } } },
} } })
Assert.eq(history["1"], "20", "旧日期记录不能覆盖最新进度")
+52
View File
@@ -0,0 +1,52 @@
--[[-- 长佩账号切换不询问,数字 uid 规范化后判断同账号续期。 @module tests.moonsource_changpei.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_changpei.client"] = function()
return {
cookie = function() return cfg.cookie end,
userAsync = function(cookie, cb)
Assert.is_true(cookie ~= "session=old", "不查询旧账号")
cb({ uid = 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.changpei")
Assert.is_true(shelf_add)
Assert.eq(cfg.cookie, "session=old", "准备失败时还须保留旧账号")
prepared = prepared + 1
return true
end }
end
local Setting = require("moonsource_changpei.setting")
local function apply(value)
local ok
Setting.browser.apply({ cookies = { { name = "session", value = value, domain = ".gongzicp.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)
Assert.eq(cfg.account_id, "111")
+141
View File
@@ -0,0 +1,141 @@
--[[--
长佩滑块验证会话:票据隔离、错误重试、取消与过期。
@module tests.moonsource_changpei.verify_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Themed = require("support.theme_stubs")
Themed.install()
local Ext = require("utils.extension")
Ext.reset()
local cookie = "device-session"
local submit, submit_cancelled, submitted_token
package.preload["l10n"] = function()
return { apply = function() end, get = function() return require("gettext") end }
end
package.preload["moonsource_changpei.client"] = function()
return {
cookie = function() return cookie end,
readCheckAsync = function(token, cb)
submitted_token, submit = token, cb
return { cancel = function() submit_cancelled = true end }
end,
}
end
local timers = {}
package.preload["ui/uimanager"] = function()
return {
scheduleIn = function(_self, _delay, fn) timers[fn] = true end,
unschedule = function(_self, fn) timers[fn] = nil end,
}
end
package.preload["ui.guard"] = function() return { wrap = function(_label, fn) return fn end } end
local path
Ext.add("web.open_page", function(url, _title, _text, opts)
path = url
return require("ui.themes.widgets.qr"):new{ close_callback = opts.close_callback }:show()
end)
local Verification = require("moonsource_changpei.verify")
local route = Ext.list("web.routes")[1]
local status, response, headers
local function request(action, params, method)
route.handle(method or "POST", action, params, function(code, body, extra)
status, response, headers = code, body, extra
end)
end
local finished, finish_err
local function newSession()
return Verification.new(function(ok, err) finished, finish_err = ok, err end)
end
local function send(ticket_value, code)
request("verify", { ticket = ticket_value, token = code })
end
local session = newSession()
local ticket = path:match("#(.+)$")
Assert.matches(path, "^/changpei_verify%.html#%x+$")
request("config", { ticket = ticket })
Assert.eq(status, 200)
Assert.eq(response.title, "长佩滑块验证")
Assert.is_nil(response.cookie, "配置不得泄露设备 Cookie")
Assert.eq(headers[1], "Cache-Control: no-store")
request("config", { ticket = "wrong" })
Assert.eq(status, 403)
request("verify", { ticket = ticket, token = {} })
Assert.eq(status, 400)
request("verify", { ticket = ticket, token = string.rep("a", 16385) })
Assert.eq(status, 400)
request("verify", { ticket = ticket, token = "proof" }, "GET")
Assert.eq(status, 405)
request("unknown", { ticket = ticket })
Assert.eq(status, 404)
send(ticket, "proof")
Assert.eq(submitted_token, "proof")
send(ticket, "another")
Assert.eq(status, 409, "同一会话不能并发提交滑块")
submit(false, "server rejected")
Assert.eq(status, 400)
Assert.eq(response.error, "server rejected")
Assert.is_nil(finished, "验证失败保留二维码让用户重试")
send(ticket, "retry-proof")
submit(true)
Assert.eq(status, 200)
Assert.is_true(finished)
Assert.is_nil(finish_err)
Assert.is_true(session.dialog.closed)
Assert.is_nil(next(timers))
send(ticket, "replay")
Assert.eq(status, 403, "成功票据不可重放")
finished = nil
session = newSession()
ticket = path:match("#(.+)$")
send(ticket, "pending-proof")
session.dialog:close()
Assert.is_false(finished)
Assert.eq(finish_err, "验证已取消")
Assert.is_true(submit_cancelled)
submit(true)
Assert.is_false(finished, "关窗后不接受迟到的验证成功")
Assert.eq(status, 403)
session = newSession()
ticket = path:match("#(.+)$")
session.timeout()
Assert.eq(finish_err, "验证已过期,请重新发起验证")
request("config", { ticket = ticket })
Assert.eq(status, 403)
session = newSession()
ticket = path:match("#(.+)$")
cookie = "another-account"
send(ticket, "proof")
Assert.eq(status, 403, "不能拿旧二维码给新账号提交验证")
session:cancel()
Assert.is_nil(next(timers))
finished = nil
session = newSession()
ticket = path:match("#(.+)$")
send(ticket, "pending")
cookie = "changed-during-request"
submit(true)
Assert.eq(status, 403)
Assert.is_false(finished, "请求中换账号,旧验证成功也不能继续开书")
session = newSession()
ticket = path:match("#(.+)$")
session.expires = os.time() - 1
request("config", { ticket = ticket })
Assert.eq(status, 403, "主循环未跑过期定时器也不能用过期票据")
session:cancel()
Ext.reset()
finished = nil
session = newSession()
Assert.is_false(finished, "远程管理未启用时立即失败,不留下悬挂回调")
Assert.eq(finish_err, "无法打开远程验证,请先连接 Wi-Fi 并启用远程管理功能")
Assert.is_true(session.done)
Assert.is_nil(next(timers))