feat: stabilize Xingtu market data sync
This commit is contained in:
@@ -2,6 +2,7 @@ import { describe, expect, test } from "vitest";
|
||||
|
||||
import {
|
||||
buildAuthorAseInfoUrl,
|
||||
buildAuthorCommerceSeedBaseInfoUrl,
|
||||
createMarketApiClient,
|
||||
mapAuthorAseInfoResponse
|
||||
} from "../src/content/market/api-client";
|
||||
@@ -59,6 +60,74 @@ describe("market-api-client", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("loads rates from the commerce seed endpoint first", async () => {
|
||||
const requestedUrls: string[] = [];
|
||||
const client = createMarketApiClient({
|
||||
fetchImpl: async (input) => {
|
||||
requestedUrls.push(input);
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
avg_search_after_view_rate: "0.1% - 0.25%",
|
||||
personal_avg_search_after_view_rate: "0.02% - 0.1%"
|
||||
})
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
await expect(client.loadAuthorAseInfo("7363217488772857856")).resolves.toMatchObject(
|
||||
{
|
||||
success: true,
|
||||
rates: {
|
||||
singleVideoAfterSearchRate: "0.1% - 0.25%",
|
||||
personalVideoAfterSearchRate: "0.02% - 0.1%"
|
||||
}
|
||||
}
|
||||
);
|
||||
expect(requestedUrls).toEqual([
|
||||
"https://xingtu.cn/gw/api/aggregator/get_author_commerce_seed_base_info?o_author_id=7363217488772857856&range=90"
|
||||
]);
|
||||
});
|
||||
|
||||
test("falls back to the ASE endpoint when the commerce seed endpoint fails", async () => {
|
||||
const requestedUrls: string[] = [];
|
||||
const client = createMarketApiClient({
|
||||
fetchImpl: async (input) => {
|
||||
requestedUrls.push(input);
|
||||
if (input.includes("get_author_commerce_seed_base_info")) {
|
||||
return {
|
||||
ok: false,
|
||||
json: async () => ({})
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
data: {
|
||||
avg_search_after_view_rate: "<0.02%",
|
||||
personal_avg_search_after_view_rate: "0.02 - 0.1%"
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
await expect(client.loadAuthorAseInfo("7363217488772857856")).resolves.toMatchObject(
|
||||
{
|
||||
success: true,
|
||||
rates: {
|
||||
singleVideoAfterSearchRate: "<0.02%",
|
||||
personalVideoAfterSearchRate: "0.02% - 0.1%"
|
||||
}
|
||||
}
|
||||
);
|
||||
expect(requestedUrls).toEqual([
|
||||
"https://xingtu.cn/gw/api/aggregator/get_author_commerce_seed_base_info?o_author_id=7363217488772857856&range=90",
|
||||
"https://xingtu.cn/gw/api/aggregator/get_author_ase_info?author_id=7363217488772857856&range=30"
|
||||
]);
|
||||
});
|
||||
|
||||
test("returns a timeout result when the request aborts", async () => {
|
||||
const client = createMarketApiClient({
|
||||
fetchImpl: async () => {
|
||||
@@ -72,4 +141,12 @@ describe("market-api-client", () => {
|
||||
reason: "timeout"
|
||||
});
|
||||
});
|
||||
|
||||
test("builds the author commerce seed info url with author id and range", () => {
|
||||
expect(
|
||||
buildAuthorCommerceSeedBaseInfoUrl("7363217488772857856", "https://xingtu.cn")
|
||||
).toBe(
|
||||
"https://xingtu.cn/gw/api/aggregator/get_author_commerce_seed_base_info?o_author_id=7363217488772857856&range=90"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user