import { createMockFetch } from "@umt/testkit"; import { describe, expect, it } from "vitest"; import { bangumiSubject, createBangumiClient, createDoubanClient, doubanSubject, MetaSourceError } from "./index.ts"; // 字段取自 2026-10-02 api.bgm.tv / m.douban.com 实际返回(删减) const BGM = { id: 400602, name: "葬送のフリーレン", name_cn: "葬送的芙莉莲", date: "2023-09-29", platform: "TV", total_episodes: 36, eps: 28, images: { large: "http://lain.bgm.tv/pic/cover/l/13/c5/400602_ZI8Y9.jpg" }, rating: { score: 8.5 }, infobox: [ { key: "中文名", value: "葬送的芙莉莲" }, { key: "别名", value: [{ v: "Frieren: Beyond Journey's End" }, { v: "Sousou no Frieren" }] }, ], }; const DOUBAN_TV = { id: "36449295", title: "兰香如故", original_title: "", aka: ["兰香缘"], year: "2026", type: "tv", is_tv: true, episodes_count: 47, pubdate: ["2026-09-11(中国大陆)"], rating: { value: 7.5 }, pic: { large: "https://img9.doubanio.com/view/photo/m_ratio_poster/public/p2933527614.jpg" }, }; describe("辅助信息源 bgm.tv / 豆瓣", () => { it("bgm 条目:中文名优先,原名与别名进 aliases,图片改 https,TV 按剧集", () => { expect(bangumiSubject(BGM)).toMatchObject({ id: "400602", title: "葬送的芙莉莲", aliases: ["葬送のフリーレン", "Frieren: Beyond Journey's End", "Sousou no Frieren"], year: 2023, kind: "tv", cover: "https://lain.bgm.tv/pic/cover/l/13/c5/400602_ZI8Y9.jpg", rating: 8.5, episodes: 36, }); expect(bangumiSubject({ ...BGM, platform: "剧场版" })?.kind).toBe("movie"); }); it("豆瓣条目:去掉标题里的方向符,首播日期与类型", () => { expect(doubanSubject({ ...DOUBAN_TV, title: "兰香如故‎" })).toMatchObject({ title: "兰香如故", aliases: ["兰香缘"], year: 2026, kind: "tv", date: "2026-09-11", episodes: 47, }); }); it("豆瓣详情类型未知:电影接口 404 再查剧集;bgm 带 UA", async () => { const m = createMockFetch({ "GET /rexxar/api/v2/movie/36449295": { status: 404, body: "" }, "GET /rexxar/api/v2/tv/36449295": { body: DOUBAN_TV }, "GET https://api.bgm.tv/v0/subjects/400602": { body: BGM }, }); const d = createDoubanClient({ fetch: m.fetch }); expect((await d.subject("36449295")).kind).toBe("tv"); expect(m.requests[0]!.headers.referer).toBe("https://m.douban.com/"); const b = createBangumiClient({ fetch: m.fetch }); expect((await b.subject("400602")).title).toBe("葬送的芙莉莲"); expect(m.requests.at(-1)!.headers["user-agent"]).toContain("uhd-media-tool"); await expect(b.subject("abc")).rejects.toBeInstanceOf(MetaSourceError); }); });