135 lines
4.2 KiB
Lua
135 lines
4.2 KiB
Lua
--[[--
|
|
进度上传:换不出 CFI 就丢掉这一版;文件还没落地则留着等下次。成功时 locator 是 CFI,百分比仍是 0..1。
|
|
|
|
@module tests.linguoland.progress_spec
|
|
--]]
|
|
---@diagnostic disable: undefined-field, missing-fields
|
|
|
|
local Assert = require("support.assert")
|
|
local JSON = require("support.json_stub")
|
|
package.loaded["json"] = JSON
|
|
package.loaded["l10n"] = {
|
|
apply = function() end,
|
|
get = function()
|
|
return function(msgid) return msgid end
|
|
end,
|
|
}
|
|
package.loaded["utils.settings"] = {
|
|
getSource = function() return { token = "test-token" } end,
|
|
saveSource = function() end,
|
|
}
|
|
package.loaded["source.auth"] = {
|
|
expired = function() return false end,
|
|
ok = function() end,
|
|
expire = function(_id, msg) return msg end,
|
|
}
|
|
package.loaded["ui/uimanager"] = {
|
|
nextTick = function(_, fn) fn() end,
|
|
}
|
|
package.loaded["ui.guard"] = {
|
|
call = function(_, fn, ...) return fn(...) end,
|
|
}
|
|
|
|
local rows = {}
|
|
package.loaded["book.books_db"] = {
|
|
get = function(_id, stable_id) return rows[stable_id] end,
|
|
}
|
|
local files = {}
|
|
package.loaded["libs/libkoreader-lfs"] = {
|
|
attributes = function(path, field)
|
|
if field == "mode" and files[path] then return "file" end
|
|
end,
|
|
}
|
|
package.loaded["moonsource_linguoland.cfi"] = {
|
|
fromFile = function(_path, xpointer)
|
|
if xpointer:find("missing", 1, true) then return nil end
|
|
return "epubcfi(/6/2!/4/2/1:4)"
|
|
end,
|
|
}
|
|
|
|
local requests = {}
|
|
local function reply()
|
|
return nil, "down"
|
|
end
|
|
package.loaded["http.request"] = {
|
|
get = function(url, _opts, cb)
|
|
requests[#requests + 1] = { method = "GET", url = url }
|
|
cb(reply("GET", url))
|
|
return { cancel = function() end }
|
|
end,
|
|
post = function(url, body, _opts, cb)
|
|
requests[#requests + 1] = { method = "POST", url = url, body = body }
|
|
cb(reply("POST", url, body))
|
|
return { cancel = function() end }
|
|
end,
|
|
request = function()
|
|
return { cancel = function() end }
|
|
end,
|
|
ok = function(code)
|
|
local n = tonumber(code)
|
|
return n ~= nil and n >= 200 and n < 300
|
|
end,
|
|
}
|
|
|
|
local Linguoland = require("moonsource_linguoland.source")
|
|
local src = Linguoland.new()
|
|
|
|
local function push(identity, pos)
|
|
local ok, err, meta
|
|
src:putProgressAsync(identity, pos, function(a, b, c)
|
|
ok, err, meta = a, b, c
|
|
end)
|
|
return ok, err, meta
|
|
end
|
|
|
|
local xp = "/body/DocFragment[1]/body/p/text().4"
|
|
|
|
local ok, err, meta = push({ stable_id = "1", book = {} }, { locator = xp, fraction = 0.5 })
|
|
Assert.is_nil(ok)
|
|
Assert.eq(err, "缺少书架条目 ID")
|
|
Assert.is_true(meta.permanent)
|
|
Assert.eq(#requests, 0)
|
|
|
|
rows["1"] = { path = "/books/book.epub", meta = { source = { shelf_entry_id = 9 } } }
|
|
ok, err, meta = push({ stable_id = "1", book = {} }, { fraction = 0.5 })
|
|
Assert.eq(err, "没有可换算的阅读位置")
|
|
Assert.is_true(meta.permanent)
|
|
|
|
ok, err, meta = push({ stable_id = "1", book = {} }, { locator = xp, fraction = 0.5 })
|
|
Assert.eq(err, "书籍文件还没下载")
|
|
Assert.is_nil(meta)
|
|
|
|
files["/books/book.txt"] = true
|
|
rows["1"].path = "/books/book.txt"
|
|
ok, err, meta = push({ stable_id = "1", book = {} }, { locator = xp, fraction = 0.5 })
|
|
Assert.eq(err, "这个格式没有 CFI")
|
|
Assert.is_true(meta.permanent)
|
|
|
|
files["/books/book.epub"] = true
|
|
rows["1"].path = "/books/book.epub"
|
|
ok, err, meta = push({ stable_id = "1", book = {} }, { locator = xp .. "missing", fraction = 0.5 })
|
|
Assert.eq(err, "位置换算失败")
|
|
Assert.is_true(meta.permanent)
|
|
|
|
requests = {}
|
|
reply = function(method)
|
|
if method == "GET" then
|
|
return '{"progress":{"updatedAt":"2026-01-01T00:00:00Z","percent":0.2}}'
|
|
end
|
|
return '{"disposition":"accepted","progress":{"updatedAt":"2026-01-02T00:00:00Z"}}'
|
|
end
|
|
ok, err = push({
|
|
stable_id = "1",
|
|
book = { path = "/books/book.epub", meta = { source = { shelf_entry_id = 9 } } },
|
|
}, { locator = xp, fraction = 0.5 })
|
|
Assert.is_true(ok)
|
|
Assert.is_nil(err)
|
|
Assert.eq(requests[1].method, "GET")
|
|
Assert.eq(requests[2].method, "POST")
|
|
local body = JSON.decode(requests[2].body)
|
|
Assert.eq(body.locator, "epubcfi(/6/2!/4/2/1:4)")
|
|
Assert.eq(body.percent, 0.5)
|
|
Assert.eq(body.shelfEntryId, 9)
|
|
Assert.eq(body.baseUpdatedAt, "2026-01-01T00:00:00Z")
|
|
Assert.is_nil(body.anchor)
|