Files
GarfieldandCopilot d8626befbc ✨ (weread): 首次加载时把旧内置版 wechat 的数据就地改名为 weread
设置、书库记录、缓存目录和库里的路径、启用状态一并迁移,用户无需重新登录。

Co-authored-by: Copilot <[email protected]>
2026-11-16 15:23:05 +08:00

60 lines
2.2 KiB
Lua

--[[--
moonsource_weread.legacy:旧内置版 id `wechat` 的设置、库记录、缓存目录就地改成 `weread`。
@module tests.weread.legacy_spec
--]]
---@diagnostic disable: undefined-field
local Assert = require("support.assert")
local Config = require("support.config")
local lfs = require("libs/libkoreader-lfs")
local root = Config.dir() .. "/weread_legacy"
os.execute(("rm -rf '%s' && mkdir -p '%s/cache/wechat/book'"):format(root, root))
local f = assert(io.open(root .. "/cache/wechat/book/1.html", "w"))
f:write("x")
f:close()
local sources = { wechat = { cookie = "c" }, weread = {} }
local common = { enabled_sources = { wechat = true }, active_source = "wechat" }
local saved = {}
package.loaded["utils.settings"] = {
getSource = function(id) sources[id] = sources[id] or {}; return sources[id] end,
saveSource = function(id) saved[#saved + 1] = id end,
get = function() return common end,
save = function() end,
}
package.loaded["utils.paths"] = { sourceCacheDir = function(id) return root .. "/cache/" .. id end }
local sql = {}
package.loaded["utils.db"] = {
transaction = function(fn) return fn() end,
exec = function(statement, ...) sql[#sql + 1] = { statement, ... }; return true end,
}
package.loaded["moonsource_weread.legacy"] = nil
local Legacy = require("moonsource_weread.legacy")
Legacy.run()
Assert.eq(sources.weread.cookie, "c", "设置搬到新 id")
Assert.is_nil(next(sources.wechat), "旧设置清空")
Assert.eq(common.enabled_sources.weread, true)
Assert.is_nil(common.enabled_sources.wechat)
Assert.eq(common.active_source, "weread")
Assert.is_nil(lfs.attributes(root .. "/cache/wechat", "mode"))
Assert.eq(lfs.attributes(root .. "/cache/weread/book/1.html", "mode"), "file", "缓存目录改名")
local tables, paths = 0, 0
for _idx, entry in ipairs(sql) do
if entry[1]:find("SET source_id=%?") and entry[2] == "weread" and entry[3] == "wechat" then tables = tables + 1 end
if entry[1]:find("REPLACE%(path") then paths = paths + 1 end
end
Assert.eq(tables, 15)
Assert.eq(paths, 3, "库里记录的路径跟着改")
-- 同一进程不重复执行
local count = #sql
Legacy.run()
Assert.eq(#sql, count)
os.execute(("rm -rf '%s'"):format(root))