319 lines
12 KiB
Lua
319 lines
12 KiB
Lua
--[[--
|
|
moonsource_kmoe.source / chapter 离线用例:书城节点与筛选组、搜索聚合、卷下载(制作中、EPUB 转 CBZ、坏文件、转不了时留 EPUB)、缓存统计。
|
|
|
|
@module tests.moonsource_kmoe.source_spec
|
|
--]]
|
|
---@diagnostic disable: need-check-nil, undefined-field, inject-field, duplicate-set-field
|
|
|
|
local Assert = require("support.assert")
|
|
local Stubs = require("support.stubs")
|
|
Stubs.install()
|
|
|
|
local WORK = os.tmpname()
|
|
os.remove(WORK)
|
|
os.execute("mkdir -p '" .. WORK .. "'")
|
|
|
|
package.preload["utils.paths"] = function()
|
|
return {
|
|
bookWorkDir = function(stable_id) return WORK .. "/" .. stable_id end,
|
|
ensureBookWork = function(stable_id) os.execute("mkdir -p '" .. WORK .. "/" .. stable_id .. "'") end,
|
|
}
|
|
end
|
|
package.preload["utils.settings"] = function()
|
|
return { getSource = function() return { cookie = "VOLSKEY=k" } end, saveSource = function() end }
|
|
end
|
|
|
|
local queries, downloads, down_body = {}, {}, nil
|
|
package.preload["http.request"] = function()
|
|
return {
|
|
download = function(opts, dest, cb)
|
|
downloads[#downloads + 1] = { url = opts.url, dest = dest }
|
|
local f = io.open(dest, "wb")
|
|
f:write(down_body)
|
|
f:close()
|
|
cb(true, nil, { code = 200 })
|
|
return { cancel = function() end }
|
|
end,
|
|
}
|
|
end
|
|
|
|
local fake = { ua = "UA", BASE = "https://mox.moe" }
|
|
package.preload["moonsource_kmoe.client"] = function()
|
|
return { new = function() return fake end }
|
|
end
|
|
function fake.loggedIn() return true end
|
|
function fake.listAsync(_self, query, page, cb)
|
|
queries[#queries + 1] = { query = query, page = page }
|
|
cb({ { source_id = "kmoe", stable_id = query.s .. page } }, nil, page < 2)
|
|
return { cancel = function() end }
|
|
end
|
|
function fake.downUrlAsync(_self, origin, cb)
|
|
cb("https://dl.example/" .. origin)
|
|
return { cancel = function() end }
|
|
end
|
|
|
|
local archives = {}
|
|
local function u16(n) return string.char(n % 256, math.floor(n / 256) % 256) end
|
|
local function u32(n)
|
|
return string.char(n % 256, math.floor(n / 256) % 256, math.floor(n / 65536) % 256, math.floor(n / 16777216) % 256)
|
|
end
|
|
--- 测试用的 store zip。桩阅读器只认这种头,不跑 libarchive。
|
|
local function zipStore(files)
|
|
local parts, central, offset = {}, {}, 0
|
|
for _, file in ipairs(files) do
|
|
local name, data = file[1], file[2]
|
|
local local_hdr = "PK\3\4" .. u16(20) .. u16(0) .. u16(0) .. u16(0) .. u16(0)
|
|
.. u32(0) .. u32(#data) .. u32(#data) .. u16(#name) .. u16(0) .. name
|
|
parts[#parts + 1] = local_hdr .. data
|
|
central[#central + 1] = "PK\1\2" .. u16(20) .. u16(20) .. u16(0) .. u16(0) .. u16(0) .. u16(0)
|
|
.. u32(0) .. u32(#data) .. u32(#data) .. u16(#name) .. u16(0) .. u16(0) .. u16(0) .. u16(0)
|
|
.. u32(0) .. u32(offset) .. name
|
|
offset = offset + #local_hdr + #data
|
|
end
|
|
local central_blob = table.concat(central)
|
|
return table.concat(parts) .. central_blob .. "PK\5\6" .. u16(0) .. u16(0) .. u16(#files) .. u16(#files)
|
|
.. u32(#central_blob) .. u32(offset) .. u16(0)
|
|
end
|
|
local function readZip(blob)
|
|
if #blob < 22 then return nil end
|
|
local entries, i = {}, 1
|
|
while i + 3 <= #blob do
|
|
local sig = blob:sub(i, i + 3)
|
|
if sig == "PK\1\2" or sig == "PK\5\6" then return entries end
|
|
if sig ~= "PK\3\4" or i + 29 > #blob then return nil end
|
|
local name_len = blob:byte(i + 26) + blob:byte(i + 27) * 256
|
|
local extra_len = blob:byte(i + 28) + blob:byte(i + 29) * 256
|
|
local size = blob:byte(i + 18) + blob:byte(i + 19) * 256 + blob:byte(i + 20) * 65536 + blob:byte(i + 21) * 16777216
|
|
local start = i + 30 + name_len + extra_len
|
|
if start - 1 + size > #blob then return nil end
|
|
local name = blob:sub(i + 30, i + 29 + name_len)
|
|
entries[#entries + 1] = {
|
|
path = name, mode = "file", size = size, index = #entries + 1,
|
|
data = blob:sub(start, start + size - 1),
|
|
}
|
|
i = start + size
|
|
end
|
|
return nil
|
|
end
|
|
package.preload["ffi/archiver"] = function()
|
|
local Reader = {}
|
|
Reader.__index = Reader
|
|
function Reader:new() return setmetatable({ index = 0, entries = {} }, Reader) end
|
|
function Reader:open(path)
|
|
local f = io.open(path, "rb")
|
|
if not f then self.err = "open"; return nil end
|
|
local blob = f:read("*a")
|
|
f:close()
|
|
local list = readZip(blob)
|
|
if not list then self.err = "bad"; return nil end
|
|
self._list, self.entries, self.index = list, {}, 0
|
|
for _, entry in ipairs(list) do
|
|
self.entries[entry.path] = entry
|
|
self.entries[entry.index] = entry
|
|
end
|
|
return true
|
|
end
|
|
function Reader:next()
|
|
self.index = self.index + 1
|
|
return self._list[self.index]
|
|
end
|
|
function Reader:iterate()
|
|
self.index = 0
|
|
return self.next, self
|
|
end
|
|
function Reader:extractToMemory(key)
|
|
local entry = self.entries[key]
|
|
return entry and entry.data or nil
|
|
end
|
|
function Reader:close() end
|
|
local Writer = {}
|
|
Writer.__index = Writer
|
|
function Writer:new() return setmetatable({ files = {} }, Writer) end
|
|
function Writer:open(path) self.path = path; self.files = {}; return true end
|
|
function Writer:setZipCompression(method)
|
|
self.method = method
|
|
return method == "store"
|
|
end
|
|
function Writer:addFileFromMemory(name, data)
|
|
self.files[#self.files + 1] = { name = name, data = data }
|
|
return true
|
|
end
|
|
function Writer:close()
|
|
if not self.path then return end
|
|
archives[self.path] = { method = self.method, files = self.files }
|
|
local f = io.open(self.path, "wb")
|
|
if f then f:write("PK\3\4stub"); f:close() end
|
|
end
|
|
return { Reader = Reader, Writer = Writer }
|
|
end
|
|
|
|
local JPG, PNG = "jpeg-page-one", "png-page-two"
|
|
local function comic(rotate)
|
|
local mark = rotate and ' kmoetag="rotate:' .. rotate .. '"' or ' kmoetag="rotate:0"'
|
|
local opf = [[
|
|
<package><manifest>
|
|
<item id="Page_2" href="html/b.html" media-type="application/xhtml+xml"/>
|
|
<item id="Page_1" href="html/a.html" media-type="application/xhtml+xml"/>
|
|
</manifest><spine>
|
|
<itemref idref="Page_1" />
|
|
<itemref idref="Page_2" />
|
|
</spine></package>
|
|
]]
|
|
return zipStore({
|
|
{ "image/zzz.png", PNG },
|
|
{ "html/b.html", '<html><body><img src="../image/zzz.png" class="singlePage"/></body></html>' },
|
|
{ "image/aaa.jpg", JPG },
|
|
{ "html/a.html", '<html><body><img src="../image/aaa.jpg" class="singlePage"' .. mark .. "/></body></html>" },
|
|
{ "vol.opf", opf },
|
|
})
|
|
end
|
|
|
|
local Kmoe = require("moonsource_kmoe.source")
|
|
local Chapter = require("moonsource_kmoe.chapter")
|
|
local src = Kmoe.new()
|
|
|
|
-- 缺省节点:综合排序、不限状态 / 语言 / 分类;四组 chip 各有名字。
|
|
do
|
|
local res
|
|
src:browseAsync({ limit = 1 }, function(r) res = r end)
|
|
Assert.eq(queries[1].query.by, "sortpoint")
|
|
Assert.eq(queries[1].query.s, "")
|
|
Assert.len(res.groups, 4)
|
|
Assert.eq(res.groups[1].active, "sortpoint,,,")
|
|
Assert.eq(res.groups[1].title, "排序")
|
|
Assert.eq(res.groups[4].title, "分类")
|
|
Assert.eq(res.groups[2].chips[2].id, "sortpoint,連載,,", "改状态保留其他三段")
|
|
Assert.eq(res.groups[4].chips[2].id, "sortpoint,,,幽默")
|
|
end
|
|
|
|
-- 选中分类:查询串 CAT*<分类>,别的段原样;认不出的段回到缺省。
|
|
do
|
|
queries = {}
|
|
local res
|
|
src:browseAsync({ node = "score,完結,jpn,幽默", limit = 1 }, function(r) res = r end)
|
|
Assert.eq(queries[1].query.s, "CAT*幽默")
|
|
Assert.eq(queries[1].query.by, "score")
|
|
Assert.eq(queries[1].query["end"], "完結")
|
|
Assert.eq(queries[1].query.lang, "jpn")
|
|
Assert.eq(res.groups[4].active, "score,完結,jpn,幽默")
|
|
|
|
queries = {}
|
|
src:browseAsync({ node = "evil,x,y,不存在", limit = 1 }, function(r) res = r end)
|
|
Assert.eq(queries[1].query.by, "sortpoint")
|
|
Assert.eq(queries[1].query.s, "")
|
|
Assert.eq(res.groups[1].active, "sortpoint,,,")
|
|
end
|
|
|
|
-- 搜索:按页聚合到源说没有更多为止。
|
|
do
|
|
queries = {}
|
|
local res
|
|
src:searchAsync("小新", function(r) res = r end)
|
|
Assert.len(queries, 2)
|
|
Assert.eq(queries[1].query.s, "小新")
|
|
Assert.len(res.data, 2)
|
|
end
|
|
|
|
local identity = { source_id = "kmoe", stable_id = "de34d9" }
|
|
|
|
-- 制作中的卷不请求下载。
|
|
do
|
|
local path, err
|
|
Chapter.materializeAsync(fake, identity, { uid = "1", origin = "o", building = true }, nil,
|
|
function(p, e) path, err = p, e end)
|
|
Stubs.flush()
|
|
Assert.is_nil(path)
|
|
Assert.matches(err, "制作中")
|
|
Assert.len(downloads, 0)
|
|
end
|
|
|
|
-- 下到的不是 zip 就删掉报错。能打开但不是漫画的 zip 留下 EPUB。
|
|
do
|
|
down_body = "<html>額度不足</html>"
|
|
local path, err
|
|
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p, e) path, err = p, e end)
|
|
Assert.is_nil(path)
|
|
Assert.matches(err, "EPUB")
|
|
Assert.is_nil(io.open(WORK .. "/de34d9/3086.epub", "rb"), "坏文件不留")
|
|
|
|
down_body = "PK\3\4epub"
|
|
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p, e) path, err = p, e end)
|
|
Assert.is_nil(path)
|
|
Assert.matches(err, "EPUB")
|
|
Assert.is_nil(io.open(WORK .. "/de34d9/3086.epub", "rb"), "截断的 zip 不留")
|
|
|
|
down_body = zipStore({ { "readme.txt", "x" } })
|
|
Chapter.materializeAsync(fake, identity, { uid = "plain", origin = "o" }, nil, function(p, e) path, err = p, e end)
|
|
Assert.eq(path, WORK .. "/de34d9/plain.epub", "没有页序就继续用 EPUB")
|
|
Assert.is_nil(io.open(WORK .. "/de34d9/plain.cbz", "rb"))
|
|
end
|
|
|
|
-- 页序跟 spine,不跟压缩包里的文件名。转完删 EPUB,已有 CBZ 不再下载。
|
|
do
|
|
down_body = comic(0)
|
|
local path, err
|
|
local phases = {}
|
|
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, function()
|
|
phases[#phases + 1] = "convert"
|
|
end, function(p, e) path, err = p, e end)
|
|
Assert.is_nil(err)
|
|
Assert.eq(path, WORK .. "/de34d9/3086.cbz")
|
|
Assert.eq(phases[1], "convert")
|
|
Assert.is_nil(io.open(WORK .. "/de34d9/3086.epub", "rb"), "转完不留 EPUB")
|
|
local packed = archives[WORK .. "/de34d9/3086.cbz.part"]
|
|
Assert.eq(packed.method, "store")
|
|
Assert.eq(packed.files[1].name, "0001.jpg")
|
|
Assert.eq(packed.files[1].data, JPG)
|
|
Assert.eq(packed.files[2].name, "0002.png")
|
|
Assert.eq(packed.files[2].data, PNG)
|
|
local n = #downloads
|
|
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p) path = p end)
|
|
Stubs.flush()
|
|
Assert.eq(path, WORK .. "/de34d9/3086.cbz")
|
|
Assert.eq(#downloads, n, "已有 CBZ 不再下载")
|
|
end
|
|
|
|
-- 磁盘上的旧 EPUB 离线转换,不请求下载地址。非 0 旋转转不了,EPUB 还在。
|
|
do
|
|
local n = #downloads
|
|
local legacy = WORK .. "/de34d9/old.epub"
|
|
local f = assert(io.open(legacy, "wb"))
|
|
f:write(comic(0))
|
|
f:close()
|
|
local path
|
|
Chapter.materializeAsync(fake, identity, { uid = "old", origin = "o" }, nil, function(p) path = p end)
|
|
Stubs.flush()
|
|
Assert.eq(path, WORK .. "/de34d9/old.cbz")
|
|
Assert.eq(#downloads, n, "旧 EPUB 不重新下载")
|
|
Assert.is_nil(io.open(legacy, "rb"))
|
|
|
|
local turned = WORK .. "/de34d9/turn.epub"
|
|
f = assert(io.open(turned, "wb"))
|
|
f:write(comic(90))
|
|
f:close()
|
|
Chapter.materializeAsync(fake, identity, { uid = "turn", origin = "o" }, nil, function(p) path = p end)
|
|
Stubs.flush()
|
|
Assert.eq(path, turned, "旋转页仍打开 EPUB")
|
|
Assert.not_nil(io.open(turned, "rb"))
|
|
Assert.is_nil(io.open(WORK .. "/de34d9/turn.cbz", "rb"))
|
|
end
|
|
|
|
-- 缓存一段:单卷失败不阻断,统计已缓存 / 失败。
|
|
do
|
|
down_body = comic(0)
|
|
local toc = {
|
|
{ uid = "a", origin = "o" }, { uid = "b", origin = "o", building = true }, { uid = "c", origin = "o" },
|
|
}
|
|
local result
|
|
Chapter.cacheAsync(fake, identity, toc, 0, 3, nil, function(cached, total, failed, err)
|
|
result = { cached, total, failed, err }
|
|
end)
|
|
Stubs.flush()
|
|
Assert.eq(result[1], 2)
|
|
Assert.eq(result[2], 3)
|
|
Assert.eq(result[3], 1)
|
|
Assert.matches(result[4], "制作中")
|
|
end
|
|
|
|
os.execute("rm -rf '" .. WORK .. "'")
|