Files
moonsource-qqreader/tests/qqreader/async_spec.lua
T
GarfieldandCopilot 730b2d6d9f ✨ 初始提交:月读第三方书源插件集合
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
2026-11-16 12:08:03 +08:00

81 lines
3.0 KiB
Lua

--[[-- QQ阅读进度多阶段取消与换号竞态。 @module tests.moonsource_qqreader.async_spec --]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local cookie, detail_delay, push_delay, late = "111", false, false, nil
local cancelled, pushed = {}, 0
local toc = { { uid = "1", title = "one" }, { uid = "2", title = "two" } }
package.preload["source.toc"] = function() return { index = function() return 2 end } end
package.preload["moonsource_qqreader.client"] = function()
return {
cookie = function() return cookie end,
detailAsync = function(_id, cb)
local function done() cb({ title = "fixture", inShelf = 1, lastReadChapterId = 2 }) end
if detail_delay then late = done else done() end
return { cancel = function() cancelled.detail = true end }
end,
updateReadAsync = function(_id, _chapter, cb)
pushed = pushed + 1
if push_delay then late = function() cb(true) end else cb(true) end
return { cancel = function() cancelled.push = true end }
end,
bookCommentsAsync = function(_id, cb)
cb({ commentCount = 32, comments = {
{ commentId = "c1", content = "fixture", user = { nickname = "author" } },
} })
return { cancel = function() end }
end,
}
end
local source = require("moonsource_qqreader.source").new()
local identity = { source_id = "ext.qqreader", stable_id = "9" }
do
source.loadTocAsync = function(_self, _identity, cb)
late = function() cb(toc) end
return { cancel = function() cancelled.toc = true end }
end
local called = false
local job = source:getProgressAsync(identity, function() called = true end)
job.cancel()
Assert.is_true(cancelled.toc)
Assert.is_nil(cancelled.detail, "同步详情不能覆盖下一阶段句柄")
late()
Assert.is_false(called)
end
for _idx, stage in ipairs({ "detail", "push" }) do
cancelled, detail_delay, push_delay = {}, stage == "detail", stage == "push"
source.loadTocAsync = function(_self, _identity, cb)
cb(toc)
return { cancel = function() cancelled.toc = true end }
end
local called = false
local job = source:putProgressAsync(identity, { chapter_idx = 2 }, function() called = true end)
job.cancel()
Assert.is_true(cancelled[stage])
Assert.is_nil(cancelled.toc)
late()
Assert.is_false(called)
end
do
cookie, detail_delay, pushed = "111", true, 0
local result, err
source:putProgressAsync(identity, { chapter_idx = 2 }, function(value, reason) result, err = value, reason end)
cookie = "222"
late()
Assert.is_nil(result)
Assert.matches(err, "账号已变化")
Assert.eq(pushed, 0)
end
do
local page
source:bookReviewsAsync(identity, function(value) page = value end)
Assert.len(page.items, 1)
Assert.eq(page.total, 32)
Assert.is_true(page.more)
Assert.matches(page.title, "预览 1/32")
end