This commit is contained in:
commit
f50b9e7711
23 files changed
+2346
No files matched your search
@@ -0,0 +1,152 @@
|
||||
--[[--
|
||||
moonsource_kanunu.source 书城浏览:节点、两种分页形式、聚合、失败与取消。
|
||||
|
||||
@module tests.moonsource_kanunu.browse_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, param-type-mismatch
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Stubs = require("support.stubs")
|
||||
|
||||
local NAV = [[<div class="nav-top"><a href="/files/chinese/">国内作家</a> | <a href="/files/7.html">港台海外</a>
|
||||
| <a href="/files/sf/">科幻</a></div>
|
||||
<div class="nav-bottom"><a href="/wuxia/">武侠小说</a> | <a href="/tuili/">侦探推理</a></div>]]
|
||||
|
||||
local function pageHtml(base, page, count, nextHref)
|
||||
local rows = {}
|
||||
for index = 1, count do
|
||||
local id = (page - 1) * count + index
|
||||
rows[#rows + 1] = string.format("<tr><td><a href='%s%s.html'>书%d</a></td></tr>", base, id, id)
|
||||
end
|
||||
return NAV .. '<div class="content book-content"><div class="neirong" id="neirong"><table>'
|
||||
.. table.concat(rows) .. '</table></div></div><div class="book-nav">'
|
||||
.. (nextHref and ("<a href='" .. nextHref .. "'>下一页</a>") or "") .. "</div>"
|
||||
end
|
||||
|
||||
-- 脚本化 client:按请求路径给页面,可延迟 / 报错 / 返坏页面。
|
||||
local requests, script = {}, {}
|
||||
package.preload["moonsource_kanunu.client"] = function()
|
||||
local Fake = {}
|
||||
function Fake:get(path, cb)
|
||||
local request = { path = path, cb = cb }
|
||||
requests[#requests + 1] = request
|
||||
local handle = { cancel = function() request.cancelled = true end }
|
||||
local step = table.remove(script, 1) or { html = pageHtml("/files/chinese/200805/", 1, 5, nil) }
|
||||
request.deliver = function()
|
||||
if request.cancelled then return end
|
||||
if step.err then cb(nil, step.err) else cb(step.html or "", nil) end
|
||||
end
|
||||
if not step.delayed then request.deliver() end
|
||||
return handle
|
||||
end
|
||||
return { new = function() return setmetatable({}, { __index = Fake }) end }
|
||||
end
|
||||
|
||||
local source = require("moonsource_kanunu.source").new()
|
||||
local result, reason
|
||||
local function done(data, err)
|
||||
result, reason = data, err
|
||||
end
|
||||
|
||||
Assert.is_true(source:configured())
|
||||
local caps = source:capabilities()
|
||||
Assert.is_false(caps.shelf_push)
|
||||
Assert.is_false(caps.shelf_cache)
|
||||
Assert.is_false(caps.scrape)
|
||||
Assert.is_nil(source.searchAsync, "站点没有站内搜索,不声明搜索能力")
|
||||
|
||||
-- 分类页:一页只有 30 本,按「下一页」链接续到第二页。
|
||||
do
|
||||
script = {
|
||||
{ html = pageHtml("/wuxia/201102/", 1, 30, "2.html") },
|
||||
{ html = pageHtml("/wuxia/201102/", 2, 30, "3.html") },
|
||||
}
|
||||
requests = {}
|
||||
source:browseAsync({ node = "/wuxia/" }, done)
|
||||
Assert.is_nil(reason)
|
||||
Assert.len(result.data, 60, "每批 60 本")
|
||||
Assert.eq(requests[1].path, "/wuxia/")
|
||||
Assert.eq(requests[2].path, "/wuxia/2.html", "分页形式跟着链接走")
|
||||
Assert.eq(result.groups[2].active, "/wuxia/", "当前分类高亮")
|
||||
Assert.eq(result.groups[1].title, "发现")
|
||||
Assert.eq(result.data[1].stable_id, "wuxia/201102/1.html")
|
||||
end
|
||||
|
||||
-- 另一类分页形式:`29-2.html`。
|
||||
do
|
||||
script = {
|
||||
{ html = pageHtml("/files/chinese/200805/", 1, 30, "29-2.html") },
|
||||
{ html = pageHtml("/files/chinese/200805/", 2, 30, "29-3.html") },
|
||||
}
|
||||
requests = {}
|
||||
source:browseAsync({ node = "/files/chinese/" }, done)
|
||||
Assert.eq(requests[2].path, "/files/chinese/29-2.html")
|
||||
Assert.len(result.data, 60)
|
||||
end
|
||||
|
||||
-- 频道页没有下一页:拿完为止。
|
||||
do
|
||||
script = { { html = pageHtml("/book8/x", 1, 25, nil) } }
|
||||
requests = {}
|
||||
source:browseAsync({ node = "/book8/" }, done)
|
||||
Assert.is_nil(reason)
|
||||
Assert.len(result.data, 25)
|
||||
Assert.eq(#requests, 1)
|
||||
Assert.eq(result.groups[1].active, "/book8/", "频道节点高亮频道")
|
||||
Assert.is_nil(result.groups[2].active)
|
||||
end
|
||||
|
||||
-- 节点缺失或不可认时走默认频道,不空手而归。
|
||||
do
|
||||
script = { { html = pageHtml("/book8/y", 1, 4, nil) } }
|
||||
requests = {}
|
||||
source:browseAsync({}, done)
|
||||
Assert.eq(requests[1].path, "/book8/")
|
||||
Assert.len(result.data, 4)
|
||||
end
|
||||
|
||||
-- limit 由调用方给定:只取一页。
|
||||
do
|
||||
script = { { html = pageHtml("/files/hot/", 1, 30, "2.html") } }
|
||||
requests = {}
|
||||
source:browseAsync({ node = "/hot.html", limit = 8 }, done)
|
||||
Assert.len(result.data, 8)
|
||||
Assert.eq(#requests, 1, "够数就不再翻页")
|
||||
end
|
||||
|
||||
-- 中途网络失败:不返回部分结果。
|
||||
do
|
||||
script = {
|
||||
{ html = pageHtml("/wuxia/201102/", 1, 30, "2.html") },
|
||||
{ err = "network down" },
|
||||
}
|
||||
source:browseAsync({ node = "/wuxia/" }, done)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(reason, "network down")
|
||||
end
|
||||
|
||||
-- 页面模板不对(认不出的链接):明确报响应无效,不伪装成空列表。
|
||||
do
|
||||
script = { { html = '<div class="content">站点维护中</div>' } }
|
||||
source:browseAsync({ node = "/wuxia/" }, done)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(reason, "响应无效")
|
||||
end
|
||||
|
||||
-- 翻页途中取消:不回调,迟到结果丢弃。
|
||||
do
|
||||
script = {
|
||||
{ html = pageHtml("/wuxia/201102/", 1, 30, "2.html") },
|
||||
{ delayed = true },
|
||||
}
|
||||
requests = {}
|
||||
local calls = 0
|
||||
local handle = source:browseAsync({ node = "/wuxia/" }, function() calls = calls + 1 end)
|
||||
Assert.eq(calls, 0, "第二页未回就不回调")
|
||||
handle.cancel()
|
||||
Assert.is_true(requests[2].cancelled)
|
||||
requests[2].deliver()
|
||||
Assert.eq(calls, 0)
|
||||
end
|
||||
|
||||
Stubs.flush()
|
||||
@@ -0,0 +1,132 @@
|
||||
--[[--
|
||||
moonsource_kanunu.client 离线用例:站内路径、GBK 转码、Cloudflare 拦截如实报错、取消。
|
||||
|
||||
@module tests.moonsource_kanunu.client_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Stubs = require("support.stubs")
|
||||
|
||||
-- 脚本化 http.request:按序消费 script;header 从 res.headers 取值。
|
||||
local calls, script = {}, {}
|
||||
package.preload["http.request"] = function()
|
||||
return {
|
||||
header = function(res, name)
|
||||
return res and res.headers and res.headers[name:lower()] or nil
|
||||
end,
|
||||
get = function(url, opts, cb)
|
||||
calls[#calls + 1] = {
|
||||
url = url,
|
||||
headers = opts and opts.headers or {},
|
||||
allow_redirects = opts and opts.allow_redirects,
|
||||
}
|
||||
local request = calls[#calls]
|
||||
local step = table.remove(script, 1) or { body = "" }
|
||||
local handle = { cancel = function() request.cancelled = true end }
|
||||
request.cb = function(body, err, res)
|
||||
if request.cancelled then return end
|
||||
cb(body, err, res)
|
||||
end
|
||||
if step.delayed then return handle end
|
||||
if step.err then
|
||||
request.cb(nil, step.err, nil)
|
||||
elseif step.code and step.code ~= 200 then
|
||||
request.cb(nil, "HTTP " .. step.code, { code = step.code, headers = step.headers or {} })
|
||||
else
|
||||
request.cb(step.body, nil, { code = 200, headers = step.headers or {} })
|
||||
end
|
||||
return handle
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local Client = require("moonsource_kanunu.client")
|
||||
|
||||
local function run(client, path)
|
||||
local out_body, out_err
|
||||
client:get(path, function(body, err)
|
||||
out_body, out_err = body, err
|
||||
end)
|
||||
return out_body, out_err
|
||||
end
|
||||
|
||||
-- GBK 页面按响应头转码。
|
||||
do
|
||||
local gbk = "GBK " .. string.char(0xD2, 0xB3, 0xC3, 0xE6, 0xA3, 0xBA)
|
||||
.. string.char(0xC5, 0xAC, 0xC5, 0xAC, 0xCA, 0xE9, 0xB7, 0xBB)
|
||||
script = { { body = gbk, headers = { ["content-type"] = "text/html; charset=gbk" } } }
|
||||
calls = {}
|
||||
local body, err = run(Client.new(), "/book8/")
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(calls[1].url, "https://www.kanunu8.com/book8/")
|
||||
Assert.is_true(calls[1].headers["User-Agent"]:find("Mozilla", 1, true) ~= nil, "带浏览器 UA")
|
||||
Assert.is_true(calls[1].allow_redirects, "跟随 301/302,缺斜杠的目录页也能取到")
|
||||
Assert.eq(body, "GBK 页面:努努书坊")
|
||||
end
|
||||
|
||||
-- 没有响应头声明时按页面 meta 判定(分类页与书页的 charset 声明不一致)。
|
||||
do
|
||||
local gbk = '<meta http-equiv="Content-Type" content="text/html; charset=gb2312">'
|
||||
.. string.char(0xC4, 0xE3, 0xBA, 0xC3)
|
||||
script = { { body = gbk } }
|
||||
local body = run(Client.new(), "/wuxia/")
|
||||
Assert.eq(body:sub(-6), "你好", "meta 声明也转码")
|
||||
end
|
||||
|
||||
-- UTF-8 页面原样返回(genres.html 是 UTF-8)。
|
||||
do
|
||||
script = { { body = "三体(全三册)", headers = { ["content-type"] = "text/html; charset=utf-8" } } }
|
||||
Assert.eq(run(Client.new(), "/genres.html"), "三体(全三册)")
|
||||
end
|
||||
|
||||
-- Cloudflare 拦截:如实报错,不重试、不伪造空列表。
|
||||
do
|
||||
script = { { code = 403 }, { body = "不该被请求" } }
|
||||
calls = {}
|
||||
local body, err = run(Client.new(), "/files/chinese/")
|
||||
Assert.is_nil(body)
|
||||
Assert.eq(err, "努努书坊把这次请求当成了爬虫,请稍后再试")
|
||||
Assert.eq(#calls, 1, "被拦截不重试")
|
||||
end
|
||||
|
||||
-- 瞬时断连:错误原样透传。
|
||||
do
|
||||
script = { { err = "SSL_ERROR_SYSCALL" } }
|
||||
local body, err = run(Client.new(), "/book8/")
|
||||
Assert.is_nil(body)
|
||||
Assert.eq(err, "SSL_ERROR_SYSCALL")
|
||||
end
|
||||
|
||||
-- 章页的站内绝对 URL 原样使用。
|
||||
do
|
||||
script = { { body = "章" } }
|
||||
calls = {}
|
||||
run(Client.new(), "https://www.kanunu8.com/book6/santi_1/11359.html")
|
||||
Assert.eq(calls[1].url, "https://www.kanunu8.com/book6/santi_1/11359.html")
|
||||
end
|
||||
|
||||
-- 外站地址不出门。
|
||||
do
|
||||
calls = {}
|
||||
local body, err
|
||||
Client.new():get("https://evil.test/x.html", function(value, failure)
|
||||
body, err = value, failure
|
||||
end)
|
||||
Stubs.flush()
|
||||
Assert.is_nil(body)
|
||||
Assert.eq(err, "响应无效")
|
||||
Assert.eq(#calls, 0, "外站不发请求")
|
||||
end
|
||||
|
||||
-- 取消后不接受迟到正文。
|
||||
do
|
||||
script = { { delayed = true } }
|
||||
calls = {}
|
||||
local client, completed = Client.new(), 0
|
||||
local handle = client:get("/book8/", function() completed = completed + 1 end)
|
||||
handle.cancel()
|
||||
Assert.is_true(calls[1].cancelled)
|
||||
calls[1].cb("迟到", nil, { code = 200, headers = {} })
|
||||
Assert.eq(completed, 0)
|
||||
end
|
||||
@@ -0,0 +1,192 @@
|
||||
--[[--
|
||||
moonsource_kanunu.mapper 离线用例:三套页面模板(新版书页、旧版书页、单页作品)、
|
||||
分类与频道 chip、两种分页形式、GBK 正文切段。
|
||||
|
||||
@module tests.moonsource_kanunu.mapper_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Mapper = require("moonsource_kanunu.mapper")
|
||||
|
||||
local NAV = [[<div class="nav-top"><a href="/files/chinese/" target="_parent">国内作家</a> |
|
||||
<a href="/files/7.html" target="_parent">港台海外</a> | <a href="https://love.kanunu8.com/">传统言情</a> |
|
||||
<a href="https://www.kanunu8.com/genres.html">更多图书分类</a></div>
|
||||
<div class="nav-bottom"><a href="/wuxia/" target="_parent">武侠小说</a> |
|
||||
<a href="/zt/" target="_parent">专题</a> | <a href="/tuili/" target="_parent">侦探推理</a></div>]]
|
||||
|
||||
-- 分类页:/wuxia/ 用 `2.html`,/files/chinese/ 用 `29-2.html`。
|
||||
local WUXIA = NAV .. [[<div class="content book-content"><h1>武侠小说</h1>
|
||||
<div class="neirong" id="neirong">
|
||||
<table width='100%'><tr height='24'><td width='2%'><img src='/images/ar.gif'></td>
|
||||
<td width='98%'><a href='/book2/11009/index.html' target='_blank'>沧月: 听雪楼(全集)</a></td></tr></table>
|
||||
<table width='100%'><tr height='24'><td width='2%'><img src='/images/ar.gif'></td>
|
||||
<td width='98%'><a href='/wuxia/201102/1727.html'>凤歌: 沧海1</a></td></tr></table>
|
||||
<table width='100%'><tr height='24'><td width='2%'><img src='/images/ar.gif'></td>
|
||||
<td width='98%'><a href='/zt/zt_10919.html'>江南: 龙族系列小说</a></td></tr></table>
|
||||
</div></div>
|
||||
<div class="book-nav"> <a href='#'>首页</a> <strong>1</strong><a href='2.html'>2</a>
|
||||
<a href='2.html'>下一页</a> <a href='10.html'>末页</a> </div>]]
|
||||
|
||||
local CHINESE = NAV .. [[<div class="content book-content"><div class="neirong" id="neirong">
|
||||
<table><tr><td><a href='/book5/changxiangsi/'>桐华:长相思(全集)</a></td></tr></table>
|
||||
</div></div>
|
||||
<div class="book-nav"><a href='29-2.html'>下一页</a></div>
|
||||
<div class="footer">© 努努书坊</div>]]
|
||||
|
||||
-- 频道页(最新更新):mulu-list 区块,没有分页。
|
||||
local LATEST = NAV .. [[<div class="catalog"><h1>2026 最新更新</h1><div class="mulu-list"><ul>
|
||||
<li><a href="/book8/songting02/" target="_blank">婚后失序</a></li>
|
||||
<li><a href="/book8/maokou10/" target="_blank">修仙,写狗血文成神</a></li></ul></div></div>]]
|
||||
|
||||
-- 分类枢纽页:推荐作家区块里的作家页不算书。
|
||||
local HUB = NAV .. [[<div class="catalog"><h1>港台、海外华人文学</h1><div class="mulu-title"><h2>推荐作家</h2></div>
|
||||
<div class="mulu-list"><ul><li><a target="_blank" href="/files/writer/95.html"><strong>张爱玲</strong></a></li>
|
||||
<li><a href="/book5/fanhua/">金宇澄:繁花</a></li></ul></div></div>]]
|
||||
|
||||
-- 真实第 2 页导航:上一页/下一页都是目录内相对路径。
|
||||
local PAGE2 = [[
|
||||
<div id="neirong"><ul><li><a href='/files/chinese/200805/519.html'>人生海海(完)</a></li></ul></div>
|
||||
<div class="book-nav"><a href='29-1.html'>上一页</a> <a href='29-3.html'>下一页</a></div>
|
||||
]]
|
||||
|
||||
Assert.len(Mapper.list(WUXIA, "/wuxia/"), 2, "专题合集不进书城")
|
||||
Assert.eq(Mapper.list(WUXIA, "/wuxia/")[1].stable_id, "book2/11009/", "index.html 归并成带斜杠的目录身份")
|
||||
Assert.eq(Mapper.list(WUXIA, "/wuxia/")[1].authors, "沧月")
|
||||
Assert.eq(Mapper.list(WUXIA, "/wuxia/")[2].stable_id, "wuxia/201102/1727.html", "旧版书页身份就是页面路径")
|
||||
Assert.eq(Mapper.list(CHINESE, "/files/chinese/")[1].title, "长相思(全集)", "作者:书名 拆开")
|
||||
Assert.eq(Mapper.list(CHINESE, "/files/chinese/")[1].authors, "桐华")
|
||||
Assert.eq(Mapper.list(HUB, "/files/7.html")[1].stable_id, "book5/fanhua/", "作家页链接跳过")
|
||||
Assert.eq(Mapper.bookPath("/book2/11009/index.html", "/wuxia/"), "/book2/11009/", "目录书页统一成带斜杠形式")
|
||||
Assert.eq(Mapper.bookPath("/book2/11009", "/wuxia/"), "/book2/11009/", "缺斜杠与上一行同身份,站点不会回 301")
|
||||
Assert.eq(Mapper.bookPath("/book6/santi.html", "/book8/"), "/book6/santi.html", "文件形式书页保持原样")
|
||||
Assert.eq(Mapper.list(LATEST, "/book8/")[2].authors, "", "没有作者就不编")
|
||||
Assert.eq(Mapper.list(NAV .. [[<div class="neirong" id="neirong"><a href="/files/chinese/">国内作品</a></div>]],
|
||||
"/files/chinese/"), nil, "认不出的链接说明不是列表页")
|
||||
Assert.is_nil(Mapper.list("<div>维护中</div>", "/wuxia/"))
|
||||
|
||||
Assert.eq(Mapper.nextPath(WUXIA, "/wuxia/"), "/wuxia/2.html", "分页形式跟着链接走")
|
||||
Assert.eq(Mapper.nextPath(CHINESE, "/files/chinese/"), "/files/chinese/29-2.html")
|
||||
Assert.eq(Mapper.nextPath(PAGE2, "/files/chinese/29-2.html"), "/files/chinese/29-3.html", "第 2 页接着往第 3 页")
|
||||
Assert.is_nil(Mapper.nextPath(LATEST, "/book8/"), "最新更新没有下一页")
|
||||
|
||||
local groups = Mapper.groups(WUXIA, "/wuxia/")
|
||||
Assert.len(groups, 2)
|
||||
Assert.eq(groups[1].title, "发现")
|
||||
Assert.eq(groups[2].active, "/wuxia/", "当前分类标出来")
|
||||
Assert.eq(groups[1].active, nil, "分类节点时频道不高亮")
|
||||
local channel = Mapper.groups(CHINESE, "/book8/")
|
||||
Assert.eq(channel[1].active, "/book8/", "频道节点时频道高亮")
|
||||
Assert.is_nil(channel[2].active)
|
||||
for _idx, chip in ipairs(groups[2].chips) do
|
||||
Assert.eq(Mapper.node(chip.id), chip.id, "每个分类节点都能当浏览节点:" .. tostring(chip.id))
|
||||
Assert.is_true(chip.title:len() > 0, "分类有名字")
|
||||
end
|
||||
Assert.is_nil(Mapper.groups("<div>没有导航</div>", "/wuxia/"))
|
||||
|
||||
Assert.is_nil(Mapper.node("/../../etc"))
|
||||
Assert.is_nil(Mapper.node("//evil.test/wuxia/"))
|
||||
Assert.is_nil(Mapper.node("/files/chinese/200805/518.html"), "书页不是书城节点")
|
||||
Assert.eq(Mapper.node("/files/chinese/"), "/files/chinese/")
|
||||
|
||||
-- 新版书页:目录分卷,章 href 跨目录。
|
||||
local SANTI = [[<div class="catalog"><h1>三体(全三册)</h1><div class="info"> 作者:刘慈欣 </div>
|
||||
<div class="summary"><b>内容简介:</b><div class="intro"><p>三体系列。</p></div></div>
|
||||
<div class="author"><a href="/files/writer/6620.html">刘慈欣作品集</a></div>
|
||||
<div class="mulu-title"><h2>三体1:地球往事</h2></div>
|
||||
<div class="mulu-list"><ul><li><a href="/book6/santi_1/11359.html">1.疯狂年代</a></li></ul></div>
|
||||
<div class="mulu-title"><h2>三体2:黑暗森林</h2></div>
|
||||
<div class="mulu-list"><ul><li><a href="/book6/santi_2/11400.html">1.末日之战</a></li></ul></div></div>
|
||||
<div class="mulu-title"><h2 class="">看过此书的人还喜欢</h3></div>
|
||||
<div class="common-list1"><a href="/book7/lingdian/"><b>1</b>《零点》</a></div>]]
|
||||
local santi = Mapper.book(SANTI, "/book6/santi.html")
|
||||
Assert.eq(santi.title, "三体(全三册)")
|
||||
Assert.eq(santi.authors, "刘慈欣")
|
||||
Assert.eq(santi.intro, "<p>三体系列。</p>")
|
||||
Assert.is_nil(santi.cover, "新版书页没有封面")
|
||||
local toc = Mapper.toc(SANTI, "/book6/santi.html")
|
||||
Assert.len(toc, 2, "推荐书不进目录")
|
||||
Assert.eq(toc[1].uid, "/book6/santi_1/11359.html")
|
||||
Assert.eq(toc[1].volume, "三体1:地球往事")
|
||||
Assert.eq(toc[2].volume, "三体2:黑暗森林")
|
||||
|
||||
-- 新版单卷书页:相对 href,卷名「正文」不占展示。
|
||||
local MODERN = [[<div class="catalog"><h1>十日终焉</h1><div class="info">作者:杀虫队队员</div>
|
||||
<div class="mulu-title"><h2>正文</h2></div>
|
||||
<div class="mulu-list"><ul><li><a href="503931.html">第1章 空屋</a></li></ul></div></div>]]
|
||||
Assert.eq(Mapper.book(MODERN, "/book7/10rizhongyan/").authors, "杀虫队队员")
|
||||
local modern = Mapper.toc(MODERN, "/book7/10rizhongyan/")
|
||||
Assert.eq(modern[1].origin, "/book7/10rizhongyan/503931.html")
|
||||
Assert.eq(modern[1].title, "第1章 空屋")
|
||||
Assert.is_nil(modern[1].volume)
|
||||
|
||||
-- 旧版书页:作者带发布时间,封面在内容区,目录表格带卷名。
|
||||
local OLD = [[<h2><b>官场小说:驻京办主任</b></h2>
|
||||
<TD height=30 class=l15> 来源: 作者:王晓方 发布时间:2008-05-29</TD>
|
||||
<td><div align="center"><img height="160" width="116" src="/temp2/02/wxf-03.jpg" /></div></td>
|
||||
<td class="p10-18"> 小说通过对东州市驻京办的刻画。<br /><a target="_blank" href="/files/writer/521.html">→王晓方作品集</a></td>
|
||||
<table class="acss"><tbody>
|
||||
<tr align="center" bgcolor="#ffffcc"><td colspan="3">第一章 迎来送往</td></tr>
|
||||
<tr><td><a target="_blank" href="/files/chinese/200805/518/1339.html">1、恭王府</a></td>
|
||||
<td><a target="_blank" href="/files/chinese/200805/518/1343.html">2、对弈</a></td></tr>
|
||||
<tr align="center" bgcolor="#ffffcc"><td colspan="3">第二章 官场的怪圈</td></tr>
|
||||
<tr><td><a target="_blank" href="/files/chinese/200805/518/1370.html">14、相约</a></td></tr>
|
||||
</tbody></table>]]
|
||||
local old = Mapper.book(OLD, "/files/chinese/200805/518.html")
|
||||
Assert.eq(old.title, "官场小说:驻京办主任", "书名里的冒号不切")
|
||||
Assert.eq(old.authors, "王晓方", "作者 cut 掉发布时间")
|
||||
Assert.eq(old.cover, "https://www.kanunu8.com/temp2/02/wxf-03.jpg")
|
||||
local oldToc = Mapper.toc(OLD, "/files/chinese/200805/518.html")
|
||||
Assert.len(oldToc, 3)
|
||||
Assert.eq(oldToc[1].origin, "/files/chinese/200805/518/1339.html")
|
||||
Assert.eq(oldToc[1].volume, "第一章 迎来送往")
|
||||
Assert.eq(oldToc[3].volume, "第二章 官场的怪圈")
|
||||
|
||||
-- 旧版武侠书页:目录里还有作品集与相对 href。
|
||||
local WUXIA_BOOK = [[<h2><b>沧海1</b></h2>
|
||||
<td><a href="/files/writer/375.html">→凤歌作品集</a></td>
|
||||
<table bgcolor="#d4d0c8"><tbody>
|
||||
<tr align="center" bgcolor="#ffffcc"><td align="center" colspan="3"><strong>正文</strong></td></tr>
|
||||
<tr bgcolor="#ffffff"><td width="33%"><a href="1726/38518.html">第一章 祖孙</a></td></tr>
|
||||
</tbody></table>]]
|
||||
local wuxiaToc = Mapper.toc(WUXIA_BOOK, "/wuxia/201102/1727.html")
|
||||
Assert.len(wuxiaToc, 1, "作品集不算章")
|
||||
Assert.eq(wuxiaToc[1].origin, "/wuxia/201102/1726/38518.html")
|
||||
Assert.is_nil(wuxiaToc[1].volume, "「正文」不当卷名")
|
||||
|
||||
-- 单页作品:书页就是正文,目录只有一章。
|
||||
local STORY = [[<div class="content book-content"><h1>合欢</h1><div class="info">作者:金宇澄</div>
|
||||
<div class="neirong" id="neirong"><p>大伯母在二楼房间里跪了四小时。<br /><br />蓓蒂以后再读《暴风骤雨》。</p></div>]]
|
||||
local storyInfo = Mapper.book(STORY, "/files/little/2024/02-08-11162.html")
|
||||
Assert.eq(storyInfo.title, "合欢", "单页作品也能出详情")
|
||||
Assert.eq(storyInfo.authors, "金宇澄")
|
||||
Assert.eq(storyInfo.intro, "", "官网没有简介就不编")
|
||||
Assert.is_nil(storyInfo.cover, "广告图不算封面")
|
||||
|
||||
local story = Mapper.toc(STORY, "/files/little/2024/02-08-11162.html")
|
||||
Assert.len(story, 1)
|
||||
Assert.eq(story[1].title, "合欢")
|
||||
Assert.eq(story[1].origin, "/files/little/2024/02-08-11162.html")
|
||||
|
||||
-- 正文:新版 neirong、旧版 content、广告与脚本剔除、段首缩进交回宿主 CSS。
|
||||
Assert.eq(Mapper.contentHtml([[<div class="neirong" id="neirong"><p> 一个老旧的钨丝灯<br />
|
||||
<br /> 静谧的气氛</p>
|
||||
<script>var ad = 1;</script></div><div class="ad-bottom">广告</div>]]),
|
||||
"<p>一个老旧的钨丝灯</p><p>静谧的气氛</p>")
|
||||
Assert.eq(Mapper.contentHtml([[<div id='content' class="connie">丁能通驻足在福字碑前。<br> <br> 其实,他有私心。</div>]]),
|
||||
"<p>丁能通驻足在福字碑前。</p><p>其实,他有私心。</p>")
|
||||
Assert.is_nil(Mapper.contentHtml('<div class="neirong" id="neirong"><script>x</script></div>'))
|
||||
Assert.is_nil(Mapper.contentHtml("<div>没有正文区</div>"))
|
||||
-- /bookN/ 这类目录书页的章页在 `class="text"` 里,页尾翻页链接不算正文。
|
||||
Assert.eq(Mapper.contentHtml([[<div class="col-left"><div id="Article"><div class="text" align="justify">
|
||||
<p> 血薇剑出鞘了。
|
||||
<p>第二段落</p>
|
||||
<table><td><script src="/advs/gg.js"></script></td></table>
|
||||
<p align="center"><a href='index.html'>上一页</a> <a href="./">回目录</a> <a href='198282.html'>下一页</a></p>
|
||||
</div></div></div><div class="side-r">侧栏推荐</div>]]),
|
||||
"<p>血薇剑出鞘了。</p><p>第二段落</p>", "目录书页正文与翻页导航分离")
|
||||
local dirToc = Mapper.toc([[<div class="mulu-title"><h2>第一部 血薇</h2></div>
|
||||
<div class="mulu-list"><ul><li><a href="198281.html">第一节</a></li></ul></div>]], "/book2/11009/")
|
||||
Assert.len(dirToc, 1, "目录书页的相对章链接能解析")
|
||||
Assert.eq(dirToc[1].origin, "/book2/11009/198281.html", "章页落在书目录内")
|
||||
Assert.eq(dirToc[1].volume, "第一部 血薇")
|
||||
@@ -0,0 +1,196 @@
|
||||
--[[--
|
||||
moonsource_kanunu.source 阅读链:详情、三种目录模板、正文转码、缓存复用与取消。
|
||||
|
||||
@module tests.moonsource_kanunu.reading_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, param-type-mismatch
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Config = require("support.config")
|
||||
|
||||
local path = "/files/chinese/200805/518.html"
|
||||
local requests, script = {}, {}
|
||||
package.preload["moonsource_kanunu.client"] = function()
|
||||
local Fake = {}
|
||||
function Fake:get(target, cb)
|
||||
local request = { path = target, cb = cb }
|
||||
requests[#requests + 1] = request
|
||||
local handle = { cancel = function() request.cancelled = true end }
|
||||
local step = table.remove(script, 1) or { html = "" }
|
||||
request.deliver = function()
|
||||
if request.cancelled then return end
|
||||
if step.err then cb(nil, step.err) else cb(step.html or "", nil) end
|
||||
end
|
||||
if not step.delayed then request.deliver() end
|
||||
return handle
|
||||
end
|
||||
return { new = function() return setmetatable({}, { __index = Fake }) end }
|
||||
end
|
||||
|
||||
-- 详情落库由 book.store 负责,这里只关心映射出来的字段。
|
||||
local stored
|
||||
package.loaded["book.store"] = { rememberDetail = function(book)
|
||||
stored = book
|
||||
return book
|
||||
end }
|
||||
|
||||
-- 目录缓存用真实现会连带 sqlite,这里只验证解析结果与调用次序。
|
||||
local toc_saved, toc_err
|
||||
package.loaded["source.toc"] = {
|
||||
readAsync = function(_source_id, _stable_id, cb)
|
||||
local cached = toc_err and nil or (toc_saved or {})
|
||||
if cached and #cached > 0 then cb(cached) else cb(nil, toc_err) end
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
parseAsync = function(_source_id, _stable_id, parse, cb)
|
||||
local toc, err = parse()
|
||||
if toc then toc_saved = toc end
|
||||
cb(toc, err)
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
|
||||
local online = true
|
||||
package.loaded["ui/network/manager"] = { isConnected = function() return online end }
|
||||
|
||||
local source = require("moonsource_kanunu.source").new()
|
||||
local result, reason, calls
|
||||
local function done(data, err)
|
||||
result, reason, calls = data, err, (calls or 0) + 1
|
||||
end
|
||||
|
||||
-- 详情:新版书页的标题、作者、简介、封面(封面按书页地址解析)。
|
||||
local MODERN = [[<div class="catalog"><h1>三体(全三册)</h1><div class="info"> 作者:刘慈欣 </div>
|
||||
<div class="intro"><p>三体系列。</p></div><img src="/temp2/03/santi.jpg">
|
||||
<div class="mulu-title"><h2>三体1:地球往事</h2></div>
|
||||
<div class="mulu-list"><ul><li><a href="/book6/santi_1/11359.html">1.疯狂年代</a></li></ul></div></div>]]
|
||||
script = { { html = MODERN } }
|
||||
requests = {}
|
||||
source:getDetailAsync({ source_id = "kanunu", stable_id = "book6/santi.html" }, done)
|
||||
Assert.is_nil(reason)
|
||||
Assert.eq(stored.title, "三体(全三册)")
|
||||
Assert.eq(stored.authors, "刘慈欣")
|
||||
Assert.eq(stored.cover, "https://www.kanunu8.com/temp2/03/santi.jpg")
|
||||
Assert.eq(requests[1].path, "/book6/santi.html")
|
||||
|
||||
script = { { html = '<div class="content">维护中</div>' } }
|
||||
source:getDetailAsync({ stable_id = "book6/santi.html" }, done)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(reason, "无法解析书籍信息")
|
||||
|
||||
-- 目录:新版分卷;推荐区块的链接不算章。
|
||||
toc_saved, toc_err = nil, nil
|
||||
script = { { html = MODERN .. [[<div class="mulu-title"><h2>看过此书的人还喜欢</h2></div>
|
||||
<div class="common-list1"><a href="/book7/lingdian/">《零点》</a></div>]] } }
|
||||
source:refreshTocAsync({ stable_id = "book6/santi.html" }, done)
|
||||
Assert.is_nil(reason)
|
||||
Assert.len(result, 1)
|
||||
Assert.eq(result[1].origin, "/book6/santi_1/11359.html")
|
||||
Assert.eq(result[1].volume, "三体1:地球往事")
|
||||
|
||||
-- 旧版表格目录:相对 href 按书页地址解析,卷名沿用。
|
||||
local OLD = [[<h2><b>驻京办主任</b></h2>
|
||||
<table><tr><td colspan="3">第一章 迎来送往</td></tr>
|
||||
<tr><td><a href="518/1339.html">1、对弈</a></td></tr>
|
||||
<tr><td><a href="/files/chinese/200805/518/1343.html">2、对弈</a></td></tr></table>]]
|
||||
script = { { html = OLD } }
|
||||
source:refreshTocAsync({ stable_id = "files/chinese/200805/518.html" }, done)
|
||||
Assert.len(result, 2)
|
||||
Assert.eq(result[1].origin, "/files/chinese/200805/518/1339.html", "相对章地址")
|
||||
Assert.eq(result[2].volume, "第一章 迎来送往")
|
||||
|
||||
-- 单页作品:书页即正文,目录只有一章。
|
||||
local STORY = [[<div class="content book-content"><h1>合欢</h1><div class="info">作者:金宇澄</div>
|
||||
<div class="neirong" id="neirong"><p>大伯母在二楼房间里跪了四小时。</p></div>]]
|
||||
script = { { html = STORY } }
|
||||
source:refreshTocAsync({ stable_id = "files/little/2024/02-08-11162.html" }, done)
|
||||
Assert.len(result, 1)
|
||||
Assert.eq(result[1].origin, "/files/little/2024/02-08-11162.html")
|
||||
|
||||
-- 没有目录也没有正文:明确报错,不给空目录。
|
||||
script = { { html = '<div class="catalog"><h1>未知书</h1></div>' } }
|
||||
source:refreshTocAsync({ stable_id = "book9/x" }, done)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(reason, "章节列表为空")
|
||||
|
||||
-- 已有目录缓存:不联网。
|
||||
toc_saved = { { idx = 1, uid = "c1", title = "旧目录", origin = "/files/chinese/200805/518/1339.html" } }
|
||||
script = {}
|
||||
requests = {}
|
||||
result = nil
|
||||
source:loadTocAsync({ stable_id = "files/chinese/200805/518.html" }, done)
|
||||
Assert.eq(result, toc_saved, "旧目录离线继续可用")
|
||||
Assert.eq(#requests, 0, "有缓存不联网")
|
||||
script = { { html = OLD } }
|
||||
source:loadTocAsync({ stable_id = "files/chinese/200805/518.html" }, done, true)
|
||||
Assert.eq(#requests, 1, "force 刷新要联网")
|
||||
|
||||
-- 目录请求失败:错误透传。
|
||||
toc_saved = nil
|
||||
script = { { err = "network down" } }
|
||||
source:refreshTocAsync({ stable_id = "files/chinese/200805/518.html" }, done)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(reason, "network down")
|
||||
|
||||
-- 打开书:正文抓取与复用策略交给公共实现。
|
||||
local ops
|
||||
package.loaded["source.chapter"] = {
|
||||
openAsync = function(_source, _identity, _book, _options, options) ops = options end,
|
||||
}
|
||||
source:openBookAsync({ source_id = "kanunu", stable_id = "files/chinese/200805/518.html" },
|
||||
{ chapter_idx = 1 }, function() end)
|
||||
Assert.not_nil(ops.fetchContent)
|
||||
Assert.not_nil(ops.reuse)
|
||||
|
||||
-- 正文:段落化、去掉导航与广告,带完整性标记。
|
||||
local CHAPTER = [[<div class="content book-content"><h1>1、对弈</h1>
|
||||
<div class="neirong" id="neirong"><p> 夜色里的长安街。<br /><br /> 车停在楼下。</p></div>
|
||||
<div class="book-nav"><a href="/files/chinese/200805/518/1340.html">下一页</a></div>
|
||||
<div class="footer">努努书坊 www.kanunu8.com</div>]]
|
||||
script = { { html = CHAPTER } }
|
||||
local payload
|
||||
ops.fetchContent({}, { origin = "/files/chinese/200805/518/1339.html", title = "1、对弈" },
|
||||
function(data, err)
|
||||
payload, reason = data, err
|
||||
end)
|
||||
Assert.is_nil(reason)
|
||||
Assert.eq(payload.title, "1、对弈")
|
||||
Assert.eq(payload.mark, "moon:kanunu:complete")
|
||||
Assert.eq(payload.html, "<p>夜色里的长安街。</p><p>车停在楼下。</p>", "<br> 切段、段首缩进交给排版")
|
||||
Assert.is_nil(payload.html:find("下一页", 1, true), "导航不进正文")
|
||||
Assert.is_nil(payload.html:find("www.kanunu8.com", 1, true), "页脚不进正文")
|
||||
|
||||
-- 章节页没有正文:不写空缓存。
|
||||
script = { { html = "<div>该页不存在</div>" } }
|
||||
payload = nil
|
||||
ops.fetchContent({}, { origin = "/files/x/1.html", title = "缺失" }, function(data, err)
|
||||
payload, reason = data, err
|
||||
end)
|
||||
Assert.is_nil(payload)
|
||||
Assert.eq(reason, "这一章没有正文")
|
||||
|
||||
-- 缓存复用:离线保留旧文件;在线只重用带完整标记的文件。
|
||||
local cache = Config.dir() .. "/kanunu-chapter.html"
|
||||
local function write(text)
|
||||
local file = assert(io.open(cache, "wb"))
|
||||
file:write(text)
|
||||
file:close()
|
||||
end
|
||||
write("<html><p>旧单页</p></html>")
|
||||
Assert.is_false(ops.reuse(cache), "在线升级旧正文")
|
||||
online = false
|
||||
Assert.is_true(ops.reuse(cache), "离线不丢弃旧缓存")
|
||||
online = true
|
||||
write("<!--moon:kanunu:complete--><html><p>完整章节</p></html>")
|
||||
Assert.is_true(ops.reuse(cache))
|
||||
os.remove(cache)
|
||||
|
||||
-- 目录请求取消:迟到结果不回调。
|
||||
script = { { delayed = true } }
|
||||
requests = {}
|
||||
local before = calls
|
||||
local handle = source:refreshTocAsync({ stable_id = "files/chinese/200805/518.html" }, done)
|
||||
handle.cancel()
|
||||
Assert.is_true(requests[1].cancelled)
|
||||
requests[1].deliver()
|
||||
Assert.eq(calls, before, "取消后不回调")
|
||||
Reference in new issue
Block a user