import { describe, expect, test } from "vitest"; import { extractAfterSearchRates } from "../src/shared/extract-after-search-rates"; describe("extractAfterSearchRates", () => { test("extracts rates from explicit keys", () => { const result = extractAfterSearchRates({ metrics: { personal_video_after_search_rate: "0.5% - 1%", single_video_after_search_rate: "1% - 3%" } }); expect(result).toMatchObject({ extractorLevel: "exact-key", matched: true, success: true, rates: { personalVideoAfterSearchRate: "0.5% - 1%", singleVideoAfterSearchRate: "1% - 3%" } }); expect(result.rawPathHints).not.toHaveLength(0); }); test("extracts rates from the commerce seed base info response keys", () => { const result = extractAfterSearchRates({ avg_a3_incr_cnt: "20,000 - 100,000", avg_search_after_view_rate: "<0.02%", avg_search_after_view_rate_rank_percent: "0.25", personal_avg_search_after_view_rate: "0.02 - 0.1%", personal_avg_search_after_view_rate_rank_percent: "0.9" }); expect(result).toMatchObject({ extractorLevel: "exact-key", matched: true, success: true, rates: { personalVideoAfterSearchRate: "0.02% - 0.1%", singleVideoAfterSearchRate: "<0.02%" } }); }); test("extracts rates from normalized label and value pairs", () => { const result = extractAfterSearchRates({ cards: [ { metrics: [ { label: "单条视频看后搜率", value: "0.5%-1%" }, { label: "个人视频看后搜率", value: "1% - 3%" } ] } ] }); expect(result).toMatchObject({ extractorLevel: "label-value", matched: true, success: true, rates: { personalVideoAfterSearchRate: "1% - 3%", singleVideoAfterSearchRate: "0.5% - 1%" } }); }); test("falls back to bounded text extraction when labels are only present in local text", () => { const result = extractAfterSearchRates({ cardTitle: "种草价值", summary: "单视频看后搜率 0.5% - 1% ,个人视频看后搜率 1% - 2%,该达人近30天表现稳定" }); expect(result).toMatchObject({ extractorLevel: "text-fallback", matched: true, success: true, rates: { personalVideoAfterSearchRate: "1% - 2%", singleVideoAfterSearchRate: "0.5% - 1%" } }); }); test("keeps partial matches as non-success", () => { const result = extractAfterSearchRates({ metrics: [ { label: "个人视频看后搜率", value: "1% - 2%" } ] }); expect(result).toMatchObject({ extractorLevel: "label-value", matched: true, success: false, rates: { personalVideoAfterSearchRate: "1% - 2%" } }); }); test("returns an unmatched result for unrelated payloads", () => { expect(extractAfterSearchRates({ foo: "bar" })).toMatchObject({ extractorLevel: "none", matched: false, success: false }); }); });