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

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

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-16 12:08:03 +08:00
commit 7a09f7c834
25 files changed
+2551

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
--[[--
知音漫客:同步前级与异步后级的取消句柄、搜索分页与失败语义。
@module tests.moonsource_zymk.async_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Stubs = require("support.stubs")
local JSON = require("support.json_stub")
package.loaded["json"] = JSON
local pending, cancelled, mode = nil, {}, "catalog"
local html = '<meta property="og:novel:book_name" content="测试"><a href="1.html">首话</a>'
local filters = '<dt class="attr-hd">题材</dt><dd class="attr-bd"><a data-tag="5" title="热血">热血</a></dd>'
package.loaded["http.request"] = {
header = function(res, key) return res.headers[key] end,
get = function(url, _opts, cb)
local kind
if url:find("/getchapterlist", 1, true) or url:find("/allComic/", 1, true) then
kind = "list"
pending = cb
elseif url:find("/sort/all.html", 1, true) then
kind = "filters"
cb(filters)
elseif url:find("/1.html", 1, true) then
kind = "redirect"
cb(nil, "HTTP 301", { code = 301, headers = {
location = mode == "bad_redirect" and "https://evil.test/kanman.com/123/"
or "https://www.kanman.com/123/first.html",
} })
elseif url:find("getsortlist", 1, true) then
local page = tonumber(url:match("page=(%d+)"))
kind = "page" .. page
if mode == "search_error" and page == 2 then cb(nil, "网络故障")
elseif mode == "search_async" and page == 2 then pending = cb
else
cb(JSON.encode({ status = 0, data = { page = { total_page = 2, comic_list = {
{ comic_id = page, comic_name = "结果" .. page },
} } } }))
end
else
kind = "detail"
cb(html)
end
return { cancel = function() cancelled[kind] = true end }
end,
}
local Client = require("moonsource_zymk.client")
local called = false
local handle = Client.catalogAsync("1", function() called = true end)
handle.cancel()
Assert.is_true(cancelled.list)
Assert.is_nil(cancelled.detail)
Assert.is_nil(cancelled.redirect)
pending(JSON.encode({ status = 0, data = {} }))
Assert.is_false(called)
cancelled = {}
handle = Client.libraryAsync(function() called = true end)
handle.cancel()
Assert.is_true(cancelled.list)
Assert.is_nil(cancelled.filters)
pending(JSON.encode({ status = 0, data = {} }))
Assert.is_false(called)
mode = "bad_redirect"
Client.catalogAsync("1", function(data, err)
Assert.is_nil(data)
Assert.eq(err, "HTTP 301")
end)
mode = "catalog"
Client.catalogAsync("1", function(data, err)
Assert.is_nil(data)
Assert.eq(err, "响应无效")
end)
pending(JSON.encode({ status = 1, data = { { chapter_name = "首话", chapter_newid = "first", order_num = 1 } } }))
mode = "search"
Client.searchAsync("结果", function(data, err)
Assert.is_nil(err)
Assert.len(data, 2)
Assert.eq(data[2].stable_id, "2")
end)
mode = "search_error"
Client.searchAsync("结果", function(data, err)
Assert.is_nil(data)
Assert.eq(err, "网络故障")
end)
mode, cancelled = "search_async", {}
handle = Client.searchAsync("结果", function() called = true end)
handle.cancel()
Assert.is_true(cancelled.page2)
Assert.is_nil(cancelled.page1)
pending(JSON.encode({ status = 0, data = { page = { comic_list = {} } } }))
Assert.is_false(called)
handle = Client.imagesAsync("bad", function() called = true end)
handle.cancel()
Stubs.flush()
Assert.is_false(called)
+63
View File
@@ -0,0 +1,63 @@
--[[--
知音漫客分类:动态分组、组合筛选、排序、limit 和异常响应。
@module tests.moonsource_zymk.browse_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Page = require("moonsource_zymk.page")
local filters = Page.filters([[
<dt class="attr-hd">题材</dt><dd class="attr-bd"><a data-tag="5" title="热血">热血</a><a data-tag="7" title="玄幻">玄幻</a></dd>
<dt class="attr-hd">进度</dt><dd class="attr-bd"><a data-tag="23" title="连载">连载</a><a data-tag="24" title="完结">完结</a></dd>
]])
Assert.len(filters, 2)
Assert.eq(filters[1].items[2].id, "7")
Assert.is_nil(Page.filters("<html>风控页</html>"))
local wire = { filters = filters, rows = {
{ comic_id = 1, comic_name = "热血连载", class_idlist = ",5,23,", update_time = "2026-11-07", total_click = 5, pingfen = "9.8" },
{ comic_id = 2, comic_name = "玄幻完结", class_idlist = ",7,24,", update_time = "2026-11-06", total_click = 30, pingfen = "9.7" },
{ comic_id = 3, comic_name = "双题材完结", class_idlist = ",5,7,24,", update_time = "2026-11-05", total_click = 10, pingfen = "9.9" },
} }
package.loaded["moonsource_zymk.client"] = {
libraryAsync = function(cb) cb(wire); return { cancel = function() end } end,
}
local Library = require("moonsource_zymk.browse")
local function load(opts)
local result, reason
Library.run(opts or {}, function(data, err) result, reason = data, err end)
return result, reason
end
local result = load({ limit = 2 })
Assert.len(result.data, 2)
Assert.eq(result.data[1].stable_id, "1")
Assert.len(result.groups, 3)
Assert.eq(result.groups[2].title, "题材")
local category_node = result.groups[2].chips[2].id
result = load({ node = category_node })
Assert.len(result.data, 2)
Assert.eq(result.groups[2].active, category_node)
local finished_node = result.groups[3].chips[3].id
result = load({ node = finished_node })
Assert.len(result.data, 1)
Assert.eq(result.data[1].stable_id, "3")
Assert.eq(result.groups[3].active, finished_node)
result = load({ node = "library?sort=total_click" })
Assert.eq(result.data[1].stable_id, "2")
result = load({ node = "library?sort=pingfen" })
Assert.eq(result.data[1].stable_id, "3")
result = load({ node = "library?tag1=7&tag2=23" })
Assert.len(result.data, 0)
Assert.is_nil(load({ node = "library?tag1=999" }))
Assert.is_nil(load({ node = "library?sort=invalid" }))
Assert.is_nil(load({ node = "library?account=1" }))
wire.rows = { { comic_name = "坏行" } }
local broken, err = load()
Assert.is_nil(broken)
Assert.eq(err, "响应无效")
require("moonsource_zymk.client").libraryAsync = function(cb) cb(nil, "网络故障") end
broken, err = load()
Assert.is_nil(broken)
Assert.eq(err, "网络故障")
+80
View File
@@ -0,0 +1,80 @@
--[[--
知音漫客图片下载:同步目录回调后取消在飞下载;失败不打包、VIP 明确拒绝。
@module tests.moonsource_zymk.chapter_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Stubs = require("support.stubs")
local Config = require("support.config")
local downloaded, pending, cancelled, built = {}, nil, {}, nil
package.loaded["utils.paths"] = {
bookWorkDir = function() return Config.dir() .. "/zymk-chapter" end,
ensureBookWork = function() end,
ensureDir = function() end,
}
package.loaded["source.cbz"] = {
ready = function() return false end,
build = function(files, archive, _state, cb) built = files; cb(archive) end,
}
package.loaded["utils.image"] = {
suffix = function() return ".jpg" end,
file = function() return ".jpg" end,
ok = function(path) return downloaded[path] == true end,
}
package.loaded["moonsource_zymk.client"] = {
imagesAsync = function(_uid, cb)
cb({ "https://img/1.jpg", "https://img/2.jpg" })
return { cancel = function() cancelled.images = true end }
end,
}
package.loaded["http.request"] = {
download = function(_opts, path, cb)
pending = function(ok, err) downloaded[path] = ok; cb(ok, err) end
return { cancel = function() cancelled.download = true end }
end,
}
local Chapter = require("moonsource_zymk.chapter")
local called = false
local identity = { source_id = "ext.zymk", stable_id = "book" }
local handle = Chapter.materializeAsync(identity, { uid = "123/first" }, nil, function() called = true end)
handle.cancel()
Assert.is_true(cancelled.download)
Assert.is_nil(cancelled.images)
pending(true)
Stubs.flush()
Assert.is_false(called)
Assert.is_nil(built)
downloaded = {}
Chapter.materializeAsync(identity, { uid = "123/failed" }, nil, function(path, err)
Assert.is_nil(path)
Assert.eq(err, "下载失败")
end)
pending(false, "下载失败")
Assert.is_nil(built)
local count, progress, result = 0, {}, nil
require("http.request").download = function(_opts, path, cb)
count = count + 1
downloaded[path] = true
cb(true)
return { cancel = function() end }
end
Chapter.materializeAsync(identity, { uid = "123/second" }, function(done, total)
progress[#progress + 1] = { done, total }
end, function(path) result = path end)
Stubs.flush()
Assert.eq(count, 2)
Assert.len(built, 2)
Assert.len(progress, 2)
Assert.eq(progress[2][1], 2)
Assert.eq(progress[2][2], 2)
Assert.matches(result, "/123_second%.cbz$")
Chapter.materializeAsync(identity, { uid = "123/vip", vip = true }, nil, function(path, err)
Assert.is_nil(path)
Assert.eq(err, "本章未购买")
end)
Stubs.flush()
+135
View File
@@ -0,0 +1,135 @@
--[[--
知音漫客:封面路径、目录排序、跳转和页图。
@module tests.moonsource_zymk.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 Mapper = require("moonsource_zymk.mapper")
local Page = require("moonsource_zymk.page")
Assert.eq(
Mapper.cover(4982),
"https://chapter-cover.yyhao.com/file/cover/000/004/982.jpg-300x400"
)
Assert.is_nil(Mapper.cover("nope"))
local found = Mapper.list({
status = 0,
data = { page = { comic_list = {
{ comic_id = 4982, comic_name = "长相思", comic_feature = "简介" },
} } },
})
Assert.eq(found[1].stable_id, "4982")
Assert.eq(found[1].title, "长相思")
Assert.eq(found[1].intro, "简介")
Assert.eq(found[1].cover, Mapper.cover(4982))
Assert.eq(#Mapper.list({ status = 0, data = { page = { comic_list = {} } } }), 0)
Assert.is_nil(Mapper.list({ status = 1 }))
Assert.is_nil(Mapper.list({ status = 0, data = { page = { comic_list = { { comic_name = "缺身份" } } } } }))
local html = [[
<meta property="og:novel:book_name" content="长相思">
<meta property="og:novel:author" content="飒飒动漫">
<meta property="og:description" content="简介">
<meta property="og:image" content="https://img/cover.jpg">
<a href="/sort/26.html">少女漫画</a>
<a href="434454.html">202话</a>
]]
local parsed = Page.detail(html)
Assert.eq(parsed.title, "长相思")
Assert.eq(parsed.authors, "飒飒动漫")
Assert.eq(parsed.chapter_file, "434454")
Assert.eq(parsed.cover, "https://img/cover.jpg")
Assert.is_nil(Page.detail("<html></html>"))
local detail = Mapper.detail(parsed, 4982)
Assert.eq(detail.authors, "飒飒动漫")
Assert.eq(detail.cover, "https://img/cover.jpg")
Assert.eq(detail.source_id, "ext.zymk")
Assert.eq(detail.intro, "简介")
local toc = Mapper.chapters(25289, {
{ chapter_name = "后一话", chapter_newid = "hou", order_num = 10, price = 5 },
{ chapter_name = "序章", chapter_newid = "xu", order_num = 3, price = 0 },
})
Assert.eq(#toc, 2)
Assert.eq(toc[1].uid, "25289/xu")
Assert.eq(toc[1].title, "序章")
Assert.eq(toc[1].vip, false)
Assert.eq(toc[1].toc_version, Mapper.TOC_VERSION)
Assert.eq(toc[2].vip, true)
Assert.is_nil(Mapper.chapters(25289, {}))
local urls = Mapper.images({
status = 0,
data = { current_chapter = { price = 0, is_vip = 0, chapter_img_list = { "https://img/1.jpg", "https://img/2.jpg" } } },
})
Assert.eq(#urls, 2)
Assert.eq(select(2, Mapper.images({
status = 0,
data = { current_chapter = { price = 5, is_vip = 0, chapter_img_list = {} } },
})), "locked")
Assert.is_nil(Mapper.images({ status = 1 }))
local requests = {}
package.preload["http.request"] = function()
return {
header = function(res, name)
local headers = res and res.headers
return headers and headers[name]
end,
get = function(url, _options, callback)
requests[#requests + 1] = url
if url:find("key=", 1, true) then
callback(Json.encode({ status = 0, data = { page = { total_page = 1, comic_list = { { comic_id = 4982, comic_name = "长相思" } } } } }), nil, nil)
elseif url:find("/apinew/getsortlist/", 1, true) then
callback(Json.encode({ status = 0, data = { page = { comic_list = { { comic_id = 101, comic_name = "凤逆天下" } } } } }), nil, nil)
elseif url:find(".html", 1, true) then
callback(nil, "HTTP 301", { code = 301, headers = { location = "https://www.kanman.com/25289/di202hua.html" } })
elseif url:find("/4982/", 1, true) then
callback(html, nil, nil)
elseif url:find("/api/getchapterlist", 1, true) then
callback(Json.encode({ status = 0, data = {
{ chapter_name = "后一话", chapter_newid = "hou", order_num = 10, price = 0 },
{ chapter_name = "序章", chapter_newid = "xu", order_num = 3, price = 0 },
} }), nil, nil)
elseif url:find("/api/getchapterinfov2", 1, true) then
callback(Json.encode({ status = 0, data = { current_chapter = { chapter_img_list = { "https://img/1.jpg" } } } }), nil, nil)
else
callback(nil, "miss", nil)
end
return { cancel = function() end }
end,
}
end
package.loaded["http.request"] = nil
package.loaded["moonsource_zymk.client"] = nil
local Client = require("moonsource_zymk.client")
local books
Client.searchAsync("长相思", function(list) books = list end)
Assert.eq(books[1].stable_id, "4982")
local loaded
Client.catalogAsync("4982", function(value) loaded = value end)
Assert.eq(loaded.book.title, "长相思")
Assert.eq(loaded.chapters[1].uid, "25289/xu")
Assert.is_true(requests[#requests]:find("comic_id=25289", 1, true) ~= nil)
local images
Client.imagesAsync("25289/xu", function(list) images = list end)
Assert.eq(images[1], "https://img/1.jpg")
Assert.is_true(requests[#requests]:find("isWebp=0", 1, true) ~= nil)
Assert.is_true(requests[#requests]:find("chapter_newid=xu", 1, true) ~= nil)
Assert.is_nil(Mapper.chapters(25289, { { chapter_name = "无序号", chapter_newid = "xu" } }))
Assert.is_nil(Mapper.images({
status = 0, data = { current_chapter = { chapter_img_list = { "https://img/1.jpg", false } } },
}))
local escaped = Page.detail(html:gsub('content="长相思"', 'content="A &amp; B"'))
Assert.eq(escaped.title, "A & B")
+206
View File
@@ -0,0 +1,206 @@
--[[--
知音漫客短信、密码与 Cookie 登录:官网字段、账号兑换、取消和换号隔离。
@module tests.moonsource_zymk.login_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch, inject-field
local Assert = require("support.assert")
local JSON = require("support.json_stub")
local Text = require("utils.text")
local Themed = require("support.theme_stubs")
Themed.install()
package.loaded["json"] = JSON
local cfg, requests, reloads, expired, connected = {}, {}, 0, false, true
local cleared = 0
package.loaded["source.logout"] = { clear = function(_id, clear)
clear(); cleared = cleared + 1; return true
end }
local synchronous
package.loaded["utils.settings"] = { getSource = function() return cfg end, saveSource = function() end }
package.loaded["source.registry"] = { reload = function() reloads = reloads + 1 end }
package.loaded["source.auth"] = {
expired = function() return expired end, ok = function() expired = false end,
}
package.loaded["ui/network/manager"] = { isConnected = function() return connected end }
package.loaded["ui.themes.widgets.row"] = { widget = function(opts) return opts end }
local function request(url, body, opts, cb)
local r = { url = url, body = body, opts = opts, cb = cb }
requests[#requests + 1] = r
if synchronous then local answer = synchronous; synchronous = nil; cb(answer) end
return { cancel = function() r.cancelled = true end }
end
package.loaded["http.request"] = {
post = request,
}
local Login = require("moonsource_zymk.login")
local Setting = require("moonsource_zymk.setting")
local Mapper = require("moonsource_zymk.mapper")
local response = JSON.encode({ status = 0, data = { openid = "account-a", type = "mkxq" } })
local token = JSON.encode({ status = 0, data = { appToken = "temporary-token" } })
local phone = "13800000000"
local account, reason
local function done(value, err) account, reason = value, err end
local session = Login.new()
Assert.is_false(Login.hasAccount())
Assert.is_nil(Mapper.account({ openid = "guest", type = "device" }))
session:sendSmsAsync(phone, nil, false, done)
Assert.matches(requests[#requests].url, "/apinew/sendsms/$")
Assert.matches(requests[#requests].body, "service=ext.zymk")
Assert.matches(requests[#requests].body, "refresh=0")
local captcha = { Image = "image", Width = 320, Height = 120, Content = "文字" }
requests[#requests].cb(JSON.encode({ status = 1031, data = captcha }))
Assert.eq(account.width, 320)
Assert.is_nil(session.sms_phone)
session:sendSmsAsync(phone, { { x = 10, y = 20 }, { x = 50, y = 30 } }, false, done)
local body = Text.urlDecode(requests[#requests].body)
Assert.matches(body, '"appId":11')
Assert.matches(body, '"verificaType":2')
requests[#requests].cb(JSON.encode({ status = 0 }))
Assert.eq(session.sms_phone, phone)
Assert.is_true(account.sent)
local count = #requests
session:sendSmsAsync(phone, nil, false, done)
Assert.eq(#requests, count)
Assert.eq(reason, "请稍后再获取验证码")
session:codeAsync("13900000000", "123456", done)
Assert.eq(#requests, count)
Assert.eq(reason, "请先获取该手机号的验证码,再填写验证码登录")
session:codeAsync(phone, "123456", done)
Assert.matches(requests[#requests].url, "/apinew/mobilevclogin/$")
Assert.matches(requests[#requests].body, "vcode=123456")
Assert.is_nil(cfg.account)
requests[#requests].cb(token)
Assert.matches(requests[#requests].url, "/apinew/getuserinfo/$")
Assert.matches(requests[#requests].body, "token=temporary%-token")
Assert.is_false(requests[#requests].opts.allow_redirects)
Assert.eq(requests[#requests].opts.headers.Origin, "https://www.zymk.cn")
Assert.eq(requests[#requests].opts.headers.Referer, "https://www.zymk.cn/login.htm")
Assert.eq(requests[#requests].opts.headers["X-Requested-With"], "XMLHttpRequest")
Assert.is_nil(cfg.account)
requests[#requests].cb(response)
Assert.eq(account.openid, "account-a")
Assert.is_nil(cfg.account.appToken)
Assert.is_nil(cfg.password)
Assert.eq(reloads, 1)
Assert.is_true(Login.hasAccount())
expired = true
Assert.is_false(Login.hasAccount())
expired = false
local previous = cfg.account
Login.new():confirm({ type = "mkxq", token = "expired" }, done)
requests[#requests].cb(nil, "network failed")
Assert.eq(reason, "network failed")
Assert.eq(cfg.account, previous)
Login.new():confirm({ type = "mkxq", token = "bad" }, done)
requests[#requests].cb("<html>login</html>")
Assert.eq(reason, "登录响应无效")
Assert.eq(cfg.account, previous)
Login.new():confirm({ type = "mkxq", token = "other" }, done)
requests[#requests].cb(JSON.encode({ status = 0, data = { openid = "account-b", type = "mkxq" } }))
Assert.is_nil(reason)
Assert.eq(cfg.account.openid, "account-b")
Assert.eq(cleared, 1)
local stale = Login.new()
stale:confirm({ type = "mkxq", token = "old" }, done)
cfg.account = nil
requests[#requests].cb(response)
Assert.eq(reason, "登录状态已变化,请重试")
Assert.is_nil(cfg.account)
session = Login.new()
session.sms_phone = phone
synchronous = token
session:codeAsync(phone, "123456", done)
local pending = requests[#requests]
session:cancel()
Assert.is_true(pending.cancelled)
account = nil
pending.cb(response)
Assert.is_nil(account)
Assert.is_nil(cfg.account)
session = Login.new()
session:passwordAsync("[email protected]", " p+a&ss ", done)
Assert.matches(requests[#requests].url, "/userapi/login/$")
Assert.eq(requests[#requests].body, Text.formEncode({ identity = "[email protected]", pwd = " p+a&ss " }))
requests[#requests].cb(token)
Assert.is_nil(cfg.account)
requests[#requests].cb(response)
Assert.is_true(Login.hasAccount())
Assert.is_nil(cfg.password)
Assert.is_nil(Mapper.captcha({ Width = 320, Height = 120 }))
package.loaded["source.logout"] = { row = function() return function() return { title = "退出登录" } end end }
local rows = Setting.rows()
Assert.eq(#rows, 5)
Assert.eq(rows[1](400).title, "短信验证码登录")
Assert.eq(rows[2](400).title, "密码登录")
Assert.eq(rows[3](400).title, "浏览器登录")
Assert.eq(rows[4](400).title, "粘贴 Cookie")
for _index, row in ipairs(rows) do
local title = row(400).title
Assert.is_true(title ~= "微信扫码登录" and title ~= "电脑浏览器登录")
end
rows[2](400).callback()
Assert.eq(Themed.last("input").fields[2].text_type, "password")
Themed.last("input").callback("reader", "password")
pending = requests[#requests]
Themed.last("progress_dialog").on_cancel()
Assert.is_true(pending.cancelled)
pending.cb(token)
Assert.eq(#Setting.rows(), 5)
local cookie = "user=" .. Text.urlEncode(JSON.encode({ openid = "account-a", type = "mkxq" }))
local cookie_previous = cfg.account
Login.new():cookieAsync(cookie, done)
requests[#requests].cb(nil, "network failed")
Assert.eq(cfg.account, cookie_previous)
Assert.eq(reason, "network failed")
Login.new():cookieAsync(cookie, done)
Assert.matches(requests[#requests].body, "openid=account%-a")
Assert.matches(requests[#requests].body, "type=mkxq")
requests[#requests].cb(response)
Assert.is_true(Login.hasAccount())
Login.new():cookieAsync(cookie, done)
requests[#requests].cb(JSON.encode({ status = 0, data = { openid = "wrong-account", type = "mkxq" } }))
Assert.eq(reason, "登录响应无效")
rows[3](400).callback()
Themed.last("input").callback(cookie)
pending = requests[#requests]
Themed.last("progress_dialog").on_cancel()
Assert.is_true(pending.cancelled)
local cookie_count = #requests
pending.cb(response)
Assert.eq(#requests, cookie_count)
count = #requests
Login.new():cookieAsync("unrelated=1", done)
Assert.eq(#requests, count)
Assert.eq(reason, "没有读到知音漫客登录 Cookie")
local sms = require("moonsource_zymk.phone_login").new(function() end)
sms:start()
Assert.eq(Themed.last("input").fields[1].input_type, "number")
count = #requests
connected = false
Themed.last("input").callback(phone)
Assert.eq(#requests, count)
Assert.eq(Themed.last("toast").text, "网络不可用,请先连接 Wi-Fi")
connected = true
sms:start()
Themed.last("input").callback(phone)
pending = requests[#requests]
Themed.last("progress_dialog").on_cancel()
Assert.is_true(pending.cancelled)
pending.cb(JSON.encode({ status = 0 }))
Assert.is_nil(sms.session.sms_phone)
sms = require("moonsource_zymk.phone_login").new(function() end)
sms:start()
Themed.last("input").callback(phone)
requests[#requests].cb(JSON.encode({ status = 0 }))
local code_input = Themed.last("input")
Assert.eq(code_input.fields[1].label, "验证码")
count = #requests
code_input.actions[1].callback()
Assert.eq(#requests, count)
Assert.eq(Themed.last("input"), code_input)
Assert.eq(Themed.last("toast").text, "请稍后再获取验证码")
sms:cancel()
+85
View File
@@ -0,0 +1,85 @@
--[[--
知音漫客开书与缓存:同步目录桩后取消在飞章节,旧缓存离线可用,空目录不崩。
@module tests.moonsource_zymk.operations_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch, inject-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
local Themed = require("support.theme_stubs")
Themed.install()
local toc = { { idx = 1, uid = "123/first", toc_version = 1 }, { idx = 2, uid = "123/second" } }
local connected, cached, touched = true, false, nil
package.loaded["source.toc"] = { read = function() return toc end }
package.loaded["ui/network/manager"] = { isConnected = function() return connected end }
package.loaded["book.progress_db"] = { get = function() return { chapter_idx = 2 } end }
package.loaded["book.store"] = {
touch = function(path, _identity, meta) touched = { path, meta.chapter_idx }; return true end,
}
local pending, cancelled = nil, {}
package.loaded["moonsource_zymk.chapter"] = {
existing = function(_identity, chapter) return cached and "/" .. chapter.uid .. ".cbz" or nil end,
materializeAsync = function(_identity, _chapter, _progress, cb)
pending = cb
return { cancel = function() cancelled.chapter = true end }
end,
prefetchAsync = function(_identity, _toc, from, count, _opts, cb)
Assert.eq(from, 0)
Assert.eq(count, 2)
pending = cb
return { cancel = function() cancelled.cache = true end }
end,
}
local source = require("moonsource_zymk.source").new()
source.loadTocAsync = function(_self, _identity, cb)
cb(toc)
return { cancel = function() cancelled.toc = true end }
end
local identity = { source_id = "ext.zymk", stable_id = "book" }
local called = false
local handle = source:openBookAsync(identity, nil, function() called = true end)
Stubs.flush()
handle.cancel()
Assert.is_true(cancelled.chapter)
Assert.is_nil(cancelled.toc)
Assert.is_true(Themed.last("progress_dialog").closed)
pending("/first.cbz")
Assert.is_false(called)
Assert.is_nil(touched)
handle = source:cacheAllChaptersAsync(identity, nil, function() called = true end)
handle.cancel()
Assert.is_true(cancelled.cache)
Assert.is_nil(cancelled.toc)
pending(2, 2, 0)
Assert.is_false(called)
connected, cached = false, true
source:openBookAsync(identity, nil, function(path) Assert.eq(path, "/123/second.cbz") end)
Stubs.flush()
Assert.eq(touched[2], 2)
cached = false
source:openBookAsync(identity, nil, function(path, err)
Assert.is_nil(path)
Assert.eq(err, "网络不可用,请先连接 Wi-Fi")
end)
Stubs.flush()
toc, cached = {}, true
source:openBookAsync(identity, nil, function(path, err)
Assert.is_nil(path)
Assert.eq(err, "网络不可用,请先连接 Wi-Fi")
end)
Stubs.flush()
source.loadTocAsync = function(_self, _identity, cb)
pending = cb
return { cancel = function() cancelled.toc = true end }
end
cancelled = {}
handle = source:cacheAllChaptersAsync(identity, nil, function() called = true end)
handle.cancel()
Assert.is_true(cancelled.toc)
Assert.is_nil(cancelled.cache)
pending(toc)
Assert.is_false(called)
+62
View File
@@ -0,0 +1,62 @@
--[[--
知音漫客整书评论:真实成功码、归属、空页、分页、网络与取消。
@module tests.moonsource_zymk.reviews_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local JSON = require("support.json_stub")
local Mapper = require("moonsource_zymk.mapper")
local row = { id = 1, ssid = 101, ssidtype = 0, uname = "测试读者", content = "<p>书评 &amp; 文字</p>", createtime = 1700000000 }
local page = Mapper.reviews("101", 1, { status = 1, data = { row } })
Assert.eq(page.items[1].text, "书评 & 文字")
Assert.eq(page.items[1].author, "测试读者")
Assert.matches(page.items[1].time, "^%d%d%d%d%-%d%d%-%d%d$")
Assert.is_false(page.more)
Assert.len(Mapper.reviews("101", 2, { status = 1, data = {} }).items, 0)
Assert.is_nil(Mapper.reviews("101", 1, { status = 0, data = { row } }))
Assert.is_nil(Mapper.reviews("102", 1, { status = 1, data = { row } }))
row.ssidtype = 1
Assert.is_nil(Mapper.reviews("101", 1, { status = 1, data = { row } }))
row.ssidtype = 0
local full = {}
for index = 1, 20 do full[index] = row end
page = Mapper.reviews("101", 2, { status = 1, data = full })
Assert.is_true(page.more)
Assert.eq(page.pages, 3)
package.loaded["json"] = JSON
local pending, cancelled = nil, false
package.loaded["http.request"] = {
get = function(url, _opts, cb)
Assert.matches(url, "^https://community%-hots%.321mh%.com/comment/newgets%?")
Assert.matches(url, "ssid=101")
Assert.matches(url, "ssidtype=0")
Assert.matches(url, "page=2")
pending = cb
return { cancel = function() cancelled = true end }
end,
}
local Client = require("moonsource_zymk.client")
Client.reviewsAsync("101", 2, function(data, err)
Assert.is_nil(err)
Assert.len(data.items, 1)
end)
pending(JSON.encode({ status = 1, data = { row } }))
Client.reviewsAsync("101", 2, function(data, err)
Assert.is_nil(data)
Assert.eq(err, "网络故障")
end)
pending(nil, "网络故障")
Client.reviewsAsync("101", 2, function(data, err)
Assert.is_nil(data)
Assert.eq(err, "响应无效")
end)
pending("bad json")
local called = false
local handle = Client.reviewsAsync("101", 2, function() called = true end)
handle.cancel()
Assert.is_true(cancelled)
pending(JSON.encode({ status = 1, data = { row } }))
Assert.is_false(called)
+118
View File
@@ -0,0 +1,118 @@
--[[--
知音漫客收藏:只读拉取、空快照、坏响应、离线、取消与换号隔离。
@module tests.moonsource_zymk.shelf_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch, inject-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
local JSON = require("support.json_stub")
local Text = require("utils.text")
package.loaded["json"] = JSON
local cfg = { account = { type = "mkxq", openid = "synthetic-account" } }
local connected, expired, automatic = true, false, true
package.loaded["utils.settings"] = { getSource = function() return cfg end }
package.loaded["source.auth"] = { expired = function() return expired end }
package.loaded["ui/network/manager"] = { isConnected = function() return connected end }
local requests = {}
local data = { ubook = { { comic_id = 4982, comic_name = "测试漫画", addtime = 1700000000123 } } }
package.loaded["http.request"] = { post = function(url, body, opts, cb)
local request = { url = url, body = body, opts = opts, cb = cb }
requests[#requests + 1] = request
if automatic then cb(JSON.encode({ status = 0, data = data })) end
return { cancel = function() request.cancelled = true end }
end }
local written, options, calls = nil, nil, 0
package.loaded["book.books_db"] = {
reconcile = function(source_id, books, opts)
Assert.eq(source_id, "ext.zymk")
written, options, calls = books, opts, calls + 1
return true, 0
end,
}
local Mapper = require("moonsource_zymk.mapper")
local Source = require("moonsource_zymk.source")
local source = Source.new()
local result, reason
local function done(value, err) result, reason = value, err end
Assert.len(Mapper.shelf({}), 0)
Assert.len(Mapper.shelf({ urecord = {} }), 0)
Assert.is_nil(Mapper.shelf({ wrong = {} }))
Assert.is_nil(Mapper.shelf({ ubook = false }))
Assert.is_nil(Mapper.shelf({ ubook = { [2] = data.ubook[1] } }))
Assert.is_nil(Mapper.shelf({ ubook = { { comic_id = "bad", comic_name = "坏身份" } } }))
Assert.is_nil(Mapper.shelf({ ubook = { { comic_id = 1 } } }))
source:syncBooksAsync({ force = true }, done)
Assert.eq(result.pulled, 1)
Assert.eq(result.pushed, 0)
Assert.is_false(result.skipped)
Assert.eq(written[1].stable_id, "4982")
Assert.eq(written[1].cover, Mapper.cover(4982))
Assert.eq(written[1].inserted_at, 1700000000)
Assert.is_true(options.force)
Assert.is_true(options.inserted_at_from_row)
Assert.is_nil(options.allow_empty)
Assert.eq(requests[1].body, Text.formEncode(cfg.account), "不带 sync,不合并浏览器本地记录")
Assert.is_nil(requests[1].opts.headers.Cookie)
Assert.is_false(requests[1].opts.allow_redirects)
Assert.matches(requests[1].url, "^https://www%.zymk%.cn/apinew/getuserrecord/$")
local count = #requests
source:syncBooksAsync({ dirty_only = true }, done)
Stubs.flush()
Assert.is_true(result.skipped)
Assert.eq(#requests, count)
data = {}
source:syncBooksAsync({ force = true }, done)
Assert.eq(result.pulled, 0)
Assert.is_nil(options.allow_empty, "空快照不能清空本地书架")
automatic = false
local reconciled = calls
source:syncBooksAsync(nil, done)
requests[#requests].cb("<html>login</html>")
Assert.is_nil(result)
Assert.eq(calls, reconciled)
Assert.eq(reason, "登录响应无效")
source:syncBooksAsync(nil, done)
requests[#requests].cb(nil, "network failed")
Assert.eq(reason, "network failed")
Assert.eq(calls, reconciled)
local handle = source:syncBooksAsync(nil, done)
local pending = requests[#requests]
handle.cancel()
pending.cb(JSON.encode({ status = 0, data = data }))
Assert.is_true(pending.cancelled)
Assert.eq(calls, reconciled)
source:syncBooksAsync(nil, done)
pending = requests[#requests]
local account = cfg.account
cfg.account = { type = "mkxq", openid = "another-account" }
pending.cb(JSON.encode({ status = 0, data = data }))
Assert.eq(reason, "登录状态已变化,请重试")
Assert.eq(calls, reconciled)
count = #requests
source:syncBooksAsync(nil, done)
Stubs.flush()
Assert.eq(#requests, count)
Assert.eq(reason, "登录状态已变化,请重试")
cfg.account = account
connected = false
source:syncBooksAsync(nil, done)
Stubs.flush()
Assert.eq(reason, "网络不可用,请先连接 Wi-Fi")
Assert.eq(#requests, count)
connected, expired = true, true
source:syncBooksAsync(nil, done)
Stubs.flush()
Assert.eq(reason, "请先登录知音漫客")
Assert.eq(#requests, count)
cfg.account = nil
Source.new():syncBooksAsync(nil, done)
Stubs.flush()
Assert.is_true(result.skipped)
Assert.eq(#requests, count)
Assert.is_false(source:capabilities().shelf_push)