✨ (shidian): 接入识典古籍,按分类分章阅读
Release / release (push) Successful in 12s

This commit is contained in:
Garfield committed 2026-11-28 13:44:23 +08:00
commit eb118c6b39
21 files changed
+2126

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
--[[--
识典古籍客户端:只打公开 loader,打开书时按章节号、书号_1、分类首屏、书名搜索往下找。
网络失败停住,不把它当成「没有这本书」。
@module tests.shidian.client_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, inject-field
local Assert = require("support.assert")
local JsonStub = require("support.json_stub")
package.loaded["json"] = nil
package.preload["json"] = function()
return { decode = JsonStub.decode, encode = JsonStub.encode }
end
local JSON = require("json")
local _ = require("l10n").get("moonsource_shidian")
local UIManager = require("ui/uimanager")
local queued_tick = UIManager.nextTick
local function syncTick()
UIManager.nextTick = function(_, fn) fn() end
end
local function restoreTick()
UIManager.nextTick = queued_tick
end
local calls, script = {}, {}
package.preload["http.request"] = function()
return {
get = function(url, _opts, cb)
calls[#calls + 1] = url
local step = table.remove(script, 1) or { body = "{}" }
local handle = { cancel = function() end }
if step.err then cb(nil, step.err) else cb(step.body) end
return handle
end,
}
end
local Client = require("moonsource_shidian.client")
local function page(id)
return JSON.encode({ bookInfo = { bookId = id, bookName = "书" }, ssrChapterId = "CH" })
end
local function run(hint)
local client = Client.new()
local got, err
client:resolveAsync(hint, function(data, e) got, err = data, e end)
return got, err
end
-- 书目上已有章节号:只请求这一章。
do
calls, script = {}, { { body = page("B1") } }
local got, err = run({ id = "B1", seed = "SEED" })
Assert.is_nil(err)
Assert.eq(got.bookInfo.bookId, "B1")
Assert.eq(#calls, 1)
Assert.is_true(calls[1]:find("/book/B1/chapter/SEED?", 1, true) ~= nil)
end
-- 书号_1 没有这本书,分类首屏有章节号,第三下才打开。
do
calls, script = {}, {
{ body = '{"isSpider":false}' },
{ body = JSON.encode({ library = { data = { bookList = {
{ bookId = "B1", extra = { hasEntityRelationChapterIds = { "FROMCATE" } } },
} } } }) },
{ body = page("B1") },
}
local got, err = run({ id = "B1", title = "周易", cate = "10000" })
Assert.is_nil(err)
Assert.eq(got.ssrChapterId, "CH")
Assert.eq(#calls, 3)
Assert.is_true(calls[1]:find("/chapter/B1_1?", 1, true) ~= nil)
Assert.is_true(calls[2]:find("/library/10000?", 1, true) ~= nil)
Assert.is_true(calls[3]:find("/chapter/FROMCATE?", 1, true) ~= nil)
end
-- 分类首屏没有它,退到书名搜索。
do
calls, script = {}, {
{ body = '{"enableWebp":false}' },
{ body = '{"library":{"status":404}}' },
{ body = JSON.encode({ data = { paragraphs = {
{ chapterId = "FROMSEARCH", bookInfo = { bookId = "B1", bookName = "周易" } },
}, recommendBooks = { books = {} } } }) },
{ body = page("B1") },
}
local got = run({ id = "B1", title = "周易", cate = "10000" })
Assert.eq(got.bookInfo.bookId, "B1")
Assert.eq(#calls, 4)
Assert.is_true(calls[3]:find("/search/", 1, true) ~= nil)
Assert.is_true(calls[4]:find("/chapter/FROMSEARCH?", 1, true) ~= nil)
end
-- 网络失败不继续试下一条。
do
calls, script = {}, { { err = "offline" } }
local got, err = run({ id = "B1", title = "周易", cate = "10000" })
Assert.is_nil(got)
Assert.eq(err, "offline")
Assert.eq(#calls, 1)
end
-- 被当成爬虫是失败,不是空章节。
do
calls, script = {}, { { body = '{"isSpider":true,"enableWebp":false}' } }
local got, err = run({ id = "B1", seed = "SEED" })
Assert.is_nil(got)
Assert.eq(err, _("识典古籍把这次请求当成了爬虫,没有返回正文"))
end
-- 坏 JSON、空搜索词都不当成空结果。
do
calls, script = {}, { { body = "<html></html>" } }
local client = Client.new()
local got, err
client:libraryAsync("10000", function(data, e) got, err = data, e end)
Assert.is_nil(got)
Assert.eq(err, _("响应无效"))
syncTick()
client:searchAsync("", function(data, e) got, err = data, e end)
restoreTick()
Assert.is_nil(got)
Assert.eq(err, _("响应无效"))
end
-- 刚取过的同一章不再请求。
do
calls, script = {}, { { body = page("B1") } }
local client = Client.new()
client:chapterAsync("B1", "CH", function() end)
Assert.eq(#calls, 1)
local again
syncTick()
client:contentAsync("B1", "CH", function(data) again = data end)
restoreTick()
Assert.eq(#calls, 1)
Assert.eq(again.bookInfo.bookId, "B1")
end
+171
View File
@@ -0,0 +1,171 @@
--[[--
识典古籍字段映射:书库首屏、搜索首屏、目录树、正文转 HTML。
@module tests.shidian.mapper_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, inject-field
local Assert = require("support.assert")
local JsonStub = require("support.json_stub")
package.loaded["json"] = nil
package.preload["json"] = function()
return { decode = JsonStub.decode, encode = JsonStub.encode }
end
local Mapper = require("moonsource_shidian.mapper")
local _ = require("l10n").get("moonsource_shidian")
local LIBRARY = {
library = {
treeData = {
{ label = "全部", key = "all", value = "all", children = {} },
{ label = "经部", key = "10000", value = "10000", children = {
{ label = "易类", key = "20000", value = "20000", children = {} },
{ label = "礼类", key = "20003", value = "20003", children = {
{ label = "周礼之属", key = "20003a", value = "20003a", children = {} },
} },
} },
{ label = "史部", key = "10001", value = "10001", children = {} },
},
data = {
bookList = {
{
bookId = "B1", bookName = "周易", dynastyCategoryName = "三国",
extra = { introduction = "阴阳", hasEntityRelationChapterIds = { "C1" } },
content = {
fileDesc = {
titleStmt = { beautifulCover = { pngUrl = "https://cover.example/b1.png" } },
sourceDesc = { biblStruct = { monogr = { authorList = {
{ persName = "王弼", responsibleTypeStr = "注" },
} } } },
},
},
},
{ bookId = "", bookName = "缺号" },
},
total = 2, pageNum = 1, hasMore = true,
},
},
}
-- 经部:跳过「全部」,第二行是它的子类;卡片带上章节号和分类。
do
local books, groups = Mapper.library(LIBRARY, "10000")
Assert.eq(#books, 1)
Assert.eq(books[1].stable_id, "B1")
Assert.eq(books[1].title, "周易")
Assert.eq(books[1].authors, "王弼 注")
Assert.eq(books[1].category, "经部 · 三国")
Assert.eq(books[1].cover, "https://cover.example/b1.png")
Assert.eq(books[1].intro, "阴阳")
Assert.eq(books[1].meta.source.shidian.seed, "C1")
Assert.eq(books[1].meta.source.shidian.cate, "10000")
Assert.eq(groups[1].title, _("部类"))
Assert.eq(groups[1].active, "10000")
Assert.eq(groups[1].chips[1].id, "10000")
Assert.eq(groups[1].chips[1].title, "经部")
Assert.is_nil(groups[1].chips[3], "没有史部以外的第三项?") -- 经部、史部
Assert.eq(#groups[1].chips, 2)
Assert.eq(groups[2].title, _("分类"))
Assert.eq(groups[2].chips[1].id, "20000")
Assert.eq(groups[2].chips[2].id, "20003")
Assert.is_nil(groups[2].active, "停在部类时,子类没有选中项")
end
-- 选中有下级的类:第二行换成它的子类。叶子则显示兄弟。
do
local _books, groups = Mapper.library(LIBRARY, "20003")
Assert.eq(groups[1].active, "10000")
Assert.eq(groups[2].title, "礼类")
Assert.eq(groups[2].chips[1].id, "20003a")
Assert.is_nil(groups[2].active)
local _books2, leaf = Mapper.library(LIBRARY, "20003a")
Assert.eq(leaf[2].active, "20003a")
Assert.eq(leaf[2].chips[1].id, "20003a")
end
-- 404 和缺字段是坏响应,不是空书库。
Assert.is_nil(Mapper.library({ library = { status = 404 } }, "10000"))
Assert.is_nil(Mapper.library({ library = { data = { bookList = {} } } }, "10000"))
local SEARCH = {
data = {
recommendBooks = { books = {
{
bookId = "B1", bookName = "论语",
authors = { { persName = "何晏", responsibleTypeStr = "集解" } },
traditionalCategory = {
{ cateId = "10000", cateName = "经部" },
{ cateId = "20007", cateName = "四书类" },
},
beautifulCover = "https://cover.example/lun.png",
},
} },
paragraphs = {
{ chapterId = "CH1", bookInfo = { bookId = "B1", bookName = "论语" } },
{ chapterId = "CH2", bookInfo = { bookId = "B2", bookName = "别书" } },
},
},
}
do
local books = Mapper.search(SEARCH)
Assert.eq(#books, 2)
Assert.eq(books[1].stable_id, "B1")
Assert.eq(books[1].meta.source.shidian.seed, "CH1", "推荐书补上同一页命中的章节号")
Assert.eq(books[1].meta.source.shidian.cate, "20007")
Assert.eq(books[1].authors, "何晏 集解")
Assert.eq(books[1].category, "经部 · 四书类")
Assert.eq(books[2].stable_id, "B2")
Assert.eq(books[2].meta.source.shidian.seed, "CH2")
Assert.eq(Mapper.searchSeed(SEARCH, "B2"), "CH2")
Assert.is_nil(Mapper.searchSeed(SEARCH, "NO"))
end
Assert.is_nil(Mapper.search({}))
Assert.len(Mapper.search({ data = { recommendBooks = { books = {} }, paragraphs = {} } }), 0)
local PAGE = {
ssrChapterId = "C0",
bookInfo = {
bookId = "B1", bookName = "周易",
authors = { { persName = "王弼", responsibleTypeStr = "注" } },
traditionalCategory = { { cateId = "20000", cateName = "易类" } },
coverUrl = "https://cover.example/page.png",
extra = { introduction = "短介绍" },
catalog = { chapters = {
{ chapterId = "C0", chapterName = { { content = "上经" } }, subChapters = {
{ chapterId = "C1", chapterName = { { content = "乾" } } },
} },
{ chapterId = "C2", chapterName = "下经" },
} },
},
paragraphList = {
{ content = '{"lines":[{"lineType":1,"content":"元亨利贞"},{"lineType":2,"content":"<注>"},{"lineType":3,"content":""},{"lineType":9,"content":"乾"}]}' },
{ content = { lines = { { lineType = 4, content = "卷一" } } }, translateContent = '{"sentences":[{"content":"白话"}]}' },
},
}
do
local book = Mapper.book(PAGE)
Assert.eq(book.stable_id, "B1")
Assert.eq(book.meta.source.shidian.seed, "C0")
Assert.eq(book.cover, "https://cover.example/page.png")
local toc = Mapper.toc(PAGE)
Assert.eq(#toc, 3)
Assert.eq(toc[1].uid, "C0")
Assert.eq(toc[1].title, "上经")
Assert.eq(toc[1].idx, 1)
Assert.eq(toc[2].uid, "C1")
Assert.eq(toc[2].title, "乾")
Assert.eq(toc[3].title, "下经")
local html = Mapper.chapterHtml(PAGE)
Assert.is_true(html:find("元亨利贞", 1, true) ~= nil)
Assert.is_true(html:find("&lt;注&gt;", 1, true) ~= nil, "夹注要转义")
Assert.is_true(html:find("<h3>乾</h3>", 1, true) ~= nil)
Assert.is_true(html:find("<h3>卷一</h3>", 1, true) ~= nil)
Assert.is_true(html:find(_("译文") .. ":白话", 1, true) ~= nil)
Assert.is_nil(html:find("<注>", 1, true))
end
Assert.is_nil(Mapper.toc({ bookInfo = { catalog = { chapters = {} } } }))
Assert.is_nil(Mapper.chapterHtml({ paragraphList = { { content = '{"lines":[{"lineType":3,"content":""}]}' } } }))