Files

310 lines
16 KiB
Lua
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
晋江收藏页 / 目录 / 网页正文映射与 Cookie 解析离线用例。
@module tests.source.jjwxc_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch
local Assert = require("support.assert")
local source_cfg = {}
package.preload["utils.settings"] = function()
return { getSource = function() return source_cfg end, saveSource = function() end }
end
package.loaded["moonsource_jjwxc.mapper"] = nil
package.loaded["moonsource_jjwxc.setting"] = nil
local Mapper = require("moonsource_jjwxc.mapper")
local Setting = require("moonsource_jjwxc.setting")
do
local html = '<ul id="favul"><li readingorbookmarktime="1"><a href="/book2/123">书名…</a> - <a href="/wapauthor/9">某作者</a> [删]</li>'
.. '<li><a href="/book2/456">另一本</a></li></ul>'
local rows = Mapper.favRows(html)
Assert.len(rows, 2)
Assert.eq(rows[1].id, "123")
Assert.eq(rows[1].author, "某作者")
Assert.is_nil(Mapper.favRows("<html>请登录</html>"))
end
do
local html = '<span> 【言情排行】 </span><div><ul><li><a href="/book2/1" class="track" data-x=\'{"a":1}\'>甲&amp;乙</a></li></ul></div>'
.. '<span>【纯爱排行】</span><ul><li><a href="/book2/2">丙</a></li><li><a href="/book2/3">丁</a></li></ul>'
local sections = Mapper.rankSections(html)
Assert.len(sections, 2)
Assert.eq(sections[1].title, "言情排行")
Assert.eq(sections[1].books[1].stable_id, "1")
Assert.eq(sections[1].books[1].title, "甲&乙")
Assert.len(sections[2].books, 2)
Assert.len(Mapper.rankSections("<html></html>"), 0)
local ranks = Mapper.storeRanks({
{ type = "img" },
{ type = "rankings", data = {
{ more = { rankName = "月榜", channellist = {
{ channel = "言情", channelid = "natural_592" },
{ channel = "纯爱", channelid = "natural_604" },
{ channel = "坏", channelid = "a/b" },
} } },
{ more = {} },
} },
})
Assert.len(ranks, 1)
Assert.eq(ranks[1].title, "月榜")
Assert.len(ranks[1].channels, 2)
Assert.eq(ranks[1].channels[2].id, "natural_604")
local books = Mapper.rankBooks({ data = { data = {
{ novelId = "1", novelName = "甲", authorName = "乙", novelIntroShort = "<p>简介</p>" },
{ novelname = "无号" },
} } })
Assert.len(books, 1)
Assert.eq(books[1].title, "甲")
Assert.eq(books[1].intro, "简介")
Assert.eq(books[1].authors, "乙")
end
do
local books = Mapper.shelfList({
{ id = "1", title = "短…", info = { novelId = 1, novelName = "全名", authorName = "甲", novelSize = "271,300", novelCover = "" } },
{ id = "2", title = "截断", local_book = { stable_id = "2", title = "本地全名" } },
{ id = "3", title = "兜底", author = "乙" },
})
Assert.eq(books[1].title, "全名")
Assert.eq(books[1].meta.words, 271300)
Assert.is_nil(books[1].cover)
Assert.eq(books[2].title, "本地全名")
Assert.eq(books[3].title, "兜底")
end
do
local toc = Mapper.toc({ chapterlist = {
{ chapterid = "0", chaptertype = "1", chaptername = "卷一", islock = "0" },
{ chapterid = "1", chaptertype = "0", chaptername = "第一章", islock = "0", chaptersize = "100" },
{ chapterid = "2", chaptertype = "0", chaptername = "锁定", islock = "1" },
{ chapterid = "3", chaptertype = "0", chaptername = "第三章", islock = "0", isvip = 2 },
} })
Assert.len(toc, 2)
Assert.eq(toc[2].uid, "3")
Assert.eq(toc[2].idx, 2)
Assert.is_true(toc[2].vip)
Assert.is_nil(toc[1].vip)
Assert.eq(toc[1].toc_version, Mapper.TOC_VERSION)
end
do
Assert.eq(Mapper.webContent('<ul class="content_ul"><li class="x">甲<br>乙&amp;丙</li></ul>'), "甲\n乙&丙")
Assert.is_nil(Mapper.webContent("<h1>提示</h1>"))
Assert.eq(Mapper.webContent('<ul class="content_ul"><li>甲<script>var a = "x";</script>乙</li></ul>'), "甲乙")
local vip = '<ul class="content_ul"><li><div id="content_1">vip内容加载中...</div><div id="contentvars">'
.. '<input type="hidden" name="novelid" value="9942287"><input type="hidden" name="chapterid" value="23">'
.. '<input type="hidden" name="readerid" value="85927522"><input type="hidden" name="accessKey" value="PxowDj">'
.. '<input type="hidden" name="cryptInfo" value="UahebT8I2W5DVP5RtJTyCv1C6busic0hCivj23MPTk2HT99HFI3DvvCodX1QFhgRjU3iX1mxo8d2Gew/rq0qmDncXWCcbF7pIJ8MOhTRzV8=">'
.. '<input type="hidden" name="content" value="Emlwl6ggvu2rApuyRLY0AEkcBQrKkbdlc4dnnXiyv2GlVkdadO7XU7ajVLp7r4yNKJsCzyUFsnFXtjXtpHOMCMWh7YMzOwF/YC/3uid37ZFyJcrWtkoot1B5mGTJYe5s0Vdy+pJl3IwBMXuHFH88vVgUfiXj+mxoaeYo/Znglqk="></div></li></ul>'
Assert.eq(Mapper.webContent(vip), "  甲&乙\n\n  丙\238\129\132丁")
Assert.is_nil(Mapper.webContent(vip:gsub("PxowDj", "xxxxxx")))
end
do
-- key / iv 取摘要里任意偏移的窗口,正文也可能是未编码的 UTF-8。
local Aes, md5, Text = require("crypto.aes"), require("ffi/sha2").md5, require("utils.text")
local function enc(plain, source, key_off, iv_off)
local d = md5(source)
d = d .. d
return Text.base64Encode(Aes.cbc_encrypt(plain, d:sub(key_off + 1, key_off + 16), d:sub(iv_off + 1, iv_off + 16)))
end
local meta = Text.base64Encode('{"time":1792544104,"key":"HqgOwYk","ver":"20250926"}')
local body = "  沈玄宁讪讪一笑,道:“我是路过的。”<br><br>  他转身走了,留下一句话:好自为之。"
local page = '<div id="contentvars"><input name="novelid" value="9942287"><input name="chapterid" value="23">'
.. '<input name="readerid" value="85927522"><input name="accessKey" value="AcsRWK">'
.. '<input name="cryptInfo" value="' .. enc(meta, "9942287.23.85927522.AcsRWK", 27, 11) .. '">'
.. '<input name="content" value="' .. enc(body, "HqgOwYk179254410485927522", 12, 28) .. '"></div>'
Assert.eq(require("moonsource_jjwxc.vip").content(page), body)
-- 书号 + 章号为奇数时官方脚本换密钥(issue #78)。密文由 Node crypto 按脚本规则生成,
-- 不跟 Lua 实现共用派生,派生错了这里对不上。奇数章若仍是旧的点号密钥,也要能解开。
local odd = '<div id="contentvars"><input name="novelid" value="100"><input name="chapterid" value="1">'
.. '<input name="readerid" value="42"><input name="accessKey" value="Ab12">'
.. '<input name="cryptInfo" value="fuO73SNX9D4tRXsRYM3+AqBEUOk/rBNxSjkep0cnWPVHs/z7E3F5Lv4aD7VMrTOr0aFEh8aQNfh5YnvP6djkUhASTVDWdyOpNHzGhYFLPfE=">'
.. '<input name="content" value="lErz8EEQyFUYbBR62OfGFxuF2cmlnWeVhUZVe7FnX36I4WLn1A2qP2KisezEaSyHcvWsLD0HZys87HvdW/rPilJ2+znYbX2ls/Myl4wocCry6gXJjrFiENS688VyxfVezdvKwwwBVBUaY5CecSG2hrU+8W8ILu4uX31NgjgUFa0="></div>'
Assert.eq(require("moonsource_jjwxc.vip").content(odd), body)
local odd_old = '<div id="contentvars"><input name="novelid" value="100"><input name="chapterid" value="1">'
.. '<input name="readerid" value="42"><input name="accessKey" value="Ab12">'
.. '<input name="cryptInfo" value="' .. enc(meta, "100.1.42.Ab12", 3, 9) .. '">'
.. '<input name="content" value="' .. enc(body, "HqgOwYk179254410442", 4, 18) .. '"></div>'
Assert.eq(require("moonsource_jjwxc.vip").content(odd_old), body)
-- 现行 jjcontent.js:cryptInfo 是 DES,正文是固定窗口 AES。密文在这里用 Des/Aes 现算,不走 Vip 内部。
local Des = require("crypto.des")
local function turn(source, access)
local hex = md5(source)
local sum = 0
for i = 1, #access do sum = sum + access:byte(i) end
local off = sum % #hex
if off ~= 0 then hex = hex:sub(off + 1) .. hex:sub(1, off) end
return hex
end
local access, novel, chapter, reader = "Ab12Cd", "8718162", "19", "7"
local spun = turn(novel .. "." .. chapter .. "." .. reader .. "." .. access, access)
local info = Des.encryptBase64('{"key":"K1","time":1700000000}', spun:sub(-16, -9), spun:sub(1, 8))
spun = turn("K117000000007", access)
local content = Text.base64Encode(Aes.cbc_encrypt(body, spun:sub(1, 16), spun:sub(-16)))
local current = '<div id="contentvars"><input name="novelid" value="' .. novel .. '">'
.. '<input name="chapterid" value="' .. chapter .. '"><input name="readerid" value="' .. reader .. '">'
.. '<input name="accessKey" value="' .. access .. '"><input name="cryptInfo" value="' .. info .. '">'
.. '<input name="content" value="' .. content .. '"></div>'
Assert.eq(require("moonsource_jjwxc.vip").content(current), body)
-- 段缝:正文切成 c_ span,缝里的字挂在同页 <style> 的 :before/:after content 上(issue #67)。
-- .c_zz 是同规则的配对类名(不在正文里),不得重复拼;\7f1d 是 CSS 十六进制转义。
Assert.eq(Mapper.webContent([[<style>.c_aa:before, .c_zz:after{content:'缝1';} .c_bb:after{content:'\7f1d 2';}</style>]]
.. [[<ul class="content_ul"><li>甲<span class='c_aa'>乙</span><span class="c_bb">丙</span>丁</li></ul>]]),
"甲缝1乙丙缝2丁")
end
do
package.loaded["moonsource_jjwxc.font"] = nil
local Font = require("moonsource_jjwxc.font")
local fh = assert(io.open(debug.getinfo(1, "S").source:match("^@(.*)/[^/]+/[^/]+$") .. "/fixtures/jjwxcfont_9eo8b.ttf", "rb"))
local ttf = fh:read("*a")
fh:close()
local map = Font.mapping(ttf)
local n = 0
for _code in pairs(map) do n = n + 1 end
Assert.eq(n, 200)
Assert.eq(map[0xE044], "他")
Assert.eq(map[0xE9BE], "已")
-- 私有区字 → 真字;字后的零宽占位去掉;未命中的私有区字原样保留。
Assert.eq(Font.decode("\u{E044}\u{200B}说\u{E9BE}\u{FEFF}\u{F8F0}", map), "他说已\u{F8F0}")
Assert.is_nil(Font.mapping("not a font"))
end
do
Assert.eq(Setting.parseCookie("b=2; a=1;bad name=3; c="), "a=1; b=2")
Assert.eq(Setting.fromPayload({ cookies = {
{ domain = ".jjwxc.com", name = "sid", value = "v" },
{ domain = "m.jjwxc.com", name = "JJSESS", value = "w" },
{ domain = "evil.com", name = "x", value = "y" },
} }), "JJSESS=w; sid=v")
end
do
local html = require("utils.text").chapterHtml("第一章", "  甲\n\n  乙〇\n")
Assert.eq(html, "<h1>第一章</h1>\n<p>甲</p>\n<p>乙〇</p>")
end
do
local book = Mapper.book({ novelId = 1, novelName = "x", novelCover = "https://i9-static.jjwxc.net/novelimage.php?novelid=1&coverid=2&ver=abc" })
Assert.eq(book.cover, "https://i9-static.jjwxc.net/novelimage.php?novelid=1&coverid=2")
end
do
local books = Mapper.search({ items = {
{ novelid = 5, novelname = "甲", authorname = "乙", novelintroshort = "简介" },
} })
Assert.len(books, 1)
Assert.eq(books[1].stable_id, "5")
Assert.eq(books[1].title, "甲")
Assert.eq(books[1].authors, "乙")
end
-- 段评:统计只留有评论的真实段(-2 是章末总评),评论页的游标是累计偏移。
do
package.loaded["moonsource_jjwxc.reviews"] = nil
local Reviews = require("moonsource_jjwxc.reviews")
local hot = Reviews.hotspots({
{ paragraph_id = "-2", comment_total = "130" },
{ paragraph_id = "3", comment_total = "7" },
{ paragraph_id = "1", comment_total = "24148" },
{ paragraph_id = "2", comment_total = "0" },
})
Assert.len(hot, 3)
Assert.eq(hot[1].range, "-2")
Assert.eq(hot[2].range, "1")
Assert.eq(hot[3].range, "3")
Assert.eq(hot[3].total, 7)
local page = Reviews.page({ commentTotal = "45", commentList = {
{ commentId = "9", commentAuthor = "甲", commentBody = "好&amp;看<br/>真的", agreenum = "6", comment_date_time = "1792324141" },
} }, 20)
Assert.eq(page.total, 45)
Assert.eq(page.max_idx, 21)
Assert.is_true(page.more)
Assert.eq(page.items[1].content, "好&看\n真的")
Assert.eq(page.items[1].likes, 6)
Assert.eq(page.items[1].time, 1792324141)
Assert.is_false(Reviews.page({ commentTotal = "1", commentList = { { commentId = "1" } } }, 0).more)
Assert.is_nil((Reviews.page({}, 0)))
end
-- App 登录用的 DES-CBC:向量来自 openssl(des-cbc,key KW8Dvm2N,iv 1ae2c94b)。
do
local Des = require("crypto.des")
local cipher = Des.encrypt("hello-世界 password 123", "KW8Dvm2N", "1ae2c94b")
local hex = cipher:gsub(".", function(c) return string.format("%02x", c:byte()) end)
Assert.eq(hex, "6c85a3005530ad3b2e41bb2f25eb77bb5fc29c01b2df52790e15fb89ec87c576")
Assert.eq(Des.decrypt(cipher, "KW8Dvm2N", "1ae2c94b"), "hello-世界 password 123")
Assert.is_nil(Des.decrypt(cipher:sub(1, 15), "KW8Dvm2N", "1ae2c94b"))
Assert.is_nil(Des.decrypt(cipher, "WRONGKEY", "1ae2c94b"))
local rows = Mapper.tokenFavRows({ { novelId = 7, novelName = "甲", authorName = "乙" }, { novelname = "无号" } })
Assert.len(rows, 1)
Assert.eq(rows[1].id, "7")
Assert.eq(rows[1].author, "乙")
end
do
local Des = require("crypto.des")
local md5, Hex = require("ffi/sha2").md5, require("crypto.hex")
package.loaded["moonsource_jjwxc.protocols.app"] = nil
local Client = require("moonsource_jjwxc.protocols.app")
local keystring = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local plain = '{"content":"正文","code":"1004"}'
-- accesskey 字节和 = 97+98+99 = 294,末字符 c(99) 为奇:盐在尾;偏移 294%62=46,长度 floor(294/65)=4
local salt, part = "0123456789ab", keystring:sub(47, 50)
local cipher = Hex.to_hex(Des.encrypt(plain, md5(part .. salt):sub(1, 8), md5(salt):sub(1, 8)))
Assert.eq(Client.decryptBody(cipher .. salt, "abc", keystring), plain)
-- 末字符为偶:盐在头
local even_sum = 97 + 98 + 100
local start2, count2 = even_sum % #keystring, math.floor(even_sum / 65)
local part2 = keystring:sub(start2 + 1, start2 + count2)
local cipher2 = Hex.to_hex(Des.encrypt(plain, md5(part2 .. salt):sub(1, 8), md5(salt):sub(1, 8)))
Assert.eq(Client.decryptBody(salt .. cipher2, "abd", keystring), plain)
local chapter = "甲<br>乙"
local chapter_cipher = Des.encryptBase64(chapter, "KW8Dvm2N", "1ae2c94b")
Assert.eq(Client.chapterText(chapter_cipher, nil), chapter)
Assert.eq(Client.chapterText(chapter_cipher, "jj"), chapter)
Assert.eq(Client.chapterText(chapter, nil), chapter)
Assert.is_nil(Client.chapterText("@@@@", "jj"))
end
do
local Settings = require("utils.settings")
local Protocols = require("moonsource_jjwxc.protocols")
local cfg = Settings.getSource("jjwxc")
cfg.cookie, cfg.token, cfg.protocol = nil, nil, nil
Assert.is_nil(Protocols.current())
cfg.token = "t"
Assert.eq(Protocols.current().id, "app")
cfg.cookie = "c=1"
Assert.eq(Protocols.current().id, "web")
Protocols.select("app")
Assert.eq(Protocols.current().id, "app")
cfg.protocol = "web"
cfg.cookie = nil
Assert.eq(Protocols.current().id, "app")
cfg.token, cfg.protocol = nil, nil
end
-- 段评接口:VIP 章节要登录态,web 带 Cookie、app 带 token 查询参数,都没有就匿名。
do
local Settings = require("utils.settings")
local Protocols = require("moonsource_jjwxc.protocols")
local cfg = Settings.getSource("jjwxc")
cfg.cookie, cfg.token, cfg.protocol = nil, nil, nil
Assert.eq(select(1, Protocols.list[1].commentAuth()), "")
Assert.is_nil(select(2, Protocols.list[1].commentAuth()).Cookie)
cfg.cookie, cfg.token = "c=1", "t k"
local query, headers = Protocols.list[1].commentAuth()
Assert.eq(query, "")
Assert.eq(headers.Cookie, "c=1")
query, headers = Protocols.list[2].commentAuth()
Assert.eq(query, "&token=t%20k")
Assert.is_nil(headers.Cookie)
cfg.cookie, cfg.token = nil, nil
end