--[[-- 京东读书源书城与本地统计能力离线用例。 @module tests.source.jdread_spec --]] ---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch, cast-type-mismatch, return-type-mismatch, cast-local-type local Assert = require("support.assert") local warnings = {} package.preload["utils.log"] = function() return { warn = function(...) warnings[#warnings + 1] = { ... } end, dbg = function() end, info = function() end, } end local fake_client = { configured = function() return true end, } package.preload["moonsource_jdread.client"] = function() return { new = function() return fake_client end, } end package.preload["utils.settings"] = function() return { getSource = function() return {} end, } end local remembered local local_library = {} local pending_deletes = {} local pending_adds = {} local finalized = {} package.preload["book.store"] = function() return { rememberMany = function(books) remembered = books; return true end, markDeleted = function() return true end, } end package.preload["book.books_db"] = function() return { reconcile = function(_, books) remembered = books return true, 0 end, remove = function(_, stable_id) finalized[#finalized + 1] = stable_id return true end, libraryStableIdsBySource = function() return local_library end, pendingDeleteIds = function() return pending_deletes end, pendingShelfAddIds = function() return pending_adds end, markSynced = function() return true end, getToc = function() return nil end, setToc = function() end, } end package.loaded["moonsource_jdread.client"] = nil package.loaded["utils.settings"] = nil package.loaded["book.store"] = nil package.loaded["book.books_db"] = nil package.loaded["moonsource_jdread.source"] = nil local Jdread = require("moonsource_jdread.source") do local source = Jdread.new() local caps = source:capabilities() Assert.is_false(caps.stats_pull) Assert.is_false(type(source.pushStatsAsync) == "function") end -- 图书馆列表行不带 cover:基类按身份查书库取 URL;非 http 或缺失给 nil do local source = Jdread.new() local stored = { ["42"] = { cover = "https://img.jd.com/42.jpg" }, ["43"] = { cover = "${free_list[i].cover}" }, } local BookDB = require("book.books_db") BookDB.get = function(source_id, stable_id) Assert.eq(source_id, "jdread") return stored[stable_id] end Assert.eq(source:coverRequest({ stable_id = "42" }).url, "https://img.jd.com/42.jpg") Assert.eq(source:coverRequest({ stable_id = "42", cover = "https://x/y.jpg" }).url, "https://x/y.jpg") local req, err = source:coverRequest({ stable_id = "43" }) Assert.is_nil(req) Assert.eq(err, "无封面") Assert.is_nil((source:coverRequest({ stable_id = "44" }))) BookDB.get = nil end -- syncBooksAsync:本地独有成员上行,pushed 填实 do local added, shelf_calls = {}, 0 local list_calls = 0 fake_client.shelfSyncAsync = function(_, cb) shelf_calls = shelf_calls + 1 cb({ { ebook_id = 10, name = "远端" } }) return { cancel = function() end } end fake_client.addToShelfAsync = function(_, book_id, cb) added[#added + 1] = tostring(book_id) cb({ ok = true }) return { cancel = function() end } end -- 绕过 mapper:直接让 shelfList 返回可控列表 local real_mapper = require("moonsource_jdread.mapper") local orig_shelf = real_mapper.shelfList real_mapper.shelfList = function() list_calls = list_calls + 1 return { { stable_id = "10", title = "远端" } } end -- 10 已在远端:只标已同步不再请求;99 是本地新加架,上行。 pending_adds = { "10", "99" } local synced = {} local db = require("book.books_db") local orig_mark = db.markSynced db.markSynced = function(_, stable_id) synced[#synced + 1] = stable_id return true end local src = Jdread.new() local result, err src:syncBooksAsync(nil, function(r, e) result, err = r, e end) require("support.stubs").flush() Assert.is_nil(err) Assert.not_nil(result) Assert.eq(result.pushed, 1) Assert.eq(#added, 1) Assert.eq(added[1], "99") Assert.eq(table.concat(synced, ","), "10,99") Assert.eq(shelf_calls, 2) Assert.eq(list_calls, 2) -- 已同步的本地书云端已移除:不得加回云端,交给 reconcile 软删。 added, shelf_calls, list_calls = {}, 0, 0 pending_adds = {} local_library = { "10", "77" } remembered = nil src:syncBooksAsync(nil, function(r, e) result, err = r, e end) require("support.stubs").flush() Assert.is_nil(err) Assert.eq(#added, 0) Assert.eq(result.pushed, 0) Assert.eq(shelf_calls, 1) Assert.eq(#remembered, 1) Assert.eq(remembered[1].stable_id, "10") real_mapper.shelfList = orig_shelf db.markSynced = orig_mark local_library = {} end -- dirty_only:远端删除失败不撕墓碑,且必须留下日志 do warnings = {} finalized = {} pending_deletes = { "gone" } pending_adds = {} fake_client.removeFromShelfAsync = function(_, stable_id, cb) cb(nil, "network") return { cancel = function() end } end local src = Jdread.new() local result, err src:syncBooksAsync({ dirty_only = true }, function(r, e) result, err = r, e end) require("support.stubs").flush() Assert.is_nil(err) Assert.eq(result.pushed, 0) Assert.eq(#finalized, 0) Assert.eq(warnings[1][1], "jdread shelf delete push failed") Assert.eq(warnings[1][2], "gone") pending_deletes = {} end -- dirty_only:云端删除成功但本地撕墓碑失败,不得计 pushed do finalized = {} pending_deletes = { "gone" } pending_adds = {} local BookDB = require("book.books_db") BookDB.remove = function(_, stable_id) finalized[#finalized + 1] = stable_id return false end fake_client.removeFromShelfAsync = function(_, _, cb) cb({ ok = true }) return { cancel = function() end } end local src = Jdread.new() local result src:syncBooksAsync({ dirty_only = true }, function(r) result = r end) require("support.stubs").flush() Assert.eq(result.pushed, 0) Assert.eq(#finalized, 1) BookDB.remove = function(_, stable_id) finalized[#finalized + 1] = stable_id return true end pending_deletes = {} end -- dirty_only:加架失败不 markSynced,且必须留下日志 do warnings = {} pending_deletes = {} pending_adds = { "99" } local synced = {} package.loaded["book.books_db"].markSynced = function(_, stable_id) synced[#synced + 1] = stable_id return true end fake_client.addToShelfAsync = function(_, stable_id, cb) cb(nil, "denied") return { cancel = function() end } end local src = Jdread.new() local result src:syncBooksAsync({ dirty_only = true }, function(r) result = r end) require("support.stubs").flush() Assert.eq(result.pushed, 0) Assert.eq(#synced, 0) Assert.eq(warnings[1][1], "jdread shelf push failed") Assert.eq(warnings[1][2], "99") pending_adds = {} end -- dirty_only:云端加架成功但本地 markSynced 失败,不得计 pushed do warnings = {} pending_deletes = {} pending_adds = { "99" } package.loaded["book.books_db"].markSynced = function() return false end fake_client.addToShelfAsync = function(_, _, cb) cb({ ok = true }) return { cancel = function() end } end local src = Jdread.new() local result src:syncBooksAsync({ dirty_only = true }, function(r) result = r end) require("support.stubs").flush() Assert.eq(result.pushed, 0) Assert.eq(warnings[1][1], "jdread shelf mark synced failed") package.loaded["book.books_db"].markSynced = function() return true end pending_adds = {} end do local prefetch_opts package.loaded["source.chapter"] = nil require("source.chapter").prefetchAsync = function(identity, _, toc, from_idx, count, opts, cb) prefetch_opts = opts Assert.eq(from_idx, 0) Assert.eq(count, #toc) opts.progress(1, #toc) opts.fetchContent(identity, toc[1], function(payload) Assert.matches(payload.html, "正文") cb(2, 2, 0) end) return { cancel = function() end } end package.loaded["moonsource_jdread.source"] = nil local Jd = require("moonsource_jdread.source") fake_client.downloadChapterAsync = function(_, _, query, cb) Assert.eq(query.indexes, "0") cb({ data = { chapter = { { content = "
正文
" } } } }) return { cancel = function() end } end local source = Jd.new() source.loadTocAsync = function(_, _, cb) cb({ { idx = 1, uid = "0", title = "第一章", toc_version = 5 }, { idx = 2, uid = "1", title = "第二章" }, }) return { cancel = function() end } end local progressed, ok, cached, total, failed source:cacheAllChaptersAsync( { source_id = "jdread", stable_id = "10", book = { stable_id = "10" } }, function(done, count) progressed = { done, count } end, function(success, count, _, all, failures) ok, cached, total, failed = success, count, all, failures end ) Assert.eq(progressed[1], 1) Assert.eq(progressed[2], 2) Assert.is_false(prefetch_opts.persist_book) Assert.eq(prefetch_opts.interval_seconds, 1.5) Assert.is_true(ok) Assert.eq(cached, 2) Assert.eq(total, 2) Assert.eq(failed, 0) end -- 目录版本 3(txt 网文)正文按网页阅读器协议 { type = 1, ids = chapter_id } 下载 do local Toc = require("source.toc") local orig_read = Toc.read Toc.read = function() return { { idx = 1, uid = "15001647875062768", title = "第一章", toc_version = 3 } } end local asked fake_client.downloadChapterAsync = function(_, book_id, query, cb) asked = { book_id = book_id, query = query } cb({ data = { content_type = "net", chapter = {{ content = " 正文一\r\n正文二" }} } }) return { cancel = function() end } end local html require("source.chapter").prefetchAsync = function(identity, _, toc, _, _, opts, cb) opts.fetchContent(identity, toc[1], function(payload) html = payload.html; cb() end) return { cancel = function() end } end local src = Jdread.new() src:prefetchChaptersAsync( { source_id = "jdread", stable_id = "34028897", book = { stable_id = "34028897" } }, { { idx = 1, uid = "15001647875062768", title = "第一章" } }, 1, 1, function() end ) Assert.eq(asked.book_id, "34028897") Assert.eq(asked.query.type, 1) Assert.eq(asked.query.ids, "15001647875062768") Assert.is_nil(asked.query.indexes) Assert.eq(html, "正文一
\n正文二
") Toc.read = orig_read end -- 失效目录(旧 cread 版本 1)读到即清库重拉;正文按新目录同 idx 的 uid 下载 do local Toc = require("source.toc") local orig_read, orig_put = Toc.read, Toc.put local stored = { { idx = 1, uid = "56958326", title = "第一章", toc_version = 1 } } Toc.read = function() return stored end Toc.put = function(_, _, list) stored = list; return true end local cleared = {} package.loaded["book.books_db"].clearToc = function(_, stable_id) cleared[#cleared + 1] = stable_id stored = nil return true end local catalog_calls = 0 fake_client.catalogAsync = function(_, _, cb) catalog_calls = catalog_calls + 1 cb({ data = { format = "epub", chapter_info = { { chapter_index = 0, chapter_name = "第一章" } } } }) return { cancel = function() end } end local asked fake_client.downloadChapterAsync = function(_, _, query, cb) asked = query cb({ data = { chapter = { { content = "新
" } } } }) return { cancel = function() end } end local html require("source.chapter").prefetchAsync = function(identity, _, toc, _, _, opts, cb) opts.fetchContent(identity, toc[1], function(payload) html = payload.html; cb() end) return { cancel = function() end } end local src = Jdread.new() local old = stored Assert.is_false(src:isTocCurrent(old)) Assert.is_false(src:isTocCurrent({ { idx = 1, uid = "0", title = "无版本" } })) Assert.is_false(src:isTocCurrent({})) src:prefetchChaptersAsync( { source_id = "jdread", stable_id = "30451107", book = { stable_id = "30451107" } }, old, 1, 1, function() end ) Assert.eq(cleared[1], "30451107") Assert.eq(catalog_calls, 1) Assert.eq(asked.indexes, "0") Assert.matches(html, "新
") Assert.eq(stored[1].toc_version, 5) Assert.is_true(src:isTocCurrent(stored)) Assert.is_false(src:isTocCurrent({ { idx = 1, uid = "0", title = "旧 EPUB 目录", toc_version = 2 } })) Assert.is_false(src:isTocCurrent({ { idx = 1, uid = "0", title = "无锚点目录", toc_version = 4 } })) -- 网页协议判定 can_read=false:提示走 App,不冒充网络/权限错误 fake_client.downloadChapterAsync = function(_, _, _, cb) cb({ data = { content_type = "net", chapter = { { chapter_id = "c37", can_read = false } } } }) return { cancel = function() end } end local denied require("source.chapter").prefetchAsync = function(identity, _, toc, _, _, opts, cb) opts.fetchContent(identity, toc[1], function(_, err) denied = err; cb() end) return { cancel = function() end } end src:prefetchChaptersAsync( { source_id = "jdread", stable_id = "30451107", book = { stable_id = "30451107" } }, stored, 1, 1, function() end ) Assert.eq(denied, "京东读书网页协议读不到本章,请在京东读书 App 内阅读") Toc.read, Toc.put = orig_read, orig_put end -- getProgressAsync:目录未缓存时拉到目录后仍按章节标题兜底(JD chapter_id 对不上本地 uid) do local Toc = require("source.toc") local orig_read, orig_put, orig_index = Toc.read, Toc.put, Toc.index local stored Toc.read = function() return stored end Toc.put = function(_, _, list) stored = list; return true end Toc.index = function() return nil end fake_client.getProgressAsync = function(_, _, cb) cb({ data = { { list = { { data_type = 0, version = 2, percent = 0.4, epub_chapter_title = "第二章", chapter_id = "jd-998", } } } } }) return { cancel = function() end } end fake_client.catalogAsync = function(_, _, cb) cb({ data = { format = "epub", chapter_info = { { chapter_index = 0, chapter_name = "第一章" }, { chapter_index = 1, chapter_name = "第二章" }, } } }) return { cancel = function() end } end local pos Jdread.new():getProgressAsync({ source_id = "jdread", stable_id = "30451107" }, function(v) pos = v end) require("support.stubs").flush() Assert.eq(pos.chapter_idx, 2) Assert.eq(pos.extra.chapter_idx, 2) -- 目录已缓存但 uid、标题都对不上:只定位一次,不再空转拉目录 stored = { { idx = 1, uid = "0", title = "第一章", toc_version = 5 } } local catalog_calls = 0 fake_client.catalogAsync = function(_, _, cb) catalog_calls = catalog_calls + 1 cb(nil, "should not fetch") return { cancel = function() end } end pos = nil Jdread.new():getProgressAsync({ source_id = "jdread", stable_id = "30451107" }, function(v) pos = v end) require("support.stubs").flush() Assert.eq(catalog_calls, 0) Assert.eq(pos.fraction, 0.4) Assert.is_nil(pos.chapter_idx) Toc.read, Toc.put, Toc.index = orig_read, orig_put, orig_index end -- putProgressAsync:章节 uid 取自目录;缺章节号直接失败,不把进度推到第 1 章 do local Toc = require("source.toc") local orig_read = Toc.read Toc.read = function() return { { idx = 1, uid = "0", title = "第一章", toc_version = 5 }, { idx = 2, uid = "1", title = "第二章" }, } end local marker fake_client.putProgressAsync = function(_, _, m, cb) marker = m cb({ result_code = 0 }) return { cancel = function() end } end local src = Jdread.new() local identity = { source_id = "jdread", stable_id = "30451107" } local ok, err src:putProgressAsync(identity, { fraction = 0.5, chapter_idx = 2 }, function(v, e) ok, err = v, e end) Assert.is_true(ok) Assert.is_nil(err) Assert.eq(marker.chapter_id, "1") Assert.eq(marker.epub_chapter_title, "第二章") Assert.eq(marker.percent, 0.5) marker, ok, err = nil, nil, nil src:putProgressAsync(identity, { fraction = 0.5 }, function(v, e) ok, err = v, e end) Assert.is_nil(ok) Assert.eq(err, "缺少章节信息") Assert.is_nil(marker) src:putProgressAsync(identity, { fraction = 0.5, chapter_idx = 9 }, function(v, e) ok, err = v, e end) Assert.eq(err, "缺少章节信息") Assert.is_nil(marker) Toc.read = orig_read end -- 封面章:网页版只给空 body,用目录首章挂的封面图补成一页 do local Toc = require("source.toc") local orig_read = Toc.read Toc.read = function() return { { idx = 1, uid = "0", title = "封面", toc_version = 5, cover = "https://img.test/c.jpg" } } end fake_client.downloadChapterAsync = function(_, _, _, cb) cb({ data = { content_type = "epub", chapter = { { content = '