♻️ 书源 id 去掉 ext. 前缀,沿用旧内置源数据

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-16 14:07:45 +08:00
1 parent 01e572a5ee
commit f8ad3f73a6
29 files changed
+173 -173

No files matched your search

@@ -7,7 +7,7 @@
local _ = require("l10n").get("moonsource_weread")
return {
id = "ext.weread",
id = "weread",
name = _("微信读书"),
type = "chapter",
description = _("微信读书书架与在线阅读"),
@@ -317,7 +317,7 @@ end
function Assets.localizeAsync(book_id, chapter, html, referer, fetch_style, cb)
local cancelled = false
local jobs = {}
local work_dir = Paths.bookWorkDir(book_id, "ext.weread")
local work_dir = Paths.bookWorkDir(book_id, "weread")
local images_dir = work_dir .. "/images"
--- 图片落地后再补样式表;已取消则丢弃。
@@ -232,7 +232,7 @@ local function sessionRequest(method, url, body, opts, cb)
attempt(true)
elseif rejected then
if renew_err then logger.warn("wechat auth renew rejected", renew_err) end
cb(nil, require("source.auth").expire("ext.weread", _("点这个提示上的「重新登录」。二维码出来以后,打开手机微信,点右上角「+」,再点「扫一扫」,对准屏幕上的二维码,在手机上点确认。")), res)
cb(nil, require("source.auth").expire("weread", _("点这个提示上的「重新登录」。二维码出来以后,打开手机微信,点右上角「+」,再点「扫一扫」,对准屏幕上的二维码,在手机上点确认。")), res)
else
cb(nil, renew_err or _("请求失败"), res)
end
@@ -246,7 +246,7 @@ local function sessionRequest(method, url, body, opts, cb)
if opts.no_expire then
cb(nil, _("微信读书拒绝了请求,请稍后再试"), res)
else
cb(nil, require("source.auth").expire("ext.weread", _("点这个提示上的「重新登录」。二维码出来以后,打开手机微信,点右上角「+」,再点「扫一扫」,对准屏幕上的二维码,在手机上点确认。")), res)
cb(nil, require("source.auth").expire("weread", _("点这个提示上的「重新登录」。二维码出来以后,打开手机微信,点右上角「+」,再点「扫一扫」,对准屏幕上的二维码,在手机上点确认。")), res)
end
return
end
@@ -254,7 +254,7 @@ local function sessionRequest(method, url, body, opts, cb)
cb(nil, "HTTP " .. tostring(code), res)
return
end
if not opts.skip_auth_retry then require("source.auth").ok("ext.weread") end
if not opts.skip_auth_retry then require("source.auth").ok("weread") end
if res then res.json = data end
cb(raw, nil, res)
end)
@@ -207,7 +207,7 @@ function Chapter.primeBatchAsync(bookId, chapters, opts, cb)
end
local param = Package.formatChaptersParam(uids)
return downloadPackageAsync(bookId, param, book_type, function(files)
local work_dir = Paths.bookWorkDir(bookId, "ext.weread")
local work_dir = Paths.bookWorkDir(bookId, "weread")
Paths.ensureDir(work_dir .. "/images")
for name, data in pairs(files) do
local base = name:match("^Images/([^/]+)$")
@@ -399,7 +399,7 @@ Chapter._noteTexts = noteTexts
---@return string[] uids
local function noteChapterUids(bookId)
local uids = {}
for _idx, ch in ipairs(Toc.read("ext.weread", bookId) or {}) do
for _idx, ch in ipairs(Toc.read("weread", bookId) or {}) do
if tostring(ch.title or ""):find("注", 1, true) then uids[#uids + 1] = ch.uid end
end
return uids
@@ -411,7 +411,7 @@ local note_files = {}
---@param bookId string
---@return string
local function notesPath(bookId)
return Paths.bookWorkDir(bookId, "ext.weread") .. "/notes.json"
return Paths.bookWorkDir(bookId, "weread") .. "/notes.json"
end
---@param bookId string
@@ -537,7 +537,7 @@ function Chapter.fetchContentAsync(bookId, chapter, cb)
cb({ title = title, html = "<h1>" .. Text.xmlEscape(title) .. "</h1>" .. html })
return
end
local work_dir = Paths.bookWorkDir(bookId, "ext.weread")
local work_dir = Paths.bookWorkDir(bookId, "weread")
asset_job = Assets.localizeAsync(bookId, chapter, html, reader_url, function(done)
-- 章包里已有样式就不再打整书包;整书包才带公共背景图。
if type(style) == "string" and style ~= "" then
@@ -49,15 +49,15 @@ local VERSION_HEADERS = {
--- wechat 源配置(会话字段与用户信息都在这里)。
---@return table
function Eink.config()
return require("utils.settings").getSource("ext.weread")
return require("utils.settings").getSource("weread")
end
---@param patch table 合并进 wechat 源配置并落盘
function Eink.save(patch)
local MoonSettings = require("utils.settings")
local c = MoonSettings.getSource("ext.weread")
local c = MoonSettings.getSource("weread")
for k, v in pairs(patch) do c[k] = v end
MoonSettings.saveSource("ext.weread", c)
MoonSettings.saveSource("weread", c)
end
--- 令牌就是网页会话(wr_vid/wr_skey 当 Cookie 可用),只落一份;eink 表只放续期要的两项。
@@ -80,7 +80,7 @@ local function saveSession(vid, access_token, refresh_token, device_id)
})
end
if current.wr_vid ~= vid then
return require("source.logout").replaceLibrary("ext.weread", commit)
return require("source.logout").replaceLibrary("weread", commit)
end
commit()
return true
@@ -56,6 +56,6 @@ Ext.add("desktop.account_feeds", {
client = function() return require(PREFIX .. "client") end,
})
Ext.add("reader.paragraph_lender", { id = "ext.weread", label = L("微信读书") })
Ext.add("reader.paragraph_lender", { id = "weread", label = L("微信读书") })
return {}
@@ -10,7 +10,7 @@ local Text = require("utils.text")
local Mapper = {}
local SOURCE_ID = "ext.weread"
local SOURCE_ID = "weread"
--- 从嵌套 book/bookInfo 行解出书籍对象。
---@param row table|nil
@@ -36,9 +36,9 @@ end
---@return integer removed
function Mp.dropLegacyBooks(BookDB)
local n = 0
for _idx, id in ipairs(BookDB.stableIdsBySource("ext.weread") or {}) do
for _idx, id in ipairs(BookDB.stableIdsBySource("weread") or {}) do
if Mp.isMp(id) then
BookDB.remove("ext.weread", id)
BookDB.remove("weread", id)
n = n + 1
end
end
@@ -18,7 +18,7 @@ local _ = require("l10n").get("moonsource_weread")
local Position = {}
local function cachePath(book_id, idx)
return Paths.bookWorkDir(book_id, "ext.weread") .. "/" .. idx .. ".position.json"
return Paths.bookWorkDir(book_id, "weread") .. "/" .. idx .. ".position.json"
end
---@param book_id string
@@ -1,6 +1,6 @@
--[[--
微信读书源设置 UI(扫码登录由本模块自绘)。
读写:utils.settings.getSource / saveSource("ext.weread")
读写:utils.settings.getSource / saveSource("weread")
@module koplugin.book.moonsource_weread.setting
--]]
@@ -8,7 +8,7 @@
local _ = require("l10n").get("moonsource_weread")
local T = require("ffi/util").template
local SOURCE_ID = "ext.weread"
local SOURCE_ID = "weread"
local Setting = {}
@@ -11,7 +11,7 @@
local Paths = require("utils.paths")
local _ = require("l10n").get("moonsource_weread")
local SOURCE_ID = "ext.weread"
local SOURCE_ID = "weread"
local ShelfImport = {}
@@ -51,7 +51,7 @@ Source.__index = Source
--- 构造微信读书源实例。
---@return WechatSource
function WeChat.new()
local cfg = require("utils.settings").getSource("ext.weread")
local cfg = require("utils.settings").getSource("weread")
local meta = WeChat.meta()
---@type WechatSource
return setmetatable({
@@ -945,7 +945,7 @@ end
local function openSerial(source_id, stable_id)
local book = require("book.books_db").get(source_id, stable_id)
local meta = book and book.meta
local data = meta and meta.source and meta.source["ext.weread"]
local data = meta and meta.source and meta.source["weread"]
local pay = tonumber(data and data.pay_type or meta and meta.weread_pay) or 0
local typ = tonumber(data and data.book_type or meta and meta.weread_type)
local ended = data and data.ended
+3 -3
View File
@@ -118,7 +118,7 @@ end
-- 拉不到时去掉坏相对 link,注入 .content 兜底缩进(否则段首顶格)。
do
local Paths = require("utils.paths")
local work_dir = Paths.bookWorkDir("book-3", "ext.weread")
local work_dir = Paths.bookWorkDir("book-3", "weread")
os.remove(work_dir .. "/stylesheets.css")
local head = '<?xml version="1.0"?><html><head><title></title>'
local link = '<link href="../Styles/stylesheets.css" rel="stylesheet" type="text/css" />'
@@ -178,7 +178,7 @@ end
-- 章首页:body.bgimg-1 的开头出血图丢掉,连续 h1 包进整页容器;样式表里 ../Images/ 改指工作目录 images/
do
local Paths = require("utils.paths")
local work_dir = Paths.bookWorkDir("book-4", "ext.weread")
local work_dir = Paths.bookWorkDir("book-4", "weread")
Paths.ensureDir(work_dir)
os.remove(work_dir .. "/stylesheets.css")
local src = '<html><head><link href="../Styles/stylesheets.css" rel="stylesheet" /></head>'
@@ -224,7 +224,7 @@ do
local icon = out:match('src="(images/[0-9a-f]+%.png)"')
Assert.is_true(icon ~= nil, "脚注图标落地: " .. case.src)
Assert.is_nil(out:find(case.src, 1, true), "原路径被改写: " .. case.src)
local work_dir = Paths.bookWorkDir(case.book, "ext.weread")
local work_dir = Paths.bookWorkDir(case.book, "weread")
local f = assert(io.open(work_dir .. "/" .. icon, "rb"))
Assert.eq(f:read(4), "\137PNG")
f:close()
+1 -1
View File
@@ -522,7 +522,7 @@ do
-- 普通接口照旧判失效
auth.webGetAsync("https://weread.qq.com/web/shelf/sync", nil, function(_d, e) err = e end)
Assert.matches(err, "扫一扫")
Assert.eq(table.concat(expired, ","), "ext.weread")
Assert.eq(table.concat(expired, ","), "weread")
end
package.loaded["source.auth"] = real_source_auth
+9 -9
View File
@@ -68,9 +68,9 @@ end
package.loaded["source.registry"] = {
resolve = function(id)
if id == "ext.weread" then
if id == "weread" then
return {
id = "ext.weread",
id = "weread",
cleanAnnotations = wechat_notes.cleanAnnotations,
prepareLocalAnnotations = function(_, prev, cur)
return wechat_notes.prepareLocalAnnotations(prev, cur)
@@ -150,10 +150,10 @@ Assert.is_nil(got[1].note)
-- 微信:删除带 bookmark id 的划线必须留下 wr_deleted 墓碑,不能只从快照抹掉。
local wr = item({ wr_bookmark_id = "bm-1", wr_range = "10-20", text = "微信句子" })
save("ext.weread", "wr-book", 3, { wr })
local wr_key = Note.itemKey("ext.weread", "wr-book", 3, wr)
save("weread", "wr-book", 3, { wr })
local wr_key = Note.itemKey("weread", "wr-book", 3, wr)
Assert.is_true(Note.mutate({ key = wr_key, remove = true }))
local wr_items = Normalize.decode(NoteDB.get("ext.weread", "wr-book", 3).payload)
local wr_items = Normalize.decode(NoteDB.get("weread", "wr-book", 3).payload)
local tombs, visible = 0, 0
for _idx, it in ipairs(wr_items) do
if it.wr_deleted then
@@ -186,10 +186,10 @@ do
local Registry = require("source.registry")
local resolve = Registry.resolve
local pending = { datetime = wr.datetime, wr_bookmark_id = "pending", wr_deleted = true }
save("ext.weread", "unavailable", 0, { wr, pending }, true)
local before = NoteDB.get("ext.weread", "unavailable", 0)
save("weread", "unavailable", 0, { wr, pending }, true)
local before = NoteDB.get("weread", "unavailable", 0)
Registry.resolve = function() return nil, "source unavailable" end
local unavailable_key = Note.itemKey("ext.weread", "unavailable", 0, wr)
local unavailable_key = Note.itemKey("weread", "unavailable", 0, wr)
for _idx, opts in ipairs({
{ key = unavailable_key, note = "不能保存" },
{ key = unavailable_key, remove = true },
@@ -197,7 +197,7 @@ do
local changed, change_err = Note.mutate(opts)
Assert.is_false(changed)
Assert.eq(change_err, "source unavailable")
local after = NoteDB.get("ext.weread", "unavailable", 0)
local after = NoteDB.get("weread", "unavailable", 0)
Assert.eq(after.payload, before.payload)
Assert.eq(after.updated_at, before.updated_at)
Assert.eq(after.sync_status, before.sync_status)
+4 -4
View File
@@ -865,15 +865,15 @@ do -- 微信同文异位的离线笔记不能在开书时丢失,也不能落
drawer = "lighten", text = "重复原文", note = "想法二" },
}
local wechat = {
id = "ext.weread",
id = "weread",
cleanAnnotations = function(_self, items, total) return Notes.cleanAnnotations(items, total) end,
mergeAnnotations = function(_self, remote, live, paging, authoritative)
return Notes.mergeAnnotations(remote, live, paging, authoritative)
end,
}
local ref = { source_id = "ext.weread", stable_id = "offline-notes", source = wechat }
local ref = { source_id = "weread", stable_id = "offline-notes", source = wechat }
open_files["/offline-notes.epub"] = ref
rows["ext.weread:offline-notes:0"] = { payload = JSON.encode({}), updated_at = 1, sync_status = 0 }
rows["weread:offline-notes:0"] = { payload = JSON.encode({}), updated_at = 1, sync_status = 0 }
local flushed = 0
local reader = {
document = { file = "/offline-notes.epub", getPageCount = function() return 10 end },
@@ -888,7 +888,7 @@ do -- 微信同文异位的离线笔记不能在开书时丢失,也不能落
Assert.eq(current[2].note, "想法二")
Assert.eq(flushed, 0, "不能把误判为重复的丢线结果刷新到 sidecar")
SessionNote.save(reader, ref)
local stored = JSON.decode(rows["ext.weread:offline-notes:0"].payload)
local stored = JSON.decode(rows["weread:offline-notes:0"].payload)
Assert.len(stored, 2)
Assert.eq(stored[1].note, "想法一")
Assert.eq(stored[2].note, "想法二")
+26 -26
View File
@@ -49,10 +49,10 @@ end
--- 走真实入库路径:mapper → replaceSynced。
local function pull(annual_days, books, fetched_at)
local result = WStats.fromWires("ext.weread", {},
local result = WStats.fromWires("weread", {},
annual_days and { { baseTime = D0, dailyReadTimes = annual_days } } or {},
nil, books, fetched_at)
Assert.not_nil(StatsDB.replaceSynced("ext.weread", result.replace, result.rows))
Assert.not_nil(StatsDB.replaceSynced("weread", result.replace, result.rows))
end
local function byYmd(rows)
@@ -62,13 +62,13 @@ local function byYmd(rows)
end
-- 本地:D0 已上传 100(快照前)、D1 未上传 50、D1 快照后已上传 30
page("ext.weread", "42", D0 + 3600, 100, true)
page("ext.weread", "42", D1 + 3600, 50, false)
page("ext.weread", "42", T1 + 3600, 30, true)
page("weread", "42", D0 + 3600, 100, true)
page("weread", "42", D1 + 3600, 50, false)
page("weread", "42", T1 + 3600, 30, true)
-- 无云端:全本地
Assert.eq(StatsDB.summaryByBook("ext.weread", "42").total_seconds, 180)
Assert.eq(byYmd(StatsDB.dailyByBook("ext.weread", "42", 30))[YMD0], 100)
Assert.eq(StatsDB.summaryByBook("weread", "42").total_seconds, 180)
Assert.eq(byYmd(StatsDB.dailyByBook("weread", "42", 30))[YMD0], 100)
pull({ [tostring(D1)] = 195 }, {
{ id = "42", reading_time = 2578, wire = { readDetail = { data = {
@@ -80,23 +80,23 @@ pull({ [tostring(D1)] = 195 }, {
}, T1)
-- 单书累计:快照 + 未上传 50 + 快照后 30;快照前已上传的 100 已含在快照里
Assert.eq(StatsDB.summaryByBook("ext.weread", "42").total_seconds, 2578 + 50 + 30)
Assert.eq(StatsDB.summaryByBook("ext.weread", "42").pages, 3, "页数只数本地逐页记录")
Assert.eq(StatsDB.summaryByBook("ext.weread", "phone_only").total_seconds, 300)
Assert.eq(StatsDB.summaryByBook("weread", "42").total_seconds, 2578 + 50 + 30)
Assert.eq(StatsDB.summaryByBook("weread", "42").pages, 3, "页数只数本地逐页记录")
Assert.eq(StatsDB.summaryByBook("weread", "phone_only").total_seconds, 300)
-- 单书按日:D0 以云端 440 为准(本地 100 已含),D1 = 134 + 50 + 30
local daily = StatsDB.dailyByBook("ext.weread", "42", 30)
local daily = StatsDB.dailyByBook("weread", "42", 30)
Assert.len(daily, 2)
Assert.eq(daily[1].ymd, YMD1, "最近在前")
Assert.eq(daily[1].seconds, 214)
Assert.eq(daily[2].seconds, 440)
Assert.len(StatsDB.dailyByBook("ext.weread", "42", 1), 1)
Assert.eq(byYmd(StatsDB.dailyByBook("ext.weread", "phone_only", 30))[YMD0], 300,
Assert.len(StatsDB.dailyByBook("weread", "42", 1), 1)
Assert.eq(byYmd(StatsDB.dailyByBook("weread", "phone_only", 30))[YMD0], 300,
"只在其他设备读过的书也有按日时长")
-- 洞察当日书单:只有云端的书页坐标为 0
local found
for _, row in ipairs(StatsDB.dailyBooksBySource("ext.weread")) do
for _, row in ipairs(StatsDB.dailyBooksBySource("weread")) do
if row.stable_id == "phone_only" then found = row end
end
Assert.eq(found.ymd, YMD0)
@@ -104,17 +104,17 @@ Assert.eq(found.seconds, 300)
Assert.eq(found.max_total_pages, 0)
-- 账单:书单与书数含其他设备读过的书
local books = StatsDB.periodBooks("ext.weread", D0, D1 + 86400, 5)
local books = StatsDB.periodBooks("weread", D0, D1 + 86400, 5)
Assert.len(books, 2)
Assert.eq(books[1].stable_id, "42")
Assert.eq(books[1].seconds, 440 + 214)
Assert.eq(books[2].stable_id, "phone_only")
local period = StatsDB.periodSummary("ext.weread", D0, D1 + 86400)
local period = StatsDB.periodSummary("weread", D0, D1 + 86400)
Assert.eq(period.book_count, 2)
Assert.eq(period.pages, 3)
-- 账户日历:D1 云端 195 + 拉取后本地新读 80;D0 无云端日桶 → 本地 100
local account = byYmd(StatsDB.dailyBySource("ext.weread"))
local account = byYmd(StatsDB.dailyBySource("weread"))
Assert.eq(account[YMD1], 195 + 50 + 30)
Assert.eq(account[YMD0], 100)
Assert.eq(period.total_seconds, 100 + 275)
@@ -124,21 +124,21 @@ local T2 = D1 + 14 * 3600
pull(nil, { { id = "42", reading_time = 2700, wire = { readDetail = { data = {
{ readDate = D1, readTime = 500 },
} } } } }, T2)
Assert.eq(StatsDB.summaryByBook("ext.weread", "42").total_seconds, 2700 + 50)
local again = byYmd(StatsDB.dailyByBook("ext.weread", "42", 30))
Assert.eq(StatsDB.summaryByBook("weread", "42").total_seconds, 2700 + 50)
local again = byYmd(StatsDB.dailyByBook("weread", "42", 30))
Assert.eq(again[YMD1], 500 + 50)
Assert.eq(again[YMD0], 100, "旧云端日明细被整本替换掉,回落本地")
Assert.eq(StatsDB.summaryByBook("ext.weread", "phone_only").total_seconds, 300, "其他书不受影响")
Assert.eq(StatsDB.summaryByBook("weread", "phone_only").total_seconds, 300, "其他书不受影响")
-- 按书精确替换:书 id 互为前缀也不误删
pull(nil, { { id = "4", reading_time = 60, wire = {} } }, T2)
Assert.eq(StatsDB.summaryByBook("ext.weread", "42").total_seconds, 2750)
Assert.eq(StatsDB.summaryByBook("ext.weread", "4").total_seconds, 60)
Assert.eq(StatsDB.summaryByBook("weread", "42").total_seconds, 2750)
Assert.eq(StatsDB.summaryByBook("weread", "4").total_seconds, 60)
-- 云端累计清零:旧快照与明细一并清掉,回落本地
pull(nil, { { id = "phone_only", reading_time = 0, wire = {} } }, T2)
Assert.eq(StatsDB.summaryByBook("ext.weread", "phone_only").total_seconds, 0)
Assert.len(StatsDB.dailyByBook("ext.weread", "phone_only", 30), 0)
Assert.eq(StatsDB.summaryByBook("weread", "phone_only").total_seconds, 0)
Assert.len(StatsDB.dailyByBook("weread", "phone_only", 30), 0)
-- 旧版拉取的日桶没有 last_time:只补未上传的,已上传的不再加(宁可少算不双计)
Assert.is_true(StatsDB.add({
@@ -151,8 +151,8 @@ Assert.eq(byYmd(StatsDB.dailyBySource("legacy"))[YMD1], 620)
-- 账户级统计拉取的区间清理(只删 day/book/total)不碰单书明细
pull({ [tostring(D1)] = 195 }, nil, T2)
Assert.eq(StatsDB.summaryByBook("ext.weread", "42").total_seconds, 2750)
Assert.eq(byYmd(StatsDB.dailyByBook("ext.weread", "42", 30))[YMD1], 550)
Assert.eq(StatsDB.summaryByBook("weread", "42").total_seconds, 2750)
Assert.eq(byYmd(StatsDB.dailyByBook("weread", "42", 30))[YMD1], 550)
Base.close()
for _, suffix in ipairs({ "", "-wal", "-shm" }) do os.remove(db_path .. suffix) end
+9 -9
View File
@@ -39,7 +39,7 @@ local PREV = DAY - 86400
local FETCHED = DAY + 22 * 3600
local function page(start_time, duration)
Assert.is_true(StatsDB.add({ source_id = "ext.weread", stable_id = "b1", record_type = "page",
Assert.is_true(StatsDB.add({ source_id = "weread", stable_id = "b1", record_type = "page",
page = 1, start_time = start_time, duration = duration, total_pages = 9, chapter_idx = 1 }, StatsDB.SYNCED))
end
@@ -47,19 +47,19 @@ end
page(DAY + 9 * 3600, 5700)
page(DAY + 14 * 3600, 7200)
page(PREV + 10 * 3600, 600)
local result = WStats.fromWires("ext.weread", { totalReadTime = 5700 }, {},
local result = WStats.fromWires("weread", { totalReadTime = 5700 }, {},
{ { readTimes = { [tostring(DAY)] = 5700 } } }, {}, FETCHED)
Assert.not_nil(StatsDB.replaceSynced("ext.weread", result.replace, result.rows))
Assert.not_nil(StatsDB.replaceSynced("weread", result.replace, result.rows))
page(FETCHED + 3600, 100)
local function day(ymd)
for _idx, row in ipairs(StatsDB.dailyBySource("ext.weread")) do
for _idx, row in ipairs(StatsDB.dailyBySource("weread")) do
if row.ymd == ymd then return row.seconds end
end
end
-- 云端优先(默认):有日桶的日期 = 云端 + 拉取后新读;总时长 = 云端 total + 拉取后新读。
Assert.eq(StatsDB.summaryBySource("ext.weread").total_seconds, 5700 + 100)
Assert.eq(StatsDB.summaryBySource("weread").total_seconds, 5700 + 100)
Assert.eq(day("2026-10-10"), 5700 + 100)
Assert.eq(day("2026-10-09"), 600, "没有云端日桶的日期本来就用本地")
@@ -67,7 +67,7 @@ Assert.eq(day("2026-10-09"), 600, "没有云端日桶的日期本来就用本地
cfg.merge_local_stats = true
Assert.eq(day("2026-10-10"), 5700 + 7200 + 100)
Assert.eq(day("2026-10-09"), 600)
Assert.eq(StatsDB.summaryBySource("ext.weread").total_seconds, 5700 + 7200 + 600 + 100)
Assert.eq(StatsDB.summaryBySource("weread").total_seconds, 5700 + 7200 + 600 + 100)
-- 云端比本地多(挂机不翻页心跳照报)时仍用云端,不往下压。
cfg.merge_local_stats = true
@@ -79,7 +79,7 @@ Assert.eq(day("2026-10-10"), 20000 + 100)
-- (云端没有这一格)、22 点拉取后新读 300s(补在云端格上)。
local H = os.time({ year = 2026, month = 10, day = 10, hour = 0 })
local function cloudHour(hour, duration)
Assert.is_true(StatsDB.add({ source_id = "ext.weread", stable_id = "__wx:hour:" .. (H + hour * 3600),
Assert.is_true(StatsDB.add({ source_id = "weread", stable_id = "__wx:hour:" .. (H + hour * 3600),
record_type = "hour", page = 0, start_time = H + hour * 3600, duration = duration,
total_pages = 0, last_time = FETCHED }, StatsDB.SYNCED))
end
@@ -90,12 +90,12 @@ page(H + 7 * 3600 + 1800, 300) -- 云端没有这一格(离线读)
page(FETCHED + 300, 300) -- 拉取之后才读的 22:05
cfg.merge_local_stats = false
local by_hour = StatsDB.hourlyBySource("ext.weread")["2026-10-10"]
local by_hour = StatsDB.hourlyBySource("weread")["2026-10-10"]
Assert.eq(by_hour[6], 600, "05 点:云端 600,本机已上报的 900 不叠加")
Assert.eq(by_hour[8], 300, "07 点:没有云端格,用本机离线 300")
Assert.eq(by_hour[23], 700, "22 点:云端 400 + 拉取后新读 300")
cfg.merge_local_stats = true
by_hour = StatsDB.hourlyBySource("ext.weread")["2026-10-10"]
by_hour = StatsDB.hourlyBySource("weread")["2026-10-10"]
Assert.eq(by_hour[6], 900, "合并打开:05 点取本机 900")
Assert.eq(by_hour[8], 300, "合并打开:07 点仍是本机 300")
Assert.eq(by_hour[23], 700, "合并打开:22 点云端 + 增量比本机大,不往下压")
+9 -9
View File
@@ -37,42 +37,42 @@ local FETCHED = os.time() - 3600
local ID = "book-1"
local function pull(total, fetched_at)
local result = WStats.fromWires("ext.weread", { totalReadTime = total }, {}, {}, {}, fetched_at)
Assert.not_nil(StatsDB.replaceSynced("ext.weread", result.replace, result.rows))
local result = WStats.fromWires("weread", { totalReadTime = total }, {}, {}, {}, fetched_at)
Assert.not_nil(StatsDB.replaceSynced("weread", result.replace, result.rows))
end
local function page(start_time, duration, synced)
Assert.is_true(StatsDB.add({ source_id = "ext.weread", stable_id = ID, record_type = "page",
Assert.is_true(StatsDB.add({ source_id = "weread", stable_id = ID, record_type = "page",
page = 1, start_time = start_time, duration = duration, total_pages = 9, chapter_idx = 1 }, synced and StatsDB.SYNCED or StatsDB.PENDING))
end
-- 拉取前已上传的已含在快照里;拉取后读的(已上传或未上传)都要补上。
page(FETCHED - 600, 50, true)
pull(7200, FETCHED)
Assert.eq(StatsDB.summaryBySource("ext.weread").total_seconds, 7200)
Assert.eq(StatsDB.summaryBySource("weread").total_seconds, 7200)
page(FETCHED + 60, 30, false)
page(FETCHED + 120, 40, true)
Assert.eq(StatsDB.summaryBySource("ext.weread").total_seconds, 7200 + 30 + 40, "总时长随本地新读增长")
Assert.eq(StatsDB.summaryBySource("weread").total_seconds, 7200 + 30 + 40, "总时长随本地新读增长")
-- 先推后拉:新快照已含之前读的,替换旧 total、不累加。
Assert.is_true(Base.exec("UPDATE reading_stats SET sync_status=1 WHERE record_type='page';") ~= nil)
pull(7300, FETCHED + 600)
Assert.eq(StatsDB.summaryBySource("ext.weread").total_seconds, 7300)
Assert.eq(StatsDB.summaryBySource("weread").total_seconds, 7300)
-- 旧版拉取没记拉取时间:只补未上传的,宁可少算也不双计。
Assert.is_true(Base.exec("UPDATE reading_stats SET last_time=0 WHERE record_type='total';") ~= nil)
page(FETCHED + 900, 20, false)
Assert.eq(StatsDB.summaryBySource("ext.weread").total_seconds, 7300 + 20)
Assert.eq(StatsDB.summaryBySource("weread").total_seconds, 7300 + 20)
-- 清除列表:源含合成桶;书只留真实 stable_id,已删/未上架没有标题。
local sources = StatsDB.distinctSources()
Assert.eq(sources[1], "ext.weread")
Assert.eq(sources[1], "weread")
local recorded, seen = StatsDB.distinctBooks(), {}
for _idx, book in ipairs(recorded) do
Assert.is_true(book.stable_id:sub(1, 2) ~= "__", book.stable_id)
seen[book.stable_id] = book
end
Assert.eq(seen[ID].source_id, "ext.weread")
Assert.eq(seen[ID].source_id, "weread")
Assert.is_nil(seen[ID].title)
Base.close()
+1 -1
View File
@@ -12,7 +12,7 @@ local sha256 = require("ffi/sha2").sha256
local state = { cfg = {}, requests = {}, responder = nil }
local switches = 0
require("source.logout").replaceLibrary = function(id, commit)
Assert.eq(id, "ext.weread")
Assert.eq(id, "weread")
switches = switches + 1
commit()
return true
+14 -14
View File
@@ -27,7 +27,7 @@ do
cover = "http://x/c.jpg",
progressPercent = 42,
})
Assert.eq(b.source_id, "ext.weread")
Assert.eq(b.source_id, "weread")
Assert.eq(b.stable_id, "wx-1")
Assert.eq(b.title, "三体")
Assert.eq(b.percent, 42)
@@ -61,9 +61,9 @@ end
do
local b = Mapper.book({ bookId = "1", finished = 1, progress = 20, payType = 2, type = 0 })
Assert.eq(b.percent, 20)
Assert.eq(b.meta.source["ext.weread"].pay_type, 2)
Assert.eq(b.meta.source["ext.weread"].book_type, 0)
Assert.eq(b.meta.source["ext.weread"].ended, true)
Assert.eq(b.meta.source["weread"].pay_type, 2)
Assert.eq(b.meta.source["weread"].book_type, 0)
Assert.eq(b.meta.source["weread"].ended, true)
end
do
@@ -97,9 +97,9 @@ end
do
local Settings = require("utils.settings")
local cfg = Settings.getSource("ext.weread")
local cfg = Settings.getSource("weread")
cfg.category_as_group = true
Settings.saveSource("ext.weread", cfg)
Settings.saveSource("weread", cfg)
local list = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
@@ -115,7 +115,7 @@ do
Assert.is_true(list.data[1].category_authoritative)
Assert.is_true(list.data[2].category_authoritative, "没进组也是这次快照的分类口径")
cfg.category_as_group = false
Settings.saveSource("ext.weread", cfg)
Settings.saveSource("weread", cfg)
local genre = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
@@ -298,7 +298,7 @@ do
cover = "http://x/c.jpg",
progressPercent = 42,
})
Assert.eq(b.source_id, "ext.weread")
Assert.eq(b.source_id, "weread")
Assert.eq(b.stable_id, "wx-1")
Assert.eq(b.title, "三体")
Assert.eq(b.percent, 42)
@@ -332,9 +332,9 @@ end
do
local b = Mapper.book({ bookId = "1", finished = 1, progress = 20, payType = 2, type = 0 })
Assert.eq(b.percent, 20)
Assert.eq(b.meta.source["ext.weread"].pay_type, 2)
Assert.eq(b.meta.source["ext.weread"].book_type, 0)
Assert.eq(b.meta.source["ext.weread"].ended, true)
Assert.eq(b.meta.source["weread"].pay_type, 2)
Assert.eq(b.meta.source["weread"].book_type, 0)
Assert.eq(b.meta.source["weread"].ended, true)
end
do
@@ -368,9 +368,9 @@ end
do
local Settings = require("utils.settings")
local cfg = Settings.getSource("ext.weread")
local cfg = Settings.getSource("weread")
cfg.category_as_group = true
Settings.saveSource("ext.weread", cfg)
Settings.saveSource("weread", cfg)
local list = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
@@ -386,7 +386,7 @@ do
Assert.is_true(list.data[1].category_authoritative)
Assert.is_true(list.data[2].category_authoritative, "没进组也是这次快照的分类口径")
cfg.category_as_group = false
Settings.saveSource("ext.weread", cfg)
Settings.saveSource("weread", cfg)
local genre = Mapper.shelfList({
books = {
{ bookId = "1", title = "A", category = "官方" },
+2 -2
View File
@@ -316,9 +316,9 @@ end
do
local removed = {}
local n = Mp.dropLegacyBooks({
stableIdsBySource = function(source) Assert.eq(source, "ext.weread"); return { "100", "MP_WXS_1", "MP_WXS_2" } end,
stableIdsBySource = function(source) Assert.eq(source, "weread"); return { "100", "MP_WXS_1", "MP_WXS_2" } end,
remove = function(source, id) removed[#removed + 1] = source .. ":" .. id end,
})
Assert.eq(n, 2)
Assert.eq(table.concat(removed, ","), "ext.weread:MP_WXS_1,ext.weread:MP_WXS_2")
Assert.eq(table.concat(removed, ","), "weread:MP_WXS_1,weread:MP_WXS_2")
end
+1 -1
View File
@@ -35,7 +35,7 @@ local function fakeClient(replies)
return client
end
local identity = { source_id = "ext.weread", stable_id = "b1", chapter_idx = 1 }
local identity = { source_id = "weread", stable_id = "b1", chapter_idx = 1 }
do -- 删除墓碑:先删想法再删划线,删完从注解里移除,后续无事可做即成功
local client = fakeClient({ bookmarkListAsync = { updated = {} }, myReviewsAsync = { reviews = {} } })
+2 -2
View File
@@ -20,7 +20,7 @@ package.preload["moonsource_weread.chapter"] = function()
end
local Position = require("moonsource_weread.progress")
local Paths = require("utils.paths")
local identity = { source_id = "ext.weread", stable_id = "progress-coordinate-spec" }
local identity = { source_id = "weread", stable_id = "progress-coordinate-spec" }
local chapter = { idx = 1, uid = "old", title = "章名" }
local path = Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/1.position.json"
local local_path = Paths.chapterPath(identity.stable_id, 1, identity.source_id)
@@ -93,7 +93,7 @@ do
.. '<div><img class="qqreader-fullimg" src="a.jpg"/></div>'
.. '<div><img class="qqreader-fullimg" src="b.jpg"/></div>'
.. '</body></html>'
local comic_id = { source_id = "ext.weread", stable_id = "progress-comic-spec" }
local comic_id = { source_id = "weread", stable_id = "progress-comic-spec" }
local comic_chapter = { idx = 4, uid = "29", title = "向日葵花园" }
local comic_local = Paths.chapterPath(comic_id.stable_id, 4, comic_id.source_id)
Paths.ensureDir(comic_local:match("(.+)/[^/]+$"))
+1 -1
View File
@@ -57,7 +57,7 @@ local ids = ShelfImport.ingest({
archive = { { name = "分组", bookIds = { "b1" } } },
})
Assert.len(ids, 2)
Assert.eq(upserted[1].source_id, "ext.weread")
Assert.eq(upserted[1].source_id, "weread")
Assert.eq(upserted[1].deleted, 0)
Assert.eq(upserted[1].percent, 50)
+3 -3
View File
@@ -4,7 +4,7 @@ local Assert = require("support.assert")
local Stats = require("moonsource_weread.stats")
do
local result = Stats.fromWires("ext.weread", {
local result = Stats.fromWires("weread", {
totalReadTime = 7200,
}, { {
baseTime = 1704067200,
@@ -54,7 +54,7 @@ do
Assert.len(bases, 1)
Assert.eq(bases[1], january)
local result = Stats.fromWires("ext.weread", {
local result = Stats.fromWires("weread", {
totalReadTime = 1200,
}, { annual }, { {
readTimes = {
@@ -82,7 +82,7 @@ do
-- 云端清零(缺 readingTime)而本地有快照:也算变化,要去清掉旧明细
local cleared = Stats.changedBooks({ { bookId = "gone" } }, { gone = 10 }, 20)
Assert.eq(cleared[1].reading_time, 0)
local rows = Stats.fromWires("ext.weread", {}, {}, nil, {
local rows = Stats.fromWires("weread", {}, {}, nil, {
{ id = "gone", reading_time = 0, wire = {} },
}, 1000).rows
Assert.len(rows, 0, "累计为 0 不写快照,靠 replace.books 清旧行")
+3 -3
View File
@@ -101,7 +101,7 @@ do
},
},
chapters = { { title = "课堂解答", chapterUid = 4, chapterIdx = 4 } },
}, nil, nil, "ext.weread", "820954")
}, nil, nil, "weread", "820954")
Assert.eq(#anns, 1)
Assert.eq(anns[1].chapter_idx, 4)
Assert.eq(anns[1].wr_range, "303-332")
@@ -128,13 +128,13 @@ do
updated = {
{ chapterUid = "8", chapterIdx = 8, markText = "x", range = "0-1", type = 1, createTime = 1 },
},
}, nil, nil, "ext.weread", "1")[1].chapter_idx)
}, 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, "ext.weread", "1")[1].chapter_idx)
}, nil, nil, "weread", "1")[1].chapter_idx)
end
--- 单条划线走 locateBatch,返回 pos0, pos1;定位不到返回 nil。
+29 -29
View File
@@ -46,8 +46,8 @@ require("book.toc_db").read = function(...)
end
do
Assert.is_nil(Toc.read("ext.weread", "missing"))
Assert.is_nil(Toc.read("ext.weread", "broken"))
Assert.is_nil(Toc.read("weread", "missing"))
Assert.is_nil(Toc.read("weread", "broken"))
end
do
@@ -55,19 +55,19 @@ do
store["b2"] = { payload = [[
[{"idx":1,"source_idx":2,"uid":"u1","toc_version":3},{"idx":2,"source_idx":4,"uid":"u2"}]
]], at = now }
Assert.eq(Toc.uid("ext.weread", "b2", 1), "u1")
Assert.eq(Toc.uid("ext.weread", "b2", 2), "u2")
Assert.is_nil(Toc.uid("ext.weread", "b2", 99))
Assert.eq(Toc.index("ext.weread", "b2", "u2"), 2)
Assert.is_nil(Toc.index("ext.weread", "b2", "u9"))
Assert.eq(Toc.sourceIndex("ext.weread", "b2", 1), 2)
Assert.eq(Toc.sourceIndex("ext.weread", "b2", 2), 4)
Assert.is_nil(Toc.sourceIndex("ext.weread", "b2", 99))
Assert.eq(Toc.uid("weread", "b2", 1), "u1")
Assert.eq(Toc.uid("weread", "b2", 2), "u2")
Assert.is_nil(Toc.uid("weread", "b2", 99))
Assert.eq(Toc.index("weread", "b2", "u2"), 2)
Assert.is_nil(Toc.index("weread", "b2", "u9"))
Assert.eq(Toc.sourceIndex("weread", "b2", 1), 2)
Assert.eq(Toc.sourceIndex("weread", "b2", 2), 4)
Assert.is_nil(Toc.sourceIndex("weread", "b2", 99))
end
do
store["old"] = { payload = [[{"idx":1,"source_idx":2,"uid":"old"}]], at = now }
local old = Toc.read("ext.weread", "old")
local old = Toc.read("weread", "old")
Assert.not_nil(old, "旧目录仍须支持离线读取")
Assert.is_false(Toc.isCurrent(old))
end
@@ -76,53 +76,53 @@ do
-- 解码结果进内存:库里的行没了也照样命中,翻页/按章补报不再反复 decode 整份目录。
local before = reads
store["b2"] = nil
require("utils.db").removeIdentity("toc_entries", "ext.weread", "b2")
require("utils.db").removeIdentity("toc_snapshots", "ext.weread", "b2")
Assert.eq(Toc.uid("ext.weread", "b2", 1), "u1")
Assert.eq(Toc.index("ext.weread", "b2", "u2"), 2)
require("utils.db").removeIdentity("toc_entries", "weread", "b2")
require("utils.db").removeIdentity("toc_snapshots", "weread", "b2")
Assert.eq(Toc.uid("weread", "b2", 1), "u1")
Assert.eq(Toc.index("weread", "b2", "u2"), 2)
Assert.eq(reads, before, "命中内存缓存时不得再查库")
-- clear 后回落到库(此时库里已无数据)
Toc.clear()
Assert.is_nil(Toc.read("ext.weread", "b2"))
Assert.is_nil(Toc.read("weread", "b2"))
Assert.is_true(reads > before)
end
do
-- put 落库并直接接管缓存,无需再 decode 一遍
local list = { { idx = 1, uid = "p1" }, { idx = 2, uid = "p2" }, { idx = 3, uid = "p3" } }
Toc.put("ext.weread", "b3", list)
Toc.put("weread", "b3", list)
Assert.is_nil(upserted["b3"], "不再写旧 JSON 列")
local before = reads
Assert.eq(Toc.uid("ext.weread", "b3", 3), "p3")
Assert.eq(Toc.index("ext.weread", "b3", "p2"), 2)
Assert.eq(Toc.uid("weread", "b3", 3), "p3")
Assert.eq(Toc.index("weread", "b3", "p2"), 2)
Assert.eq(reads, before)
-- 章内进度 + 章节序号 → 全书 fraction
Assert.eq(Toc.wholeFraction("ext.weread", "b3", 1, 0), 0)
Assert.eq(Toc.wholeFraction("ext.weread", "b3", 2, 0.5), 0.5)
Assert.eq(Toc.wholeFraction("ext.weread", "b3", 3, 1), 1)
Assert.is_nil(Toc.wholeFraction("ext.weread", "unknown", 1, 0))
Assert.eq(Toc.wholeFraction("weread", "b3", 1, 0), 0)
Assert.eq(Toc.wholeFraction("weread", "b3", 2, 0.5), 0.5)
Assert.eq(Toc.wholeFraction("weread", "b3", 3, 1), 1)
Assert.is_nil(Toc.wholeFraction("weread", "unknown", 1, 0))
local cached_reads = reads
Assert.eq(Toc.uid("ext.weread", "b3", 1), "p1")
Assert.eq(Toc.uid("weread", "b3", 1), "p1")
Assert.eq(reads, cached_reads)
Toc.clear()
Toc.read("ext.weread", "b3")
Toc.read("weread", "b3")
Assert.is_true(reads > cached_reads, "clear 后必须回库")
end
do
local list = { { idx = 1, uid = "failed" } }
writes = false
Assert.is_false(Toc.put("ext.weread", "failed", list))
Assert.is_nil(Toc.read("ext.weread", "failed"))
Assert.is_false(Toc.put("weread", "failed", list))
Assert.is_nil(Toc.read("weread", "failed"))
writes = true
-- 清内存后直接从逐章表恢复。
Toc.clear()
Assert.eq(Toc.read("ext.weread", "b3")[1].uid, "p1")
Assert.eq(Toc.read("weread", "b3")[1].uid, "p1")
now = now + 6 * 60 * 60
Assert.eq(Toc.read("ext.weread", "b3")[1].uid, "p1", "目录不做时间保鲜,旧缓存仍可用")
Assert.eq(Toc.read("weread", "b3")[1].uid, "p1", "目录不做时间保鲜,旧缓存仍可用")
end
os.time = original_time
+33 -33
View File
@@ -188,7 +188,7 @@ end)
package.loaded["moonsource_weread.source"] = nil
local WeChat = require("moonsource_weread.source")
local REF = { source_id = "ext.weread", stable_id = "b1", book = { title = "微信书" } }
local REF = { source_id = "weread", stable_id = "b1", book = { title = "微信书" } }
do
local caps = WeChat.new():capabilities()
@@ -254,7 +254,7 @@ do
local StatsDB = require("book.stats_db")
local real_book_totals = StatsDB.bookTotals
StatsDB.bookTotals = function(source_id)
Assert.eq(source_id, "ext.weread")
Assert.eq(source_id, "weread")
return { same = 500, broken = 90 }
end
local result
@@ -396,7 +396,7 @@ do
local Session = require("reader.session")
local orig_current = Session.current
Session.current = function()
return { identity = { source_id = "ext.weread", stable_id = "bp" } }
return { identity = { source_id = "weread", stable_id = "bp" } }
end
ProgressDB.get = orig_get
posted = {}
@@ -430,7 +430,7 @@ do
stub("book.stats", function()
return { push = function(source) stats_pushes[#stats_pushes + 1] = source end }
end)
local identity = { source_id = "ext.weread", stable_id = "bp", chapter_idx = 1 }
local identity = { source_id = "weread", stable_id = "bp", chapter_idx = 1 }
src:onEvent("chapter_changed", {
identity = identity,
position = { fraction = 0.1, chapter_idx = 1, chapter_fraction = 0.2 },
@@ -489,7 +489,7 @@ do
cb({ book = { chapterUid = 33, chapterIdx = 9, chapterOffset = 120, progress = 40 } })
return { cancel = function() end }
end
local identity = { source_id = "ext.weread", stable_id = "bp", chapter_idx = 1 }
local identity = { source_id = "weread", stable_id = "bp", chapter_idx = 1 }
Position.loadAsync = function(_identity, _chapter, cb) cb(nil, "offline") end
local pos, err
@@ -670,7 +670,7 @@ do
BookDB.get = function()
return { meta = {
weread_ended = true,
source = { ["ext.weread"] = { pay_type = 2, ended = false } },
source = { ["weread"] = { pay_type = 2, ended = false } },
} }
end
Assert.is_true(put({ fraction = 1, extra = UID }))
@@ -750,7 +750,7 @@ do
Assert.is_nil(map_err)
Assert.eq(captured.chapter_offset, 0, "对不上且没有可用比例/偏移时按章首上报")
toc_payload["b1"] = nil
require("book.toc_db").removeIdentity("ext.weread", "b1")
require("book.toc_db").removeIdentity("weread", "b1")
require("moonsource_weread.toc").clear()
StatsDB.unsyncedBySource, StatsDB.markSynced = orig_unsynced, orig_mark
end
@@ -874,7 +874,7 @@ do
Assert.not_nil(result)
-- 合法 http(s) 封面入缓存,coverRequest 带回会话头
local req = src:coverRequest({ source_id = "ext.weread", stable_id = "b1" })
local req = src:coverRequest({ source_id = "weread", stable_id = "b1" })
Assert.not_nil(req)
Assert.eq(req.url, "https://cdn.example.com/a.jpg")
Assert.eq(req.headers.Cookie, "wr_skey=stub")
@@ -882,18 +882,18 @@ do
-- 新源实例没有内存缓存,必须从 Book/数据库恢复封面。
local fresh = WeChat.new()
local direct = fresh:coverRequest({
source_id = "ext.weread", stable_id = "b1", cover = "https://cdn.example.com/a.jpg",
source_id = "weread", stable_id = "b1", cover = "https://cdn.example.com/a.jpg",
})
Assert.eq(direct.url, "https://cdn.example.com/a.jpg")
Assert.eq(direct.headers.Cookie, "wr_skey=stub")
-- 非 http(s) 前缀的封面不入缓存
local req2, err2 = src:coverRequest({ source_id = "ext.weread", stable_id = "b2" })
local req2, err2 = src:coverRequest({ source_id = "weread", stable_id = "b2" })
Assert.is_nil(req2)
Assert.eq(err2, "无封面")
-- 无封面字段的书籍
local req3 = src:coverRequest({ source_id = "ext.weread", stable_id = "b3" })
local req3 = src:coverRequest({ source_id = "weread", stable_id = "b3" })
Assert.is_nil(req3)
-- 重复列表刷新封面缓存(后写覆盖先写)
@@ -907,12 +907,12 @@ do
end
src:syncBooksAsync(nil, function() end)
require("support.stubs").flush()
Assert.eq(src:coverRequest({ source_id = "ext.weread", stable_id = "b1" }).url,
Assert.eq(src:coverRequest({ source_id = "weread", stable_id = "b1" }).url,
"https://cdn.example.com/a2.jpg")
-- close 清空实例的封面缓存
src:close()
local req4 = src:coverRequest({ source_id = "ext.weread", stable_id = "b1" })
local req4 = src:coverRequest({ source_id = "weread", stable_id = "b1" })
Assert.is_nil(req4)
end
@@ -971,14 +971,14 @@ do
end
local annotations
WeChat.new():pullNotesAsync({
source_id = "ext.weread", stable_id = "bpull", chapter_idx = 5,
source_id = "weread", stable_id = "bpull", chapter_idx = 5,
}, function(value) annotations = value end)
require("support.stubs").flush()
Assert.eq(annotations[1].chapter_idx, 5)
-- 目录缓存没有、也拉不到:章节号留空,绝不回退成原始序号 6(本地第 6 章是别的章)。
toc_payload["bpull"] = nil
require("book.toc_db").removeIdentity("ext.weread", "bpull")
require("book.toc_db").removeIdentity("weread", "bpull")
require("source.toc").clear()
fake_client.chapterInfosAsync = function(_, _, cb)
cb(nil, "目录拉取失败")
@@ -986,7 +986,7 @@ do
end
annotations = nil
WeChat.new():pullNotesAsync({
source_id = "ext.weread", stable_id = "bpull", chapter_idx = 5,
source_id = "weread", stable_id = "bpull", chapter_idx = 5,
}, function(value) annotations = value end)
require("support.stubs").flush()
Assert.len(annotations, 1)
@@ -997,7 +997,7 @@ end
do
chapter_fetches = 0
local NOTE_REF = {
source_id = "ext.weread",
source_id = "weread",
stable_id = "bpush",
chapter_idx = 2,
book = { title = "微信书" },
@@ -1015,10 +1015,10 @@ do
chapter_range_html = "<p>旧划线</p><p>新划线</p><p>补偿划线</p>"
.. "<p>部分一</p><p>部分二</p>"
local Paths = require("utils.paths")
local chapter_path = Paths.chapterPath("bpush", 2, "ext.weread")
assert(Paths.ensureDir(Paths.bookWorkDir("bpush", "ext.weread")))
local chapter_path = Paths.chapterPath("bpush", 2, "weread")
assert(Paths.ensureDir(Paths.bookWorkDir("bpush", "weread")))
-- 沙箱跨运行保留:清掉上次留下的 range 原文缓存,首个定位必走一次拉取并回填缓存
os.remove(Paths.bookWorkDir("bpush", "ext.weread") .. "/2.position.json")
os.remove(Paths.bookWorkDir("bpush", "weread") .. "/2.position.json")
local chapter_file = assert(io.open(chapter_path, "wb"))
chapter_file:write("<html><body><h1>微信书</h1><p>旧划线</p><p>新划线</p>"
.. "<p>补偿划线</p><p>部分一</p><p>部分二</p></body></html>")
@@ -1293,13 +1293,13 @@ do
package.loaded["workers.tasks"] = { enqueue = function(spec) enqueued[#enqueued + 1] = spec.key; return spec end }
local src = WeChat.new()
local ok
src:deleteBookAsync({ source_id = "ext.weread", stable_id = "del1" }, function(success)
src:deleteBookAsync({ source_id = "weread", stable_id = "del1" }, function(success)
ok = success
end)
require("support.stubs").flush()
Assert.is_true(ok)
Assert.eq(remembered_stats_books.marked_deleted.stable_id, "del1")
Assert.eq(enqueued[1], "dirty\0ext.weread")
Assert.eq(enqueued[1], "dirty\0weread")
src:syncBooksAsync({ dirty_only = true }, function() end)
require("support.stubs").flush()
Assert.eq(removed, "del1")
@@ -1312,7 +1312,7 @@ end
-- 段评:开关读源配置;按目录解析本章 chapter_uid,章节 HTML 读本地文件定位。
do
toc_payload["b1"] = [[ [{"idx":1,"source_idx":2,"uid":"40"}] ]]
require("book.toc_db").removeIdentity("ext.weread", "b1")
require("book.toc_db").removeIdentity("weread", "b1")
local Settings = require("utils.settings")
local src = WeChat.new()
Assert.is_false(src:paragraphReviewsEnabled())
@@ -1365,7 +1365,7 @@ do
Assert.not_nil(err)
Assert.len(calls, 3)
toc_payload["b1"] = nil
require("book.toc_db").removeIdentity("ext.weread", "b1")
require("book.toc_db").removeIdentity("weread", "b1")
require("moonsource_weread.toc").clear()
-- 书名搜索:只留带 bookId 的书目。
@@ -1402,7 +1402,7 @@ do
local src = WeChat.new()
local called = false
local job = src:pushNotesAsync(
{ source_id = "ext.weread", stable_id = "bcancel", chapter_idx = 1 },
{ source_id = "weread", stable_id = "bcancel", chapter_idx = 1 },
{ { drawer = "lighten", text = "甲" } },
function() called = true end
)
@@ -1417,13 +1417,13 @@ do
-- 无事可做直接成功,不发请求。
local ok
Assert.is_nil(src:pushNotesAsync({ source_id = "ext.weread", stable_id = "bcancel", chapter_idx = 1 },
Assert.is_nil(src:pushNotesAsync({ source_id = "weread", stable_id = "bcancel", chapter_idx = 1 },
{ { drawer = "lighten", text = "甲", wr_bookmark_id = "b", wr_range = "0-1" } },
function(v) ok = v end))
Assert.is_true(ok)
Assert.eq(requests, 1)
toc_payload["bcancel"] = nil
require("book.toc_db").removeIdentity("ext.weread", "bcancel")
require("book.toc_db").removeIdentity("weread", "bcancel")
fake_client.bookmarkListAsync = nil
fake_client.myReviewsAsync = nil
end
@@ -1433,9 +1433,9 @@ do
remembered_stats_books.local_library = { "a", "b", "gone" }
remembered_stats_books.pending_adds = {}
remembered_stats_books.local_rows = {
a = { source_id = "ext.weread", stable_id = "a", title = "甲", deleted = 0 },
b = { source_id = "ext.weread", stable_id = "b", title = "乙旧", category = "我的分组", deleted = 0 },
gone = { source_id = "ext.weread", stable_id = "gone", title = "删", deleted = 0 },
a = { source_id = "weread", stable_id = "a", title = "甲", deleted = 0 },
b = { source_id = "weread", stable_id = "b", title = "乙旧", category = "我的分组", deleted = 0 },
gone = { source_id = "weread", stable_id = "gone", title = "删", deleted = 0 },
}
local ids_req, books_req, full_called = nil, nil, false
fake_client.shelfSyncIdsAsync = function(_, synckey, count, cb)
@@ -1564,7 +1564,7 @@ do
remembered_stats_books.local_library = { "a" }
remembered_stats_books.pending_adds = {}
remembered_stats_books.local_rows = {
a = { source_id = "ext.weread", stable_id = "a", title = "甲", deleted = 0 },
a = { source_id = "weread", stable_id = "a", title = "甲", deleted = 0 },
}
local full_called = false
fake_client.shelfSyncIdsAsync = function(_, _synckey, _count, cb)
@@ -1664,7 +1664,7 @@ do
local old_is_connected = Network.isConnected
local connected = true
local active = {
identity = { source_id = "ext.weread", stable_id = "page-sync" },
identity = { source_id = "weread", stable_id = "page-sync" },
}
local saves, scheduled, ticks, sync_callbacks = 0, {}, {}, {}
local cancelled = 0
@@ -1756,7 +1756,7 @@ do
local old_uid, old_source_index = Toc.uid, Toc.sourceIndex
local old_report = fake_client.reportReadAsync
local active = {
identity = { source_id = "ext.weread", stable_id = "presence" },
identity = { source_id = "weread", stable_id = "presence" },
}
local scheduled, sent = {}, {}
Session.current = function() return active end