--[[-- 京东读书客户端分页与下载接口离线用例。 @module tests.moonsource_jdread.client_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 CookieOf = require("support.cookie") local JSONStub = require("support.json_stub") package.preload["json"] = function() return { decode = JSONStub.decode, encode = JSONStub.encode } end local requests = {} package.preload["http.request"] = function() return { get = function(url, opts, cb) requests[#requests + 1] = { url = url, opts = opts } if url:find("/jdread/api/bookshelf/sort", 1, true) then cb('{"data":{"book_ids":[{"ebook_id":2},{"ebook_id":1}]},"result_code":0}') elseif url:find("/jdread/api/ebooks/lite/2,1", 1, true) then cb('{"data":[{"ebook_id":2,"name":"二"},{"ebook_id":1,"name":"一"}],"result_code":0}') elseif url:find("/recommend", 1, true) then cb('{"data":[{"ebook_id":4,"name":"推荐书"}],"result_code":0}') elseif url:find("/jdread/api/ebook/catalog/v2/30394360", 1, true) then cb('{"data":{"format":"epub","has_more":false,' .. '"chapter_info":[{"chapter_index":0,"chapter_name":"封面"}]},"result_code":0}') elseif url:find("/jdread/api/ebook/catalog/v2/30451107", 1, true) then cb('{"data":{"format":"txt","has_more":false,"chapter_info":[]},"result_code":0}') elseif url:find("/jdread/api/download/chapter/30394360", 1, true) then cb('{"result_code":1,"message":"UNKNOWN_ERROR"}') elseif url:find("/jdread/api/download/chapter/34028897", 1, true) then cb('{"result_code":101,"message":"can not download"}') else cb('{"code":"-1","msg":"stop"}') end return { cancel = function() end } end, post = function(url, body, opts, cb) requests[#requests + 1] = { url = url, body = body, opts = opts } cb('{"data":[],"result_code":0,"message":"SUCCESS"}') return { cancel = function() end } end, } end package.loaded["json"] = nil package.loaded["http.request"] = nil package.loaded["moonsource_jdread.protocol"] = nil package.loaded["moonsource_jdread.client"] = nil local Client = require("moonsource_jdread.client") local client = Client:new{ cookie = "thor=test", uuid = "h5-test" } do Assert.is_true(client:configured()) local wire, err client:shelfSyncAsync(function(value, e) wire, err = value, e end) Assert.is_nil(err) Assert.len(wire, 2) Assert.eq(wire[1].ebook_id, 2) Assert.matches(requests[1].url, "/jdread/api/bookshelf/sort%?") Assert.matches(requests[2].url, "/jdread/api/ebooks/lite/2,1%?") Assert.eq(CookieOf(requests[1]), "thor=test") end do local batched = Client:new{ cookie = "thor=test", uuid = "h5-test" } local paths = {} function batched:apiGetAsync(path, _, cb) paths[#paths + 1] = path if path == "/jdread/api/bookshelf/sort" then local ids = {} for i = 1, 20 do ids[i] = { ebook_id = i } end cb({ data = { book_ids = ids } }) else local rows = {} for id in path:match("/ebooks/lite/(.+)$"):gmatch("[^,]+") do rows[#rows + 1] = { ebook_id = tonumber(id) } end cb({ data = rows }) end return { cancel = function() end } end local wire batched:shelfSyncAsync(function(value) wire = value end) Assert.len(wire, 20) Assert.matches(paths[2], "/ebooks/lite/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18$") Assert.matches(paths[3], "/ebooks/lite/19,20$") end do local wire, err client:addToShelfAsync("30533530", function(value, e) wire, err = value, e end) local req = requests[#requests] Assert.is_nil(err) Assert.not_nil(wire) Assert.matches(req.url, "/jdread/api/bookshelf/book/sync%?") local body = require("json").decode(req.body) Assert.eq(body.first_sync, 1) Assert.eq(body.items[1].action, 0) Assert.eq(body.items[1].ebook_id, 30533530) end -- 非法 ID 跳过、不进 URL;合法 ID 照常分批。全是脏行则空快照成功返回(不下架策略由 shelf 管)。 do for _idx, bad in ipairs({ {}, { book_id = 2 }, { ebook_id = false }, "2 3", "" }) do local malformed = Client:new{ cookie = "thor=test", uuid = "h5-test" } local paths = {} function malformed:apiGetAsync(path, _extra, cb) paths[#paths + 1] = path if #paths == 1 then cb({ data = { book_ids = { { ebook_id = 1 }, bad, { ebook_id = 3 } } } }) else cb({ data = { { ebook_id = 1, name = "一" }, { ebook_id = 3, name = "三" } } }) end return { cancel = function() end } end local wire, err, meta malformed:shelfSyncAsync(function(value, e, m) wire, err, meta = value, e, m end) Assert.is_nil(err) Assert.eq(paths[2], "/jdread/api/ebooks/lite/1,3") Assert.len(wire, 2) Assert.eq(wire[1].ebook_id, 1) Assert.eq(wire[2].ebook_id, 3) Assert.is_true(meta.incomplete, "合法条目可用,但坏 ID 使快照不完整") end end do local all_bad = Client:new{ cookie = "thor=test", uuid = "h5-test" } local calls = 0 function all_bad:apiGetAsync(_path, _extra, cb) calls = calls + 1 cb({ data = { book_ids = { {}, { ebook_id = false }, "" } } }) return { cancel = function() end } end local wire, err, meta all_bad:shelfSyncAsync(function(value, e, m) wire, err, meta = value, e, m end) Assert.eq(calls, 1, "全脏行不应发起分批请求") Assert.is_nil(err) Assert.len(wire, 0) Assert.is_true(meta.incomplete) end do local mixed = Client:new{ cookie = "thor=test", uuid = "h5-test" } local paths = {} function mixed:apiGetAsync(path, _extra, cb) paths[#paths + 1] = path if #paths == 1 then cb({ data = { book_ids = { 2, { ebook_id = "1" }, "2" } } }) else cb({ data = { { ebook_id = 2 }, { ebook_id = 1 } } }) end return { cancel = function() end } end local wire mixed:shelfSyncAsync(function(value) wire = value end) Assert.eq(paths[2], "/jdread/api/ebooks/lite/2,1") Assert.len(wire, 2) end do local wire, err client:removeFromShelfAsync("30533530", function(value, e) wire, err = value, e end) local body = require("json").decode(requests[#requests].body) Assert.is_nil(err) Assert.not_nil(wire) Assert.eq(body.items[1].action, 1) Assert.eq(body.items[1].ebook_id, 30533530) end -- 空目录只回空结果,不再落到任何旧接口 do local wire, err local first = #requests + 1 client:catalogAsync("30451107", function(value, e) wire, err = value, e end) Assert.matches(requests[first].url, "^https://e%.m%.jd%.com/jdread/api/ebook/catalog/v2/30451107%?") Assert.matches(requests[first].url, "[?&]index=0") Assert.matches(requests[first].url, "[?&]page_size=2000") Assert.len(requests, first) Assert.is_nil(err) Assert.len(wire.data.chapter_info, 0) end do local wire, err client:catalogAsync("30394360", function(value, e) wire, err = value, e end) Assert.is_nil(err) Assert.eq(wire.data.chapter_info[1].chapter_name, "封面") end do local _, err local first = #requests + 1 client:downloadChapterAsync("30394360", { indexes = 0 }, function(value, e) _, err = value, e end) local req = requests[first] Assert.matches(req.url, "^https://e%.m%.jd%.com/jdread/api/download/chapter/30394360%?") Assert.matches(req.url, "[?&]enc=1") Assert.matches(req.url, "[?&]indexes=0") Assert.matches(req.url, "[?&]params=") Assert.eq(err, "UNKNOWN_ERROR") end -- txt 网文:网页阅读器协议 type + ids;101 = 未购买且非试读,给出权限文案而非英文原文 do local wire, err local first = #requests + 1 client:downloadChapterAsync("34028897", { type = 1, ids = "15001647875062768" }, function(value, e) wire, err = value, e end) local req = requests[first] Assert.matches(req.url, "^https://e%.m%.jd%.com/jdread/api/download/chapter/34028897%?") Assert.matches(req.url, "[?&]type=1") Assert.matches(req.url, "[?&]ids=15001647875062768") Assert.is_nil(req.url:find("indexes=", 1, true)) Assert.is_nil(wire) Assert.eq(err, "京东读书网页协议读不到本章,请在京东读书 App 内阅读") end -- v2 目录按行偏移分页,直到 has_more=false,合并成一份 chapter_info do local paged = Client:new{ cookie = "thor=test", uuid = "h5-test" } local calls = {} function paged:apiGetAsync(path, params, cb) calls[#calls + 1] = { path = path, index = params.index, size = params.page_size } local rows = {} local count = params.index == 0 and 2 or 1 for i = 1, count do rows[i] = { chapter_id = tostring(params.index + i), type = 1 } end cb({ data = { format = "txt", has_more = params.index == 0, chapter_info = rows }, result_code = 0 }) return { cancel = function() end } end local wire paged:catalogAsync("34028897", function(value) wire = value end) Assert.len(calls, 2) Assert.eq(calls[1].path, "/jdread/api/ebook/catalog/v2/34028897") Assert.eq(calls[1].index, 0) Assert.eq(calls[2].index, 2) Assert.eq(calls[1].size, 2000) Assert.len(wire.data.chapter_info, 3) Assert.eq(wire.data.chapter_info[3].chapter_id, "3") Assert.eq(wire.data.format, "txt") end do for _, req in ipairs(requests) do Assert.is_nil(req.url:find("cread.jd.com", 1, true)) end end do local wire, err client:getProgressAsync("30451107", function(value, e) wire, err = value, e end) local req = requests[#requests] Assert.is_nil(err) Assert.not_nil(wire) Assert.matches(req.url, "^https://e%.m%.jd%.com/jdread/api/marker/sync%?") Assert.eq(req.opts.content_type, "application/json") local body = require("json").decode(req.body) Assert.eq(body[1].ebook_id, 30451107) Assert.eq(body[1].format, 1) end -- 推进度与拉进度同一接口,ebook_id 同为数字 do client:putProgressAsync("30451107", { data_type = 0 }, function() end) local body = require("json").decode(requests[#requests].body) Assert.eq(body[1].ebook_id, 30451107) Assert.eq(body[1].list[1].data_type, 0) end -- NOT.LOGIN:英文错误码也要标登录失效,不能把服务端原串弹给用户。 do local expired, oks = {}, 0 package.preload["source.auth"] = function() return { expire = function(id, msg) expired[#expired + 1] = { id = id, msg = msg }; return msg end, ok = function() oks = oks + 1 end, } end local bodies = { '{"result_code":1,"message":"NOT.LOGIN"}' } package.loaded["http.request"] = { get = function(_url, _opts, cb) cb(bodies[1]) return { cancel = function() end } end, post = function(_url, _body, _opts, cb) cb(bodies[1]) return { cancel = function() end } end, } package.loaded["source.auth"] = nil package.loaded["moonsource_jdread.client"] = nil local AuthClient = require("moonsource_jdread.client") local c = AuthClient:new{ cookie = "thor=stale", uuid = "h5-test" } local got_err c:apiGetAsync("/jdread/api/bookshelf/sort", nil, function(_wire, err) got_err = err end) Assert.eq(expired[1].id, "ext.jdread") Assert.matches(got_err, "京东 App") Assert.eq(got_err, expired[1].msg) -- 中文「登录」同样走失效路径,文案仍用统一中文,不附原文 bodies[1] = '{"result_code":1,"message":"请重新登录"}' c:apiGetAsync("/jdread/api/bookshelf/sort", nil, function(_wire, err) got_err = err end) Assert.matches(got_err, "京东 App") Assert.len(expired, 2) -- 匿名请求(没配 Cookie 逛榜单)成功不算登录态有效,不能把「已提示过期」清掉;带 Cookie 的成功才算 bodies[1] = '{"result_code":0,"data":{}}' local anon = AuthClient:new{ uuid = "h5-anon" } anon:apiGetAsync("/jdread/api/rank", nil, function() end) Assert.eq(oks, 0, "匿名成功不 Auth.ok") c:apiGetAsync("/jdread/api/bookshelf/sort", nil, function() end) Assert.eq(oks, 1) -- 候选会话校验不允许把旧账号标过期,也不能在保存新凭证之前清掉旧过期状态。 bodies[1] = '{"result_code":3,"message":"NOT.LOGIN"}' c:checkSessionAsync(function(_wire, err) got_err = err end) Assert.eq(got_err, "京东登录校验失败") Assert.len(expired, 2) bodies[1] = '{"result_code":0,"data":{"book_ids":[]}}' local checked c:checkSessionAsync(function(wire, err) checked = wire Assert.is_nil(err) end) Assert.not_nil(checked) Assert.eq(oks, 1) end