♻️ 书源 id 去掉 ext. 前缀,沿用旧内置源数据
Co-authored-by: Copilot <[email protected]>
This commit is contained in:
1 parent
01e572a5ee
commit
f8ad3f73a6
29 files changed
+173
-173
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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, "想法二")
|
||||
|
||||
@@ -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
|
||||
@@ -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 点云端 + 增量比本机大,不往下压")
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user