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

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

70 lines
2.5 KiB
Lua

--[[--
快看源操作:命中旧缓存可离线开书,同步目录桩后取消仍指向章节任务。
@module tests.moonsource_kuaikan.operations_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch, inject-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
local Themed = require("support.theme_stubs")
Themed.install()
local toc = { { idx = 1, uid = "1", toc_version = 1 }, { idx = 2, uid = "2" } }
local connected, cached, touched = true, false, nil
package.loaded["source.toc"] = { read = function() return toc end }
package.loaded["ui/network/manager"] = { isConnected = function() return connected end }
package.loaded["book.progress_db"] = { get = function() return { chapter_idx = 2 } end }
package.loaded["book.store"] = {
touch = function(path, _identity, meta) touched = { path, meta.chapter_idx }; return true end,
}
local pending, cancelled = nil, {}
package.loaded["moonsource_kuaikan.chapter"] = {
existing = function(_identity, chapter) return cached and "/" .. chapter.uid .. ".cbz" or nil end,
materializeAsync = function(_identity, _chapter, _progress, cb)
pending = cb
return { cancel = function() cancelled.chapter = true end }
end,
prefetchAsync = function(_identity, _toc, from, count, _opts, cb)
Assert.eq(from, 0)
Assert.eq(count, 2)
pending = cb
return { cancel = function() cancelled.cache = true end }
end,
}
local source = require("moonsource_kuaikan.source").new()
source.loadTocAsync = function(_self, _identity, cb)
cb(toc)
return { cancel = function() cancelled.toc = true end }
end
local identity = { source_id = "ext.kuaikan", stable_id = "book" }
local called = false
local handle = source:openBookAsync(identity, nil, function() called = true end)
Stubs.flush()
handle.cancel()
Assert.is_true(cancelled.chapter)
Assert.is_nil(cancelled.toc)
Assert.is_true(Themed.last("progress_dialog").closed)
pending("/1.cbz")
Assert.is_false(called)
Assert.is_nil(touched)
handle = source:cacheAllChaptersAsync(identity, nil, function() called = true end)
handle.cancel()
Assert.is_true(cancelled.cache)
Assert.is_nil(cancelled.toc)
pending(2, 2, 0)
Assert.is_false(called)
connected, cached = false, true
local opened
source:openBookAsync(identity, nil, function(path) opened = path end)
Stubs.flush()
Assert.eq(opened, "/2.cbz")
Assert.eq(touched[2], 2)
cached = false
source:openBookAsync(identity, nil, function(path, err)
Assert.is_nil(path)
Assert.eq(err, "网络不可用,请先连接 Wi-Fi")
end)
Stubs.flush()