--[[-- reader3 本地 EPUB:正文资源与图片的成功、失败、取消及同步回调接力。 @module tests.moonsource_legado.source_content_spec --]] ---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch, assign-type-mismatch local Assert = require("support.assert") local JSONStub = require("support.json_stub") local Paths = require("utils.paths") local cfg = { server_address = "http://reader.example/reader3" } local requests, sync_replies, writes = {}, {}, {} local payload, reason, calls local write_failed = false package.preload["utils.settings"] = function() return { getSource = function() return cfg end } end package.preload["json"] = function() return { encode = JSONStub.encode, decode = JSONStub.decode } end package.preload["http.request"] = function() local function request(url, cb) local entry = { url = url, cb = cb, cancelled = false } requests[#requests + 1] = entry local reply = table.remove(sync_replies, 1) if reply then cb(reply.body, reply.err) end return { cancel = function() entry.cancelled = true end } end return { get = function(url, _opts, cb) return request(url, cb) end, post = function(url, _body, _opts, cb) return request(url, cb) end, } end package.preload["source.chapter"] = function() return { prefetchAsync = function(identity, _book, toc, _from, _count, ops, cb) return ops.fetchContent(identity, toc[1], cb) end, } end for _idx, name in ipairs({ "utils.settings", "json", "http.request", "source.chapter", "source.assets", "moonsource_legado.source", "moonsource_legado.client" }) do package.loaded[name] = nil end local Assets = require("source.assets") Assets.materializeImage = function(dir, data) writes[#writes + 1] = { dir = dir, data = data } if write_failed then return nil end return "images/fixture.png" end local source = require("moonsource_legado.source").new() local identity = { source_id = "legado", stable_id = "legado-content-fixture", book = {} } local toc = { { idx = 1, uid = "0", title = "章" } } local resource = "/book-assets/book/Text/chapter%20one.xhtml" local image_body = '
'
local function wire(text) return JSONStub.encode({ isSuccess = true, data = text }) end
local function start(replies)
requests, writes, sync_replies = {}, {}, replies or {}
payload, reason, calls, write_failed = nil, nil, 0, false
return source:prefetchChaptersAsync(identity, toc, 1, 1, function(value, err)
payload, reason, calls = value, err, calls + 1
end)
end
-- 普通正文不能因为以 .html 结尾而被当作资源,包括 App 和轻阅读。
for _idx, address in ipairs({ "http://reader.example/reader3", "http://reader.example", "http://reader.example/api/5" }) do
cfg.server_address = address
start()
requests[1].cb(wire("Documentation is at index.html"))
Assert.eq(payload.html, address == "http://reader.example"
and 'Documentation is at index.html
' or "Documentation is at index.html
") Assert.len(requests, 1) end cfg.server_address = "http://reader.example/reader3" -- 资源路径保留已有编码;合法的 src = 属性在图片落地后不再留下远程引用。 do start() requests[1].cb(wire(resource)) Assert.eq(requests[2].url, "http://reader.example" .. resource) requests[2].cb(image_body) Assert.eq(requests[3].url, "http://reader.example/book-assets/book/Images/cover%20one.png") requests[3].cb("image data") Assert.eq(payload.html, '
')
Assert.eq(writes[1].dir, Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/images")
Assert.eq(calls, 1)
end
-- 资源 GET 的错误直接上报;标题和空白实体不能让空正文成功。
do
start()
requests[1].cb(wire(resource))
requests[2].cb(nil, "HTTP 404")
Assert.is_nil(payload)
Assert.eq(reason, "HTTP 404")
start()
requests[1].cb(wire(resource))
requests[2].cb("
')
Assert.eq(requests[4].url, "http://reader.example/book-assets/book/Images/cover.png?accessToken=fixture-token")
requests[4].cb("image data")
Assert.eq(requests[5].url, "http://cdn.example/cover.png")
requests[5].cb("cdn image data")
Assert.is_nil(reason)
Assert.eq(calls, 1)
end