import { createMockFetch, SUNNYPT_USER_ERROR, SUNNYPT_USER_MESSAGES, SUNNYPT_USER_PROFILE } from "@umt/testkit"; import { describe, expect, it } from "vitest"; import { SunnyPTSiteUserInfo } from "./sunnypt.ts"; import { runParser } from "./testing.ts"; const API = "https://api.sunnypt.top/api/v1/mp"; describe("SunnyPTSiteUserInfo(MP sunnypt.py)", () => { it("profile + 未读消息分页(不标记已读)", async () => { const m = createMockFetch({ [`GET ${API}/profile`]: { body: SUNNYPT_USER_PROFILE }, [`GET ${API}/messages?unread_only=true&page=1`]: { body: SUNNYPT_USER_MESSAGES }, }); const info = await runParser(SunnyPTSiteUserInfo, { fetch: m.fetch, url: "https://sunnypt.top/", apikey: "sk", fetchMessages: true, }); expect(info).toMatchObject({ schema: "SunnyPT", userId: "12", username: "kate", userLevel: "Elite User", joinAt: "2023-04-05 06:07:08", upload: 4000, download: 2000, ratio: 2, bonus: 88.8, seeding: 7, seedingSize: 7000, leeching: 1, leechingSize: 100, messageUnread: 1, messageUnreadContents: [{ head: "通知", date: "2026-09-20 08:00:00", content: "内容" }], }); expect(m.requests[0]!.headers["x-api-key"]).toBe("sk"); expect(m.requests.every((r) => r.method === "GET")).toBe(true); }); it("API 错误:errMsg 为站点 msg", async () => { const m = createMockFetch({ [`GET ${API}/profile`]: { body: SUNNYPT_USER_ERROR } }); const info = await runParser(SunnyPTSiteUserInfo, { fetch: m.fetch, url: "https://sunnypt.top/", apikey: "x" }); expect(info.userId).toBeNull(); expect(info.errMsg).toBe("API Key 无效"); }); });