Files
moonsource-shidian/tests/shidian/mapper_spec.lua
T
2026-11-28 13:44:23 +08:00

172 lines
7.1 KiB
Lua

--[[--
识典古籍字段映射:书库首屏、搜索首屏、目录树、正文转 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":""}]}' } } }))