--[[-- moonsource_kmoe.source / chapter 离线用例:书城节点与筛选组、搜索聚合、卷下载(制作中、已有文件、非 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 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 就删掉报错;是 EPUB 就交出路径,下次直接复用不再下载。 do down_body = "額度不足" 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.eq(path, WORK .. "/de34d9/3086.epub") Assert.eq(downloads[#downloads].url, "https://dl.example/o") 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.epub") Assert.eq(#downloads, n, "已有文件不再下载") end -- 缓存一段:单卷失败不阻断,统计已缓存 / 失败。 do down_body = "PK\3\4epub" 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 .. "'")