--[[--
塔读:微信扫码、书架页、搜索卡片、目录序号、正文去广告。
@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 = [[
三体宇宙
星际
您还没有收藏
") Assert.eq(#empty, 0) local kong = Mapper.shelfPage('作者甲
作者甲
曾经。
]], "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 '正文
塔读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)
系统劈腿,判给我半个系统