--[[-- moonsource_legadorule.remote 离线用例:导入三种格式、在线单次导入、导入即禁用不支持的书源、有效性检查、启停删除,设置与 http 走桩。 @module tests.legadorule.remote_spec --]] ---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch local Assert = require("support.assert") package.preload["utils.awake"] = function() return { poke = function() end } end package.preload["utils.charset"] = function() return { toUtf8 = function(b) return b end } end local pages = {} package.preload["http.request"] = function() return { header = function() return nil end, get = function(url, _o, cb) if pages[url] then cb(pages[url], nil, { code = 200 }) else cb(nil, "HTTP 404", { code = 404 }) end return { cancel = function() end } end, } end local Remote = require("moonsource_legadorule.remote") local function call(method, action, params) local status, body Remote.handle(method, action, params or {}, function(s, b) status, body = s, b end) return status, body end local function tmp(text) local path = os.tmpname() local f = io.open(path, "wb") f:write(text) f:close() return path end -- 测试沙箱的库跨次运行保留:先清空 do local _, r = call("GET", "list") local keys = {} for _idx, src in ipairs(r.sources) do keys[#keys + 1] = src.key end call("POST", "delete", { keys = keys }) end local one = '{"bookSourceUrl":"http://a.com","bookSourceName":"A"}' local lines = '{"bookSourceUrl":"http://b.com","bookSourceName":"B"}\n{"bookSourceUrl":"http://c.com","bookSourceName":"C"}\n' local array = '[{"bookSourceUrl":"http://d.com"},{"bookSourceUrl":"http://e.com"},{"x":1}]' do local s, r = call("POST", "import", { temp = tmp(one) }) Assert.eq(s, 200); Assert.eq(r.added, 1) s, r = call("POST", "import", { temp = tmp(lines) }) Assert.eq(r.total, 2) s, r = call("POST", "import", { temp = tmp(array) }) Assert.eq(r.total, 2); Assert.eq(r.skipped, 1) s, r = call("POST", "import", { temp = tmp("hello") }) Assert.eq(s, 422) s, r = call("GET", "list") Assert.len(r.sources, 5) end do pages["http://x.com/all.json"] = lines local s, r = call("POST", "import_url", { url = "http://x.com/all.json" }) Assert.eq(s, 200); Assert.eq(r.total, 2) s = call("POST", "import_url", { url = "http://x.com/missing.json" }) Assert.eq(s, 502) end do -- 含 JS 的书源导入即禁用并记原因 local js = '{"bookSourceUrl":"http://js.com","searchUrl":"@js:java.get(1)"}' local s, r = call("POST", "import", { temp = tmp(js) }) Assert.eq(r.disabled, 1) local _, l = call("GET", "list") local found for _idx, src in ipairs(l.sources) do if src.url == "http://js.com" then found = src end end Assert.is_false(found.enabled) Assert.not_nil(found.reason) -- 手动启用后原因清除 call("POST", "enable", { keys = { found.key }, enabled = true }) _, l = call("GET", "list") for _idx, src in ipairs(l.sources) do if src.url == "http://js.com" then Assert.is_nil(src.reason) end end call("POST", "delete", { keys = { found.key } }) end do -- 检查:有效的保持启用并记响应时间;搜不了的禁用并记原因 pages["http://v.com/s?q=%E6%88%91%E7%9A%84"] = '{"list":[{"n":"书","u":"/b/1"}]}' pages["http://v.com/b/1"] = '{"ch":[{"t":"第一章","u":"/c/1"}]}' pages["http://v.com/c/1"] = '{"text":"正文内容"}' local good = '{"bookSourceUrl":"http://v.com","searchUrl":"http://v.com/s?q={{key}}","ruleSearch":{"bookList":"$.list","name":"$.n","bookUrl":"$.u"},"ruleToc":{"chapterList":"$.ch","chapterName":"$.t","chapterUrl":"$.u"},"ruleContent":{"content":"$.text"}}' call("POST", "import", { temp = tmp(good) }) -- 搜索能用但正文坏了的书源也要禁用 pages["http://w.com/s?q=%E6%88%91%E7%9A%84"] = '{"list":[{"n":"书","u":"/b/1"}]}' pages["http://w.com/b/1"] = '{"ch":[{"t":"第一章","u":"/c/1"}]}' call("POST", "import", { temp = tmp((good:gsub("v%.com", "w.com"))) }) local s, r = call("POST", "check", {}) Assert.eq(s, 200); Assert.is_true(r.started) local _, st = call("GET", "check_status") Assert.is_false(st.running) Assert.eq(st.total, 7) Assert.eq(st.disabled, 6) local _, l = call("GET", "list") for _idx, src in ipairs(l.sources) do Assert.not_nil(src.ms) if src.url == "http://v.com" then Assert.is_true(src.enabled, tostring(src.reason)); Assert.is_nil(src.reason) else Assert.is_false(src.enabled); Assert.not_nil(src.reason) end end end do -- 书架换源:后台在其他启用书源里搜同名书,选定后只改映射 pages["http://v.com/s?q=%E4%B9%A6"] = '{"list":[{"n":"书","u":"/b/7"}]}' local Sources = require("moonsource_legadorule.sources") local Mapper = require("moonsource_legadorule.mapper") local wkey = Sources.key("http://w.com") local oldId = Mapper.stableId(wkey, "http://w.com/b/1") local s, r = call("POST", "switch_search", { stable_id = oldId, title = "书", authors = "" }) Assert.eq(s, 200); Assert.is_true(r.started) local _, st = call("GET", "switch_status") Assert.is_false(st.running) Assert.eq(st.total, 1); Assert.eq(st.done, 1) Assert.eq(#st.results, 1) Assert.eq(st.results[1].title, "书") Assert.eq(st.results[1].score, 1) local s2 = call("POST", "switch_apply", { stable_id = oldId, target = st.results[1].stable_id }) Assert.eq(s2, 200) local key, url = Sources.alias(oldId) Assert.eq(key, Sources.key("http://v.com")); Assert.eq(url, "http://v.com/b/7") Assert.eq(call("POST", "switch_apply", { stable_id = oldId, target = "bad" }), 422) end do local _, r = call("GET", "list") local key = r.sources[1].key call("POST", "enable", { keys = { key }, enabled = false }) _, r = call("GET", "list") Assert.is_false(r.sources[1].enabled) local s, d = call("POST", "delete", { keys = { key } }) Assert.eq(d.removed, 1) end do -- 分页、搜索与状态筛选在服务端 local _, r = call("GET", "list", { page = "1", size = "2" }) Assert.len(r.sources, 2) Assert.eq(r.total, r.counts.all) local second _, second = call("GET", "list", { page = "2", size = "2" }) Assert.len(second.sources, 2) Assert.is_true(second.sources[1].key ~= r.sources[1].key) _, r = call("GET", "list", { q = "V.COM" }) Assert.eq(r.total, 1) _, r = call("GET", "list", { status = "on" }) Assert.eq(r.total, r.counts.on) -- 跨页全选:key 数量等于筛选后的总数,不受分页限制 local k _, k = call("GET", "keys", {}) Assert.len(k.keys, r.counts.all) _, k = call("GET", "keys", { q = "V.COM" }) Assert.len(k.keys, 1) _, k = call("GET", "keys", { status = "on" }) Assert.len(k.keys, r.counts.on) end do -- 换源映射:身份不变,读取指向新书源 local Sources = require("moonsource_legadorule.sources") require("utils.db").exec("DELETE FROM legadorule_alias;") Assert.is_nil(Sources.alias("old|http://x/1")) Sources.setAlias("old|http://x/1", "abcd1234", "http://y/9") local key, url = Sources.alias("old|http://x/1") Assert.eq(key, "abcd1234"); Assert.eq(url, "http://y/9") Sources.setAlias("old|http://x/1", "ffff0000", "http://z/1") Assert.eq((Sources.alias("old|http://x/1")), "ffff0000") end print(" ok")