This commit is contained in:
commit
99d94c0d51
36 files changed
+4061
No files matched your search
@@ -0,0 +1,127 @@
|
||||
--[[--
|
||||
站点映射保持单一 API 协议;书架写入必须回读确认,取消不能漏到下一阶段。
|
||||
|
||||
@module tests.moonsource_copymanga.api_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local JSON = require("support.json_stub")
|
||||
package.loaded["json"] = JSON
|
||||
local Client = require("moonsource_copymanga.client")
|
||||
local settings = require("utils.settings")
|
||||
local calls, replies, cancelled = {}, {}, {}
|
||||
package.loaded["http.request"] = {
|
||||
get = function(url, opts, cb)
|
||||
calls[#calls + 1] = { url = url, headers = opts.headers, redirects = opts.allow_redirects }
|
||||
local reply = table.remove(replies, 1)
|
||||
local id = #calls
|
||||
if reply.hold then reply.callback = cb else cb(reply.raw, reply.err) end
|
||||
return { cancel = function() cancelled[id] = true end }
|
||||
end,
|
||||
post = function(url, body, opts, cb)
|
||||
calls[#calls + 1] = { url = url, headers = opts.headers, body = body, redirects = opts.allow_redirects }
|
||||
local reply = table.remove(replies, 1)
|
||||
local id = #calls
|
||||
if reply.hold then reply.callback = cb else cb(reply.raw, reply.err) end
|
||||
return { cancel = function() cancelled[id] = true end }
|
||||
end,
|
||||
}
|
||||
local detail = '{"code":200,"results":{"comic":{"uuid":"comic-id","path_word":"test","name":"Test"}}}'
|
||||
local empty = '{"code":200,"results":{"list":[],"total":0}}'
|
||||
local collected = '{"code":200,"results":{"list":[{"comic":{"uuid":"comic-id","path_word":"test"}}],"total":1}}'
|
||||
|
||||
for _idx, site in ipairs(Client.SITES) do
|
||||
local api = site:gsub("https://www%.", "https://api.")
|
||||
Assert.eq(Client.normalizeBaseUrl(site .. "/"), api)
|
||||
Assert.eq(Client.normalizeBaseUrl(api), api)
|
||||
local cfg = settings.getSource("copymanga")
|
||||
cfg.base_url, cfg.token, cfg.user_id = site, "private-token", "1"
|
||||
settings.saveSource("copymanga", cfg)
|
||||
local client = Client:new(cfg)
|
||||
local result, err
|
||||
replies = { { raw = empty } }
|
||||
client:searchAsync("one", function(wire, e) result, err = wire, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(result ~= nil)
|
||||
Assert.is_true(calls[#calls].url:find(api .. "/api/v3/search/comic?", 1, true) == 1)
|
||||
Assert.eq(calls[#calls].headers["User-Agent"], "COPY/3.0.0")
|
||||
Assert.is_nil(calls[#calls].headers.Authorization)
|
||||
|
||||
replies = { { raw = '{"code":200,"results":{"chapter":{"contents":[{"url":"https://images.example/page.jpg"}]}}}' } }
|
||||
client:chapterAsync("test", "chapter-id", function(wire, e) result, err = wire, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(result ~= nil)
|
||||
local endpoint = api == Client.CURRENT_BASE_URL and "chapter" or "chapter2"
|
||||
Assert.eq(calls[#calls].url, api .. "/api/v3/comic/test/" .. endpoint .. "/chapter-id?platform=1")
|
||||
|
||||
replies = { { raw = detail }, { raw = '{"code":200}' }, { raw = collected } }
|
||||
client:addToShelfAsync("test", function(wire, e) result, err = wire, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(result ~= nil)
|
||||
Assert.eq(calls[#calls - 1].url, api .. "/api/v3/member/collect/comic")
|
||||
Assert.is_true(calls[#calls - 1].body:find("is_collect=1", 1, true) ~= nil)
|
||||
Assert.eq(calls[#calls].headers.Authorization, "Token private-token")
|
||||
Assert.is_false(calls[#calls].redirects)
|
||||
Assert.is_false(calls[#calls - 1].redirects)
|
||||
|
||||
replies = { { raw = detail }, { raw = '{"code":200}' }, { raw = collected } }
|
||||
client:removeFromShelfAsync("test", function(wire, e) result, err = wire, e end)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "书架同步未确认")
|
||||
|
||||
replies = { { raw = detail }, { raw = '{"code":200}' }, { raw = empty } }
|
||||
client:removeFromShelfAsync("test", function(wire, e) result, err = wire, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(result ~= nil)
|
||||
|
||||
local held = { hold = true }
|
||||
replies = { { raw = detail }, { raw = '{"code":200}' }, held }
|
||||
local completed = false
|
||||
local job = client:addToShelfAsync("test", function() completed = true end)
|
||||
job.cancel()
|
||||
Assert.is_true(cancelled[#calls], "取消回读阶段,不得被同步返回的旧句柄覆盖")
|
||||
held.callback(collected)
|
||||
Assert.is_false(completed)
|
||||
|
||||
local source = require("moonsource_copymanga.source").new()
|
||||
if api == Client.CURRENT_BASE_URL then
|
||||
Assert.is_nil(source.getProgressAsync)
|
||||
Assert.is_nil(source.putProgressAsync)
|
||||
else
|
||||
Assert.eq(type(source.getProgressAsync), "function")
|
||||
Assert.eq(type(source.putProgressAsync), "function")
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local client = Client:new(settings.getSource("copymanga"))
|
||||
local result, err
|
||||
replies = { { raw = '{"code":200,"results":{}}' } }
|
||||
client:shelfSyncAsync(function(rows, e) result, err = rows, e end)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "响应无效")
|
||||
replies = { { raw = '{"code":200,"results":{"list":[{}],"total":1}}' } }
|
||||
client:shelfSyncAsync(function(rows, e) result, err = rows, e end)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "响应无效")
|
||||
replies = { { raw = '{"code":200,"results":{"list":[],"total":2}}' } }
|
||||
client:shelfSyncAsync(function(rows, e) result, err = rows, e end)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "响应无效")
|
||||
|
||||
replies = {
|
||||
{ raw = '{"code":200,"results":{"list":[{"comic":{"path_word":"one"}}],"total":2}}' },
|
||||
{ raw = '{"code":200,"results":{"list":[{"comic":{"path_word":"two"}}],"total":2}}' },
|
||||
}
|
||||
client:shelfSyncAsync(function(rows, e) result, err = rows, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.len(result, 2)
|
||||
Assert.is_true(calls[#calls].url:find("offset=1", 1, true) ~= nil)
|
||||
require("moonsource_copymanga.auth").clearSession()
|
||||
Assert.eq(client:_latestToken(), "")
|
||||
local cfg = settings.getSource("copymanga")
|
||||
cfg.base_url, cfg.token = Client.SITES[1], "new-site-token"
|
||||
settings.saveSource("copymanga", cfg)
|
||||
Assert.eq(client:_latestToken(), "")
|
||||
end
|
||||
require("moonsource_copymanga.auth").clearSession()
|
||||
@@ -0,0 +1,88 @@
|
||||
--[[--
|
||||
拷贝漫画登录密码编码用例。
|
||||
|
||||
@module tests.moonsource_copymanga.auth_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
package.loaded["json"] = require("support.json_stub")
|
||||
local switches = 0
|
||||
require("source.logout").replaceLibrary = function(id, commit)
|
||||
Assert.eq(id, "copymanga")
|
||||
switches = switches + 1
|
||||
commit()
|
||||
return true
|
||||
end
|
||||
local Auth = require("moonsource_copymanga.auth")
|
||||
local Text = require("utils.text")
|
||||
|
||||
do
|
||||
-- 与站点前端一致:base64(password .. "-" .. salt)
|
||||
Assert.eq(Auth.encodePassword("secret", 123456), Text.base64Encode("secret-123456"))
|
||||
Assert.eq(Auth.encodePassword("密 码", "000001"), Text.base64Encode("密 码-000001"))
|
||||
end
|
||||
|
||||
do
|
||||
local settings = require("utils.settings")
|
||||
local current = settings.getSource("copymanga")
|
||||
local original_base = current.base_url
|
||||
current.base_url = "https://www.copy4000.com"
|
||||
settings.saveSource("copymanga", current)
|
||||
local pending, request
|
||||
local cancelled = false
|
||||
package.loaded["http.request"] = {
|
||||
request = function(opts, cb)
|
||||
request, pending = opts, cb
|
||||
return { cancel = function() cancelled = true end }
|
||||
end,
|
||||
}
|
||||
Auth.clearSession()
|
||||
local ok, err
|
||||
local job = Auth.loginAsync("user", " spaced password ", function(result, e) ok, err = result, e end)
|
||||
Assert.eq(request.url, "https://api.copy4000.com/api/v3/login")
|
||||
Assert.is_false(request.allow_redirects)
|
||||
Assert.eq(request.cache_ttl, 0)
|
||||
Assert.is_true(request.body:find(require("utils.text").urlEncode(Auth.encodePassword(" spaced password ",
|
||||
require("utils.text").urlDecode(request.body:match("salt=([^&]+)")))), 1, true) ~= nil)
|
||||
pending({ body = '{"code":200,"results":{"token":"first","user_id":"1"}}' })
|
||||
Assert.is_true(ok)
|
||||
Assert.eq(current.password, " spaced password ", "登录成功必须落盘密码,401 才能自动重登")
|
||||
Assert.eq(current.username, "user")
|
||||
|
||||
job = Auth.loginAsync("other", "pass", function(result, e) ok, err = result, e end)
|
||||
pending({ body = '{"code":200,"results":{"token":"other","user_id":"2"}}' })
|
||||
Assert.is_true(ok)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(current.token, "other")
|
||||
Assert.eq(switches, 2, "换账号准备同步,不再要求先退出清数据")
|
||||
|
||||
job = Auth.loginAsync("user", "pass", function(result, e) ok, err = result, e end)
|
||||
job.cancel()
|
||||
pending({ body = '{"code":200,"results":{"token":"cancelled","user_id":"1"}}' })
|
||||
Assert.is_true(cancelled)
|
||||
Assert.eq(current.token, "other")
|
||||
Auth.clearSession()
|
||||
current.base_url = original_base
|
||||
settings.saveSource("copymanga", current)
|
||||
end
|
||||
|
||||
do
|
||||
Assert.is_false(Auth.hasSession())
|
||||
Assert.is_nil(Auth.userLabel())
|
||||
Assert.is_nil(Auth.credentials())
|
||||
end
|
||||
|
||||
do
|
||||
local settings = require("utils.settings")
|
||||
local cfg = settings.getSource("copymanga")
|
||||
cfg.username = "user"
|
||||
cfg.password = "secret"
|
||||
cfg.token = "tok"
|
||||
settings.saveSource("copymanga", cfg)
|
||||
local username, password = Auth.credentials()
|
||||
Assert.eq(username, "user")
|
||||
Assert.eq(password, "secret")
|
||||
Auth.clearSession()
|
||||
Assert.is_nil(Auth.credentials())
|
||||
Assert.is_false(Auth.hasSession())
|
||||
end
|
||||
@@ -0,0 +1,57 @@
|
||||
--[[--
|
||||
拷贝漫画书城浏览:榜单与题材共用一套 chip,题材节点走 /comics 且不高亮周期 / 受众。
|
||||
|
||||
@module tests.moonsource_copymanga.browse_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
|
||||
local fake_client = {}
|
||||
local calls = {}
|
||||
|
||||
local function stub(name, factory)
|
||||
package.preload[name] = factory
|
||||
package.loaded[name] = nil
|
||||
end
|
||||
|
||||
stub("utils.settings", function()
|
||||
return { getSource = function() return { token = "tok" } end, saveSource = function() end }
|
||||
end)
|
||||
stub("moonsource_copymanga.client", function()
|
||||
return { new = function() return fake_client end, headers = function() return {} end,
|
||||
normalizeBaseUrl = function(url) return url end }
|
||||
end)
|
||||
|
||||
local function page(name)
|
||||
return { results = { list = { { path_word = name, name = name } } } }
|
||||
end
|
||||
fake_client.rankAsync = function(_, period, audience, offset, cb)
|
||||
calls[#calls + 1] = "rank:" .. period .. ":" .. tostring(audience) .. ":" .. offset
|
||||
cb(page("r"))
|
||||
return { cancel = function() end }
|
||||
end
|
||||
fake_client.themeAsync = function(_, theme, offset, cb)
|
||||
calls[#calls + 1] = "theme:" .. theme .. ":" .. offset
|
||||
cb(page("t"))
|
||||
return { cancel = function() end }
|
||||
end
|
||||
|
||||
package.loaded["moonsource_copymanga.source"] = nil
|
||||
local src = require("moonsource_copymanga.source").new()
|
||||
|
||||
local res
|
||||
src:browseAsync({ node = "theme:rexue", limit = 1 }, function(r) res = r end)
|
||||
Assert.eq(calls[1], "theme:rexue:0")
|
||||
Assert.eq(res.data[1].stable_id, "t")
|
||||
Assert.len(res.groups, 2)
|
||||
Assert.eq(res.groups[2].active, "theme:rexue")
|
||||
Assert.is_nil(res.groups[1].active)
|
||||
|
||||
src:browseAsync({ limit = 1 }, function(r) res = r end)
|
||||
Assert.eq(calls[2], "rank:day:nil:0")
|
||||
Assert.eq(res.groups[1].active, "day:all")
|
||||
Assert.eq(res.groups[2].active, "day:all")
|
||||
|
||||
src:browseAsync({ node = "web:ordering=-popular&theme=rexue", limit = 1 }, function(r) res = r end)
|
||||
Assert.eq(calls[3], "rank:day:nil:0", "切回 App 后不能把官网节点当榜单周期")
|
||||
Assert.eq(res.groups[1].active, "day:all")
|
||||
@@ -0,0 +1,122 @@
|
||||
--[[--
|
||||
拷贝漫画章节预取 / 全书缓存用例。
|
||||
|
||||
@module tests.moonsource_copymanga.chapter_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")
|
||||
local Stubs = require("support.stubs")
|
||||
Stubs.install()
|
||||
|
||||
package.preload["ffi/archiver"] = function()
|
||||
return { Writer = { new = function() return {} end } }
|
||||
end
|
||||
package.preload["utils.paths"] = function()
|
||||
return { ensureBookWork = function() end, bookWorkDir = function() return "/tmp" end }
|
||||
end
|
||||
package.preload["moonsource_copymanga.protocol"] = function()
|
||||
return { chapterImages = function() end }
|
||||
end
|
||||
package.preload["moonsource_copymanga.client"] = function()
|
||||
return { headers = function() return {} end }
|
||||
end
|
||||
package.loaded["moonsource_copymanga.chapter"] = nil
|
||||
|
||||
local Chapter = require("moonsource_copymanga.chapter")
|
||||
|
||||
local calls = {}
|
||||
local fail_at
|
||||
-- 章节 CBZ 按 uuid 命名,不随目录里的连续章号漂移。
|
||||
Assert.eq(Chapter.archivePath({ source_id = "copymanga", stable_id = "comic" }, { idx = 7, uid = "uuid-7" }),
|
||||
"/tmp/uuid-7.cbz")
|
||||
|
||||
Chapter.materializeAsync = function(_, _, chapter, _, cb)
|
||||
local idx = chapter.idx
|
||||
calls[#calls + 1] = idx
|
||||
if fail_at == idx then
|
||||
require("ui/uimanager"):nextTick(function() cb(nil, "fail") end)
|
||||
else
|
||||
require("ui/uimanager"):nextTick(function() cb("/tmp/" .. idx .. ".cbz") end)
|
||||
end
|
||||
return { cancel = function() end }
|
||||
end
|
||||
|
||||
local toc = {
|
||||
{ idx = 1, uid = "a" },
|
||||
{ idx = 2, uid = "b" },
|
||||
{ idx = 3, uid = "c" },
|
||||
{ idx = 4, uid = "d" },
|
||||
{ idx = 5, uid = "e" },
|
||||
}
|
||||
local identity = { source_id = "copymanga", stable_id = "comic" }
|
||||
|
||||
do
|
||||
local cached, total, failed
|
||||
Chapter.prefetchAsync({}, identity, toc, 2, 3, nil, function(c, t, f)
|
||||
cached, total, failed = c, t, f
|
||||
end)
|
||||
Stubs.flush()
|
||||
Assert.eq(calls[1], 3)
|
||||
Assert.eq(calls[2], 4)
|
||||
Assert.eq(calls[3], 5)
|
||||
Assert.len(calls, 3)
|
||||
Assert.eq(cached, 3)
|
||||
Assert.eq(total, 3)
|
||||
Assert.eq(failed, 0)
|
||||
end
|
||||
|
||||
do
|
||||
calls = {}
|
||||
local cached, total, failed
|
||||
Chapter.prefetchAsync({}, identity, toc, 5, 3, nil, function(c, t, f)
|
||||
cached, total, failed = c, t, f
|
||||
end)
|
||||
Stubs.flush()
|
||||
Assert.len(calls, 0)
|
||||
Assert.eq(cached, 0)
|
||||
Assert.eq(total, 0)
|
||||
Assert.eq(failed, 0)
|
||||
end
|
||||
|
||||
do
|
||||
calls = {}
|
||||
fail_at = 4
|
||||
local cached, total, failed, last_err
|
||||
local progressed = {}
|
||||
Chapter.prefetchAsync({}, identity, toc, 2, 3, {
|
||||
progress = function(done, all) progressed[#progressed + 1] = { done, all } end,
|
||||
}, function(c, t, f, err)
|
||||
cached, total, failed, last_err = c, t, f, err
|
||||
end)
|
||||
Stubs.flush()
|
||||
Assert.len(calls, 3)
|
||||
Assert.eq(cached, 2)
|
||||
Assert.eq(total, 3)
|
||||
Assert.eq(failed, 1)
|
||||
Assert.eq(last_err, "fail")
|
||||
Assert.eq(progressed[#progressed][1], 3)
|
||||
Assert.eq(progressed[#progressed][2], 3)
|
||||
fail_at = nil
|
||||
end
|
||||
|
||||
do
|
||||
calls = {}
|
||||
local cached
|
||||
Chapter.prefetchAsync({}, identity, toc, 0, #toc, {
|
||||
interval_seconds = 1.5,
|
||||
}, function(c) cached = c end)
|
||||
Stubs.flush()
|
||||
Assert.len(calls, 5)
|
||||
Assert.eq(cached, 5)
|
||||
end
|
||||
|
||||
do
|
||||
calls = {}
|
||||
local done = false
|
||||
local job = Chapter.prefetchAsync({}, identity, toc, 1, 3, nil, function() done = true end)
|
||||
job.cancel()
|
||||
Stubs.flush()
|
||||
Assert.len(calls, 0)
|
||||
Assert.is_false(done)
|
||||
end
|
||||
@@ -0,0 +1,187 @@
|
||||
--[[--
|
||||
拷贝漫画 App 客户端:常量与令牌失效自动重登。
|
||||
|
||||
@module tests.moonsource_copymanga.client_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
|
||||
-- 记录请求;get 按队列回放 { code, body },request 是登录 POST。
|
||||
local gets, logins, replies, login_ok = {}, 0, {}, true
|
||||
-- login_hold:登录回调先挂起,模拟并发;login_net:登录走网络错误。
|
||||
local login_hold, login_net = nil, false
|
||||
package.loaded["http.request"] = {
|
||||
get = function(url, opts, cb)
|
||||
gets[#gets + 1] = { url = url, auth = opts.headers.Authorization }
|
||||
local reply = table.remove(replies, 1)
|
||||
if reply.code == 200 then
|
||||
cb(reply.body, nil, { code = 200, body = reply.body })
|
||||
else
|
||||
cb(nil, "HTTP " .. reply.code, { code = reply.code, body = reply.body })
|
||||
end
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
request = function(_opts, cb)
|
||||
logins = logins + 1
|
||||
if login_net then
|
||||
cb(nil, "connection timed out")
|
||||
return { cancel = function() end }
|
||||
end
|
||||
local body = login_ok
|
||||
and '{"code":200,"results":{"token":"fresh","user_id":"1","username":"user"}}'
|
||||
or '{"code":400,"message":"密码错误"}'
|
||||
if login_hold then
|
||||
login_hold[#login_hold + 1] = function() cb({ code = 200, body = body }) end
|
||||
else
|
||||
cb({ code = 200, body = body })
|
||||
end
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
package.loaded["json"] = { decode = require("support.json_stub").decode }
|
||||
package.loaded["moonsource_copymanga.auth"] = nil
|
||||
package.loaded["moonsource_copymanga.client"] = nil
|
||||
local Client = require("moonsource_copymanga.client")
|
||||
|
||||
do
|
||||
Assert.eq(Client.normalizeBaseUrl(""), Client.DEFAULT_BASE_URL)
|
||||
Assert.eq(Client.normalizeBaseUrl("https://api.example.com/"), "https://api.example.com")
|
||||
Assert.eq(Client.normalizeBaseUrl("https://copy4000.com"), "https://copy4000.com")
|
||||
end
|
||||
|
||||
do
|
||||
local headers = Client.headers("abc")
|
||||
Assert.eq(headers["User-Agent"], "COPY/3.0.0")
|
||||
Assert.eq(headers.platform, "1")
|
||||
Assert.eq(headers.Authorization, "Token abc")
|
||||
Assert.is_nil(Client.headers("").Authorization)
|
||||
end
|
||||
|
||||
do
|
||||
local client = Client:new({})
|
||||
Assert.eq(client.base_url, Client.DEFAULT_BASE_URL)
|
||||
Assert.is_true(client:configured())
|
||||
end
|
||||
|
||||
local settings = require("utils.settings")
|
||||
local function session(password)
|
||||
local cfg = settings.getSource("copymanga")
|
||||
cfg.base_url = nil
|
||||
cfg.user_id = "1"
|
||||
cfg.token, cfg.username, cfg.password = "stale", "user", password
|
||||
settings.saveSource("copymanga", cfg)
|
||||
end
|
||||
local INVALID = '{"code":401,"message":"Invalid token header. Please login again."}'
|
||||
local OK = '{"code":200,"results":{"list":[],"total":0}}'
|
||||
|
||||
-- 匿名 API 被拒绝时保留真实原因,不误称用户账号被风控,也不自动重登。
|
||||
do
|
||||
session("secret")
|
||||
gets, logins, replies = {}, 0, {
|
||||
{ code = 200, body = '{"code":210,"message":"请更新官方 App 并等待限制解除"}' },
|
||||
}
|
||||
local result, err
|
||||
Client:new(settings.getSource("copymanga")):detailAsync("test", function(wire, e) result, err = wire, e end)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "请更新官方 App 并等待限制解除")
|
||||
Assert.is_nil(gets[1].auth)
|
||||
Assert.eq(logins, 0)
|
||||
result, err = Client.decode('{"code":210}')
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "拷贝漫画 API 拒绝请求(210),请按官网提示处理")
|
||||
result, err = Client.decode('{"code":210,"message":false}')
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "拷贝漫画 API 拒绝请求(210),请按官网提示处理")
|
||||
end
|
||||
|
||||
-- #42:令牌被挤掉后返回 401,用保存的账号密码重登一次,换新令牌重试。
|
||||
do
|
||||
session("secret")
|
||||
gets, logins, replies, login_ok = {}, 0, { { code = 401, body = INVALID }, { code = 200, body = OK } }, true
|
||||
local client = Client:new(settings.getSource("copymanga"))
|
||||
local wire, err
|
||||
client:collectAllAsync(function(rows, e) wire, err = rows, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.len(wire, 0)
|
||||
Assert.eq(logins, 1)
|
||||
Assert.len(gets, 2)
|
||||
Assert.eq(gets[1].auth, "Token stale")
|
||||
Assert.eq(gets[2].auth, "Token fresh")
|
||||
Assert.eq(client.token, "fresh")
|
||||
end
|
||||
|
||||
-- 重登后仍 401:只重登一次,不死循环,给出可读提示而不是「HTTP 401」。
|
||||
do
|
||||
session("secret")
|
||||
gets, logins, replies, login_ok = {}, 0, { { code = 401, body = INVALID }, { code = 401, body = INVALID } }, true
|
||||
local err
|
||||
Client:new(settings.getSource("copymanga")):collectAllAsync(function(_rows, e) err = e end)
|
||||
Assert.eq(logins, 1)
|
||||
Assert.matches(err, "拷贝漫画账号")
|
||||
end
|
||||
|
||||
-- 重登失败(密码已改)或没存密码:直接提示重新登录。
|
||||
do
|
||||
session("secret")
|
||||
gets, logins, replies, login_ok = {}, 0, { { code = 401, body = INVALID } }, false
|
||||
local err
|
||||
Client:new(settings.getSource("copymanga")):collectAllAsync(function(_rows, e) err = e end)
|
||||
Assert.eq(logins, 1)
|
||||
Assert.matches(err, "拷贝漫画账号")
|
||||
|
||||
session("")
|
||||
gets, logins, replies = {}, 0, { { code = 401, body = INVALID } }
|
||||
Client:new(settings.getSource("copymanga")):collectAllAsync(function(_rows, e) err = e end)
|
||||
Assert.eq(logins, 0)
|
||||
Assert.matches(err, "拷贝漫画账号")
|
||||
end
|
||||
|
||||
-- 并发 401:只登录一次,所有等待的请求都用新令牌重试成功;另一个实例也拿到新令牌。
|
||||
do
|
||||
session("secret")
|
||||
gets, logins, login_ok, login_hold = {}, 0, true, {}
|
||||
replies = { { code = 401, body = INVALID }, { code = 401, body = INVALID },
|
||||
{ code = 200, body = OK }, { code = 200, body = OK } }
|
||||
local a, b = Client:new(settings.getSource("copymanga")), Client:new(settings.getSource("copymanga"))
|
||||
local errs, done = {}, 0
|
||||
local function finish(_rows, e) done = done + 1; errs[#errs + 1] = e end
|
||||
a:collectAllAsync(finish)
|
||||
b:collectAllAsync(finish)
|
||||
Assert.eq(logins, 1, "并发 401 只登录一次")
|
||||
login_hold[1]()
|
||||
login_hold = nil
|
||||
Assert.eq(done, 2)
|
||||
Assert.len(errs, 0)
|
||||
Assert.eq(gets[3].auth, "Token fresh")
|
||||
Assert.eq(gets[4].auth, "Token fresh")
|
||||
Assert.eq(b.token, "fresh")
|
||||
|
||||
-- 另一个请求已换过令牌:用新令牌直接重试,不再登录。
|
||||
local cfg = settings.getSource("copymanga")
|
||||
cfg.token = "stale"
|
||||
settings.saveSource("copymanga", cfg)
|
||||
local c = Client:new(cfg)
|
||||
logins, gets = 0, {}
|
||||
replies = { { code = 200, body = OK } }
|
||||
cfg.token = "newer"
|
||||
settings.saveSource("copymanga", cfg)
|
||||
c:collectAllAsync(function() end)
|
||||
Assert.eq(gets[1].auth, "Token newer", "总是用设置里的最新令牌")
|
||||
Assert.eq(logins, 0)
|
||||
end
|
||||
|
||||
-- 重登遇到网络错误:原样回传,不误导用户去重新登录。
|
||||
do
|
||||
session("secret")
|
||||
gets, logins, replies, login_net = {}, 0, { { code = 401, body = INVALID } }, true
|
||||
local err
|
||||
Client:new(settings.getSource("copymanga")):collectAllAsync(function(_rows, e) err = e end)
|
||||
login_net = false
|
||||
Assert.eq(logins, 1)
|
||||
Assert.eq(err, "connection timed out")
|
||||
end
|
||||
|
||||
require("moonsource_copymanga.auth").clearSession()
|
||||
package.loaded["http.request"] = nil
|
||||
|
||||
return true
|
||||
@@ -0,0 +1,131 @@
|
||||
--[[--
|
||||
拷贝漫画数据映射用例。
|
||||
|
||||
@module tests.moonsource_copymanga.mapper_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")
|
||||
local Mapper = require("moonsource_copymanga.mapper")
|
||||
|
||||
do
|
||||
local book = Mapper.book({
|
||||
name = "测试漫画",
|
||||
path_word = "test-comic",
|
||||
cover = "https://img.example/cover.jpg",
|
||||
author = { { name = "作者甲" }, { name = "作者乙" } },
|
||||
})
|
||||
Assert.eq(book.source_id, "copymanga")
|
||||
Assert.eq(book.stable_id, "test-comic")
|
||||
Assert.eq(book.authors, "作者甲, 作者乙")
|
||||
Assert.is_nil(Mapper.book({ name = "没有 path_word" }))
|
||||
end
|
||||
|
||||
do
|
||||
local wire = {
|
||||
results = {
|
||||
comic = {
|
||||
uuid = "comic-uuid-1",
|
||||
name = "测试漫画",
|
||||
path_word = "test-comic",
|
||||
cover = "https://img.example/cover.jpg",
|
||||
author = { { name = "作者甲" } },
|
||||
theme = { { name = "冒險" } },
|
||||
brief = "第一行第二行",
|
||||
},
|
||||
},
|
||||
}
|
||||
Assert.eq(Mapper.comicId(wire), "comic-uuid-1")
|
||||
Assert.is_nil(Mapper.comicId({ results = { comic = { name = "无id" } } }))
|
||||
local book = Mapper.detail(wire)
|
||||
Assert.eq(book.stable_id, "test-comic")
|
||||
Assert.eq(book.title, "测试漫画")
|
||||
Assert.eq(book.authors, "作者甲")
|
||||
Assert.eq(book.category, "冒險")
|
||||
Assert.eq(book.intro, "第一行第二行")
|
||||
Assert.eq(book.cover, "https://img.example/cover.jpg")
|
||||
end
|
||||
|
||||
do
|
||||
local groups = Mapper.groups({
|
||||
results = {
|
||||
groups = {
|
||||
extra = { name = "番外", path_word = "extra" },
|
||||
default = { name = "默认", path_word = "default" },
|
||||
},
|
||||
},
|
||||
})
|
||||
Assert.len(groups, 2)
|
||||
Assert.eq(groups[1].path_word, "default")
|
||||
Assert.eq(groups[2].path_word, "extra")
|
||||
end
|
||||
|
||||
do
|
||||
local groups = Mapper.groups({ results = {} })
|
||||
Assert.len(groups, 1)
|
||||
Assert.eq(groups[1].path_word, "default")
|
||||
end
|
||||
|
||||
do
|
||||
local chapters = Mapper.chapters({
|
||||
{ uuid = "uuid-1", name = "第一話", group_path = "default", group_name = "默认" },
|
||||
{ uuid = "uuid-2", name = "第二話", group_path = "default", group_name = "默认" },
|
||||
{ uuid = "uuid-1", name = "第一話", group_path = "default", group_name = "默认" },
|
||||
{ uuid = "uuid-x", name = "番外1", group_path = "extra", group_name = "番外" },
|
||||
})
|
||||
Assert.len(chapters, 3)
|
||||
Assert.eq(chapters[1].idx, 1)
|
||||
Assert.eq(chapters[1].uid, "uuid-1")
|
||||
Assert.eq(chapters[2].title, "第二話")
|
||||
Assert.eq(chapters[3].title, "番外 · 番外1")
|
||||
end
|
||||
|
||||
do
|
||||
local books = Mapper.collect({
|
||||
{
|
||||
comic = {
|
||||
name = "收藏漫画",
|
||||
path_word = "fav-comic",
|
||||
cover = "https://img.example/fav.jpg",
|
||||
author = { { name = "作者丙" } },
|
||||
},
|
||||
},
|
||||
})
|
||||
Assert.len(books, 1)
|
||||
Assert.eq(books[1].stable_id, "fav-comic")
|
||||
Assert.eq(books[1].title, "收藏漫画")
|
||||
Assert.eq(books[1].authors, "作者丙")
|
||||
Assert.eq(books[1].deleted, 0)
|
||||
local invalid, err = Mapper.collect({ { comic = { name = "坏行" } } })
|
||||
Assert.is_nil(invalid)
|
||||
Assert.eq(err, "响应无效")
|
||||
end
|
||||
|
||||
do
|
||||
local pos, uid = Mapper.progress({
|
||||
results = {
|
||||
browse = {
|
||||
chapter_uuid = "chap-uuid-9",
|
||||
chapter_name = "第九话",
|
||||
},
|
||||
},
|
||||
})
|
||||
Assert.eq(uid, "chap-uuid-9")
|
||||
Assert.eq(pos.chapter_title, "第九话")
|
||||
Assert.eq(pos.fraction, 0)
|
||||
end
|
||||
|
||||
do
|
||||
local pos, uid = Mapper.progress({
|
||||
results = { browse = { chapter_id = "fallback-id" } },
|
||||
})
|
||||
Assert.eq(uid, "fallback-id")
|
||||
Assert.is_nil(pos.chapter_title)
|
||||
end
|
||||
|
||||
do
|
||||
Assert.is_nil(Mapper.progress({ results = { browse = nil } }))
|
||||
Assert.is_nil(Mapper.progress({ results = { browse = { chapter_name = "无id" } } }))
|
||||
Assert.is_nil(Mapper.progress({ results = {} }))
|
||||
Assert.is_nil(Mapper.progress(nil))
|
||||
end
|
||||
@@ -0,0 +1,151 @@
|
||||
--[[--
|
||||
拷贝漫画离线打开:已有 CBZ(按章节 uuid 命名)时不触发目录/详情/下载;
|
||||
起始章取显式章号或本地进度;没缓存又没网立即失败。
|
||||
|
||||
@module tests.moonsource_copymanga.open_spec
|
||||
--]]
|
||||
---@diagnostic disable: undefined-field
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Stubs = require("support.stubs")
|
||||
Stubs.install()
|
||||
|
||||
local calls = 0
|
||||
local touched
|
||||
local pending
|
||||
local connected = false
|
||||
|
||||
local function stub(name, factory)
|
||||
package.preload[name] = factory
|
||||
package.loaded[name] = nil
|
||||
end
|
||||
|
||||
stub("source.base", function()
|
||||
return {
|
||||
loadMeta = function(id) return { id = id, name = id, type = "book" } end,
|
||||
defer = function(cb, ...)
|
||||
local args = { n = select("#", ...), ... }
|
||||
require("ui/uimanager"):nextTick(function() cb(unpack(args, 1, args.n)) end)
|
||||
return { cancel = function() end }
|
||||
end,
|
||||
}
|
||||
end)
|
||||
stub("source.shelf", function() return {} end)
|
||||
stub("source.toc", function()
|
||||
return {
|
||||
-- 统一目录接口始终提供持久化快照,不再绕回旧 JSON 字段。
|
||||
read = function()
|
||||
return { { idx = 1, uid = "uuid-1" }, { idx = 2, uid = "uuid-2" }, { idx = 3, uid = "uuid-3" } }
|
||||
end,
|
||||
}
|
||||
end)
|
||||
stub("moonsource_copymanga.mapper", function() return {} end)
|
||||
stub("utils.settings", function()
|
||||
return { getSource = function() return {} end }
|
||||
end)
|
||||
stub("moonsource_copymanga.client", function()
|
||||
return {
|
||||
new = function()
|
||||
return setmetatable({}, {
|
||||
__index = function()
|
||||
return function()
|
||||
calls = calls + 1
|
||||
error("network path reached")
|
||||
end
|
||||
end,
|
||||
})
|
||||
end,
|
||||
headers = function() return {} end,
|
||||
}
|
||||
end)
|
||||
stub("ffi/archiver", function() return { Writer = { new = function() return {} end } } end)
|
||||
stub("utils.paths", function()
|
||||
return {
|
||||
bookWorkDir = function() return "/offline-cache/comic" end,
|
||||
}
|
||||
end)
|
||||
stub("libs/libkoreader-lfs", function()
|
||||
return {
|
||||
attributes = function(path)
|
||||
if path == "/offline-cache/comic/uuid-2.cbz" then
|
||||
return { mode = "file", size = 42 }
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
}
|
||||
end)
|
||||
stub("json", function()
|
||||
return { decode = require("support.json_stub").decode }
|
||||
end)
|
||||
stub("book.books_db", function()
|
||||
return {
|
||||
getToc = function(_, _, max_age)
|
||||
Assert.is_nil(max_age, "离线开书读目录不看 TTL")
|
||||
return '[{"idx":1,"uid":"uuid-1"},{"idx":2,"uid":"uuid-2"},{"idx":3,"uid":"uuid-3"}]'
|
||||
end,
|
||||
}
|
||||
end)
|
||||
stub("book.progress_db", function()
|
||||
return { get = function() return pending end }
|
||||
end)
|
||||
stub("ui/network/manager", function()
|
||||
return { isConnected = function() return connected end }
|
||||
end)
|
||||
stub("book.store", function()
|
||||
return {
|
||||
touch = function(path, identity, opts)
|
||||
touched = { path = path, identity = identity, opts = opts }
|
||||
return true
|
||||
end,
|
||||
}
|
||||
end)
|
||||
|
||||
package.loaded["moonsource_copymanga.chapter"] = nil
|
||||
package.loaded["moonsource_copymanga.source"] = nil
|
||||
local Copymanga = require("moonsource_copymanga.source")
|
||||
local source = Copymanga.new()
|
||||
local identity = {
|
||||
source_id = "copymanga",
|
||||
stable_id = "comic",
|
||||
book = {},
|
||||
}
|
||||
|
||||
local function open(opts)
|
||||
local result, err
|
||||
source:openBookAsync(identity, opts, function(path, open_err)
|
||||
result, err = path, open_err
|
||||
end)
|
||||
Stubs.flush()
|
||||
return result, err
|
||||
end
|
||||
|
||||
-- 显式章号:按目录换成 uuid,命中已下载的 CBZ。
|
||||
do
|
||||
touched = nil
|
||||
local result, err = open({ chapter_idx = 2 })
|
||||
Assert.eq(calls, 0)
|
||||
Assert.eq(result, "/offline-cache/comic/uuid-2.cbz")
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(touched.path, result)
|
||||
Assert.eq(touched.opts.chapter_idx, 2)
|
||||
end
|
||||
|
||||
-- 不带章号:用本地进度的章号。
|
||||
do
|
||||
pending = { chapter_idx = 2, fraction = 0.1 }
|
||||
local result = open(nil)
|
||||
Assert.eq(result, "/offline-cache/comic/uuid-2.cbz")
|
||||
pending = nil
|
||||
end
|
||||
|
||||
-- 本地进度没有章号:按全书比例换算(2/3 → 第 3 章,没下载);离线立即失败,不发请求。
|
||||
do
|
||||
pending = { fraction = 0.7 }
|
||||
touched = nil
|
||||
local result, err = open(nil)
|
||||
Assert.is_nil(result)
|
||||
Assert.eq(err, "网络不可用,请先连接 Wi-Fi")
|
||||
Assert.is_nil(touched)
|
||||
Assert.eq(calls, 0)
|
||||
pending = nil
|
||||
end
|
||||
@@ -0,0 +1,197 @@
|
||||
--[[--
|
||||
拷贝漫画进度:query 拉浏览记录,chapter2 推当前章。
|
||||
|
||||
@module tests.moonsource_copymanga.progress_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")
|
||||
local Stubs = require("support.stubs")
|
||||
|
||||
local fake_client = {}
|
||||
local logged_in = true
|
||||
local toc_store = {}
|
||||
|
||||
local function stub(name, factory)
|
||||
package.preload[name] = factory
|
||||
package.loaded[name] = nil
|
||||
end
|
||||
|
||||
stub("json", function()
|
||||
return {
|
||||
decode = require("support.json_stub").decode,
|
||||
encode = require("support.json_stub").encode,
|
||||
}
|
||||
end)
|
||||
stub("book.books_db", function()
|
||||
return {
|
||||
getToc = function(source_id, stable_id)
|
||||
return toc_store[source_id .. "\31" .. stable_id]
|
||||
end,
|
||||
setToc = function(source_id, stable_id, payload)
|
||||
toc_store[source_id .. "\31" .. stable_id] = payload
|
||||
return true
|
||||
end,
|
||||
get = function() return nil end,
|
||||
}
|
||||
end)
|
||||
stub("utils.settings", function()
|
||||
return {
|
||||
getSource = function() return { token = "tok" } end,
|
||||
saveSource = function() end,
|
||||
}
|
||||
end)
|
||||
stub("moonsource_copymanga.auth", function()
|
||||
return { hasSession = function() return logged_in end }
|
||||
end)
|
||||
stub("moonsource_copymanga.client", function()
|
||||
return {
|
||||
new = function() return fake_client end,
|
||||
headers = function() return {} end,
|
||||
normalizeBaseUrl = function(url) return url end,
|
||||
}
|
||||
end)
|
||||
|
||||
package.loaded["source.toc"] = nil
|
||||
package.loaded["moonsource_copymanga.source"] = nil
|
||||
local Copymanga = require("moonsource_copymanga.source")
|
||||
local Toc = require("source.toc")
|
||||
local src = Copymanga.new()
|
||||
|
||||
local IDENTITY = {
|
||||
source_id = "copymanga",
|
||||
stable_id = "test-comic",
|
||||
chapter_idx = 2,
|
||||
}
|
||||
|
||||
Toc.put("copymanga", "test-comic", {
|
||||
{ idx = 1, uid = "chap-1", title = "第一话" },
|
||||
{ idx = 2, uid = "chap-2", title = "第二话" },
|
||||
{ idx = 3, uid = "chap-3", title = "第三话" },
|
||||
})
|
||||
|
||||
do
|
||||
logged_in = false
|
||||
local pos, err, meta
|
||||
src:getProgressAsync(IDENTITY, function(p, e, m) pos, err, meta = p, e, m end)
|
||||
Stubs.flush()
|
||||
Assert.is_nil(pos)
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(meta.empty)
|
||||
logged_in = true
|
||||
end
|
||||
|
||||
do
|
||||
fake_client.getProgressAsync = function(_, _, cb)
|
||||
cb({ results = { browse = nil } })
|
||||
return { cancel = function() end }
|
||||
end
|
||||
local pos, err, meta
|
||||
src:getProgressAsync(IDENTITY, function(p, e, m) pos, err, meta = p, e, m end)
|
||||
Assert.is_nil(pos)
|
||||
Assert.is_nil(err)
|
||||
Assert.is_true(meta.empty)
|
||||
end
|
||||
|
||||
do
|
||||
fake_client.getProgressAsync = function(_, _, cb)
|
||||
cb({
|
||||
results = {
|
||||
browse = { chapter_uuid = "chap-2", chapter_name = "第二话" },
|
||||
},
|
||||
})
|
||||
return { cancel = function() end }
|
||||
end
|
||||
local pos, err
|
||||
src:getProgressAsync(IDENTITY, function(p, e) pos, err = p, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(pos.chapter_idx, 2)
|
||||
Assert.eq(pos.chapter_title, "第二话")
|
||||
Assert.eq(pos.fraction, 1 / 3)
|
||||
Assert.eq(pos.extra.chapter_uid, "chap-2")
|
||||
end
|
||||
|
||||
do
|
||||
fake_client.getProgressAsync = function(_, _, cb)
|
||||
cb(nil, "网络错误")
|
||||
return { cancel = function() end }
|
||||
end
|
||||
local pos, err
|
||||
src:getProgressAsync(IDENTITY, function(p, e) pos, err = p, e end)
|
||||
Assert.is_nil(pos)
|
||||
Assert.eq(err, "网络错误")
|
||||
end
|
||||
|
||||
-- 云端读到缓存目录里没有的新话:缓存没过期也强制重拉目录再定位。
|
||||
do
|
||||
local detail_calls = 0
|
||||
fake_client.getProgressAsync = function(_, _, cb)
|
||||
cb({ results = { browse = { chapter_uuid = "chap-4", chapter_name = "第四话" } } })
|
||||
return { cancel = function() end }
|
||||
end
|
||||
fake_client.detailAsync = function(_, _, cb)
|
||||
detail_calls = detail_calls + 1
|
||||
cb({ results = { comic = { path_word = "test-comic" } } })
|
||||
return { cancel = function() end }
|
||||
end
|
||||
fake_client.chaptersAsync = function(_, _, _, cb)
|
||||
cb({
|
||||
{ uuid = "chap-1", name = "第一话" }, { uuid = "chap-2", name = "第二话" },
|
||||
{ uuid = "chap-3", name = "第三话" }, { uuid = "chap-4", name = "第四话" },
|
||||
})
|
||||
return { cancel = function() end }
|
||||
end
|
||||
local pos, err
|
||||
src:getProgressAsync(IDENTITY, function(p, e) pos, err = p, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(detail_calls, 1)
|
||||
Assert.eq(pos.chapter_idx, 4)
|
||||
Assert.eq(pos.extra.chapter_uid, "chap-4")
|
||||
end
|
||||
|
||||
-- 没有章号不能默认推第一章,否则会把云端浏览记录改回开头。
|
||||
do
|
||||
local ok, err
|
||||
src:putProgressAsync({ source_id = "copymanga", stable_id = "test-comic" }, {},
|
||||
function(value, e) ok, err = value, e end)
|
||||
Stubs.flush()
|
||||
Assert.is_nil(ok)
|
||||
Assert.eq(err, "缺少章节信息")
|
||||
end
|
||||
|
||||
do
|
||||
logged_in = false
|
||||
local ok, err
|
||||
src:putProgressAsync(IDENTITY, { chapter_idx = 2 }, function(value, e) ok, err = value, e end)
|
||||
Stubs.flush()
|
||||
Assert.is_nil(ok)
|
||||
Assert.eq(err, "请先登录拷贝漫画账号")
|
||||
logged_in = true
|
||||
end
|
||||
|
||||
do
|
||||
local pushed
|
||||
fake_client.chapterAsync = function(_, stable_id, uid, cb)
|
||||
pushed = { stable_id = stable_id, uid = uid }
|
||||
cb({ code = 200 })
|
||||
return { cancel = function() end }
|
||||
end
|
||||
local ok, err
|
||||
src:putProgressAsync(IDENTITY, { chapter_idx = 3 }, function(value, e) ok, err = value, e end)
|
||||
Assert.is_true(ok)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(pushed.stable_id, "test-comic")
|
||||
Assert.eq(pushed.uid, "chap-3")
|
||||
end
|
||||
|
||||
do
|
||||
local ok, err
|
||||
src:putProgressAsync(IDENTITY, {
|
||||
chapter_idx = 1,
|
||||
extra = { chapter_uid = "chap-1", chapter_idx = 1 },
|
||||
}, function(value, e) ok, err = value, e end)
|
||||
Assert.is_true(ok)
|
||||
Assert.is_nil(err)
|
||||
end
|
||||
|
||||
Stubs.flush()
|
||||
@@ -0,0 +1,69 @@
|
||||
--[[--
|
||||
拷贝漫画 chapter2 图片排序用例。
|
||||
|
||||
@module tests.moonsource_copymanga.protocol_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")
|
||||
local Protocol = require("moonsource_copymanga.protocol")
|
||||
|
||||
do
|
||||
local images, err = Protocol.chapterImages({
|
||||
results = {
|
||||
chapter = {
|
||||
contents = {
|
||||
{ url = "https://img.example/002.c800x.webp" },
|
||||
{ url = "https://img.example/001.c800x.webp" },
|
||||
},
|
||||
words = { 1, 0 },
|
||||
},
|
||||
},
|
||||
})
|
||||
Assert.is_nil(err)
|
||||
Assert.len(images, 2)
|
||||
Assert.eq(images[1], "https://img.example/001.c800x.jpg")
|
||||
Assert.eq(images[2], "https://img.example/002.c800x.jpg")
|
||||
end
|
||||
|
||||
do
|
||||
local images, err = Protocol.chapterImages({
|
||||
chapter = {
|
||||
contents = {
|
||||
{ url = "https://img.example/a.webp.c1500x.webp" },
|
||||
{ url = "https://img.example/b.jpg.c2000x.jpg" },
|
||||
},
|
||||
},
|
||||
})
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(images[1], "https://img.example/a.jpg.c800x.jpg")
|
||||
Assert.eq(images[2], "https://img.example/b.jpg.c800x.jpg")
|
||||
end
|
||||
|
||||
do
|
||||
local images, err = Protocol.chapterImages({
|
||||
chapter = {
|
||||
contents = {
|
||||
{ url = "https://img.example/001.webp" },
|
||||
{ url = "https://img.example/002.webp" },
|
||||
},
|
||||
},
|
||||
})
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(images[1], "https://img.example/001.webp")
|
||||
Assert.eq(images[2], "https://img.example/002.webp")
|
||||
end
|
||||
|
||||
do
|
||||
local images, err = Protocol.chapterImages({})
|
||||
Assert.is_nil(images)
|
||||
Assert.eq(err, "章节图片解析失败")
|
||||
end
|
||||
|
||||
do
|
||||
local images, err = Protocol.chapterImages({
|
||||
chapter = { contents = { { url = "not-a-url" } }, words = { 0 } },
|
||||
})
|
||||
Assert.is_nil(images)
|
||||
Assert.eq(err, "章节图片解析失败")
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
--[[--
|
||||
站点选择保留已有配置,自定义非法地址不保存;登录离线立即反馈。
|
||||
|
||||
@module tests.moonsource_copymanga.setting_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
local Themed = require("support.theme_stubs")
|
||||
Themed.install()
|
||||
local cfg = { base_url = "https://api.example", token = "keep" }
|
||||
local saves, reloads = 0, 0
|
||||
package.loaded["utils.settings"] = {
|
||||
getSource = function() return cfg end,
|
||||
saveSource = function() saves = saves + 1 end,
|
||||
}
|
||||
package.loaded["source.registry"] = { reload = function() reloads = reloads + 1 end }
|
||||
package.loaded["ui.themes.widgets.row"] = { widget = function(spec) return spec end }
|
||||
local Setting = require("moonsource_copymanga.setting")
|
||||
local rows = Setting.rows()
|
||||
rows[1](500).callback()
|
||||
local sheet = Themed.last("sheet")
|
||||
Assert.eq(sheet.items[1].text, "https://www.mangacopy.com")
|
||||
Assert.eq(sheet.items[2].text, "https://www.copy4000.com")
|
||||
Assert.eq(sheet.items[3].text, "https://api.manga2026.xyz")
|
||||
Themed.choose(sheet, 2)
|
||||
Assert.eq(cfg.base_url, "https://www.copy4000.com")
|
||||
Assert.eq(cfg.token, "keep")
|
||||
Assert.eq(saves, 1)
|
||||
Assert.eq(reloads, 1)
|
||||
|
||||
rows[1](500).callback()
|
||||
Assert.eq(Themed.last("sheet").items[2].variant, "primary")
|
||||
Themed.choose(Themed.last("sheet"), 4)
|
||||
Themed.last("input").callback("https://site.example/path")
|
||||
Assert.eq(cfg.base_url, "https://www.copy4000.com")
|
||||
Assert.eq(saves, 1)
|
||||
Assert.eq(Themed.last("toast").text, "站点地址无效")
|
||||
Themed.last("input").callback(" https://api.example/ ")
|
||||
Assert.eq(cfg.base_url, "https://api.example")
|
||||
Assert.eq(cfg.token, "keep")
|
||||
|
||||
rows[1](500).callback()
|
||||
Themed.choose(Themed.last("sheet"), 3)
|
||||
Assert.eq(cfg.base_url, "https://api.manga2026.xyz")
|
||||
Assert.eq(cfg.token, "keep")
|
||||
|
||||
local network = require("ui/network/manager")
|
||||
network.isConnected = function() return false end
|
||||
rows[2](500).callback()
|
||||
Themed.last("input").callback("user", "pass")
|
||||
Assert.eq(Themed.last("toast").text, "网络不可用,请先连接 Wi-Fi")
|
||||
Assert.is_nil(Themed.last("progress_dialog"))
|
||||
@@ -0,0 +1,91 @@
|
||||
--[[--
|
||||
拷贝漫画书架同步:复用 Shelf 的本地加删上行与远端对账。
|
||||
|
||||
@module tests.moonsource_copymanga.sync_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
|
||||
local fake_client = {}
|
||||
local calls = {}
|
||||
local finalized = {}
|
||||
|
||||
local function stub(name, factory)
|
||||
package.preload[name] = factory
|
||||
package.loaded[name] = nil
|
||||
end
|
||||
|
||||
stub("book.books_db", function()
|
||||
return {
|
||||
pendingDeleteIds = function() return { "gone-comic" } end,
|
||||
pendingShelfAddIds = function() return { "new-comic" } end,
|
||||
markSynced = function(_source_id, stable_id)
|
||||
calls[#calls + 1] = "synced:" .. stable_id
|
||||
return true
|
||||
end,
|
||||
get = function() return nil end,
|
||||
remove = function(_, stable_id)
|
||||
finalized[#finalized + 1] = stable_id
|
||||
return true
|
||||
end,
|
||||
reconcile = function()
|
||||
calls[#calls + 1] = "reconcile"
|
||||
return true, 0
|
||||
end,
|
||||
}
|
||||
end)
|
||||
stub("utils.settings", function()
|
||||
return {
|
||||
getSource = function() return { token = "tok" } end,
|
||||
saveSource = function() end,
|
||||
}
|
||||
end)
|
||||
stub("moonsource_copymanga.auth", function()
|
||||
return { hasSession = function() return true end }
|
||||
end)
|
||||
stub("moonsource_copymanga.client", function()
|
||||
return {
|
||||
new = function() return fake_client end,
|
||||
headers = function() return {} end,
|
||||
normalizeBaseUrl = function(url) return url end,
|
||||
}
|
||||
end)
|
||||
|
||||
fake_client.removeFromShelfAsync = function(_client, stable_id, cb)
|
||||
calls[#calls + 1] = "remove:" .. stable_id
|
||||
cb({ code = 200 })
|
||||
return { cancel = function() end }
|
||||
end
|
||||
fake_client.addToShelfAsync = function(_client, stable_id, cb)
|
||||
calls[#calls + 1] = "add:" .. stable_id
|
||||
cb({ code = 200 })
|
||||
return { cancel = function() end }
|
||||
end
|
||||
fake_client.shelfSyncAsync = function(_client, cb)
|
||||
calls[#calls + 1] = "shelf"
|
||||
cb({})
|
||||
return { cancel = function() end }
|
||||
end
|
||||
|
||||
package.loaded["moonsource_copymanga.source"] = nil
|
||||
local src = require("moonsource_copymanga.source").new()
|
||||
|
||||
local result, err
|
||||
src:syncBooksAsync({}, function(r, e) result, err = r, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(calls[1], "remove:gone-comic")
|
||||
Assert.eq(calls[2], "shelf")
|
||||
Assert.eq(calls[3], "add:new-comic")
|
||||
Assert.eq(calls[4], "synced:new-comic")
|
||||
Assert.eq(calls[5], "shelf")
|
||||
Assert.eq(calls[6], "reconcile")
|
||||
Assert.eq(finalized[1], "gone-comic")
|
||||
Assert.eq(result.pushed, 2)
|
||||
|
||||
calls = {}
|
||||
src:syncBooksAsync({ dirty_only = true }, function(r, e) result, err = r, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(calls[1], "remove:gone-comic")
|
||||
Assert.eq(calls[2], "add:new-comic")
|
||||
Assert.eq(calls[3], "synced:new-comic")
|
||||
Assert.eq(result.pushed, 2)
|
||||
@@ -0,0 +1,70 @@
|
||||
--[[--
|
||||
拷贝漫画目录缓存:uid ↔ idx 与全书 fraction。
|
||||
|
||||
@module tests.moonsource_copymanga.toc_spec
|
||||
--]]
|
||||
|
||||
local Assert = require("support.assert")
|
||||
|
||||
local store = {}
|
||||
local now = 1000
|
||||
local original_time = os.time
|
||||
os.time = function() return now end
|
||||
package.preload["book.books_db"] = function()
|
||||
return {
|
||||
getToc = function(source_id, stable_id, max_age)
|
||||
local row = store[source_id .. "\31" .. stable_id]
|
||||
if not row or (max_age and now - row.at >= max_age) then return nil end
|
||||
return row.payload, row.at
|
||||
end,
|
||||
setToc = function(source_id, stable_id, payload)
|
||||
store[source_id .. "\31" .. stable_id] = { payload = payload, at = now }
|
||||
return true
|
||||
end,
|
||||
}
|
||||
end
|
||||
package.preload["json"] = function()
|
||||
return {
|
||||
decode = require("support.json_stub").decode,
|
||||
encode = require("support.json_stub").encode,
|
||||
}
|
||||
end
|
||||
|
||||
package.loaded["source.toc"] = nil
|
||||
local Toc = require("source.toc")
|
||||
|
||||
local list = {
|
||||
{ idx = 1, uid = "u1", title = "第一话" },
|
||||
{ idx = 2, uid = "u2", title = "第二话" },
|
||||
{ idx = 3, uid = "u3", title = "第三话" },
|
||||
}
|
||||
|
||||
do
|
||||
Assert.is_true(Toc.put("copymanga", "comic-a", list))
|
||||
Assert.eq(Toc.uid("copymanga", "comic-a", 2), "u2")
|
||||
Assert.eq(Toc.index("copymanga", "comic-a", "u3"), 3)
|
||||
Assert.is_nil(Toc.uid("copymanga", "comic-a", 99))
|
||||
Assert.is_nil(Toc.index("copymanga", "comic-a", "missing"))
|
||||
Assert.is_nil(Toc.index("copymanga", "comic-a", nil))
|
||||
-- 数字 uid 与字符串 uid 同一键
|
||||
local numbered = { { idx = 1, uid = 10, title = "话" } }
|
||||
Assert.is_true(Toc.put("copymanga", "comic-n", numbered))
|
||||
Assert.eq(Toc.index("copymanga", "comic-n", 10), 1)
|
||||
Assert.eq(Toc.index("copymanga", "comic-n", "10"), 1)
|
||||
end
|
||||
|
||||
do
|
||||
Assert.eq(Toc.wholeFraction("copymanga", "comic-a", 1, 0), 0)
|
||||
Assert.eq(Toc.wholeFraction("copymanga", "comic-a", 2, 0), 1 / 3)
|
||||
Assert.eq(Toc.wholeFraction("copymanga", "comic-a", 3, 0.5), (2 + 0.5) / 3)
|
||||
Assert.is_nil(Toc.wholeFraction("copymanga", "missing", 1, 0))
|
||||
end
|
||||
|
||||
do
|
||||
Toc.clear()
|
||||
Assert.eq(Toc.index("copymanga", "comic-a", "u1"), 1)
|
||||
end
|
||||
|
||||
now = now + 6 * 60 * 60
|
||||
Assert.eq(Toc.read("copymanga", "comic-a")[1].uid, "u1", "目录不做时间保鲜")
|
||||
os.time = original_time
|
||||
Reference in new issue
Block a user