Files
moonsource-kanunu/tests/kanunu/reading_spec.lua
T

197 lines
8.2 KiB
Lua

--[[--
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>&nbsp;&nbsp;&nbsp;&nbsp;夜色里的长安街。<br /><br />&nbsp;&nbsp;车停在楼下。</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, "取消后不回调")