star-chart-search-enhancer/tests/business-ability-client.test.ts

123 lines
3.2 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import {
buildBusinessAbilityEstimateUrl,
createBusinessAbilityClient,
mapBusinessAbilityEstimateResponse
} from "../src/content/market/business-ability-client";
describe("business-ability-client", () => {
test("builds the commerce spread estimate url used by the Xingtu detail page", () => {
expect(
buildBusinessAbilityEstimateUrl(
"6724241209444794382",
"https://www.xingtu.cn"
)
).toBe(
"https://www.xingtu.cn/gw/api/aggregator/get_author_commerce_spread_info?o_author_id=6724241209444794382"
);
});
test("maps duration estimates into page-style display values", () => {
expect(mapBusinessAbilityEstimateResponse(buildEstimatePayload())).toEqual({
oneToTwenty: {
expectedCpe: "2.1",
expectedCpm: "120.0",
expectedPlay: "250w",
hotRate: "100%"
},
overSixty: {
expectedCpe: "4.2",
expectedCpm: "240.0",
expectedPlay: "250w",
hotRate: "100%"
},
twentyToSixty: {
expectedCpe: "3.7",
expectedCpm: "212.0",
expectedPlay: "250w",
hotRate: "100%"
}
});
});
test("keeps decimal CPM values and marks missing hot rate", () => {
expect(mapBusinessAbilityEstimateResponse({
base_resp: { status_code: 0, status_message: "" },
cpe_1_20: "1.6347",
cpe_20_60: "2.002",
cpe_60: "2.104",
cpm_1_20: "21.7955",
cpm_20_60: "27.5628",
cpm_60: "29.877",
vv: "1010234"
})).toEqual({
oneToTwenty: {
expectedCpe: "1.6",
expectedCpm: "21.8",
expectedPlay: "101w",
hotRate: "缺失"
},
overSixty: {
expectedCpe: "2.1",
expectedCpm: "29.9",
expectedPlay: "101w",
hotRate: "缺失"
},
twentyToSixty: {
expectedCpe: "2",
expectedCpm: "27.6",
expectedPlay: "101w",
hotRate: "缺失"
}
});
});
test("loads duration estimate metrics without requesting legacy video content metrics", async () => {
const requestedUrls: string[] = [];
const fetchImpl = vi.fn(async (input: string) => {
requestedUrls.push(input);
return {
json: async () => buildEstimatePayload(),
ok: true
};
});
const client = createBusinessAbilityClient({
baseUrl: "https://www.xingtu.cn",
fetchImpl,
timeoutMs: 1000
});
await expect(
client.loadBusinessAbility({
authorId: "6724241209444794382",
authorName: "李蠕蠕",
status: "success"
})
).resolves.toMatchObject({
estimates: expect.objectContaining({
twentyToSixty: expect.objectContaining({ expectedCpm: "212.0" })
}),
status: "success"
});
expect(requestedUrls).toEqual([
"https://www.xingtu.cn/gw/api/aggregator/get_author_commerce_spread_info?o_author_id=6724241209444794382"
]);
});
});
function buildEstimatePayload() {
return {
base_resp: { status_code: 0, status_message: "" },
cpe_1_20: "2.1035",
cpe_20_60: "3.7161",
cpe_60: "4.2069",
cpm_1_20: "119.9976",
cpm_20_60: "211.9958",
cpm_60: "239.9953",
platform_hot_rate: "1",
vv: "2500049"
};
}