143 lines
4.7 KiB
Lua
143 lines
4.7 KiB
Lua
--[[--
|
|
识典古籍客户端:只打公开 loader,打开书时按章节号、书号_1、分类首屏、书名搜索往下找。
|
|
网络失败停住,不把它当成「没有这本书」。
|
|
|
|
@module tests.shidian.client_spec
|
|
--]]
|
|
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, inject-field
|
|
|
|
local Assert = require("support.assert")
|
|
local JsonStub = require("support.json_stub")
|
|
package.loaded["json"] = nil
|
|
package.preload["json"] = function()
|
|
return { decode = JsonStub.decode, encode = JsonStub.encode }
|
|
end
|
|
local JSON = require("json")
|
|
local _ = require("l10n").get("moonsource_shidian")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
local queued_tick = UIManager.nextTick
|
|
local function syncTick()
|
|
UIManager.nextTick = function(_, fn) fn() end
|
|
end
|
|
local function restoreTick()
|
|
UIManager.nextTick = queued_tick
|
|
end
|
|
|
|
local calls, script = {}, {}
|
|
package.preload["http.request"] = function()
|
|
return {
|
|
get = function(url, _opts, cb)
|
|
calls[#calls + 1] = url
|
|
local step = table.remove(script, 1) or { body = "{}" }
|
|
local handle = { cancel = function() end }
|
|
if step.err then cb(nil, step.err) else cb(step.body) end
|
|
return handle
|
|
end,
|
|
}
|
|
end
|
|
|
|
local Client = require("moonsource_shidian.client")
|
|
|
|
local function page(id)
|
|
return JSON.encode({ bookInfo = { bookId = id, bookName = "书" }, ssrChapterId = "CH" })
|
|
end
|
|
|
|
local function run(hint)
|
|
local client = Client.new()
|
|
local got, err
|
|
client:resolveAsync(hint, function(data, e) got, err = data, e end)
|
|
return got, err
|
|
end
|
|
|
|
-- 书目上已有章节号:只请求这一章。
|
|
do
|
|
calls, script = {}, { { body = page("B1") } }
|
|
local got, err = run({ id = "B1", seed = "SEED" })
|
|
Assert.is_nil(err)
|
|
Assert.eq(got.bookInfo.bookId, "B1")
|
|
Assert.eq(#calls, 1)
|
|
Assert.is_true(calls[1]:find("/book/B1/chapter/SEED?", 1, true) ~= nil)
|
|
end
|
|
|
|
-- 书号_1 没有这本书,分类首屏有章节号,第三下才打开。
|
|
do
|
|
calls, script = {}, {
|
|
{ body = '{"isSpider":false}' },
|
|
{ body = JSON.encode({ library = { data = { bookList = {
|
|
{ bookId = "B1", extra = { hasEntityRelationChapterIds = { "FROMCATE" } } },
|
|
} } } }) },
|
|
{ body = page("B1") },
|
|
}
|
|
local got, err = run({ id = "B1", title = "周易", cate = "10000" })
|
|
Assert.is_nil(err)
|
|
Assert.eq(got.ssrChapterId, "CH")
|
|
Assert.eq(#calls, 3)
|
|
Assert.is_true(calls[1]:find("/chapter/B1_1?", 1, true) ~= nil)
|
|
Assert.is_true(calls[2]:find("/library/10000?", 1, true) ~= nil)
|
|
Assert.is_true(calls[3]:find("/chapter/FROMCATE?", 1, true) ~= nil)
|
|
end
|
|
|
|
-- 分类首屏没有它,退到书名搜索。
|
|
do
|
|
calls, script = {}, {
|
|
{ body = '{"enableWebp":false}' },
|
|
{ body = '{"library":{"status":404}}' },
|
|
{ body = JSON.encode({ data = { paragraphs = {
|
|
{ chapterId = "FROMSEARCH", bookInfo = { bookId = "B1", bookName = "周易" } },
|
|
}, recommendBooks = { books = {} } } }) },
|
|
{ body = page("B1") },
|
|
}
|
|
local got = run({ id = "B1", title = "周易", cate = "10000" })
|
|
Assert.eq(got.bookInfo.bookId, "B1")
|
|
Assert.eq(#calls, 4)
|
|
Assert.is_true(calls[3]:find("/search/", 1, true) ~= nil)
|
|
Assert.is_true(calls[4]:find("/chapter/FROMSEARCH?", 1, true) ~= nil)
|
|
end
|
|
|
|
-- 网络失败不继续试下一条。
|
|
do
|
|
calls, script = {}, { { err = "offline" } }
|
|
local got, err = run({ id = "B1", title = "周易", cate = "10000" })
|
|
Assert.is_nil(got)
|
|
Assert.eq(err, "offline")
|
|
Assert.eq(#calls, 1)
|
|
end
|
|
|
|
-- 被当成爬虫是失败,不是空章节。
|
|
do
|
|
calls, script = {}, { { body = '{"isSpider":true,"enableWebp":false}' } }
|
|
local got, err = run({ id = "B1", seed = "SEED" })
|
|
Assert.is_nil(got)
|
|
Assert.eq(err, _("识典古籍把这次请求当成了爬虫,没有返回正文"))
|
|
end
|
|
|
|
-- 坏 JSON、空搜索词都不当成空结果。
|
|
do
|
|
calls, script = {}, { { body = "<html></html>" } }
|
|
local client = Client.new()
|
|
local got, err
|
|
client:libraryAsync("10000", function(data, e) got, err = data, e end)
|
|
Assert.is_nil(got)
|
|
Assert.eq(err, _("响应无效"))
|
|
syncTick()
|
|
client:searchAsync("", function(data, e) got, err = data, e end)
|
|
restoreTick()
|
|
Assert.is_nil(got)
|
|
Assert.eq(err, _("响应无效"))
|
|
end
|
|
|
|
-- 刚取过的同一章不再请求。
|
|
do
|
|
calls, script = {}, { { body = page("B1") } }
|
|
local client = Client.new()
|
|
client:chapterAsync("B1", "CH", function() end)
|
|
Assert.eq(#calls, 1)
|
|
local again
|
|
syncTick()
|
|
client:contentAsync("B1", "CH", function(data) again = data end)
|
|
restoreTick()
|
|
Assert.eq(#calls, 1)
|
|
Assert.eq(again.bookInfo.bookId, "B1")
|
|
end
|