Files

525 lines
20 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
moonsource_weread.mapper 离线用例
@module tests.moonsource_weread.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")
package.preload["moonsource_weread.mapper"] = nil
package.loaded["moonsource_weread.mapper"] = nil
package.preload["source.contract"] = nil
-- contract 可能仍在 loaded,保留即可
local Mapper = require("moonsource_weread.mapper")
-- 搜索书目带译者。没有 translator 的书给空串,公众号条目丢掉。
do
local books = Mapper.searchBooks({ books = {
{ type = 0, bookInfo = { bookId = 31503072, title = "地下室手记(果麦经典)", author = "[俄]陀思妥耶夫斯基", translator = "曾思艺" } },
{ type = 0, bookInfo = { bookId = 3300154232, title = "地下室手记(经典译本无删减)", author = "[俄]陀思妥耶夫斯基著" } },
{ type = 3, mpInfo = { title = "公众号" } },
} })
Assert.len(books, 2)
Assert.eq(books[1].translator, "曾思艺")
Assert.eq(books[1].author, "[俄]陀思妥耶夫斯基")
Assert.eq(books[2].translator, "", "接口没给译者")
end
do
local _b, cover = Mapper.book({ bookId = "wx-2", cover = "https://cdn.weread.qq.com/weread/cover/80/y_1/s_y_1.jpg" })
Assert.eq(cover, "https://cdn.weread.qq.com/weread/cover/80/y_1/t7_y_1.jpg")
end
do
local b, cover = Mapper.book({
bookId = "wx-1",
bookName = "三体",
author = "刘慈欣",
cover = "http://x/c.jpg",
progressPercent = 42,
})
Assert.eq(b.source_id, "weread")
Assert.eq(b.stable_id, "wx-1")
Assert.eq(b.title, "三体")
Assert.eq(b.percent, 42)
Assert.eq(cover, "http://x/c.jpg")
Assert.is_nil(b.id)
end
-- 空封面必须视为缺失,不能参与远端 upsert 覆盖本地已有封面。
do
local b, cover = Mapper.book({ bookId = "wx-empty", title = "保留封面", cover = "" })
Assert.is_nil(b.cover)
Assert.is_nil(cover)
end
-- bookInfo 扩展元数据原样交给 meta(规范化在 BookDB 入库时做),newRating 千分制折成十分制
do
local b = Mapper.book({
bookId = "wx-meta", publisher = "湖南文艺出版社", publishTime = "2022-01-01 00:00:00",
isbn = "9787572606878", language = "zh", totalWords = 68000, newRating = 955,
})
Assert.eq(b.meta.publisher, "湖南文艺出版社")
Assert.eq(b.meta.published, "2022-01-01 00:00:00")
Assert.eq(b.meta.isbn, "9787572606878")
Assert.eq(b.meta.language, "zh")
Assert.eq(b.meta.words, 68000)
Assert.eq(b.meta.rating.score, 9.55)
Assert.eq(b.meta.rating.by, "weread")
end
-- 微信作品完结 finished=1 不当作用户读完。连载判断要的 payType / type / 完结留下。
do
local b = Mapper.book({ bookId = "1", finished = 1, progress = 20, payType = 2, type = 0 })
Assert.eq(b.percent, 20)
Assert.eq(b.meta.source["weread"].pay_type, 2)
Assert.eq(b.meta.source["weread"].book_type, 0)
Assert.eq(b.meta.source["weread"].ended, true)
end
do
local shelf = {
books = { { bookId = "1", title = "A" } },
recentBooks = { { bookId = "2", title = "B" } },
finishReadBooks = { { bookId = "1", title = "A dup" }, { bookId = "3", title = "C" } },
bookProgress = { { bookId = "1", progress = 33 } },
albums = {},
}
local list = Mapper.shelfList(shelf)
Assert.eq(#list.data, 3)
local ids = {}
for _, b in ipairs(list.data) do ids[b.stable_id] = b.title end
Assert.eq(ids["1"], "A")
Assert.eq(ids["2"], "B")
Assert.eq(ids["3"], "C")
Assert.eq(list.data[1].percent, 33)
end
do
local shelf = {
books = { { bookId = "1", title = "A" } },
bookProgress = { { bookId = "1", progress = 33 } },
albums = {},
}
local list = Mapper.shelfList(shelf)
Assert.eq(#list.data, 1)
Assert.eq(list.data[1].percent, 33)
end
do
local Settings = require("utils.settings")
local cfg = Settings.getSource("weread")
cfg.category_as_group = true
Settings.saveSource("weread", cfg)
local list = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
{ bookId = "2", title = "B", category = "官方" },
},
archive = {
{ name = "我的", bookIds = { 1 } },
{ name = "其它", bookIds = { 1 } },
},
})
Assert.eq(list.data[1].category, "我的", "开:进组用分组名")
Assert.is_nil(list.data[2].category, "开:没进组不写题材,避免混合")
Assert.is_true(list.data[1].category_authoritative)
Assert.is_true(list.data[2].category_authoritative, "没进组也是这次快照的分类口径")
cfg.category_as_group = false
Settings.saveSource("weread", cfg)
local genre = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
{ bookId = "2", title = "B", category = "官方" },
},
archive = { { name = "我的", bookIds = { 1 } } },
})
Assert.eq(genre.data[1].category, "官方", "关:只用题材")
Assert.eq(genre.data[2].category, "官方", "关:忽略分组")
end
do
local chapters = Mapper.chapters({
data = {
{
bookId = "1",
updated = {
{ chapterIdx = 2, chapterUid = "u2", title = "二", wordCount = 10, level = 2 },
{
chapterIdx = 1, chapterUid = "u1", title = "一", wordCount = 10, level = 1,
anchors = { { title = "第一章", level = 2 } },
},
{ chapterIdx = 3, chapterUid = "u3", title = "封面", wordCount = 10 },
},
},
},
}, "1")
Assert.eq(#chapters, 2)
Assert.eq(chapters[1].idx, 1)
Assert.eq(chapters[1].uid, "u1")
Assert.eq(chapters[1].depth, 1)
Assert.eq(chapters[1].anchors[1].title, "第一章")
Assert.eq(chapters[1].anchors[1].depth, 2)
Assert.eq(chapters[1].toc_version, 5)
Assert.not_nil(chapters[1].words, "带字数:关联书按篇幅对齐")
Assert.eq(chapters[2].title, "二", "出版书(epub)章名原样")
Assert.eq(chapters[2].depth, 2)
end
do
-- 微信给每章挂的同名同层自锚点(就是章首)不是章内小节,丢掉;同名更深与异名小节保留。
local chapters = Mapper.chapters({
data = {
{
bookId = "1",
updated = {
{ chapterIdx = 1, chapterUid = "u1", title = "龙卷风", wordCount = 10, level = 2,
anchors = { { title = "龙卷风", level = 2 } } },
{ chapterIdx = 2, chapterUid = "u2", title = "序", wordCount = 10, level = 1,
anchors = { { title = "序", level = 2 }, { title = "小节", level = 2 } } },
},
},
},
}, "1")
Assert.is_nil(chapters[1].anchors, "同名同层自锚点丢弃,目录不再一章两行")
Assert.eq(#chapters[2].anchors, 2, "同名更深与异名小节保留")
end
do
-- 网文(book.format=txt,chapterInfos 实测字段):按 chapterIdx 补「第 N 章」,已带序号的不重复补,空章名只留序号。
local chapters = Mapper.chapters({
data = {
{
bookId = "820954",
book = { bookId = "820954", format = "txt" },
updated = {
{ chapterIdx = 1, chapterUid = 1, title = "万年之后", wordCount = 1290 },
{ chapterIdx = 2, chapterUid = 2, title = "第二章 想整我?", wordCount = 1430 },
{ chapterIdx = 3, chapterUid = 3, title = "第一次见面", wordCount = 10 },
{ chapterIdx = 4, chapterUid = 4, title = "", wordCount = 10 },
{ chapterIdx = 5, chapterUid = 5, title = "第12章", wordCount = 10 },
},
},
},
}, "820954")
local titles = {}
for i, ch in ipairs(chapters) do titles[i] = ch.title end
Assert.eq(table.concat(titles, "|"), "第1章 万年之后|第二章 想整我?|第3章 第一次见面|第4章|第12章")
Assert.eq(chapters[1].format, "txt")
end
do
local chapters = Mapper.chapters({
data = {
{
bookId = "1",
updated = {
{ chapterIdx = 1, chapterUid = "u1", title = "图章", wordCount = 10, tar = "/web/tar/1" },
},
},
},
}, "1")
Assert.eq(chapters[1].tar, "/web/tar/1")
end
do
local pos = Mapper.progress({ progress = 50, chapterUid = "u1" })
Assert.eq(pos.fraction, 0.5)
end
do
local pos, uid = Mapper.progress({
bookId = "42",
book = { chapterUid = "u7", progress = 77, chapterOffset = 5000 },
})
Assert.eq(uid, "u7")
Assert.eq(pos.fraction, 0.77)
Assert.is_nil(pos.chapter_fraction, "原文偏移不是章内万分比")
Assert.eq(pos.extra.chapter_offset, 5000)
end
do
local pos = Mapper.progress({ book = { chapterUid = "u7", progress = 1, chapterOffset = 1 } })
Assert.is_nil(pos.chapter_fraction)
Assert.eq(pos.extra.chapter_offset, 1)
pos = Mapper.progress({ book = { chapterUid = "u7", progress = 1, chapterOffset = 0 } })
Assert.is_nil(pos.chapter_fraction)
Assert.eq(pos.extra.chapter_offset, 0)
pos = Mapper.progress({ book = { chapterUid = "u7", progress = 1 } })
Assert.is_nil(pos.chapter_fraction)
end
-- 云端最近阅读时间:书行 readUpdateTime 与 bookProgress.updateTime 取较新的
do
local b = Mapper.book({ bookId = "wx-r", title = "R", readUpdateTime = 1700000000 })
Assert.eq(b.read_at, 1700000000)
Mapper.applyProgress(b, { bookId = "wx-r", progress = 10, updateTime = 1700000500 })
Assert.eq(b.read_at, 1700000500)
Mapper.applyProgress(b, { bookId = "wx-r", progress = 10, updateTime = 1600000000 })
Assert.eq(b.read_at, 1700000500, "旧的不覆盖新的")
local list = Mapper.shelfList({
books = { { bookId = "a", title = "A" } },
bookProgress = { { bookId = "a", progress = 5, updateTime = 1234 } },
})
Assert.eq(list.data[1].read_at, 1234)
-- 公众号只走订阅导入,书架同步不收进书库
local mixed = Mapper.shelfList({ books = { { bookId = "a", title = "A" }, { bookId = "MP_WXS_1", title = "号" } } })
Assert.len(mixed.data, 1)
Assert.eq(mixed.data[1].stable_id, "a")
end
do -- 书城目录:榜单与带子分类的分类,其余丢弃
local tree = Mapper.storeTree({ data = {
{ categories = {
{ CategoryId = "rising", title = "飙升榜", ranklist = 1, sublist = {} },
{ CategoryId = "6200", title = "有声小说榜", sublist = {} },
} },
{ categories = {
{ CategoryId = 700000, title = "计算机", sublist = { { CategoryId = 700001, title = "软件学习" } } },
} },
} })
Assert.len(tree.rankings, 1)
Assert.eq(tree.rankings[1].id, "rising")
Assert.len(tree.categories, 1)
Assert.eq(tree.categories[1].id, "700000")
Assert.eq(tree.categories[1].subs[1].id, "700001")
end
--[[--
moonsource_weread.mapper 离线用例
@module tests.moonsource_weread.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")
package.preload["moonsource_weread.mapper"] = nil
package.loaded["moonsource_weread.mapper"] = nil
package.preload["source.contract"] = nil
-- contract 可能仍在 loaded,保留即可
local Mapper = require("moonsource_weread.mapper")
do
local b, cover = Mapper.book({
bookId = "wx-1",
bookName = "三体",
author = "刘慈欣",
cover = "http://x/c.jpg",
progressPercent = 42,
})
Assert.eq(b.source_id, "weread")
Assert.eq(b.stable_id, "wx-1")
Assert.eq(b.title, "三体")
Assert.eq(b.percent, 42)
Assert.eq(cover, "http://x/c.jpg")
Assert.is_nil(b.id)
end
-- 空封面必须视为缺失,不能参与远端 upsert 覆盖本地已有封面。
do
local b, cover = Mapper.book({ bookId = "wx-empty", title = "保留封面", cover = "" })
Assert.is_nil(b.cover)
Assert.is_nil(cover)
end
-- bookInfo 扩展元数据原样交给 meta(规范化在 BookDB 入库时做),newRating 千分制折成十分制
do
local b = Mapper.book({
bookId = "wx-meta", publisher = "湖南文艺出版社", publishTime = "2022-01-01 00:00:00",
isbn = "9787572606878", language = "zh", totalWords = 68000, newRating = 955,
})
Assert.eq(b.meta.publisher, "湖南文艺出版社")
Assert.eq(b.meta.published, "2022-01-01 00:00:00")
Assert.eq(b.meta.isbn, "9787572606878")
Assert.eq(b.meta.language, "zh")
Assert.eq(b.meta.words, 68000)
Assert.eq(b.meta.rating.score, 9.55)
Assert.eq(b.meta.rating.by, "weread")
end
-- 微信作品完结 finished=1 不当作用户读完。连载判断要的 payType / type / 完结留下。
do
local b = Mapper.book({ bookId = "1", finished = 1, progress = 20, payType = 2, type = 0 })
Assert.eq(b.percent, 20)
Assert.eq(b.meta.source["weread"].pay_type, 2)
Assert.eq(b.meta.source["weread"].book_type, 0)
Assert.eq(b.meta.source["weread"].ended, true)
end
do
local shelf = {
books = { { bookId = "1", title = "A" } },
recentBooks = { { bookId = "2", title = "B" } },
finishReadBooks = { { bookId = "1", title = "A dup" }, { bookId = "3", title = "C" } },
bookProgress = { { bookId = "1", progress = 33 } },
albums = {},
}
local list = Mapper.shelfList(shelf)
Assert.eq(#list.data, 3)
local ids = {}
for _, b in ipairs(list.data) do ids[b.stable_id] = b.title end
Assert.eq(ids["1"], "A")
Assert.eq(ids["2"], "B")
Assert.eq(ids["3"], "C")
Assert.eq(list.data[1].percent, 33)
end
do
local shelf = {
books = { { bookId = "1", title = "A" } },
bookProgress = { { bookId = "1", progress = 33 } },
albums = {},
}
local list = Mapper.shelfList(shelf)
Assert.eq(#list.data, 1)
Assert.eq(list.data[1].percent, 33)
end
do
local Settings = require("utils.settings")
local cfg = Settings.getSource("weread")
cfg.category_as_group = true
Settings.saveSource("weread", cfg)
local list = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
{ bookId = "2", title = "B", category = "官方" },
},
archive = {
{ name = "我的", bookIds = { 1 } },
{ name = "其它", bookIds = { 1 } },
},
})
Assert.eq(list.data[1].category, "我的", "开:进组用分组名")
Assert.is_nil(list.data[2].category, "开:没进组不写题材,避免混合")
Assert.is_true(list.data[1].category_authoritative)
Assert.is_true(list.data[2].category_authoritative, "没进组也是这次快照的分类口径")
cfg.category_as_group = false
Settings.saveSource("weread", cfg)
local genre = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
{ bookId = "2", title = "B", category = "官方" },
},
archive = { { name = "我的", bookIds = { 1 } } },
})
Assert.eq(genre.data[1].category, "官方", "关:只用题材")
Assert.eq(genre.data[2].category, "官方", "关:忽略分组")
end
do
local chapters = Mapper.chapters({
data = {
{
bookId = "1",
updated = {
{ chapterIdx = 2, chapterUid = "u2", title = "二", wordCount = 10, level = 2 },
{
chapterIdx = 1, chapterUid = "u1", title = "一", wordCount = 10, level = 1,
anchors = { { title = "第一章", level = 2 } },
},
{ chapterIdx = 3, chapterUid = "u3", title = "封面", wordCount = 10 },
},
},
},
}, "1")
Assert.eq(#chapters, 2)
Assert.eq(chapters[1].idx, 1)
Assert.eq(chapters[1].uid, "u1")
Assert.eq(chapters[1].depth, 1)
Assert.eq(chapters[1].anchors[1].title, "第一章")
Assert.eq(chapters[1].anchors[1].depth, 2)
Assert.eq(chapters[1].toc_version, 5)
Assert.not_nil(chapters[1].words, "带字数:关联书按篇幅对齐")
Assert.eq(chapters[2].title, "二", "出版书(epub)章名原样")
Assert.eq(chapters[2].depth, 2)
end
do
-- 网文(book.format=txt,chapterInfos 实测字段):按 chapterIdx 补「第 N 章」,已带序号的不重复补,空章名只留序号。
local chapters = Mapper.chapters({
data = {
{
bookId = "820954",
book = { bookId = "820954", format = "txt" },
updated = {
{ chapterIdx = 1, chapterUid = 1, title = "万年之后", wordCount = 1290 },
{ chapterIdx = 2, chapterUid = 2, title = "第二章 想整我?", wordCount = 1430 },
{ chapterIdx = 3, chapterUid = 3, title = "第一次见面", wordCount = 10 },
{ chapterIdx = 4, chapterUid = 4, title = "", wordCount = 10 },
{ chapterIdx = 5, chapterUid = 5, title = "第12章", wordCount = 10 },
},
},
},
}, "820954")
local titles = {}
for i, ch in ipairs(chapters) do titles[i] = ch.title end
Assert.eq(table.concat(titles, "|"), "第1章 万年之后|第二章 想整我?|第3章 第一次见面|第4章|第12章")
Assert.eq(chapters[1].format, "txt")
end
do
local chapters = Mapper.chapters({
data = {
{
bookId = "1",
updated = {
{ chapterIdx = 1, chapterUid = "u1", title = "图章", wordCount = 10, tar = "/web/tar/1" },
},
},
},
}, "1")
Assert.eq(chapters[1].tar, "/web/tar/1")
end
do
local pos = Mapper.progress({ progress = 50, chapterUid = "u1" })
Assert.eq(pos.fraction, 0.5)
end
do
local pos, uid = Mapper.progress({
bookId = "42",
book = { chapterUid = "u7", progress = 77, chapterOffset = 5000 },
})
Assert.eq(uid, "u7")
Assert.eq(pos.fraction, 0.77)
Assert.is_nil(pos.chapter_fraction)
Assert.eq(pos.extra.chapter_offset, 5000)
end
do
local pos = Mapper.progress({ book = { chapterUid = "u7", progress = 1, chapterOffset = 1 } })
Assert.is_nil(pos.chapter_fraction)
Assert.eq(pos.extra.chapter_offset, 1)
pos = Mapper.progress({ book = { chapterUid = "u7", progress = 1, chapterOffset = 0 } })
Assert.is_nil(pos.chapter_fraction)
Assert.eq(pos.extra.chapter_offset, 0)
pos = Mapper.progress({ book = { chapterUid = "u7", progress = 1 } })
Assert.is_nil(pos.chapter_fraction)
end
-- 云端最近阅读时间:书行 readUpdateTime 与 bookProgress.updateTime 取较新的
do
local b = Mapper.book({ bookId = "wx-r", title = "R", readUpdateTime = 1700000000 })
Assert.eq(b.read_at, 1700000000)
Mapper.applyProgress(b, { bookId = "wx-r", progress = 10, updateTime = 1700000500 })
Assert.eq(b.read_at, 1700000500)
Mapper.applyProgress(b, { bookId = "wx-r", progress = 10, updateTime = 1600000000 })
Assert.eq(b.read_at, 1700000500, "旧的不覆盖新的")
local list = Mapper.shelfList({
books = { { bookId = "a", title = "A" } },
bookProgress = { { bookId = "a", progress = 5, updateTime = 1234 } },
})
Assert.eq(list.data[1].read_at, 1234)
-- 公众号只走订阅导入,书架同步不收进书库
local mixed = Mapper.shelfList({ books = { { bookId = "a", title = "A" }, { bookId = "MP_WXS_1", title = "号" } } })
Assert.len(mixed.data, 1)
Assert.eq(mixed.data[1].stable_id, "a")
end