--[[-- 微信章节正文的 range 坐标原文与 reader 状态回归测试。 @module tests.moonsource_weread.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") package.preload["json"] = function() return { encode = require("support.json_stub").encode, decode = require("support.json_stub").decode, } end local queries = {} local app_err = nil local trial = false local bom = false local txt_zip = false local zip_text_missing = false local info_requests = {} local app_response package.preload["workers.job"] = function() return { run = function(fn, opts) opts.on_done(fn()) return { cancel = function() end } end, } end package.preload["http.request"] = function() return { header = function() return "encrypt-key" end, ok = function(code) return type(code) == "number" and code >= 200 and code < 300 end, } end package.preload["moonsource_weread.eink"] = function() return { credentials = function() return { vid = "42" } end, callAsync = function(method, path, opts, cb) if path == "/book/info" then info_requests[#info_requests + 1] = opts.query else queries[#queries + 1] = opts.query end if app_response then return app_response(method, path, opts, cb) end if path == "/book/info" then cb({ book = { version = 7 } }) return { cancel = function() end } end if app_err then cb(nil, app_err) else local body if opts.query.bookType == "txt" and not txt_zip then body = string.rep("\0", 257) .. "ustar" else body = "PK" end cb({ body = body, code = 200 }) end return { cancel = function() end } end, } end package.preload["moonsource_weread.auth"] = function() return { hasSession = function() return true end, webPostAsync = function() error("chapter download must not fall back to the web reader") end, webGetAsync = function() error("chapter download must not fall back to the web reader") end, } end local FULL = '
正文'
.. '
\n'
.. '第二段文档
' local PLAIN = "\239\187\191第一行 \n 第二行" local TRIAL = "“第四路,已过浔阳!”\n这是李善德发明的脚程格眼。..." package.preload["moonsource_weread.protocol"] = function() return { readerUrl = function() return "https://weread.qq.com/web/reader/id" end, } end package.preload["moonsource_weread.package"] = function() return { password = function() return "pw" end, unzipNeed = function() return 0 end, formatChaptersParam = function(uids) uids = uids or {} if #uids == 0 then return "0" end local nums = {} for i, u in ipairs(uids) do local n = tonumber(u) if not n then local parts = { "0" } for _idx, x in ipairs(uids) do parts[#parts + 1] = tostring(x) end return table.concat(parts, ",") end nums[i] = n end local ok = true for i = 2, #nums do if nums[i] ~= nums[i - 1] + 1 then ok = false break end end if ok then local a, b = nums[1], nums[#nums] if a == 1 then return "0-" .. b end if a == b then return "0," .. a end return "0," .. a .. "-" .. b end local parts = { "0" } for _idx, n in ipairs(nums) do parts[#parts + 1] = tostring(n) end return table.concat(parts, ",") end, extractChapters = function(files, _book, uids, book_type) local out, style = {}, files["Styles/stylesheets.css"] for _idx, uid in ipairs(uids) do local html = FULL if trial then html = TRIAL elseif bom then html = "98791" .. FULL end -- keep real BOM via bytes below in separate fix out[tostring(uid)] = { text = html, format = book_type == "txt" and "txt" or "html" } end return out, style end, unzip = function() local q = queries[#queries] local ch = tostring(q.chapters or "") if ch == "0" or ch:find("^0%-") or ch:find("^0,") then return { ["Styles/stylesheets.css"] = ".content{text-indent:2em}", dummy = true } end return { dummy = true } end, pickStyle = function(files) return files["Styles/stylesheets.css"] or files["Styles/stylesheet.css"] end, chapterHtml = function() if txt_zip then error("TXT ZIP with text entry must not fall back to chapterHtml") end if trial then return TRIAL end if bom then return "\239\187\191" .. FULL end return FULL end, chapterText = function() return PLAIN end, chapterZipText = function() if txt_zip and not zip_text_missing then return PLAIN end return nil end, } end package.preload["moonsource_weread.assets"] = function() return { localizeAsync = function(_book, _chapter, html, _referer, fetch_style, cb) return fetch_style(function(css) Assert.eq(css, ".content{text-indent:2em}") cb((html:gsub("https://res%.weread%.qq%.com/x%.png", "images/x.png"))) end) end, } end for _, name in ipairs({ "workers.job", "http.request", "moonsource_weread.eink", "moonsource_weread.package", "moonsource_weread.chapter", }) do package.loaded[name] = nil end local Chapter = require("moonsource_weread.chapter") local Context = require("moonsource_weread.context") Context.clear() Context.rememberBookVersion("book", 7) do -- EPUB:解码后的 xhtml 原样交出,不抽 body、不删虚线 / title、不改脚注;range 坐标原文同一份。 local html_out, range_source, format Chapter.fetchHtmlAsync("book", { uid = "chapter" }, function(html, err, raw, fmt) Assert.is_nil(err) html_out, range_source, format = html, raw, fmt end) Assert.eq(html_out, FULL) Assert.eq(range_source, FULL) Assert.eq(format, "html") Assert.eq(#queries, 1) Assert.eq(queries[1].bookType, "epub") Assert.eq(queries[1].chapters, "chapter") Assert.eq(queries[1].bookVersion, 7) Assert.eq(queries[1].offline, "0", "单章在线阅读 offline=0") end do -- TXT:bookType=txt,range 按包内纯文本逐 rune 计,段落化后的 HTML 只用于显示。 local body, range_source, format Chapter.fetchHtmlAsync("book", { uid = "c2", format = "txt" }, function(html, err, raw, fmt) Assert.is_nil(err) body, range_source, format = html, raw, fmt end) Assert.eq(range_source, PLAIN) Assert.eq(format, "txt") Assert.is_true(body:find("", 1, true) ~= nil) -- 段首空白只在显示用 HTML 里剥掉,缩进统一由 CSS 给。 Assert.is_true(body:find("
第二行
", 1, true) ~= nil) Assert.eq(queries[#queries].bookType, "txt") Assert.eq(queries[#queries].chapters, "c2") end do -- 交给宿主:EPUB 是整份 document,只改资源引用;TXT 仍是 html 片段走外壳。 local payload local before = #queries Chapter.fetchContentAsync("book", { idx = 1, uid = "chapter", title = "第一章" }, function(p) payload = p end) -- 正文一次,样式表一次(chapters=0),没有网页分片。 Assert.eq(queries[before + 1].chapters, "chapter") Assert.eq(queries[before + 1].bookType, "epub") Assert.eq(queries[before + 2].chapters, "0") Assert.is_nil(payload.html) local note = '
'
local expected = FULL:gsub("https://res%.weread%.qq%.com/x%.png", "images/x.png")
:gsub(note:gsub("%p", "%%%0"), ''
.. '
')
-- 两份拼接文档:aside 进最后一个 body;epub:type 让 KOReader 页内脚注 / 弹窗都能认。
:gsub("第二段文档