✨ 初始提交:月读第三方书源插件集合

每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-16 12:08:03 +08:00
commit 77c37bc89a
27 files changed
+4305

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
--[[--
豆瓣阅读时长:秒级账户快照、日期窗口、重复同步和本地未上传时长。
@module tests.moonsource_douban.stats_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
package.preload["json"] = function() return require("support.json_stub") end
local Mapper = require("moonsource_douban.mapper")
local StatsDB = require("book.stats_db")
local now = os.time({ year = 2026, month = 11, day = 7, hour = 15 })
local wire = { data = { user = {
readingTimeTotal = { duration = 7200, normalDuration = 6000, textToSpeechDuration = 1200 },
readingTimeHistory = {
{ date = "2026-11-06", readingTime = { duration = 600 } },
{ date = "2026-11-07", readingTime = { duration = 1200 } },
},
} } }
local snapshot = Mapper.readingStats(wire, "2026-10-09", now)
Assert.eq(#snapshot.rows, 3)
Assert.eq(snapshot.rows[1].record_type, "total")
Assert.eq(snapshot.rows[1].duration, 7200)
Assert.eq(snapshot.rows[1].event_count, 0)
Assert.eq(snapshot.rows[2].duration, 600)
Assert.is_nil(Mapper.readingStats({ errors = { { message = "failed" } } }, "2026-10-09", now))
wire.errors = { { message = "partial response" } }
Assert.is_nil(Mapper.readingStats(wire, "2026-10-09", now))
wire.errors = nil
wire.data.user.readingTimeHistory[2].date = "2026-02-30"
Assert.is_nil(Mapper.readingStats(wire, "2026-10-09", now))
wire.data.user.readingTimeHistory[2].date = "2026-11-06"
Assert.is_nil(Mapper.readingStats(wire, "2026-10-09", now))
wire.data.user.readingTimeHistory[2].date = "2026-11-07"
wire.data.user.readingTimeHistory[2].readingTime.duration = -1
Assert.is_nil(Mapper.readingStats(wire, "2026-10-09", now))
wire.data.user.readingTimeHistory[2].readingTime.duration = 1200
Assert.is_true(StatsDB.removeSource("ext.douban"))
Assert.is_true(StatsDB.add({
source_id = "ext.douban", stable_id = "test-book", record_type = "page",
start_time = now - 600, duration = 60, page = 1, total_pages = 10,
}, StatsDB.PENDING))
Assert.is_true(StatsDB.replaceSynced("ext.douban", snapshot.replace, snapshot.rows) ~= nil)
Assert.eq(StatsDB.summaryBySource("ext.douban").total_seconds, 7260)
Assert.eq(StatsDB.dailyBySource("ext.douban")[2].seconds, 1260)
Assert.eq(#StatsDB.unsyncedBySource("ext.douban", 200), 1)
Assert.is_true(StatsDB.replaceSynced("ext.douban", snapshot.replace, snapshot.rows) ~= nil)
Assert.eq(StatsDB.summaryBySource("ext.douban").total_seconds, 7260)
wire.data.user.readingTimeHistory[1].readingTime.duration = 0
snapshot = Mapper.readingStats(wire, "2026-10-09", now)
Assert.is_true(StatsDB.replaceSynced("ext.douban", snapshot.replace, snapshot.rows) ~= nil)
Assert.eq(#StatsDB.dailyBySource("ext.douban"), 1)
Assert.eq(StatsDB.summaryBySource("ext.douban").total_pages, 1)
local request
package.preload["utils.settings"] = function()
return { getSource = function() return { cookie = "session=test", logged_in = true, user_id = "42" } end }
end
package.preload["http.request"] = function()
return {
headerAll = function() return {} end,
request = function(opts, cb)
request = opts
if opts.method == "GET" then cb({ code = 200, body = "CSRF_TOKEN = 'test';" })
else cb({ code = 200, body = require("support.json_stub").encode(wire) }) end
return { cancel = function() end }
end,
}
end
local source = require("moonsource_douban.source").new()
package.loaded["utils.settings"] = nil
Assert.eq(source:capabilities().stats_pull, true)
local result
source:pullStatsAsync(function(value) result = value end)
Assert.is_true(result ~= nil)
local payload = require("support.json_stub").decode(request.body)
Assert.eq(payload.variables.userId, "42")
Assert.is_true(payload.variables.beginDate:match("^%d%d%d%d%-%d%d%-%d%d$") ~= nil)
Assert.eq(request.headers["X-CSRF-Token"], "test")
Assert.eq(payload.extensions.persistedQuery.sha256Hash, Mapper.READING_TIME_HASH)
StatsDB.removeSource("ext.douban")