550 lines
24 KiB
Lua
550 lines
24 KiB
Lua
--[[--
|
||
source.wechat notes/annotations/mapper 离线用例
|
||
|
||
@module tests.moonsource_weread.sync_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.loaded["moonsource_weread.mapper"] = nil
|
||
package.loaded["moonsource_weread.notes"] = nil
|
||
package.loaded["book.annotations"] = nil
|
||
-- 本地目录:书 820954 的微信 uid 4 对应本地第 4 章;其余书未缓存。
|
||
package.loaded["source.toc"] = {
|
||
index = function(_source_id, stable_id, uid)
|
||
return stable_id == "820954" and uid == "4" and 4 or nil
|
||
end,
|
||
}
|
||
|
||
local Mapper = require("moonsource_weread.mapper")
|
||
local Notes = require("moonsource_weread.notes")
|
||
local Annotations = require("book.annotations")
|
||
|
||
do
|
||
local anns = Notes.toAnnotations({
|
||
updated = {
|
||
{
|
||
chapterUid = "9",
|
||
markText = "hello",
|
||
range = "0-5",
|
||
createTime = 100,
|
||
},
|
||
},
|
||
}, "9", {
|
||
reviews = {
|
||
{ review = { range = "0-5", content = "想法", reviewId = "rv1" } },
|
||
{ review = { content = "整本书评,无 range" } },
|
||
},
|
||
})
|
||
Assert.eq(#anns, 1)
|
||
Assert.eq(anns[1].text, "hello")
|
||
Assert.eq(anns[1].note, "想法")
|
||
Assert.eq(anns[1].wr_review_id, "rv1")
|
||
end
|
||
|
||
do
|
||
-- 真实行为:直接发表选中文本想法时,bookmarklist 没有同 range 划线。
|
||
local anns = Notes.toAnnotations({
|
||
updated = {
|
||
{
|
||
chapterUid = "41", chapterIdx = 6, bookmarkId = "bm1",
|
||
type = 1, range = "387-390", markText = "许三观", createTime = 1,
|
||
},
|
||
},
|
||
}, "41", {
|
||
reviews = {
|
||
{
|
||
review = {
|
||
reviewId = "rv1", chapterUid = 41, chapterIdx = 6,
|
||
range = "425-431", abstract = "拍打着屁股,", content = "冲冲冲",
|
||
},
|
||
},
|
||
},
|
||
})
|
||
Assert.eq(#anns, 2)
|
||
Assert.is_true(anns[2].wr_review_only)
|
||
Assert.eq(anns[2].note, "冲冲冲")
|
||
|
||
-- 单条「许三观」重复无法定位;与后面的唯一想法一起按 range 顺序即可锁定章首。
|
||
local shell = "<html><body><h1>第二章</h1><p>许三观坐着,拍打着屁股,尘土就在许三观身上。</p>"
|
||
.. "<p>许三观又站起来。</p></body></html>"
|
||
local path = os.tmpname()
|
||
local file = assert(io.open(path, "wb")); file:write(shell); file:close()
|
||
local localized = Notes.localizeAnnotations(nil, anns, path, {
|
||
{
|
||
wr_bookmark_id = "bm1", drawer = "lighten", text = "许三观",
|
||
page = "/html/body/p[2]/text().0",
|
||
pos0 = "/html/body/p[2]/text().0", pos1 = "/html/body/p[2]/text().3",
|
||
},
|
||
})
|
||
os.remove(path)
|
||
Assert.eq(localized[1].pos0, "/html/body/p[1]/text().0")
|
||
Assert.eq(localized[2].note, "冲冲冲")
|
||
Assert.is_true(type(localized[2].pos0) == "string")
|
||
end
|
||
|
||
do
|
||
-- 真实 /book/bookmarklist 回包:markText 明文、chapterIdx 只在 chapters 里
|
||
local anns = Notes.toAnnotations({
|
||
synckey = 1787872819,
|
||
updated = {
|
||
{
|
||
bookId = "820954",
|
||
chapterUid = 4,
|
||
bookmarkId = "820954_4_303-332",
|
||
type = 1,
|
||
colorStyle = 2,
|
||
createTime = 1601272592,
|
||
range = "303-332",
|
||
markText = "“当然不是!”几乎是下意识的,牧云开口回应道。",
|
||
},
|
||
},
|
||
chapters = { { title = "课堂解答", chapterUid = 4, chapterIdx = 4 } },
|
||
}, nil, nil, "weread", "820954")
|
||
Assert.eq(#anns, 1)
|
||
Assert.eq(anns[1].chapter_idx, 4)
|
||
Assert.eq(anns[1].wr_range, "303-332")
|
||
Assert.eq(anns[1].wr_bookmark_id, "820954_4_303-332")
|
||
Assert.eq(anns[1].text, "“当然不是!”几乎是下意识的,牧云开口回应道。")
|
||
Assert.is_nil(anns[1].note)
|
||
Assert.eq(anns[1].color, "green")
|
||
end
|
||
|
||
do
|
||
local wire = {
|
||
updated = {
|
||
{ chapterUid = "9", markText = "hello", range = "0-5", createTime = 100, type = 1 },
|
||
{ chapterUid = "9", markText = "skip", range = "1-2", createTime = 101, type = 0 },
|
||
},
|
||
items = {
|
||
{ chapterUid = "9", markText = "popular", range = "2-5", userVid = "999" },
|
||
},
|
||
}
|
||
Assert.eq(#Notes.toAnnotations(wire, "9"), 1)
|
||
Assert.eq(#Notes.toAnnotations({ items = wire.items }, "9"), 0)
|
||
-- 本地目录映射不到:原始 chapterIdx / wire.chapters 都不是本地章号,留空。
|
||
Assert.is_nil(Notes.toAnnotations({
|
||
updated = {
|
||
{ chapterUid = "8", chapterIdx = 8, markText = "x", range = "0-1", type = 1, createTime = 1 },
|
||
},
|
||
}, nil, nil, "weread", "1")[1].chapter_idx)
|
||
Assert.is_nil(Notes.toAnnotations({
|
||
chapters = { { chapterUid = "9", chapterIdx = 9 } },
|
||
updated = {
|
||
{ chapterUid = "9", markText = "x", range = "0-1", type = 1, createTime = 1 },
|
||
},
|
||
}, nil, nil, "weread", "1")[1].chapter_idx)
|
||
end
|
||
|
||
--- 单条划线走 locateBatch,返回 pos0, pos1;定位不到返回 nil。
|
||
local function locate(html, text, range)
|
||
local item = { text = text, wr_range = range }
|
||
local hit = Annotations.locateBatch(html, { item })[item]
|
||
if hit then return hit.pos0, hit.pos1 end
|
||
end
|
||
|
||
do
|
||
-- 章节壳:<h1> 不占 p 编号;段首缩进折叠为 1 空格,故 p[2] 从 offset 1 起。
|
||
-- 期望值取自 KOReader 自己写出的 xpointer(pos1 是半开上界)。
|
||
local shell = table.concat({
|
||
"<html><head><title>指婚</title></head><body><h1>指婚</h1>",
|
||
"<p>莫问毕竟是六星炼丹师。</p>\n",
|
||
"<p> 要是牧钱反应过来。</p>\n",
|
||
"<p>重复段落文本。</p>\n",
|
||
"<p>重复段落文本。</p>",
|
||
"</body></html>",
|
||
})
|
||
|
||
local pos0, pos1 = locate(shell, "莫问毕竟是六星炼丹师。")
|
||
Assert.eq(pos0, "/html/body/p[1]/text().0")
|
||
Assert.eq(pos1, "/html/body/p[1]/text().11")
|
||
|
||
pos0, pos1 = locate(shell, "要是牧钱反应过来。")
|
||
Assert.eq(pos0, "/html/body/p[2]/text().1")
|
||
Assert.eq(pos1, "/html/body/p[2]/text().10")
|
||
|
||
-- markText 带换行/多空格时按 crengine 规则折叠后再比
|
||
Assert.eq(locate(shell, " 莫问毕竟是六星炼丹师。\n"), "/html/body/p[1]/text().0")
|
||
|
||
-- wire range 与本地壳不是同一坐标系;重复文本不能靠 range 猜,宁可不画错位置。
|
||
Assert.is_nil(locate(shell, "重复段落文本。"))
|
||
Assert.is_nil(locate(shell, "重复段落文本。", "34-41"))
|
||
|
||
Assert.is_nil(locate(shell, "正文里不存在的句子"))
|
||
Assert.is_nil(locate(shell, ""))
|
||
|
||
-- 已同步条目即使文本重复,也按 bookmarkId 复用原生坐标并接受远端笔记更新。
|
||
local path = os.tmpname()
|
||
local file = assert(io.open(path, "wb")); file:write(shell); file:close()
|
||
local localized = Notes.localizeAnnotations(nil, {
|
||
{ drawer = "lighten", text = "重复段落文本。", note = "远端新想法", wr_bookmark_id = "bm-2" },
|
||
}, path, {
|
||
{
|
||
drawer = "lighten", text = "重复段落文本。", note = "旧想法", wr_bookmark_id = "bm-2",
|
||
page = "/html/body/p[4]/text().0",
|
||
pos0 = "/html/body/p[4]/text().0", pos1 = "/html/body/p[4]/text().7",
|
||
},
|
||
})
|
||
os.remove(path)
|
||
Assert.eq(localized[1].pos0, "/html/body/p[4]/text().0")
|
||
Assert.eq(localized[1].note, "远端新想法")
|
||
end
|
||
|
||
do
|
||
-- 本地图片 URL、章节壳与 wire HTML 不同,range 仍必须落在 wire rune 坐标。
|
||
local wire = "\239\187\191<p>A&B</p>"
|
||
.. '<p><img src="https://remote/a.jpg"/>重复</p><p>重复</p>'
|
||
local shell = "<html><body><h1>标题</h1><p>A&B</p>"
|
||
.. '<p><img src="/tmp/a.jpg"/>重复</p><p>重复</p></body></html>'
|
||
local mapping, flow = Annotations.rangeMapping(wire), Annotations.flow(shell)
|
||
local range, err = Annotations.toWireRange(
|
||
mapping, flow, "重复", "/html/body/p[3]/text().0", "/html/body/p[3]/text().2"
|
||
)
|
||
Assert.is_nil(err)
|
||
Assert.eq(range, "59-61")
|
||
local first = Annotations.toWireRange(
|
||
mapping, flow, "A&B", "/html/body/p[1]/text().0", "/html/body/p[1]/text().3"
|
||
)
|
||
Assert.eq(first, "3-10")
|
||
local ambiguous, ambiguous_err = Annotations.toWireRange(mapping, flow, "重复")
|
||
Assert.is_nil(ambiguous)
|
||
Assert.eq(ambiguous_err, "ambiguous local highlight")
|
||
Assert.eq(Annotations.wireMapping("<p>x</p><style>bad</style><p>y</p>").text, "xy")
|
||
end
|
||
|
||
do
|
||
-- range 基准是完整解码 xhtml:head 与 xml 声明计入偏移,但其中文字不参与匹配。
|
||
local wire = '<?xml version="1.0"?><html><head><title>正文</title></head><body><p>正文</p></body></html>'
|
||
local shell = "<html><body><p>正文</p></body></html>"
|
||
local prefix = wire:sub(1, wire:find("<p>正文", 1, true) + 2)
|
||
local start = select(2, prefix:gsub("[^\128-\191]", ""))
|
||
local range = Annotations.toWireRange(Annotations.rangeMapping(wire, "html"),
|
||
Annotations.flow(shell), "正文", "/html/body/p/text().0", "/html/body/p/text().2")
|
||
Assert.eq(range, string.format("%d-%d", start, start + 2))
|
||
end
|
||
|
||
do
|
||
-- TXT:range 对纯文本逐 rune 计(BOM 不计,"<" 不是标签),与 weread 的 Source.plain 一致。
|
||
local plain = "\239\187\191甲<&乙\n 丙丁"
|
||
local shell = "<html><body>" .. require("utils.text").textToBody(plain:sub(4)) .. "</body></html>"
|
||
local flow = Annotations.flow(shell)
|
||
Assert.eq(Annotations.toWireRange(Annotations.rangeMapping(plain, "txt"), flow,
|
||
"<&", "/html/body/p[1]/text().1", "/html/body/p[1]/text().3"), "1-3")
|
||
Assert.eq(Annotations.toWireRange(Annotations.rangeMapping(plain, "txt"), flow,
|
||
"丙丁", "/html/body/p[2]/text().0", "/html/body/p[2]/text().2"), "7-9")
|
||
end
|
||
|
||
do
|
||
-- KOReader 跨段选区在段间带换行与缩进;微信正文段间也有缩进空白。都必须能定位。
|
||
local wire = "<body><p> 第一段结尾</p>\n<p> 第二段开头</p></body>"
|
||
local shell = "<html><body><p> 第一段结尾</p><p> 第二段开头</p></body></html>"
|
||
local range, err = Annotations.toWireRange(Annotations.rangeMapping(wire), Annotations.flow(shell),
|
||
"结尾\n 第二", "/html/body/p[1]/text().5", "/html/body/p[2]/text().4")
|
||
Assert.is_nil(err)
|
||
local head = wire:find("结尾", 1, true)
|
||
local runes_before = select(2, wire:sub(1, head - 1):gsub("[^\128-\191]", ""))
|
||
Assert.eq(range:match("^(%d+)"), tostring(runes_before))
|
||
|
||
-- 起点 xpointer 对不上(旧版按扁平 p[N] 写出的坐标)时靠唯一文本定位,不拿终点去比。
|
||
Assert.eq(Annotations.toWireRange(Annotations.rangeMapping(wire), Annotations.flow(shell),
|
||
"第二段", "/html/body/div/p[9]/span/text().0", "/html/body/div/p[9]/text().3") ~= nil, true)
|
||
end
|
||
|
||
do
|
||
-- xpointer 与 crengine 写法逐字一致(期望值由 crengine getTextFromXPointers 反查确认):
|
||
-- 同名兄弟唯一时不带 [k],拼接的多份文档是 html[k],内联元素把文本拆成 text()[k],脚注正文不参与匹配。
|
||
local shell = table.concat({
|
||
'<?xml version="1.0" encoding="utf-8"?><html xmlns:epub="http://www.idpf.org/2007/ops">',
|
||
'<head><title>a</title></head><body>',
|
||
'<div class="c"><p>第一段<a epub:type="noteref" href="#n1"><img src="x.png"/></a>注后文字</p>\n<p>第二段</p></div>',
|
||
'<aside epub:type="footnote" id="n1"><p>脚注正文</p></aside>',
|
||
'</body></html>',
|
||
'<?xml version="1.0" encoding="utf-8"?><html><head><title>b</title></head><body>',
|
||
'<p>次文档首段</p><p>次文档 <b>粗体</b> 尾巴</p>',
|
||
'</body></html>',
|
||
})
|
||
local cases = {
|
||
{ "注后文字", "/html[1]/body/div/p[1]/text()[2].0", "/html[1]/body/div/p[1]/text()[2].4" },
|
||
{ "第二段", "/html[1]/body/div/p[2]/text().0", "/html[1]/body/div/p[2]/text().3" },
|
||
{ "次文档首段", "/html[2]/body/p[1]/text().0", "/html[2]/body/p[1]/text().5" },
|
||
{ "尾巴", "/html[2]/body/p[2]/text()[2].1", "/html[2]/body/p[2]/text()[2].3" },
|
||
{ "粗体", "/html[2]/body/p[2]/b/text().0", "/html[2]/body/p[2]/b/text().2" },
|
||
}
|
||
for _idx, case in ipairs(cases) do
|
||
local pos0, pos1 = locate(shell, case[1])
|
||
Assert.eq(pos0, case[2])
|
||
Assert.eq(pos1, case[3])
|
||
end
|
||
Assert.is_nil(locate(shell, "脚注正文"))
|
||
Assert.eq(locate("<html><body><p>正文</p></body></html>", "正文"), "/html/body/p/text().0")
|
||
end
|
||
|
||
do
|
||
-- 跨段划线:微信 markText 直接跨 <p> 边界,段首缩进与段间换行都不在其中
|
||
local shell = table.concat({
|
||
"<html><body><h1>课堂解答</h1>",
|
||
'<p> “当然不是!”</p>\n',
|
||
"<p> 几乎是下意识的,牧云开口回应道。</p>",
|
||
"</body></html>",
|
||
})
|
||
local pos0, pos1 = locate(shell, "“当然不是!”几乎是下意识的,牧云开口回应道。")
|
||
Assert.eq(pos0, "/html/body/p[1]/text().1")
|
||
Assert.eq(pos1, "/html/body/p[2]/text().17")
|
||
|
||
-- 单段仍按原样定位,不受跨段流影响
|
||
Assert.eq(locate(shell, "牧云开口回应道。"), "/html/body/p[2]/text().9")
|
||
end
|
||
|
||
do
|
||
-- 定位失败的远端划线不能进 doc_settings:drawSavedHighlight 会解 pos0 并抛错
|
||
local localized = Notes.localizeAnnotations(nil, {
|
||
{ drawer = "lighten", text = "找不到的原文", wr_range = "0-6" },
|
||
}, nil)
|
||
Assert.eq(#localized, 1)
|
||
Assert.is_nil(localized[1].pos0)
|
||
end
|
||
|
||
do
|
||
-- range 是原始 HTML 索引(含标签),不能按可见文本切片。旧实现先用 textAtRange
|
||
-- 把 "0-4" 当成可见文本偏移切成 "AAAA",导致划线定位被错误覆盖。
|
||
local html = '<html><head><title>t</title></head><body><h1>t</h1>'
|
||
.. '<p><span class="x">AAAA</span>作者:罗贯<span>BBBB</span></p>'
|
||
.. '</body></html>'
|
||
local path = os.tmpname()
|
||
local f = assert(io.open(path, "wb")); f:write(html); f:close()
|
||
local localized = Notes.localizeAnnotations(nil, {
|
||
{ drawer = "lighten", text = "作者:罗贯", wr_range = "0-4" },
|
||
}, path)
|
||
os.remove(path)
|
||
Assert.eq(#localized, 1)
|
||
Assert.eq(localized[1].pos0, "/html/body/p/text().0")
|
||
Assert.eq(localized[1].pos1, "/html/body/p/text().5")
|
||
end
|
||
|
||
do
|
||
Assert.eq(Notes.decodeMarkText("5L2c6ICF77ya572X6LSv"), "作者:罗贯")
|
||
Assert.eq(require("utils.text").base64Encode("作者:罗贯"), "5L2c6ICF77ya572X6LSv")
|
||
local body = Notes.toBookmarkBody("1", 9, 2, 395275575, {
|
||
text = "hello",
|
||
drawer = "lighten",
|
||
wr_range = "0-5",
|
||
})
|
||
Assert.eq(body.bookId, "1")
|
||
Assert.eq(body.chapterUid, 9)
|
||
Assert.eq(body.bookVersion, 395275575)
|
||
Assert.eq(body.range, "0-5")
|
||
Assert.eq(body.markText, "hello")
|
||
Assert.eq(#Notes.pushCandidates({
|
||
{ drawer = "lighten", text = "a" },
|
||
{ drawer = "lighten", text = "note", note = "想法" },
|
||
{ drawer = "lighten", text = "b", wr_bookmark_id = 1 },
|
||
}), 1)
|
||
Assert.eq(#Notes.notePushCandidates({
|
||
{ note = "想法", wr_range = "0-1", wr_bookmark_id = "bm1" },
|
||
{ note = "独立想法", wr_range = "2-3", wr_review_only = true },
|
||
{ note = "未修改", wr_range = "3-4", wr_review_id = "rv1" },
|
||
{ note = "已修改", wr_range = "4-5", wr_review_id = "rv2", wr_update_review = true },
|
||
{ note = "无划线", wr_range = "0-1" },
|
||
}), 3)
|
||
local review_body = Notes.toReviewBody("1", 9, {
|
||
note = "想法",
|
||
text = "原文",
|
||
wr_range = "0-5",
|
||
})
|
||
Assert.eq(review_body.bookId, "1")
|
||
Assert.eq(review_body.content, "想法")
|
||
Assert.eq(review_body.abstract, "原文")
|
||
Assert.eq(review_body.range, "0-5")
|
||
Assert.eq(review_body.chapterUid, 9)
|
||
Assert.eq(review_body.isPrivate, 0, "非私密阅读的新想法明确公开")
|
||
local secret_body = Notes.toReviewBody("1", 9, {
|
||
note = "想法", text = "原文", wr_range = "0-5",
|
||
}, 1)
|
||
Assert.eq(secret_body.isPrivate, 1)
|
||
local kept = Notes.toReviewBody("1", 9, {
|
||
note = "想法", text = "原文", wr_range = "0-5", wr_private = 1,
|
||
}, 0)
|
||
Assert.eq(kept.isPrivate, 1, "条目自己的可见范围优先于书籍设置")
|
||
local edit_unknown = Notes.toReviewEditBody("1", 9, {
|
||
note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1",
|
||
}, 1)
|
||
Assert.is_nil(edit_unknown.isPrivate, "没记过可见范围的旧想法不能在修改时改成公开或私密")
|
||
local edit_private = Notes.toReviewEditBody("1", 9, {
|
||
note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1", wr_private = 1,
|
||
}, 0)
|
||
Assert.eq(edit_private.isPrivate, 1)
|
||
Assert.eq(edit_private.reviewId, "rv1")
|
||
local pulled_private = Notes.toAnnotations({ updated = {} }, "9", {
|
||
reviews = { { review = {
|
||
reviewId = "rv-p", chapterUid = "9", range = "0-1",
|
||
abstract = "原文", content = "私密", isPrivate = 1,
|
||
} } },
|
||
})
|
||
Assert.eq(pulled_private[1].wr_private, 1)
|
||
local reconciled = { note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1" }
|
||
Notes.reconcileReviews({ reconciled }, {
|
||
reviews = { { review = { reviewId = "rv1", isPrivate = 1, range = "0-5", abstract = "原文", content = "想法" } } },
|
||
})
|
||
Assert.eq(reconciled.wr_private, 1)
|
||
local pending_private = {
|
||
note = "想法", text = "原文", wr_range = "0-5", wr_review_id = "rv1", wr_private = 0,
|
||
}
|
||
Notes.reconcileReviews({ pending_private }, {
|
||
reviews = { { review = { reviewId = "rv1", isPrivate = 1 } } },
|
||
})
|
||
Assert.eq(pending_private.wr_private, 0, "本地已记下的可见范围不被列表覆盖")
|
||
Assert.eq(Mapper.bookSecret({ book = { bookId = "1", secret = 1 } }), 1)
|
||
Assert.eq(Mapper.bookSecret({ book = { bookId = "1", version = 3 } }), 0)
|
||
Assert.is_nil(Mapper.bookSecret({}))
|
||
Assert.eq(Notes.privateOf({ review = { isPrivate = 1 } }), 1)
|
||
Assert.eq(Notes.privateOf({ review = { isPrivate = 0 } }), 0)
|
||
Assert.is_nil(Notes.privateOf({ review = {} }))
|
||
local pending = {
|
||
text = "原文", note = "想法", wr_range = "0-5", wr_review_only = true,
|
||
}
|
||
local confirmed = Notes.reconcileReviews({ pending }, {
|
||
reviews = { { review = {
|
||
reviewId = "rv-recovered", range = "9-14", abstract = "原文", content = "想法",
|
||
} } },
|
||
})
|
||
Assert.eq(pending.wr_review_id, "rv-recovered")
|
||
Assert.is_true(confirmed[pending])
|
||
local current = Notes.prepareLocalAnnotations({
|
||
{
|
||
datetime = "old", page = "/body/a", drawer = "lighten",
|
||
note = "旧笔记", wr_bookmark_id = "bm1", wr_review_id = "rv1",
|
||
},
|
||
{
|
||
datetime = "old", page = "/body/b", drawer = "lighten",
|
||
wr_bookmark_id = "bm2",
|
||
},
|
||
}, {
|
||
{
|
||
datetime = "new", page = "/body/a", drawer = "lighten",
|
||
wr_bookmark_id = "bm1",
|
||
},
|
||
})
|
||
Assert.is_true(current[1].wr_delete_review)
|
||
Assert.is_true(current[2].wr_deleted)
|
||
Assert.eq(current[2].wr_bookmark_id, "bm2")
|
||
|
||
local remote_note = { {
|
||
page = "/body/n", datetime = "r", text = "原文", note = "想法",
|
||
wr_range = "100-102", wr_review_id = "rv1",
|
||
} }
|
||
local local_note = { {
|
||
page = "/body/n", datetime = "l", text = "原文", note = "想法",
|
||
wr_range = "300-302",
|
||
} }
|
||
Assert.len(Notes.mergeAnnotations(remote_note, local_note, false, true), 1)
|
||
|
||
local twins = {
|
||
{ page = "/body/a", text = "同一处", wr_range = "10-20", wr_bookmark_id = "b1" },
|
||
{ page = "/body/a", text = "同一处", wr_range = "10-20", wr_bookmark_id = "b2" },
|
||
}
|
||
Assert.len(Notes.mergeAnnotations(twins, { { page = "/body/a", text = "同一处", wr_range = "10-20", wr_bookmark_id = "b2" } }, false, true), 1)
|
||
-- 上传后确认没回来的本地划线(无 id / range)与远端同坐标:只留一条
|
||
local unconfirmed = { { page = "/body/a", pos0 = "/p.0", pos1 = "/p.9", text = "同一处", drawer = "lighten" } }
|
||
local pulled = { { page = "/body/a", pos0 = "/p.0", pos1 = "/p.9", text = "同一处", wr_range = "10-20", wr_bookmark_id = "b1" } }
|
||
local merged = Notes.mergeAnnotations(pulled, unconfirmed, false, true)
|
||
Assert.len(merged, 1)
|
||
Assert.eq(merged[1].wr_bookmark_id, "b1")
|
||
local version = Mapper.bookVersion({ book = { version = 123 } })
|
||
Assert.eq(version, 123)
|
||
end
|
||
|
||
do -- 同文、同备注或同范围不能覆盖不同位置的真实划线。
|
||
local first = {
|
||
page = "/p[1]", pos0 = "/p[1].0", pos1 = "/p[1].4",
|
||
text = "重复原文", note = "想法一", drawer = "lighten",
|
||
}
|
||
local second = {
|
||
page = "/p[8]", pos0 = "/p[8].0", pos1 = "/p[8].4",
|
||
text = first.text, note = "想法二", drawer = "lighten",
|
||
}
|
||
local merged, dropped, deduped = Notes.mergeAnnotations({}, { first, second }, false, false)
|
||
Assert.len(merged, 2)
|
||
Assert.eq(dropped, 0)
|
||
Assert.eq(deduped, 0)
|
||
Assert.eq(merged[2].note, "想法二")
|
||
second.note = first.note
|
||
Assert.len(Notes.mergeAnnotations({ first }, { second }, false, false), 2)
|
||
first.wr_range, second.wr_range = "0-4", "0-4"
|
||
Assert.len(Notes.mergeAnnotations({ first, second }, {}, false, false), 2)
|
||
Assert.len(Notes.mergeAnnotations({ first, second }, { first, second }, false, false), 2)
|
||
end
|
||
|
||
do -- 想法列表没拿到:本地已有的想法接回拉下来的划线,review-only 条目保留
|
||
local anns = {
|
||
{ text = "a", wr_bookmark_id = "b1" },
|
||
{ text = "c", wr_bookmark_id = "b2" },
|
||
}
|
||
local out = Notes.keepLocalReviews(anns, {
|
||
{ text = "a", note = "想法A", wr_bookmark_id = "b1", wr_review_id = "r1" },
|
||
{ text = "x", note = "只有想法", wr_review_id = "r2", wr_review_only = true },
|
||
{ text = "gone", note = "无关", wr_review_id = "r3" },
|
||
})
|
||
Assert.eq(out[1].note, "想法A")
|
||
Assert.eq(out[1].wr_review_id, "r1")
|
||
Assert.is_nil(out[2].note)
|
||
Assert.eq(#out, 3)
|
||
Assert.eq(out[3].wr_review_id, "r2")
|
||
end
|
||
|
||
do -- 无 text/range 的位置记录:合并时按内容键去重,不能在开书时再翻倍
|
||
local marker = {
|
||
datetime = "2026-11-03 14:54:54",
|
||
page = "/html/body/div[2]/p[431]/text()[1].37",
|
||
pageno = 45,
|
||
total_pages = 448,
|
||
}
|
||
local highlight = {
|
||
datetime = "2026-11-04 10:00:00",
|
||
page = "/html/body/div[2]/p[2]/text()[1].0",
|
||
pos0 = "/html/body/div[2]/p[2]/text()[1].0",
|
||
pos1 = "/html/body/div[2]/p[2]/text()[1].6",
|
||
drawer = "lighten",
|
||
text = "合法高亮",
|
||
wr_range = "10-20",
|
||
wr_bookmark_id = "bm-keep",
|
||
}
|
||
local review = {
|
||
datetime = "2026-11-04 10:01:00",
|
||
page = "/html/body/div[2]/p[3]/text()[1].0",
|
||
pos0 = "/html/body/div[2]/p[3]/text()[1].0",
|
||
pos1 = "/html/body/div[2]/p[3]/text()[1].4",
|
||
drawer = "lighten",
|
||
text = "带想法的原文",
|
||
note = "一条想法",
|
||
wr_range = "30-40",
|
||
wr_review_id = "rv-keep",
|
||
wr_review_only = true,
|
||
}
|
||
local bloated = {}
|
||
for _i = 1, 4096 do bloated[#bloated + 1] = marker end
|
||
bloated[#bloated + 1] = highlight
|
||
bloated[#bloated + 1] = review
|
||
|
||
local cleaned = Notes.cleanAnnotations(bloated, 448)
|
||
Assert.len(cleaned, 3, "清洗后应只剩 1 条位置记录 + 高亮 + 想法")
|
||
local texts = {}
|
||
for _idx, item in ipairs(cleaned) do texts[item.text or ""] = true end
|
||
Assert.is_true(texts["合法高亮"])
|
||
Assert.is_true(texts["带想法的原文"])
|
||
|
||
local merged, dropped, deduped = Notes.mergeAnnotations(bloated, bloated, false, false)
|
||
Assert.eq(dropped, 0)
|
||
Assert.len(merged, 3, "远端和本地各有 4096 份拷贝时合并不得膨胀")
|
||
Assert.is_true(deduped >= 4096)
|
||
local again = Notes.mergeAnnotations(merged, merged, false, false)
|
||
Assert.len(again, 3, "再合并一次仍是 3 条")
|
||
local kept = {}
|
||
for _idx, item in ipairs(merged) do
|
||
if item.wr_bookmark_id then kept.highlight = item end
|
||
if item.wr_review_id then kept.review = item end
|
||
end
|
||
Assert.eq(kept.highlight.wr_bookmark_id, "bm-keep")
|
||
Assert.eq(kept.review.wr_review_id, "rv-keep")
|
||
end
|