片段
--[[--
爱看书多页章节完整交付、中途失败和取消,兼容旧缓存与目录落盘失败。
@module tests.moonsource_18ta.reading_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local Stubs = require("support.stubs")
local Config = require("support.config")
local path = "/aks/1/9/100.html"
local requests, delayed, failed, malformed = {}, false, false, false
package.loaded["http.request"] = {
headerAll = function() return {} end,
get = function(url, _opts, cb)
local request = { url = url, cb = cb }
requests[#requests + 1] = request
local handle = { cancel = function() request.cancelled = true end }
local page = tonumber(url:match("_(%d+)%.html$")) or 1
if delayed and page == 2 then return handle end
if failed and page == 2 then cb(nil, "timeout"); return handle end
if malformed then cb(' 片段 第' .. page .. '页
第1页
\n第2页
") Assert.eq(calls, 1) Assert.len(requests, 2, "不能取下一章") failed = true client:contentAsync(path, done) Assert.is_nil(result, "第二页失败不能交付第一页") Assert.eq(reason, "timeout") failed, malformed = false, true client:contentAsync(path, done) Assert.is_nil(result) Assert.eq(reason, "响应无效") malformed, delayed = false, true local before = calls local handle = client:contentAsync(path, done) handle.cancel() Assert.is_true(requests[#requests].cancelled) requests[#requests].cb('迟到
旧单页
") file:close() Assert.is_false(ops.reuse(cache), "在线升级旧单页缓存") online = false Assert.is_true(ops.reuse(cache), "离线不丢弃旧缓存") online = true file = assert(io.open(cache, "wb")) file:write("完整章节
") file:close() Assert.is_true(ops.reuse(cache)) os.remove(cache) delayed = false local payload ops.fetchContent({}, { origin = "https://www.18ta.com" .. path, title = "章节" }, function(data) payload = data end) Assert.eq(payload.mark, "moon:18ta:complete") Assert.is_true(payload.html:find("第2页", 1, true) ~= nil) source:loadTocAsync({ stable_id = "9" }, function(data) result = data end) Stubs.flush() Assert.eq(result, toc, "旧目录离线继续可用") source.client.bookAsync = function(_self, _id, cb) cb('') end source:refreshTocAsync({ stable_id = "9" }, done) Stubs.flush() Assert.is_nil(result) Assert.eq(reason, "章节目录保存失败")