✨ (kanunu): 接入努努书坊,频道与分类浏览、按章阅读、整本缓存
Release / release (push) Successful in 14s

This commit is contained in:
garfield committed 2026-12-02 09:17:14 +08:00
commit f50b9e7711
23 files changed
+2346

No files matched your search

+152
View File
@@ -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()