import { createMockFetch, UNIT3D_USER_DETAIL, UNIT3D_USER_INDEX, UNIT3D_USER_SEEDING } from "@umt/testkit"; import { describe, expect, it } from "vitest"; import { runParser } from "./testing.ts"; import { Unit3dSiteUserInfo } from "./unit3d.ts"; const GiB = 1024 ** 3; describe("Unit3dSiteUserInfo(MP unit3d.py)", () => { it("用户名来自 settings 链接;流量/等级/注册日期/做种列按 thead 定位", async () => { const m = createMockFetch({ "/": { body: UNIT3D_USER_INDEX }, "/users/carol": { body: UNIT3D_USER_DETAIL }, "/index.php": { body: UNIT3D_USER_INDEX }, "/users/carol/active": { body: UNIT3D_USER_SEEDING }, }); const info = await runParser(Unit3dSiteUserInfo, { fetch: m.fetch }); expect(info).toMatchObject({ schema: "Unit3d", username: "carol", userLevel: "Elite", joinAt: "2020-02-03 04:05:06", bonus: 5678.9, upload: 2 * 1024 ** 4, download: 500 * GiB, ratio: 4.1, seeding: 2, seedingSize: 4 * GiB, seedingInfo: [ [9, 3 * GiB], [1, GiB], ], }); expect(m.requests.find((r) => r.url.pathname === "/users/carol/active")!.url.searchParams.get("seeding")).toBe( "include", ); }); it("首页取不到内容(HTTP 404):未登录并记录请求失败", async () => { const m = createMockFetch({}); const info = await runParser(Unit3dSiteUserInfo, { fetch: m.fetch }); expect(info.username).toBeNull(); expect(info.errMsg).toContain("未检测到已登陆"); expect(info.raw.http_errors).toEqual([expect.stringContaining("HTTP 404")]); }); });