--[[-- 书旗官网书库:动态筛选、分页、旧节点迁移和取消。 @module tests.moonsource_shuqi.browse_spec --]] ---@diagnostic disable: undefined-field, missing-fields, param-type-mismatch local Assert = require("support.assert") local Mapper = require("moonsource_shuqi.mapper") local category = "fc=7&st=0&sz=0&tm=0&wd=10" local filters = [[
作品类型:
作品分类:
]] local function pageHtml(page, next_page) local cards = {} for index = 1, 24 do local id = (page - 1) * 24 + index cards[#cards + 1] = '
  • 书' .. id .. '

    作者:作者

    ' .. '

    简介&摘要

  • ' end return filters .. '' .. (next_page and '下一页' or "") end local books, groups, more = Mapper.store(pageHtml(1, true)) Assert.len(books, 24) Assert.eq(books[1].authors, "作者") Assert.eq(books[1].intro, "简介&摘要") Assert.eq(books[1].cover, "https://img.test/cover.jpg") Assert.len(groups, 2) Assert.eq(groups[1].active, "sz=0") Assert.eq(groups[2].chips[2].id, category) Assert.eq(groups[2].active, "fc=0&st=0&sz=0&tm=0&wd=10") Assert.is_true(more) Assert.is_nil(Mapper.store("维护")) Assert.len(Mapper.store(filters .. ''), 0) Assert.is_nil(Mapper.storeNode("sz=0&cookie=secret")) Assert.is_nil(Mapper.storeNode("sz=0&sz=2")) Assert.is_nil(Mapper.storeNode("../../myself")) Assert.eq(Mapper.storeNode("sz=0&fc=34%2C102"), "fc=34%2C102&sz=0") local requests, delayed, failed = {}, false, false package.loaded["http.request"] = { get = function(url, options, cb) local request = { url = url, options = options, cb = cb } requests[#requests + 1] = request local handle = { cancel = function() request.cancelled = true end } local page = tonumber(url:match("page=(%d+)")) if delayed and page == 2 then return handle end if failed and page == 2 then cb(nil, "network down"); return handle end cb(pageHtml(page, page < 3)) return handle end, } local source = require("moonsource_shuqi.source").new() local result, reason source:browseAsync({ limit = 60 }, function(value, err) result, reason = value, err end) Assert.is_nil(reason) Assert.len(result.data, 60) Assert.eq(result.data[21].stable_id, "21") Assert.is_nil(requests[1].options.headers.Cookie) Assert.matches(requests[3].url, "page=3$") requests = {} source:browseAsync({ node = "都市", limit = 20 }, function(value) result = value end) Assert.matches(requests[2].url, "fc=7") Assert.len(result.data, 20) failed = true source:browseAsync({}, function(value, err) result, reason = value, err end) Assert.is_nil(result) Assert.eq(reason, "network down") failed, delayed = false, true local calls = 0 local handle = source:browseAsync({}, function() calls = calls + 1 end) handle:cancel() Assert.is_true(requests[#requests].cancelled, "同步第一页回调不能覆盖第二页句柄") requests[#requests].cb(pageHtml(2, true)) Assert.eq(calls, 0)