import { createMockFetch, NEXUS_PHP_LOGIN_PAGE, RABBIT_USER_DETAILS, RABBIT_USER_INDEX, RABBIT_USER_SEEDING_JSON, } from "@umt/testkit"; import { describe, expect, it } from "vitest"; import { NexusRabbitSiteUserInfo } from "./nexus-rabbit.ts"; import { runParser } from "./testing.ts"; describe("NexusRabbitSiteUserInfo(MP nexus_rabbit.py)", () => { it("首页奶糖余额 + 详情 layui-hares + api/general JSON 做种", async () => { const m = createMockFetch({ "/": { body: RABBIT_USER_INDEX }, "/user.php": { body: RABBIT_USER_DETAILS }, "POST /api/general": { body: RABBIT_USER_SEEDING_JSON }, }); const info = await runParser(NexusRabbitSiteUserInfo, { fetch: m.fetch }); expect(info).toMatchObject({ schema: "NexusRabbit", userId: "5", username: "dave", userLevel: "Power User", joinAt: "2022-01-02 03:04:05", upload: Math.round(1.5 * 1024 ** 4), download: 500 * 1024 ** 3, ratio: 3.072, bonus: 1024.5, seeding: 2, seedingSize: Math.round(3.5 * 1024 ** 3), seedingInfo: [ [3, 1024 ** 3], [7, Math.round(2.5 * 1024 ** 3)], ], }); const api = m.requests.find((r) => r.url.pathname === "/api/general")!; expect(api.headers["x-requested-with"]).toBe("XMLHttpRequest"); expect(JSON.parse(api.body!)).toMatchObject({ action: "userTorrentsList", data: { type: "seeding", id: 5 } }); }); it("未登录:errMsg 且不请求 API", async () => { const m = createMockFetch({ "/": { body: NEXUS_PHP_LOGIN_PAGE } }); const info = await runParser(NexusRabbitSiteUserInfo, { fetch: m.fetch }); expect(info.errMsg).toContain("未检测到已登陆"); expect(m.requests).toHaveLength(1); }); });