--[[-- moonsource_18ta.mapper 离线用例:片段取自 18ta.com 真实页面(分类列表 / 书页 / 章节页)。 @module tests.moonsource_18ta.mapper_spec --]] ---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch, cast-type-mismatch, return-type-mismatch, cast-local-type local Assert = require("support.assert") local Mapper = require("moonsource_18ta.mapper") -- /sort//.html 的一张书目卡片(完整
  • 块)。 local LIST_HTML = [[
    ]] do local list = Mapper.list(LIST_HTML) Assert.eq(#list, 1) local b = list[1] Assert.eq(b.source_id, "18ta") Assert.eq(b.stable_id, "287895") Assert.eq(b.title, "从黄金一代开始") Assert.eq(b.authors, "呆呆海参") Assert.eq(b.category, "玄幻") Assert.eq(b.cover, "https://www.18ta.com/files/article/image/287/287895/287895s.jpg") Assert.is_true(b.intro:find("天赋爆棚", 1, true), "简介保留正文") Assert.eq(b.meta.status, "连载") end -- 空页与坏卡片:不报错、不产书。 Assert.is_nil(Mapper.list("
    "), "坏响应不能当空书库") Assert.is_nil(Mapper.list('')) Assert.len(Mapper.list('
      '), 0) -- 书页:og:novel meta + .intro 简介 + og:image 封面。 local BOOK_HTML = [[

      天赋爆棚的年轻人们拥有了,展示自己的表演场所。小说群:864747664

      《从黄金一代开始》最新章节
      ]] do local b = Mapper.book(BOOK_HTML) Assert.eq(b.title, "从黄金一代开始") Assert.eq(b.authors, "呆呆海参") Assert.eq(b.category, "玄幻奇幻") Assert.eq(b.cover, "https://www.18ta.com/files/article/image/287/287895/287895s.jpg") Assert.eq(b.meta.status, "连载") Assert.is_true(b.intro:find("天赋爆棚", 1, true), "简介取 .intro 文本") end Assert.is_nil(Mapper.book("no meta")) -- 目录只取 ul_all_chapters(页首「最新章节」预览块不得混入),章序即列表顺序。 do local toc = Mapper.toc(BOOK_HTML) Assert.eq(#toc, 2, "预览块不混入目录") Assert.eq(toc[1].idx, 1) Assert.eq(toc[1].uid, "/aks/287/287895/5447961.html") Assert.eq(toc[1].title, "第一章 红人大哥") Assert.eq(toc[1].origin, "/aks/287/287895/5447961.html") Assert.eq(toc[2].title, "第二章 刻苦训练") end Assert.eq(#Mapper.toc(""), 0) -- 章节正文:article 内部纯段落。 local CH_HTML = [[

      “教练,我想打篮球。”

      室内篮球场上,约翰教练有些意外的看着眼前的小伙子。

      ]] do local body = Mapper.chapterContent(CH_HTML) Assert.is_true(body:find(" 段落") Assert.is_true(body:find("教练,我想打篮球", 1, true)) Assert.is_nil(body:find("", 1, true), "不含 article 标签本身") end Assert.is_nil(Mapper.chapterContent("
      no article
      ")) Assert.is_nil(Mapper.chapterContent('')) Assert.is_nil(Mapper.chapterContent('

      ')) Assert.eq(Mapper.chapterContent('

      正文

      '), "

      正文

      ") local path = "/aks/287/287895/5447961.html" local function nextPage(href) return '下一页' end Assert.eq(Mapper.chapterNext(nextPage("/aks/287/287895/5447961_2.html"), path), "/aks/287/287895/5447961_2.html") Assert.is_nil(Mapper.chapterNext(nextPage("/aks/287/287895/5447962.html"), path)) Assert.is_nil(Mapper.chapterNext(nextPage("/akshtml287895.html"), path)) for _idx, href in ipairs({ path, "/aks/287/287895/5447961_4.html", "/aks/1/2/3.html", "https://evil.test/page" }) do local next_path, err = Mapper.chapterNext(nextPage(href), path) Assert.is_nil(next_path) Assert.eq(err, "响应无效") end local _next_path, err = Mapper.chapterNext("

      没有导航

      ", path) Assert.eq(err, "响应无效", "坏导航不能将第一段正文当完整章节")