star-chart-search-enhancer/tests/backend-metrics-client.test.ts

138 lines
3.9 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { DEFAULT_BACKEND_METRICS_BASE_URL } from "../src/shared/backend-metrics-config";
import {
buildBackendMetricsSearchRequestBody,
buildBackendMetricsSearchUrl,
createBackendMetricsClient,
mapBackendMetricsSearchResponse
} from "../src/shared/backend-metrics-client";
describe("backend-metrics-client", () => {
test("exports the default backend metrics base url", () => {
expect(DEFAULT_BACKEND_METRICS_BASE_URL).toBe(
"https://talent-search.intelligrow.cn"
);
});
test("builds the backend search url", () => {
expect(buildBackendMetricsSearchUrl("https://talent-search.intelligrow.cn")).toBe(
"https://talent-search.intelligrow.cn/api/v1/history/talents/search"
);
});
test("builds a star_id batch request body", () => {
expect(
buildBackendMetricsSearchRequestBody(["7252982749131178039", "7290491710910496809"])
).toEqual({
page: 1,
size: 20,
type: "star_id",
values: ["7252982749131178039", "7290491710910496809"]
});
});
test("maps backend metrics rows into display-ready values", () => {
expect(
mapBackendMetricsSearchResponse({
data: {
data: [
{
avg_a3_increase_cnt: 78366.22448979592,
avg_after_view_search_cnt: 9689.959183673469,
avg_after_view_search_rate: 0.0036203703369054683,
avg_new_a3_rate: 0.034428135017531614,
cp_search: 14.460581961550774,
cpa3: 1.788046443373538,
star_id: "7252982749131178039"
}
]
},
success: true
})
).toEqual([
{
a3IncreaseCount: "78,366.22",
afterViewSearchCount: "9,689.96",
afterViewSearchRate: "0.36%",
cpSearch: "14.46",
cpa3: "1.79",
newA3Rate: "3.44%",
starId: "7252982749131178039"
}
]);
});
test("derives A3 count and CPA3 from the live aggregate response shape", () => {
expect(
mapBackendMetricsSearchResponse({
data: {
data: [
{
avg_after_view_search_cnt: 25982,
avg_after_view_search_rate: 0.0010872130261527625,
avg_new_a3_rate: 0.11075860229946684,
cp_search: 21.168501270110077,
cpe: 0.630604497471276,
cpm: 23.014670324994974,
star_id: "7021245050621263906",
total_estimated_video_cost: 1100000,
total_play_cnt: 47795601,
video_count: 2
}
]
},
success: true
})
).toEqual([
{
a3IncreaseCount: "2,646,886.98",
afterViewSearchCount: "25,982.00",
afterViewSearchRate: "0.11%",
cpSearch: "21.17",
cpa3: "0.21",
newA3Rate: "11.08%",
starId: "7021245050621263906"
}
]);
});
test("posts star ids with bearer auth when searching backend metrics", async () => {
const fetchImpl = async (_input: string, init?: RequestInit) => ({
json: async () => ({
data: {
data: []
},
success: true
}),
ok: true,
status: 200,
url: "https://talent-search.intelligrow.cn/api/v1/history/talents/search"
});
const fetchSpy = vi.fn(fetchImpl);
const client = createBackendMetricsClient({
fetchImpl: fetchSpy,
getAccessToken: async () => "test-token"
});
await client.searchByStarIds(["111", "222"]);
expect(fetchSpy).toHaveBeenCalledWith(
"https://talent-search.intelligrow.cn/api/v1/history/talents/search",
expect.objectContaining({
body: JSON.stringify({
page: 1,
size: 20,
type: "star_id",
values: ["111", "222"]
}),
headers: {
Authorization: "Bearer test-token",
"Content-Type": "application/json"
},
method: "POST"
})
);
});
});