--[[--
moonsource_weread.assets 离线用例
@module tests.moonsource_weread.assets_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")
package.preload["moonsource_weread.assets"] = nil
package.loaded["moonsource_weread.assets"] = nil
package.preload["moonsource_weread.auth"] = nil
package.loaded["moonsource_weread.auth"] = nil
local Assets = require("moonsource_weread.assets")
local function noStyle()
error("不该拉样式表")
end
do
local html = '
'
local out = require("source.assets").rewriteImageSources(html, {
["foo.jpg"] = "images/aaa.png",
["https://cdn/x.png"] = "images/bbb.png",
})
Assert.matches(out, 'src="images/aaa.png"')
Assert.matches(out, 'src="images/bbb.png"')
end
local function tarHeader(name, size)
local h = string.rep("\0", 512)
local function poke(off, s)
h = h:sub(1, off - 1) .. s .. h:sub(off + #s)
end
poke(1, name:sub(1, 100))
poke(125, string.format("%011o", size))
poke(157, "0")
poke(258, "ustar\x00")
poke(264, "00")
local sum = 0
for i = 1, 512 do
sum = sum + h:byte(i)
end
poke(149, string.format("%06o\0 ", sum))
return h
end
local function makeTar(name, data)
local padded = data .. string.rep("\0", (512 - (#data % 512)) % 512)
return tarHeader(name, #data) .. padded .. string.rep("\0", 1024)
end
do
local png = "\137PNG\r\n\026\n" .. string.rep("x", 32)
local tar = makeTar("pic.png", png)
package.preload["moonsource_weread.auth"] = function()
return {
webGetAsync = function(url, _opts, cb)
if url:find("/tar/", 1, true) then
cb(tar)
else
cb(nil, "unexpected url")
end
end,
}
end
package.loaded["moonsource_weread.auth"] = nil
package.loaded["moonsource_weread.assets"] = nil
Assets = require("moonsource_weread.assets")
local done, html_out = false, nil
Assets.localizeAsync("book-1", {
uid = "u1",
tar = "/web/book/chapter/tar/abc",
}, '
', "https://weread.qq.com/web/reader/1/u1", noStyle, function(html)
done = true
html_out = html
end)
Assert.is_true(done)
Assert.matches(html_out, 'src="images/[0-9a-f]+%.png"')
end
do
local png = "\137PNG\r\n\026\n" .. string.rep("y", 16)
local remote = "https://res.weread.qq.com/wrepub/CB_test.png"
package.preload["moonsource_weread.auth"] = function()
return {
webGetAsync = function(url, opts, cb)
Assert.eq(url, remote)
Assert.is_true(opts.skip_auth_retry)
Assert.is_true(opts.allow_redirects)
Assert.eq(opts.headers["Referer"], "https://weread.qq.com/")
cb(png)
end,
}
end
package.loaded["moonsource_weread.auth"] = nil
package.loaded["moonsource_weread.assets"] = nil
Assets = require("moonsource_weread.assets")
local sample = string.format(
'
正文

正文
' local out Assets.localizeAsync("book-4", { uid = "u4" }, src, nil, function(done) done("a{background:url(../Images/bq.jpg)}") return { cancel = function() end } end, function(html) out = html end) Assert.is_true(out:find('\n正文
', 1, true) ~= nil, out) Assert.is_nil(out:find("bleed-pic", 1, true)) local f = assert(io.open(work_dir .. "/stylesheets.css", "rb")) Assert.eq(f:read("*a"), "a{background:url(images/bq.jpg)}") f:close() end -- 脚注图标路径不统一:qqreader-footnote 或任意 note.png 都注入自带「注」标,不要求写成 ../Images/note.png do local Paths = require("utils.paths") local cases = { { book = "book-fn-bare", src = "note.png" }, { book = "book-fn-images", src = "Images/note.png" }, { book = "book-fn-parent", src = "../Images/note.png" }, { book = "book-fn-noclass", src = "note.png", plain = true }, } local icons = {} for _, case in ipairs(cases) do local html if case.plain then html = string.format('