star-chart-search-enhancer/tests/market-api-client.test.ts

76 lines
1.9 KiB
TypeScript

import { describe, expect, test } from "vitest";
import {
buildAuthorAseInfoUrl,
createMarketApiClient,
mapAuthorAseInfoResponse
} from "../src/content/market/api-client";
describe("market-api-client", () => {
test("builds the author ase info url with author id and range", () => {
expect(
buildAuthorAseInfoUrl("123", "https://xingtu.cn")
).toBe(
"https://xingtu.cn/gw/api/aggregator/get_author_ase_info?author_id=123&range=30"
);
});
test("maps a valid ASE payload into normalized rates", () => {
expect(
mapAuthorAseInfoResponse({
data: {
avg_search_after_view_rate: "<0.02%",
personal_avg_search_after_view_rate: "0.02 - 0.1%"
}
})
).toMatchObject({
success: true,
rates: {
singleVideoAfterSearchRate: "<0.02%",
personalVideoAfterSearchRate: "0.02% - 0.1%"
}
});
});
test("returns a missing-rate failure when the payload omits a required field", () => {
expect(
mapAuthorAseInfoResponse({
data: {
avg_search_after_view_rate: "<0.02%"
}
})
).toMatchObject({
success: false,
reason: "missing-rate"
});
});
test("returns a request-failed result for non-ok responses", async () => {
const client = createMarketApiClient({
fetchImpl: async () => ({
ok: false,
json: async () => ({})
})
});
await expect(client.loadAuthorAseInfo("123")).resolves.toMatchObject({
success: false,
reason: "request-failed"
});
});
test("returns a timeout result when the request aborts", async () => {
const client = createMarketApiClient({
fetchImpl: async () => {
throw new DOMException("Timed out", "AbortError");
},
timeoutMs: 1
});
await expect(client.loadAuthorAseInfo("123")).resolves.toMatchObject({
success: false,
reason: "timeout"
});
});
});