🚚 (split): 从月读拆出独立仓库,沿用原书源 id
Release / release (push) Successful in 10s

This commit is contained in:
Garfield committed 2026-11-27 08:08:02 +08:00
commit aae155c43f
23 files changed
+1912

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
--[[--
爱看书动态分类、完本首页错链、聚合分页与取消。
@module tests.moonsource_18ta.browse_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Mapper = require("moonsource_18ta.mapper")
local filters = [[<div id="after_menu"><div>只看全本</div><div>
<a href="/sort/">全部分类</a><a href="/sort/1/1.html">玄幻奇幻</a>
<a href="/sort/42/1.html">新增分类</a></div></div>]]
local groups = Mapper.storeGroups(filters, "/quanben/sort/1/1.html")
Assert.len(groups, 2)
Assert.eq(groups[1].chips[3].id, "/quanben/sort/42/1.html", "分类取官网,不硬编码ID")
Assert.eq(groups[2].chips[1].id, "/sort/1/1.html", "切状态保留分类")
Assert.is_nil(Mapper.storeGroups("<html>维护</html>", "/sort/"))
Assert.is_nil(Mapper.storeNode("/sort/../../account"))
Assert.is_nil(Mapper.storeNode("//evil.test/sort/"))
Assert.eq(Mapper.storePage("/quanben/sort/", 2), "/quanben/sort/2.html")
Assert.is_true(Mapper.storeMore('<div id="pagelink"><a href="/sort/1/2.html">2</a></div>', "/quanben/sort/1/1.html", 1))
Assert.is_false(Mapper.storeMore('<a href="/sort/1/2.html">推荐链接</a>', "/sort/1/1.html", 1))
local requests, delayed, failed, malformed = {}, false, false, false
package.loaded["http.request"] = {
headerAll = function() return {} end,
get = function(url, _opts, cb)
local request = { url = url, cb = cb }
requests[#requests + 1] = request
local handle = { cancel = function() request.cancelled = true end }
local page = tonumber(url:match("/(%d+)%.html$")) or 1
if delayed and page == 2 then return handle end
if failed and page == 2 then cb(nil, "network down"); return handle end
if malformed then cb("<title>维护</title>"); return handle end
local cards = {}
for index = 1, 30 do
local id = (page - 1) * 30 + index
cards[#cards + 1] = '<li><div class="img_span"><a href="/akshtml' .. id
.. '.html"><h2>书' .. id .. '</h2><span>玄幻 / 全本</span></a></div></li>'
end
cb(filters .. '<ul class="flex">' .. table.concat(cards) .. '</ul><div id="pagelink">'
.. (page < 3 and '<a href="/sort/1/' .. (page + 1) .. '.html">下一页</a>' or "") .. '</div>')
return handle
end,
}
local source = require("moonsource_18ta.source").new()
local result, reason
source:browseAsync({ node = "/quanben/sort/1/1.html" }, function(data, err) result, reason = data, err end)
Assert.is_nil(reason)
Assert.len(result.data, 60)
Assert.eq(requests[2].url, "https://www.18ta.com/quanben/sort/1/2.html")
Assert.eq(result.groups[1].active, "/quanben/sort/1/1.html")
source:browseAsync({ node = "1", limit = 8 }, function(data, err) result, reason = data, err end)
Assert.len(result.data, 8)
Assert.eq(requests[#requests].url, "https://www.18ta.com/sort/1/1.html", "旧数字节点保持兼容")
Assert.is_false(source:capabilities().shelf_push)
failed = true
source:browseAsync({ node = "1" }, function(data, err) result, reason = data, err end)
Assert.is_nil(result)
Assert.eq(reason, "network down")
failed, malformed = false, true
source:browseAsync({}, function(data, err) result, reason = data, err end)
Assert.is_nil(result)
Assert.eq(reason, "响应无效")
malformed, delayed = false, true
local calls = 0
local handle = source:browseAsync({ node = "1" }, function() calls = calls + 1 end)
handle.cancel()
Assert.is_true(requests[#requests].cancelled, "同步第一页不能覆盖第二页句柄")
requests[#requests].cb(filters)
Assert.eq(calls, 0)
+153
View File
@@ -0,0 +1,153 @@
--[[--
moonsource_18ta.client 离线用例:WAF 403 → Set-Cookie → 重发的兜底逻辑。
@module tests.moonsource_18ta.client_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch, cast-type-mismatch, return-type-mismatch, cast-local-type
local Assert = require("support.assert")
-- 脚本化 http.request:get 按序消费 script,headerAll 从 res.headers 取 Set-Cookie;
-- opts.jar 照真实 Request:发前带 Cookie,回调前(未取消时)收 Set-Cookie。
local calls = {}
local script = {}
package.preload["http.request"] = function()
local function headerAll(res, name)
local out = {}
if name == "Set-Cookie" and res and res.headers then
local raw = res.headers["Set-Cookie"]
if type(raw) == "table" then
for _idx, line in ipairs(raw) do out[#out + 1] = line end
elseif type(raw) == "string" then
out[1] = raw
end
end
return out
end
return {
get = function(url, opts, cb)
local headers = {}
for key, value in pairs(opts and opts.headers or {}) do headers[key] = value end
local jar = opts and opts.jar
if jar then headers.Cookie = jar:header(url) end
local call = { url = url, headers = headers }
local function deliver(body, err, res)
if jar and res and not call.cancelled then jar:absorb(url, headerAll(res, "Set-Cookie")) end
cb(body, err, res)
end
call.cb = deliver
calls[#calls + 1] = call
local step = table.remove(script, 1) or { code = 200, body = "" }
local handle = { cancel = function() call.cancelled = true end }
if step.delayed then return handle end
if step.err then
deliver(nil, step.err, nil)
elseif step.code and step.code ~= 200 then
deliver(nil, "HTTP " .. step.code, { code = step.code, headers = step.headers or {} })
else
deliver(step.body, nil, { code = 200, body = step.body, headers = step.headers or {} })
end
return handle
end,
headerAll = headerAll,
}
end
local Client = require("moonsource_18ta.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
-- 首次 403 下发 Cookie:收 Cookie 重发一次,拿到正文。
do
script = {
{ code = 403, headers = { ["Set-Cookie"] = { "a1345448228df7d678feec207bf34007=abc; Path=/", "server_session_8ab1edd4=xyz; Path=/; HttpOnly" } } },
{ code = 200, body = "LISTING" },
}
calls = {}
local client = Client.new()
local body, err = run(client, "/sort/1/1.html")
Assert.is_nil(err)
Assert.eq(body, "LISTING")
Assert.eq(#calls, 2, "403 后重发一次")
Assert.eq(calls[1].url, "https://www.18ta.com/sort/1/1.html")
Assert.is_nil(calls[1].headers.Cookie, "首请求不带 Cookie")
Assert.is_true(calls[2].headers.Cookie:find("a1345448228df7d678feec207bf34007=abc", 1, true), "重发带首个 Cookie")
Assert.is_true(calls[2].headers.Cookie:find("server_session_8ab1edd4=xyz", 1, true), "重发带全部 Cookie")
end
-- 已有 Cookie 直通 200:不重发。
do
script = { { code = 200, body = "OK" } }
calls = {}
local client = Client.new()
client.jar:seed("k=v")
local body = run(client, "/akshtml123.html")
Assert.eq(body, "OK")
Assert.eq(#calls, 1)
Assert.is_true(calls[1].headers.Cookie:find("k=v", 1, true), "自带 Cookie 头")
end
-- 403 但没下发 Cookie:不重发,错误透传。
do
script = { { code = 403 } }
calls = {}
local body, err = run(Client.new(), "/sort/1/1.html")
Assert.is_nil(body)
Assert.eq(err, "HTTP 403")
Assert.eq(#calls, 1, "没有 Cookie 不重发")
end
-- 传输错误:不重发,错误透传。
do
script = { { err = "timeout" } }
calls = {}
local body, err = run(Client.new(), "/sort/1/1.html")
Assert.is_nil(body)
Assert.eq(err, "timeout")
Assert.eq(#calls, 1)
end
-- Cookie 随成功响应刷新:下一个请求带上新 Cookie。
do
script = {
{ code = 200, body = "A", headers = { ["Set-Cookie"] = { "sess=1; Path=/" } } },
{ code = 200, body = "B" },
}
calls = {}
local client = Client.new()
run(client, "/sort/1/1.html")
local body = run(client, "/sort/1/2.html")
Assert.eq(body, "B")
Assert.is_true(calls[2].headers.Cookie:find("sess=1", 1, true), "刷新后的 Cookie 参与后续请求")
end
-- 完整 URL 原样透传(目录 href 的 origin 是绝对地址)。
do
script = { { code = 200, body = "CH" } }
calls = {}
local body = run(Client.new(), "https://www.18ta.com/aks/287/287895/5447961.html")
Assert.eq(body, "CH")
Assert.eq(calls[1].url, "https://www.18ta.com/aks/287/287895/5447961.html")
end
-- 首请求同步 403、重试异步:取消须作用于重试,不接受迟到 Cookie 和正文。
do
script = {
{ code = 403, headers = { ["Set-Cookie"] = "waf=pending; Path=/" } },
{ delayed = true },
}
calls = {}
local client, completed = Client.new(), 0
local handle = client:get("/sort/", function() completed = completed + 1 end)
handle.cancel()
Assert.is_true(calls[2].cancelled)
calls[2].cb("late", nil, { headers = { ["Set-Cookie"] = "waf=late; Path=/" } })
Assert.eq(completed, 0)
Assert.eq(client.jar:get("waf"), "pending")
end
+142
View File
@@ -0,0 +1,142 @@
--[[--
moonsource_18ta.mapper 离线用例:片段取自 18ta.com 真实页面(分类列表 / 书页 / 章节页)。
@module tests.moonsource_18ta.mapper_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch, cast-type-mismatch, return-type-mismatch, cast-local-type
local Assert = require("support.assert")
local Mapper = require("moonsource_18ta.mapper")
-- /sort/<cat>/<page>.html 的一张书目卡片(完整 <li> 块)。
local LIST_HTML = [[
<div class="book_list">
<ul class="flex">
<li>
<div class="img_span"><a href="/akshtml287895.html"><img class="lazy" src="https://www.18ta.com/static/shipsay/nocover.jpg" data-original="https://www.18ta.com/files/article/image/287/287895/287895s.jpg" title="从黄金一代开始" /><span>玄幻 / 连载</span></a></div>
<div class="w100">
<a href="/akshtml287895.html"><h2>从黄金一代开始</h2></a>
<p class="indent"> 天赋爆棚的年轻人们拥有了,展示自己的表演场所。小说群:864747664
</p>
<div class="li_bottom">
<a href="/author/%E5%91%86%E5%91%86%E6%B5%B7%E5%8F%82/"><i class="fa fa-user-circle-o">&nbsp;呆呆海参</i></a>
<div>
<em class="orange">53万字</em><em class="blue">12小时前</em>
</div>
</div>
</div>
</li>
</div>
</ul>
]]
do
local list = Mapper.list(LIST_HTML)
Assert.eq(#list, 1)
local b = list[1]
Assert.eq(b.source_id, "18ta")
Assert.eq(b.stable_id, "287895")
Assert.eq(b.title, "从黄金一代开始")
Assert.eq(b.authors, "呆呆海参")
Assert.eq(b.category, "玄幻")
Assert.eq(b.cover, "https://www.18ta.com/files/article/image/287/287895/287895s.jpg")
Assert.is_true(b.intro:find("天赋爆棚", 1, true), "简介保留正文")
Assert.eq(b.meta.status, "连载")
end
-- 空页与坏卡片:不报错、不产书。
Assert.is_nil(Mapper.list("<div></div>"), "坏响应不能当空书库")
Assert.is_nil(Mapper.list('<ul class="flex"><li><div class="img_span"><a href="/akshtmlx.html">x</a></div></li></ul>'))
Assert.len(Mapper.list('<ul class="flex"></ul>'), 0)
-- 书页:og:novel meta + .intro 简介 + og:image 封面。
local BOOK_HTML = [[
<html><head>
<meta property="og:novel:category" content="玄幻奇幻">
<meta property="og:novel:author" content="呆呆海参">
<meta property="og:novel:book_name" content="从黄金一代开始">
<meta property="og:novel:status" content="连载">
<meta property="og:image" content="https://www.18ta.com/files/article/image/287/287895/287895s.jpg"/>
</head><body>
<div id="info">
<div class="intro">
<p><p> 天赋爆棚的年轻人们拥有了,展示自己的表演场所。小说群:864747664</p></p>
</div>
<div class="section chapter_list">
<div class="title jcc">《从黄金一代开始》最新章节</div>
<ul>
<li><a href="/aks/287/287895/5448372.html" title="从黄金一代开始 第416章 再战火枪手雄鹿">第416章 再战火枪手雄鹿</a></li>
</ul>
</div>
</div>
<div id="catalog">
<div class="section chapter_list">
<ul id="ul_all_chapters">
<li><a href="/aks/287/287895/5447961.html" title="从黄金一代开始 第一章 红人大哥">第一章 红人大哥</a></li>
<li><a href="/aks/287/287895/5447962.html" title="从黄金一代开始 第二章 刻苦训练">第二章 刻苦训练</a></li>
</ul>
</div>
</div>
</body></html>
]]
do
local b = Mapper.book(BOOK_HTML)
Assert.eq(b.title, "从黄金一代开始")
Assert.eq(b.authors, "呆呆海参")
Assert.eq(b.category, "玄幻奇幻")
Assert.eq(b.cover, "https://www.18ta.com/files/article/image/287/287895/287895s.jpg")
Assert.eq(b.meta.status, "连载")
Assert.is_true(b.intro:find("天赋爆棚", 1, true), "简介取 .intro 文本")
end
Assert.is_nil(Mapper.book("<html><body>no meta</body></html>"))
-- 目录只取 ul_all_chapters(页首「最新章节」预览块不得混入),章序即列表顺序。
do
local toc = Mapper.toc(BOOK_HTML)
Assert.eq(#toc, 2, "预览块不混入目录")
Assert.eq(toc[1].idx, 1)
Assert.eq(toc[1].uid, "/aks/287/287895/5447961.html")
Assert.eq(toc[1].title, "第一章 红人大哥")
Assert.eq(toc[1].origin, "/aks/287/287895/5447961.html")
Assert.eq(toc[2].title, "第二章 刻苦训练")
end
Assert.eq(#Mapper.toc("<html><body></body></html>"), 0)
-- 章节正文:article 内部纯段落。
local CH_HTML = [[
<html><body>
<article id="article" class="content"><p> “教练,我想打篮球。”
</p><p> 室内篮球场上,约翰教练有些意外的看着眼前的小伙子。
</p></article>
</body></html>
]]
do
local body = Mapper.chapterContent(CH_HTML)
Assert.is_true(body:find("<p", 1, true), "正文是 <p> 段落")
Assert.is_true(body:find("教练,我想打篮球", 1, true))
Assert.is_nil(body:find("</article>", 1, true), "不含 article 标签本身")
end
Assert.is_nil(Mapper.chapterContent("<html><body><div>no article</div></body></html>"))
Assert.is_nil(Mapper.chapterContent('<article id="advert"><p>广告</p></article>'))
Assert.is_nil(Mapper.chapterContent('<article id="article"><p> </p></article>'))
Assert.eq(Mapper.chapterContent('<article id="article"><p>正文</p><script>bad()</script></article>'), "<p>正文</p>")
local path = "/aks/287/287895/5447961.html"
local function nextPage(href)
return '<a id="next_url" href="' .. href .. '">下一页</a>'
end
Assert.eq(Mapper.chapterNext(nextPage("/aks/287/287895/5447961_2.html"), path), "/aks/287/287895/5447961_2.html")
Assert.is_nil(Mapper.chapterNext(nextPage("/aks/287/287895/5447962.html"), path))
Assert.is_nil(Mapper.chapterNext(nextPage("/akshtml287895.html"), path))
for _idx, href in ipairs({ path, "/aks/287/287895/5447961_4.html", "/aks/1/2/3.html", "https://evil.test/page" }) do
local next_path, err = Mapper.chapterNext(nextPage(href), path)
Assert.is_nil(next_path)
Assert.eq(err, "响应无效")
end
local _next_path, err = Mapper.chapterNext("<p>没有导航</p>", path)
Assert.eq(err, "响应无效", "坏导航不能将第一段正文当完整章节")
+96
View File
@@ -0,0 +1,96 @@
--[[--
爱看书多页章节完整交付、中途失败和取消,兼容旧缓存与目录落盘失败。
@module tests.moonsource_18ta.reading_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 path = "/aks/1/9/100.html"
local requests, delayed, failed, malformed = {}, false, false, false
package.loaded["http.request"] = {
headerAll = function() return {} end,
get = function(url, _opts, cb)
local request = { url = url, cb = cb }
requests[#requests + 1] = request
local handle = { cancel = function() request.cancelled = true end }
local page = tonumber(url:match("_(%d+)%.html$")) or 1
if delayed and page == 2 then return handle end
if failed and page == 2 then cb(nil, "timeout"); return handle end
if malformed then cb('<article id="article"><p>片段</p></article>'); return handle end
cb('<article id="article"><p>第' .. page .. '页</p></article><a id="next_url" href="'
.. (page == 1 and "/aks/1/9/100_2.html" or "/aks/1/9/101.html") .. '">下一页</a>')
return handle
end,
}
local Client = require("moonsource_18ta.client")
local client = Client.new()
local result, reason, calls = nil, nil, 0
local function done(html, err) result, reason, calls = html, err, calls + 1 end
client:contentAsync("https://www.18ta.com" .. path, done)
Assert.eq(result, "<p>第1页</p>\n<p>第2页</p>")
Assert.eq(calls, 1)
Assert.len(requests, 2, "不能取下一章")
failed = true
client:contentAsync(path, done)
Assert.is_nil(result, "第二页失败不能交付第一页")
Assert.eq(reason, "timeout")
failed, malformed = false, true
client:contentAsync(path, done)
Assert.is_nil(result)
Assert.eq(reason, "响应无效")
malformed, delayed = false, true
local before = calls
local handle = client:contentAsync(path, done)
handle.cancel()
Assert.is_true(requests[#requests].cancelled)
requests[#requests].cb('<article id="article"><p>迟到</p></article>')
Assert.eq(calls, before)
local online = true
package.loaded["ui/network/manager"] = { isConnected = function() return online end }
local ops
local toc = { { idx = 1, uid = path } }
package.loaded["source.toc"] = {
readAsync = function(_source_id, _stable_id, cb) return require("source.base").defer(cb, toc) end,
parseAsync = function(_source_id, _stable_id, parse, cb)
Assert.len(parse(), 1)
return require("source.base").defer(cb, nil, "章节目录保存失败")
end,
}
package.loaded["source.chapter"] = {
openAsync = function(_source, _identity, _book, _options, options) ops = options end,
}
local source = require("moonsource_18ta.source").new()
source:openBookAsync({ source_id = "18ta", stable_id = "9" }, { chapter_idx = 1 }, function() end)
local cache = Config.dir() .. "/18ta-legacy.html"
local file = assert(io.open(cache, "wb"))
file:write("<html><p>旧单页</p></html>")
file:close()
Assert.is_false(ops.reuse(cache), "在线升级旧单页缓存")
online = false
Assert.is_true(ops.reuse(cache), "离线不丢弃旧缓存")
online = true
file = assert(io.open(cache, "wb"))
file:write("<!--moon:18ta:complete--><html><p>完整章节</p></html>")
file:close()
Assert.is_true(ops.reuse(cache))
os.remove(cache)
delayed = false
local payload
ops.fetchContent({}, { origin = "https://www.18ta.com" .. path, title = "章节" }, function(data) payload = data end)
Assert.eq(payload.mark, "moon:18ta:complete")
Assert.is_true(payload.html:find("第2页", 1, true) ~= nil)
source:loadTocAsync({ stable_id = "9" }, function(data) result = data end)
Stubs.flush()
Assert.eq(result, toc, "旧目录离线继续可用")
source.client.bookAsync = function(_self, _id, cb)
cb('<ul id="ul_all_chapters"><li><a href="' .. path .. '" title="章">章节</a></li></ul>')
end
source:refreshTocAsync({ stable_id = "9" }, done)
Stubs.flush()
Assert.is_nil(result)
Assert.eq(reason, "章节目录保存失败")