Files
moonsource-kmoe/tests/kmoe/mapper_spec.lua
T
Garfield 1dd1cccb68 ✨ (kmoe): 新增 Kmoe 漫画书源,按卷下载 EPUB 阅读
书城搜索与筛选、详情、卷列表、整卷 EPUB 下载与缓存、收藏书架同步。
支持邮箱密码、浏览器授权、粘贴 Cookie 登录;下载要求 UA 与登录时一致,
会话 UA 随 Cookie 一起保存。
2026-11-27 13:00:01 +08:00

134 lines
6.5 KiB
Lua
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
moonsource_kmoe.mapper 离线用例:书单 JSON、详情页 HTML、卷列表、下载地址、书架页。样本结构取自 mox.moe 实际回包。
@module tests.moonsource_kmoe.mapper_spec
--]]
---@diagnostic disable: need-check-nil, undefined-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
Stubs.install()
package.loaded["json"] = { decode = require("support.json_stub").decode }
local Mapper = require("moonsource_kmoe.mapper")
-- 书单:搜索结果名字带 <b> 高亮;nowpage < totalpage 才还有下一页。
do
local body = [[{"uin":"","total":2,"totalpage":2,"nowcount":1,"nowpage":1,"data":[
{"status":"\u5b8c\u7d50","url_book":"https:\/\/mox.moe\/c\/f1bc90.htm","url_cover":"https:\/\/kmimg.moex.ink\/cover\/x.jpg?sign=a",
"newvol":"\u5377 31","name":"<b>\u881f\u7b46<\/b>\u5c0f\u65b0","author":"\u81fc\u4e95\u5100\u4eba","score":"9.1"},
{"url_book":"https:\/\/mox.moe\/about.htm","name":"x"}]}]]
local books, err, more = Mapper.list(body)
Assert.is_nil(err)
Assert.len(books, 1, "认不出书 key 的行丢掉")
Assert.eq(books[1].stable_id, "f1bc90")
Assert.eq(books[1].source_id, "kmoe")
Assert.eq(books[1].title, "蠟筆小新")
Assert.eq(books[1].authors, "臼井儀人")
Assert.eq(books[1].cover, "https://kmimg.moex.ink/cover/x.jpg?sign=a")
Assert.is_true(more)
local _b, _e, last = Mapper.list([[{"totalpage":3,"nowpage":3,"data":[]}]])
Assert.is_false(last)
local none, bad = Mapper.list("<html>")
Assert.is_nil(none)
Assert.not_nil(bad, "非 JSON 是响应无效,不是空结果")
end
local DETAIL = [[
<img class="cover_l border_gray" src="https://kmimg.moex.ink/cover/l.jpg!cover_l?sign=b" alt="蠟筆小新" border="0" /><br />
<b class='font_size_l' style="line-height:30px;">蠟筆小新</b><br>
<span class="font_size_m" style="line-height:26px;">
作者:<font style="display:"><a href="https://mox.moe/m/idx.php?s=%E8%87%BC" class="color_link">臼井儀人</a></font>
<font style="display:"> <a href="https://mox.moe/m/idx.php?s=x" class="color_link">UY Team</a></font>
<font style="display:none"> <a href="https://mox.moe/m/idx.php?s=" class="color_link"></a></font>
<font style="display:none"> ...</font>
<br />
狀態:完結
<br />
分類:<font style="display:">幽默</font>
&nbsp; <font style="display:">治癒</font>
<br />
<script>
var uin = "@UIN@";
var bookid = "35063";
var can_do_fav = "1";
document.getElementById("div_desc_content").innerHTML = "五歲的小孩,<br />引發 \"笑料\"&nbsp;Crayon";
window.iframe_action2.location.href = "/book_data.php?h=17957395811f1bc9";
</script>]]
-- 详情:只取可见作者 / 分类;简介去标签解实体;uin 非空才算登录态页面。
do
local d = Mapper.detail("f1bc90", DETAIL:gsub("@UIN@", ""))
Assert.eq(d.bookid, "35063")
Assert.eq(d.hash, "17957395811f1bc9")
Assert.is_false(d.logged_in)
Assert.eq(d.book.stable_id, "f1bc90")
Assert.eq(d.book.title, "蠟筆小新")
Assert.eq(d.book.authors, "臼井儀人, UY Team")
Assert.eq(d.book.category, "幽默 治癒")
Assert.eq(d.book.cover, "https://kmimg.moex.ink/cover/l.jpg!cover_l?sign=b")
Assert.eq(d.book.intro, "五歲的小孩,\n引發 \"笑料\" Crayon")
Assert.is_true(Mapper.detail("f1bc90", (DETAIL:gsub("@UIN@", "123"))).logged_in)
Assert.is_nil(Mapper.detail("f1bc90", "<html>維護中</html>"), "认不出书号就是解析失败")
end
-- 卷列表:保序,卷号做 uid,EPUB 大小 0 标为制作中;空回包 / 空列表都是错误。
do
local body = [==[{"msgid":0,"bookkey":"de34d9","voldata":[
["1001",0,0,"單行本",1,"卷 01",120,240,"60.1","70.2","0","70.3","","2024-01-01"],
["3086",0,0,"話",2,"話 086-090",90,180,"20","25","0","0","","2026-11-23"]]}]==]
local chapters, err = Mapper.chapters(body, "34410")
Assert.is_nil(err)
Assert.len(chapters, 2)
Assert.eq(chapters[1].idx, 1)
Assert.eq(chapters[1].uid, "1001")
Assert.eq(chapters[1].title, "卷 01")
Assert.eq(chapters[1].origin, "/getdownurl.php?b=34410&v=1001&mobi=2&vip=0&json=1")
Assert.is_nil(chapters[1].building)
Assert.is_true(chapters[2].building)
local none, empty_err = Mapper.chapters("", "1")
Assert.is_nil(none)
Assert.not_nil(empty_err, "空回包不能当成没有卷")
local _n, no_vol = Mapper.chapters([[{"voldata":[]}]], "1")
Assert.not_nil(no_vol)
end
-- 下载地址:有 url 返回;没有就把站点消息带出来。
do
Assert.eq(Mapper.downUrl([[{"url":"https:\/\/dl.example\/a.epub","name":"a.epub"}]]), "https://dl.example/a.epub")
local url, err = Mapper.downUrl([[{"msg":"額度不足"}]])
Assert.is_nil(url)
Assert.eq(err, "額度不足")
Assert.is_nil((Mapper.downUrl("<html>")))
end
local function row(key, bookid, title, fav)
return ([[<tr style="height:36px;"> <td width='10px'></td> <td width='350px' style='text-align:left'><a href='https://mox.moe/c/%s.htm' target='_blank'>%s</a></td> <td width='190px' style='text-align:left'>コノシマルカ</td> <td> <img src="https://img.mxomo.com/moe/icon-tick.png" style="width:20px;display:none"> <select id="sel_pushtype_%s"></select> </td> <td> <img src="https://img.mxomo.com/moe/icon-tick-fav.png" style="width:70px;height:20px;vertical-align:middle;display:%s"> </td> </tr>]])
:format(key, title, bookid, fav)
end
-- 书架:只收打了收藏的行;页脚的 /c/10001.htm 不在表格行里;出现分页链接时标不完整。
do
local html = '<a href="https://mox.moe/myfollow.php?f=7">⬆︎</a><table>'
.. row("de34d9", "34410", "有鬼拍不到", "") .. row("abc123", "1", "只訂閱", "none")
.. '</table><a href="https://mox.moe/c/10001.htm">說明</a>'
local rows, err, paged = Mapper.shelf(html)
Assert.is_nil(err)
Assert.len(rows, 1)
Assert.eq(rows[1].book.stable_id, "de34d9")
Assert.eq(rows[1].book.title, "有鬼拍不到")
Assert.eq(rows[1].book.authors, "コノシマルカ")
Assert.eq(rows[1].bookid, "34410")
Assert.is_false(paged)
local _r, _e, more = Mapper.shelf(html .. '<a href="https://mox.moe/myfollow.php?f=7&p=2">2</a>')
Assert.is_true(more)
local empty = Mapper.shelf('<a href="https://mox.moe/myfollow.php?f=7">⬆︎</a>')
Assert.len(empty, 0, "空收藏是空列表")
local none, bad = Mapper.shelf("<html>維護中</html>")
Assert.is_nil(none)
Assert.not_nil(bad, "认不出的页面不能当空书架")
end