--[[-- 塔读:微信扫码、书架页、搜索卡片、目录序号、正文去广告。 @module tests.moonsource_tadu.client_spec --]] ---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch package.preload["json"] = function() return require("support.json_stub") end local Assert = require("support.assert") local Json = require("support.json_stub") local Mapper = require("moonsource_tadu.mapper") Assert.eq(Mapper.qrUuid(''), "abc123") Assert.eq(Mapper.qrText("abc123"), "https://open.weixin.qq.com/connect/confirm?uuid=abc123") Assert.eq((Mapper.wxStatus('{"wx_errcode":408}')), "wait") local kind, wx_code, errcode = Mapper.wxStatus('{"wx_errcode":405,"wx_code":"wx1"}') Assert.eq(kind, "ok") Assert.eq(wx_code, "wx1") Assert.eq(errcode, 405) Assert.eq((Mapper.wxStatus("window.wx_errcode=403;window.wx_code='';")), "fail") Assert.eq((Mapper.wxStatus("window.wx_errcode=402;")), "expired") Assert.eq(Mapper.user({ data = { user = { nickname = "塔" } } }).name, "塔") Assert.is_nil(Mapper.user({ data = {} })) local card = [[ 三体宇宙 星际
刘慈欣
]] local found = Mapper.books(card) Assert.eq(#found, 1) Assert.eq(found[1].stable_id, "988420") Assert.eq(found[1].title, "三体宇宙") Assert.eq(found[1].authors, "刘慈欣") Assert.eq(found[1].intro, "星际") Assert.eq(found[1].cover, "https://img/a.jpg") Assert.eq(found[1].source_id, "ext.tadu") local home, home_err = Mapper.shelfPage("小说,小说网-塔读小说网|免费阅读最新热门小说") Assert.is_nil(home) Assert.eq(home_err, "未登录塔读") local empty = Mapper.shelfPage("我的书架

您还没有收藏

") Assert.eq(#empty, 0) local kong = Mapper.shelfPage('个人中心-塔读文学网
') Assert.eq(#kong, 0) local gone = Mapper.shelfPage("出错了-塔读文学网") Assert.is_nil(gone) local row = Mapper.shelf([[ 一书

作者甲

]]) Assert.eq(#row, 1) Assert.eq(row[1].stable_id, "9") Assert.eq(row[1].title, "一书") Assert.eq(row[1].authors, "作者甲") local quoted = Mapper.shelf([[ 一书 第1章

作者甲

]]) Assert.eq(quoted[1].title, "一书") Assert.eq(quoted[1].authors, "作者甲") Assert.is_true(Mapper.shelfHasNext('', 1)) Assert.is_true(Mapper.shelfHasNext('', 1)) Assert.is_true(not Mapper.shelfHasNext('', 1)) local detail = Mapper.detail([[ 系统劈腿,判给我半个系统 小小怪 著

曾经。

]], "1034377") Assert.eq(detail.title, "系统劈腿,判给我半个系统") Assert.eq(detail.authors, "小小怪") Assert.eq(detail.intro, "曾经。") Assert.eq(detail.cover, "https://img/cover_a.jpg") local toc = Mapper.chapters([[ 第1章 要留清白在人间 第2章 系统脚踏两条船 第1章 要留清白在人间 ]]) Assert.eq(#toc, 2) Assert.eq(toc[1].uid, "102154602") Assert.eq(toc[1].source_idx, "1") Assert.eq(toc[1].toc_version, Mapper.TOC_VERSION) Assert.eq(toc[2].title, "第2章 系统脚踏两条船") local text = Mapper.content({ status = 200, data = { content = '

正文中提及塔读

隐藏

下一段

' }, }, "9") Assert.eq(text, "

正文中提及塔读

下一段

") local denied = select(2, Mapper.content({ status = 500, msg = "请登录" })) Assert.eq(denied, "请登录") local settings = {} local requests = {} local deleted = false local function reply(url, payload) if url:find("long.open.weixin.qq.com", 1, true) then return '{"wx_errcode":405,"wx_code":"wx1"}' end if url:find("open.weixin.qq.com/connect/qrconnect", 1, true) then return '' end if url:find("/webchat/invoke/", 1, true) then return "ok", { "session=s1" } end if url:find("/user/info", 1, true) then return Json.encode({ data = { user = { nickname = "塔" } } }) end if url:find("pgNum=1", 1, true) then if deleted then return '个人中心-塔读文学网
' end return '个人中心-塔读文学网一书' end if url:find("pgNum=2", 1, true) then return '个人中心-塔读文学网二书' end if url:find("/api/v1/favorite/", 1, true) then return Json.encode({ status = 902, msg = "已收藏" }) end if url:find("/delFavorites", 1, true) then deleted = true return Json.encode({ status = 200 }) end if url:find("/search", 1, true) then return card end if url:find("/book/1034377/9/", 1, true) then return '' end if url:find("/getPartContentByCodeTable/", 1, true) then return Json.encode({ status = 200, data = { content = '

正文

塔读APP

' } }) end return payload end package.preload["utils.settings"] = function() return { getSource = function() return settings end, saveSource = function() end, } end package.preload["http.request"] = function() return { headerAll = function(res) return res and res.cookies or {} end, get = function(url, options, callback) requests[#requests + 1] = { method = "GET", url = url, headers = options.headers } local body, cookies = reply(url) callback(body, nil, cookies and { cookies = cookies } or nil) return { cancel = function() end } end, post = function(url, payload, options, callback) requests[#requests + 1] = { method = "POST", url = url, body = payload, headers = options.headers } local body, cookies = reply(url, payload) callback(body, nil, cookies and { cookies = cookies } or nil) return { cancel = function() end } end, } end local Client = require("moonsource_tadu.client") local uuid Client.qrCreateAsync(function(value) uuid = value end) Assert.eq(uuid, "abc123") local polled, code Client.qrPollAsync("abc123", 408, function(value, wx) polled, code = value, wx end) Assert.eq(polled, "ok") Assert.eq(code, "wx1") Assert.is_true(requests[#requests].url:find("last=408", 1, true) ~= nil) local user Client.exchangeAsync("wx1", function(value) user = value end) Assert.eq(user.name, "塔") Assert.eq(settings.logged_in, true) Assert.eq(settings.name, "塔") Assert.is_true(settings.cookie:find("session=s1", 1, true) ~= nil) local shelf_html Client.shelfAsync(function(html) shelf_html = html end) local shelf = Mapper.shelf(shelf_html) Assert.eq(#shelf, 2) Assert.eq(shelf[1].stable_id, "9") Assert.eq(shelf[2].stable_id, "8") local added Client.addAsync("9", function(ok) added = ok end) Assert.eq(added, true) local removed Client.removeAsync("9", function(ok) removed = ok end) Assert.eq(removed, true) local removal for _idx, request in ipairs(requests) do if request.url:find("/delFavorites", 1, true) then removal = request end end Assert.eq(removal.body, "bookId=9") local books Client.searchAsync("三体", function(html) books = Mapper.books(html) end) Assert.eq(books[1].title, "三体宇宙") Assert.is_true(requests[#requests].body:find("query=", 1, true) ~= nil) local chapter Client.contentAsync("1034377", "1", function(html) chapter = html end, "9") Assert.eq(chapter, "

正文

") Assert.is_true(requests[#requests].url:find("/getPartContentByCodeTable/1034377/1", 1, true) ~= nil)