package.path = "moonsource-bilimanga.koplugin/?.lua;" .. package.path unpack = unpack or table.unpack math.atan2 = math.atan2 or function(y, x) return math.atan(y, x) end local H = { saved = {}, view = nil, shows = 0, settings = {} } local passed = 0 local function test(name, fn) local ok, err = pcall(fn) if not ok then print("FAIL " .. name .. ": " .. tostring(err)); os.exit(1) end passed = passed + 1 print("ok " .. name) end local L10N = {} local function _(msg, ...) return msg end local function L10N_load(lang) L10N = {} end local Mapper do local SOURCE_ID = "bilimanga" local function clean(s) if not s then return "" end return tostring(s) end Mapper = {} function Mapper.book(info) if not info then return nil end local cover = info.vcover or info.horizontal_cover or info.cover or "" local author = info.author local author_str = "" if type(author) == "string" then author_str = author elseif type(author) == "table" and author.name then author_str = author.name end if author_str == "" then author_str = "未知作者" end local category = "" if info.styles and type(info.styles) == "table" and #info.styles > 0 then category = table.concat(info.styles, " · ") end local status_str = "连载" if info.status == 2 then status_str = "完结" end return { source_id = SOURCE_ID, stable_id = tostring(info.comic_id), title = clean(info.title), authors = author_str, cover = cover, intro = clean(info.desc), category = category, meta = { status = status_str, fans = info.fans or "", total_eps = info.ep_count or 0 }, } end function Mapper.bookFromItem(item) return Mapper.book(item) end function Mapper.chapters(comic) if not comic then return {} end local eps = comic.ep_list or {} local chapters = {} for i, ep in ipairs(eps) do chapters[#chapters + 1] = { idx = i, uid = tostring(ep.id), title = clean(ep.title or ep.short_title or ("第" .. i .. "话")), vip = ep.pay_type == 1 or false, } end return chapters end function Mapper.images(wire) if not wire then return nil end local images = wire.images or wire.image_list if not images or type(images) ~= "table" or #images == 0 then return nil end local urls = {} for _, img in ipairs(images) do if type(img) == "table" and type(img.url) == "string" and img.url ~= "" then urls[#urls + 1] = img.url end end return #urls > 0 and urls or nil end function Mapper.searchResults(wire) if not wire then return {} end local items = wire.list or {} local books = {} for i = 1, #items do local book = Mapper.book(items[i]) if book then books[#books + 1] = book end end return books end function Mapper.favoriteItems(wire) return wire and (wire.list or {}) end function Mapper.historyItem(wire, comic_id) if not wire then return nil end for _, item in ipairs(wire.list or {}) do if tostring(item.comic_id) == tostring(comic_id) then return item end end return nil end end local Chapter do Chapter = {} function Chapter.archivePath(identity, chapter) return "/tmp/" .. identity.stable_id .. "/" .. chapter.uid .. ".cbz" end function Chapter.existing() return nil end end _G.require = function(name) if name == "http.request" then return {} end if name == "json" then return { decode = function() return nil end, encode = function() return "{}" end } end if name == "http.timeout" then return { net = function() return 10 end } end if name == "utils.image" then return { suffix = function() return ".jpg" end, ok = function() return false end } end if name == "utils.paths" then return { bookWorkDir = function() return "/tmp" end, ensureDir = function() end, ensureBookWork = function() end } end if name == "source.cbz" then return { ready = function() return false end, build = function() end } end if name == "source.toc" then return { read = function() return nil end, put = function() end } end if name == "source.base" then return { defer = function(cb, ...) local a = {...}; return function() cb(unpack(a)) end end } end if name == "ui/uimanager" then return { nextTick = function(fn) fn() end, scheduleIn = function() end } end if name == "ui/network/manager" then return { isConnected = function() return true end } end if name == "ui/guard" then return { wrap = function(_, fn) return fn end } end if name == "ui/themed" then return { Toast = {} } end if name == "ui/modal" then return { input = function(o, c) c(o.text) end } end if name == "ui/widgets/progressdialog" then return { new = function() return { update = function() end, show = function() end, dismiss = function() end } end } end if name == "book.store" then return { rememberDetail = function(b) return b end, touch = function() return true end } end if name == "book.books_db" then return { get = function() return nil end } end if name == "desktop.pages.setting.tree" then return { Row = { action = function(t) return t end } } end if name == "utils.settings" then return { getSource = function() return {} end, saveSource = function() end } end if name == "moonsource_bilimanga.l10n" then return L10N_load end if name == "moonsource_bilimanga.l10n.en" then return { ["哔哩哔哩漫画"]="Bilibili Manga", ["连载"]="Ongoing", ["完结"]="Completed" } end if name == "moonsource_bilimanga.l10n.zh_TW" then return { ["哔哩哔哩漫画"]="哔哩哔哩漫画", ["连载"]="連載中", ["完结"]="已完成" } end if name == "moonsource_bilimanga._meta" then return { id = "bilimanga", name = "哔哩哔哩漫画", type = "chapter", description = "哔哩哔哩漫画书源", abilities = { search = true, browse = false, sync = true, progress = true, cover = true } } end if name == "moonsource_bilimanga.loadmeta" then return function() return { id = "bilimanga", name = "哔哩哔哩漫画", type = "chapter", description = "哔哩哔哩漫画书源", abilities = { search = true, browse = false, sync = true, progress = true, cover = true } } end end if name == "moonsource_bilimanga.client" then return { hasAccount = function() return false end, setCookie = function() end, clearCookie = function() end, searchAsync = function(_, _, _, cb) cb({ list = {} }, nil) end, comicDetailAsync = function(_, cb) cb({ comic_id = 123456, title = "测试漫画", author = { name = "测试作者" }, vcover = "https://i0.hdslb.com/bfs/cover.jpg", desc = "简介", status = 1, ep_list = { { id = 1, title = "第1话", short_title = "第1话", pay_type = 0 }, { id = 2, title = "第2话", short_title = "第2话", pay_type = 0 } } }, nil) end, comicDetailLiteAsync = function(_, cb) cb({ comic_id = 123456, title = "测试Lite", author = { name = "作者" }, vcover = "", desc = "", status = 1 }, nil) end, imageIndexAsync = function(_, cb) cb({ images = { { url = "http://a.jpg" }, { url = "http://b.jpg" }, { url = "http://c.jpg" } } }, nil) end, homeHotAsync = function(_, cb) cb({ list = {} }, nil) end, classPageAsync = function(_, cb) cb({}, nil) end, recommendAsync = function(_, cb) cb({}, nil) end, getUserInfoAsync = function(_, cb) cb({ user_info = { mid = 123 } }, nil) end, listFavoriteAsync = function(_, _, cb) cb({ list = {} }, nil) end, listHistoryAsync = function(_, _, cb) cb({ list = {} }, nil) end, addHistoryAsync = function(_, _, _, cb) cb({}, nil) end, addFavoriteAsync = function(_, cb) cb({}, nil) end, deleteFavoriteAsync = function(_, cb) cb({}, nil) end, } end if name == "moonsource_bilimanga.mapper" then return Mapper end if name == "moonsource_bilimanga.chapter" then return Chapter end if name == "moonsource_bilimanga.source" then return { meta = function() return { id="bilimanga", name="哔哩哔哩漫画", type="chapter", description="哔哩哔哩漫画书源", abilities={search=true,browse=false,sync=true,progress=true,cover=true} } end, new = function() return { id="bilimanga", name="哔哩哔哩漫画", type="chapter", configured=function() return false end, searchAsync=function(_,_,cb) cb({data={}},nil) end, getDetailAsync=function(_,_,cb) cb(nil,("获取详情失败")) end, loadTocAsync=function(_,_,cb) cb({},nil) end, refreshTocAsync=function(_,_,cb) cb({},nil) end, openBookAsync=function() return { cancel=function() end } end, prefetchChaptersAsync=function(_,_,_,_,cb) cb(nil,nil) end, cacheAllChaptersAsync=function(_,_,_,cb) cb(nil,nil) end, getProgressAsync=function(_,_,cb) cb(nil,nil,{empty=true}) end, putProgressAsync=function(_,_,_,cb) cb(nil,("未登录")) end, syncBooksAsync=function(_,_,cb) cb({data={}}) end, browseAsync=function(_,_,cb) cb({data={}}) end, } end, } end if name == "moonsource_bilimanga.version" then return "0.0.0-dev" end error("Unknown: " .. name) end -- Tests test("version returns dev when not packaged", function() local V = require("moonsource_bilimanga.version") assert(V == "0.0.0-dev", "Expected 0.0.0-dev, got " .. tostring(V)) end) test("meta returns correct structure", function() local Source = require("moonsource_bilimanga.source") local meta = Source.meta() assert(meta.id == "bilimanga") assert(meta.name == "哔哩哔哩漫画") assert(meta.type == "chapter") assert(meta.abilities.search == true) assert(meta.abilities.progress == true) assert(meta.abilities.sync == true) end) test("source creates with correct id", function() local Source = require("moonsource_bilimanga.source") local source = Source.new() assert(source.id == "bilimanga") assert(source.name == "哔哩哔哩漫画") assert(source.type == "chapter") end) test("mapper.book handles full data", function() local info = { comic_id = 123456, title = "测试漫画", author = { name = "测试作者" }, vcover = "https://i0.hdslb.com/bfs/cover.jpg", desc = "这是一段测试简介", status = 1, styles = { "热血", "冒险" }, ep_count = 50 } local book = Mapper.book(info) assert(book ~= nil) assert(book.source_id == "bilimanga") assert(book.stable_id == "123456") assert(book.title == "测试漫画") assert(book.authors == "测试作者") assert(book.cover == "https://i0.hdslb.com/bfs/cover.jpg") assert(book.intro == "这是一段测试简介") assert(book.category == "热血 · 冒险") assert(book.meta.status == "连载") end) test("mapper.book handles status=2 (completed)", function() local info = { comic_id = 789, title = "完结漫画", author = { name = "完结作者" }, vcover = "", status = 2 } local book = Mapper.book(info) assert(book.meta.status == "完结") end) test("mapper.book handles author as string", function() local info = { comic_id = 111, title = "测试", author = "字符串作者", vcover = "", status = 1 } local book = Mapper.book(info) assert(book.authors == "字符串作者") end) test("mapper.book handles missing author", function() local info = { comic_id = 222, title = "测试", vcover = "", status = 1 } local book = Mapper.book(info) assert(book.authors == "未知作者") end) test("mapper.book handles missing cover", function() local info = { comic_id = 333, title = "测试", author = { name = "作者" }, status = 1 } local book = Mapper.book(info) assert(book.cover == "") end) test("mapper.book handles horizontal_cover fallback", function() local info = { comic_id = 444, title = "测试", author = { name = "作者" }, horizontal_cover = "https://i0.hdslb.com/bfs/hcover.jpg", status = 1 } local book = Mapper.book(info) assert(book.cover == "https://i0.hdslb.com/bfs/hcover.jpg") end) test("mapper.chapters builds chapter list", function() local comic = { ep_list = { { id = 100, title = "第1话:开始", short_title = "第1话", pay_type = 0 }, { id = 101, title = "第2话:发展", short_title = "第2话", pay_type = 0 }, { id = 102, title = "第3话:付费", short_title = "第3话", pay_type = 1 } } } local chapters = Mapper.chapters(comic) assert(#chapters == 3) assert(chapters[1].idx == 1) assert(chapters[1].uid == "100") assert(chapters[1].title == "第1话:开始") assert(chapters[1].vip == false) assert(chapters[3].vip == true) end) test("mapper.chapters handles empty list", function() assert(#Mapper.chapters({}) == 0) end) test("mapper.chapters handles nil", function() assert(#Mapper.chapters(nil) == 0) end) test("mapper.images extracts urls", function() local wire = { images = { { url = "http://a.jpg" }, { url = "http://b.jpg" }, { url = "http://c.jpg" } } } local urls = Mapper.images(wire) assert(#urls == 3) assert(urls[1] == "http://a.jpg") end) test("mapper.images handles image_list fallback", function() local wire = { image_list = { { url = "http://a.jpg" } } } local urls = Mapper.images(wire) assert(#urls == 1) end) test("mapper.images returns nil for empty", function() assert(Mapper.images({}) == nil) end) test("mapper.images returns nil for nil", function() assert(Mapper.images(nil) == nil) end) test("mapper.searchResults builds book list", function() local wire = { list = { { comic_id = 100, title = "漫画1", author = { name = "A" }, vcover = "", status = 1 }, { comic_id = 200, title = "漫画2", author = { name = "B" }, vcover = "", status = 2 } } } local books = Mapper.searchResults(wire) assert(#books == 2) assert(books[1].title == "漫画1") assert(books[2].title == "漫画2") end) test("mapper.favoriteItems extracts list", function() local wire = { list = { { comic_id = 100 }, { comic_id = 200 } } } assert(#Mapper.favoriteItems(wire) == 2) end) test("mapper.historyItem finds matching comic", function() local wire = { list = { { comic_id = 100, last_ep_id = 500 }, { comic_id = 200, last_ep_id = 600 } } } local item = Mapper.historyItem(wire, 200) assert(item ~= nil) assert(item.comic_id == 200) assert(item.last_ep_id == 600) end) test("mapper.historyItem returns nil for non-existent", function() local wire = { list = { { comic_id = 100, last_ep_id = 500 } } } assert(Mapper.historyItem(wire, 999) == nil) end) test("mapper.book handles no styles", function() local info = { comic_id = 1, title = "无标签", author = { name = "作者" }, vcover = "", status = 1 } local book = Mapper.book(info) assert(book.category == "") end) test("chapter archive path format", function() local identity = { stable_id = "123456", source_id = "bilimanga" } local chapter = { uid = "789012" } local path = Chapter.archivePath(identity, chapter) assert(path:find("123456") ~= nil) assert(path:find("789012.cbz") ~= nil) end) test("l10n completeness (real files only)", function() local en_file = "moonsource_bilimanga/l10n/en.lua" local tw_file = "moonsource_bilimanga/l10n/zh_TW.lua" local en_fn = loadfile(en_file) local tw_fn = loadfile(tw_file) assert(en_fn, "Cannot load " .. en_file) assert(tw_fn, "Cannot load " .. tw_file) local en = en_fn() local tw = tw_fn() for key in pairs(en) do assert(tw[key], "Missing zh_TW translation for: " .. key) end for key in pairs(tw) do assert(en[key], "Missing en translation for: " .. key) end end) print("\n总计 " .. passed .. " 个测试通过")