Files
moonsource-weread/tests/weread/notes_push_spec.lua
T
Garfield 352ee1bd99 🐛 (notes): 想法按私密阅读设置发表并回读
新想法跟这本书的私密阅读走,修改已有想法沿用原来的可见范围。
回读对不上就当这轮失败,不把 HTTP 成功当成已经私密。
2026-12-01 12:47:12 +08:00

290 lines
12 KiB
Lua
Raw 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.notes_push:删除先行、预检失败整轮失败、远端已没有的 bookmarkId 被清掉、取消后不回调。
@module tests.moonsource_weread.notes_push_spec
--]]
local Assert = require("support.assert")
local Stubs = require("support.stubs")
Stubs.install()
local NotesPush = require("moonsource_weread.notes_push")
--- 只记调用、按表回包的假客户端(回包走 nextTick,模拟真异步)。
local function fakeClient(replies)
local calls = {}
---@type { calls: string[], [string]: any }
local client = { calls = calls, sent = {} }
local function method(name)
return function(_self, ...)
local args = { ... }
local cb = args[#args]
calls[#calls + 1] = name
client.sent[name] = args
local reply = replies[name]
require("ui/uimanager"):nextTick(function()
if type(reply) == "function" then reply = reply() end
if reply == false then cb(nil, name .. " failed") else cb(reply or {}) end
end)
return { cancel = function() calls[#calls + 1] = "cancel:" .. name end }
end
end
for _idx, name in ipairs({ "removeBookmarkAsync", "deleteReviewAsync", "bookmarkListAsync",
"myReviewsAsync", "addBookmarkAsync", "updateBookmarkAsync", "editReviewAsync",
"addReviewAsync", "bookInfoAsync", "reviewSingleAsync" }) do
client[name] = method(name)
end
return client
end
local identity = { source_id = "weread", stable_id = "b1", chapter_idx = 1 }
do -- 删除墓碑:先删想法再删划线,删完从注解里移除,后续无事可做即成功
local client = fakeClient({ bookmarkListAsync = { updated = {} }, myReviewsAsync = { reviews = {} } })
local annotations = { { wr_deleted = true, wr_bookmark_id = "bm1", wr_review_id = "rv1" } }
local ok, err
NotesPush.run(client, identity, "c1", "<p>x</p>", annotations, function(o, e) ok, err = o, e end)
Stubs.flush()
Assert.is_true(ok)
Assert.is_nil(err)
Assert.eq(client.calls[1], "deleteReviewAsync")
Assert.eq(client.calls[2], "removeBookmarkAsync")
Assert.len(annotations, 0)
end
do -- 只有想法的墓碑(没记过划线 id):按 range 在远端找到划线一并删掉
local client = fakeClient({
bookmarkListAsync = { updated = { { chapterUid = "c1", range = "10-20", bookmarkId = "bm9" } } },
myReviewsAsync = { reviews = {} },
})
local annotations = { { wr_deleted = true, wr_review_id = "rv1", wr_range = "10-20" } }
local ok
NotesPush.run(client, identity, "c1", "<p>x</p>", annotations, function(o) ok = o end)
Stubs.flush()
Assert.is_true(ok)
Assert.eq(client.calls[1], "deleteReviewAsync")
Assert.eq(client.calls[2], "bookmarkListAsync")
Assert.eq(client.calls[3], "removeBookmarkAsync")
Assert.len(annotations, 0)
end
do -- 预检失败:整轮失败,带上错误
local client = fakeClient({ bookmarkListAsync = false })
local ok, err
NotesPush.run(client, identity, "c1", "<p>x</p>", {}, function(o, e) ok, err = o, e end)
Stubs.flush()
Assert.is_nil(ok)
Assert.eq(err, "bookmarkListAsync failed")
end
do -- 远端已没有的 bookmarkId 被清掉(下轮当新划线重传),删除请求失败则停在删除
local client = fakeClient({ bookmarkListAsync = { updated = {} }, myReviewsAsync = { reviews = {} } })
local item = { drawer = "lighten", text = "t", wr_bookmark_id = "gone" }
NotesPush.run(client, identity, "c1", nil, { item }, function() end)
Stubs.flush()
Assert.is_nil(item.wr_bookmark_id)
client = fakeClient({ removeBookmarkAsync = false })
local ok
local annotations = { { wr_deleted = true, wr_bookmark_id = "bm1" } }
NotesPush.run(client, identity, "c1", nil, annotations, function(o) ok = o end)
Stubs.flush()
Assert.is_nil(ok)
Assert.len(annotations, 1, "删除失败保留墓碑下轮重试")
end
do -- 取消:在途请求被取消,迟到回包不回调
local client = fakeClient({})
local called = false
local job = NotesPush.run(client, identity, "c1", nil, {}, function() called = true end)
job.cancel()
Stubs.flush()
Assert.is_false(called)
Assert.eq(client.calls[2], "cancel:bookmarkListAsync")
end
do -- 想法列表预检失败只跳过想法对账,不阻断划线:已有 review id 保留
local client = fakeClient({ bookmarkListAsync = { updated = {} }, myReviewsAsync = false })
local item = { drawer = "lighten", text = "t", wr_bookmark_id = "gone", wr_review_id = "rv1" }
local ok, err
NotesPush.run(client, identity, "c1", nil, { item }, function(o, e) ok, err = o, e end)
Stubs.flush()
Assert.is_nil(err)
Assert.eq(item.wr_review_id, "rv1")
Assert.is_nil(item.wr_bookmark_id)
end
do -- 新划线定位:range 原文走 Position 缓存(开书记住的那份),同步命中不重拉整章
local Toc = require("moonsource_weread.toc")
local Position = require("moonsource_weread.progress")
local old_read, old_load = Toc.read, Position.loadAsync
Toc.read = function() return { { uid = "c1" } } end
local load_calls = 0
Position.loadAsync = function(_identity, _chapter, cb)
load_calls = load_calls + 1
-- 缓存命中:同步回调、返回 nil(与 Position.loadAsync 真实行为一致)
cb('<html><head><title>tttttttttt</title></head><body><p>hello world</p></body></html>', "html")
return nil
end
local submitted = false
local item = {
drawer = "lighten", text = "hello",
pos0 = "/html/body/p/text().0", pos1 = "/html/body/p/text().5",
}
local client = fakeClient({
bookmarkListAsync = function()
if not submitted then return { updated = {} } end
return { updated = { {
chapterUid = "c1", range = item.wr_range, bookmarkId = "bm-new",
markText = "hello", type = 1,
} } }
end,
myReviewsAsync = { reviews = {} },
addBookmarkAsync = function() submitted = true; return { bookmarkId = "bm-new" } end,
bookInfoAsync = { book = { version = 7 } },
})
local ok, err
NotesPush.run(client, identity, "c1",
"<html><body><p>hello world</p></body></html>", { item },
function(o, e) ok, err = o, e end)
Stubs.flush()
Toc.read, Position.loadAsync = old_read, old_load
Assert.is_true(ok)
Assert.is_nil(err)
Assert.eq(load_calls, 1)
-- 53 = wire 原文里 <p> 之前的全部 rune(head/title 计入),与本地壳无关
Assert.eq(item.wr_range, "53-58")
Assert.eq(item.wr_bookmark_id, "bm-new")
end
do -- 想法清单拿不到:新想法不推(认不回上轮已成功的会重复),报失败留脏;划线与已有想法照常
local client = fakeClient({ bookmarkListAsync = { updated = {} }, myReviewsAsync = false })
local item = { drawer = "lighten", text = "t", note = "想法", wr_review_only = true, wr_range = "1-2" }
local ok, err
NotesPush.run(client, identity, "c1", nil, { item }, function(o, e) ok, err = o, e end)
Stubs.flush()
Assert.is_nil(ok)
Assert.not_nil(err)
Assert.is_nil(item.wr_review_id)
for _, name in ipairs(client.calls) do Assert.is_true(name ~= "addReviewAsync", "不得重复新增想法") end
end
do -- 私密阅读的新想法带 isPrivate=1,回读确认后才算成功
require("moonsource_weread.context").clear()
local Toc = require("moonsource_weread.toc")
local Position = require("moonsource_weread.progress")
local old_read, old_load = Toc.read, Position.loadAsync
Toc.read = function() return { { uid = "c1" } } end
Position.loadAsync = function(_identity, _chapter, cb)
cb("<html><body><p>原文</p></body></html>", "html")
return nil
end
local item = {
drawer = "lighten", text = "原文", note = "只给自己看",
wr_range = "1-2", wr_review_only = true,
pos0 = "/html/body/p/text().0", pos1 = "/html/body/p/text().2",
}
local client = fakeClient({
bookmarkListAsync = { updated = {} },
myReviewsAsync = { reviews = {}, hasMore = 0, totalCount = 0 },
bookInfoAsync = { book = { bookId = "b1", version = 1, secret = 1 } },
addReviewAsync = { reviewId = "rv-priv" },
reviewSingleAsync = { review = { reviewId = "rv-priv", isPrivate = 1 } },
})
local ok, err
NotesPush.run(client, identity, "c1",
"<html><body><p>原文</p></body></html>", { item }, function(o, e) ok, err = o, e end)
Stubs.flush()
Toc.read, Position.loadAsync = old_read, old_load
Assert.is_true(ok, tostring(err))
Assert.eq(client.sent.addReviewAsync[1].isPrivate, 1)
Assert.eq(item.wr_review_id, "rv-priv")
Assert.eq(item.wr_private, 1)
Assert.is_nil(item.wr_update_review)
end
do -- 回读不是私密:留下 reviewId,整轮失败,不再当成成功
require("moonsource_weread.context").clear()
local Toc = require("moonsource_weread.toc")
local Position = require("moonsource_weread.progress")
local old_read, old_load = Toc.read, Position.loadAsync
Toc.read = function() return { { uid = "c1" } } end
Position.loadAsync = function(_identity, _chapter, cb)
cb("<html><body><p>原文</p></body></html>", "html")
return nil
end
local item = {
drawer = "lighten", text = "原文", note = "只给自己看",
wr_range = "1-2", wr_review_only = true,
pos0 = "/html/body/p/text().0", pos1 = "/html/body/p/text().2",
}
local client = fakeClient({
bookmarkListAsync = { updated = {} },
myReviewsAsync = { reviews = {}, hasMore = 0, totalCount = 0 },
bookInfoAsync = { book = { bookId = "b1", version = 1, secret = 1 } },
addReviewAsync = { reviewId = "rv-pub" },
reviewSingleAsync = { review = { isPrivate = 0 } },
})
local ok, err
NotesPush.run(client, identity, "c1",
"<html><body><p>原文</p></body></html>", { item }, function(o, e) ok, err = o, e end)
Stubs.flush()
Toc.read, Position.loadAsync = old_read, old_load
Assert.is_nil(ok)
Assert.eq(err, "想法的可见范围没有按设置保存")
Assert.eq(item.wr_review_id, "rv-pub")
Assert.eq(item.wr_private, 0)
Assert.is_nil(item.wr_update_review)
end
do -- 改已有想法:沿用拉下来的 isPrivate,不按书籍公开设置改掉
require("moonsource_weread.context").clear()
local item = {
drawer = "lighten", text = "原文", note = "改过",
wr_range = "1-2", wr_bookmark_id = "bm1", wr_review_id = "rv1",
wr_private = 1, wr_update_review = true,
}
local client = fakeClient({
bookmarkListAsync = { updated = { {
chapterUid = "c1", range = "1-2", bookmarkId = "bm1", markText = "原文", type = 1,
} } },
myReviewsAsync = { reviews = { { review = {
reviewId = "rv1", isPrivate = 0, range = "1-2", abstract = "原文", content = "改过",
} } }, hasMore = 0, totalCount = 1 },
editReviewAsync = { succ = 1 },
reviewSingleAsync = { review = { isPrivate = 1 } },
})
local ok, err
NotesPush.run(client, identity, "c1", nil, { item }, function(o, e) ok, err = o, e end)
Stubs.flush()
Assert.is_true(ok, tostring(err))
Assert.eq(client.sent.editReviewAsync[1].isPrivate, 1)
for _, name in ipairs(client.calls) do
Assert.is_true(name ~= "bookInfoAsync", "已有可见范围不必再读私密阅读设置")
end
Assert.is_nil(item.wr_update_review)
end
do -- 没记过可见范围的旧想法:修改不带 isPrivate,也不回读
require("moonsource_weread.context").clear()
local item = {
drawer = "lighten", text = "原文", note = "改过",
wr_range = "1-2", wr_bookmark_id = "bm1", wr_review_id = "rv1",
wr_update_review = true,
}
local client = fakeClient({
bookmarkListAsync = { updated = { {
chapterUid = "c1", range = "1-2", bookmarkId = "bm1", markText = "原文", type = 1,
} } },
myReviewsAsync = { reviews = { { review = { reviewId = "rv1" } } }, hasMore = 0, totalCount = 1 },
editReviewAsync = { succ = 1 },
})
local ok, err
NotesPush.run(client, identity, "c1", nil, { item }, function(o, e) ok, err = o, e end)
Stubs.flush()
Assert.is_true(ok, tostring(err))
Assert.is_nil(client.sent.editReviewAsync[1].isPrivate)
for _, name in ipairs(client.calls) do
Assert.is_true(name ~= "reviewSingleAsync")
end
end