55 行
1.5 KiB
TypeScript
55 行
1.5 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { fetchTutorialMetrics, shouldRefreshTutorialMetrics } from "./tutorialMetrics";
|
|
|
|
describe("shouldRefreshTutorialMetrics", () => {
|
|
it("returns false without a supported source", () => {
|
|
expect(shouldRefreshTutorialMetrics({ sourcePlatform: "site", platformVideoId: null })).toBe(false);
|
|
});
|
|
|
|
it("returns true when metrics are missing", () => {
|
|
expect(shouldRefreshTutorialMetrics({
|
|
sourcePlatform: "bilibili",
|
|
platformVideoId: "BV1test",
|
|
metricsFetchedAt: null,
|
|
})).toBe(true);
|
|
});
|
|
|
|
it("returns false for fresh cached metrics", () => {
|
|
expect(shouldRefreshTutorialMetrics({
|
|
sourcePlatform: "bilibili",
|
|
platformVideoId: "BV1test",
|
|
metricsFetchedAt: new Date(),
|
|
viewCount: 120,
|
|
commentCount: 8,
|
|
})).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("fetchTutorialMetrics", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("parses bilibili metrics payloads", async () => {
|
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({
|
|
data: {
|
|
pic: "http://i0.hdslb.com/demo.jpg",
|
|
stat: {
|
|
view: 3210,
|
|
reply: 42,
|
|
},
|
|
},
|
|
}),
|
|
}));
|
|
|
|
const result = await fetchTutorialMetrics("bilibili", "BV1demo");
|
|
expect(result).toMatchObject({
|
|
viewCount: 3210,
|
|
commentCount: 42,
|
|
thumbnailUrl: "https://i0.hdslb.com/demo.jpg",
|
|
});
|
|
});
|
|
});
|