import { describe, expect, it } from "vitest";
import { JSDOM } from "jsdom";

import {
  extractSearchResultCandidatesFromDocument,
  looksLikeMerchantName,
  looksLikeSearchResultTitle,
} from "./search-result-extractor.js";

describe("extractSearchResultCandidatesFromDocument", () => {
  it("picks the product title from the card instead of the merchant name", () => {
    const dom = new JSDOM(`
      <body>
        <div class="searchOfferWrapper searchOfferItem">
          <a href="https://detail.1688.com/offer/10001.html"><img src="https://img.example.com/10001.jpg" /></a>
          <div class="offerTitle">电磁阀4V210-08/310-10换向阀24V气动控制阀220V二位五通电子阀12</div>
          <div class="offerPrice">¥12.50/个</div>
          <a href="https://shop123.1688.com/page/offerlist.htm">温州熊大气动有限公司</a>
          <a href="https://air.1688.com/app/ocms-fusion-components-1688/def_cbu_web_im/index.html?uid=tester">旺旺在线</a>
        </div>
      </body>
    `);

    const items = extractSearchResultCandidatesFromDocument(dom.window.document, 5, { visibleOnly: false });

    expect(items).toHaveLength(1);
    expect(items[0]).toMatchObject({
      title: "电磁阀4V210-08/310-10换向阀24V气动控制阀220V二位五通电子阀12",
      merchantName: "温州熊大气动有限公司",
      offerUrl: "https://detail.1688.com/offer/10001.html",
      rawCardText: expect.stringContaining("电磁阀4V210-08/310-10换向阀24V气动控制阀220V二位五通电子阀12"),
    });
    expect(items[0]).not.toHaveProperty("contactUrl");
  });

  it("collects highlighted fragments from search cards", () => {
    const dom = new JSDOM(`
      <body>
        <div class="searchOfferWrapper searchOfferItem">
          <a href="https://detail.m.1688.com/page/index.html?offerId=10002">
            <div class="titleWrap"><span>圆形</span><strong>球墨铸铁井盖</strong><span>井座加工</span></div>
          </a>
          <a href="https://shop456.1688.com/page/offerlist.htm">湖南路文新材料有限公司</a>
        </div>
      </body>
    `);

    const items = extractSearchResultCandidatesFromDocument(dom.window.document, 5, { visibleOnly: false });

    expect(items).toHaveLength(1);
    expect(items[0]?.highlightTexts).toContain("球墨铸铁井盖");
    expect(items[0]?.rawCardText).toContain("球墨铸铁井盖");
  });

  it("filters out interaction prompt copy and keeps the real product title", () => {
    const dom = new JSDOM(`
      <body>
        <div class="searchOfferWrapper searchOfferItem">
          <a href="https://detail.1688.com/offer/10003.html">
            <div class="offerTitle">国标BV1.5 2.5 4 6平方单芯铜芯电线阻燃防水电源线电缆线批发</div>
            <span class="offerDesc">点击可以直接和卖家交流选好的宝贝，或者互动购网站体验，还支持语音视频哦。</span>
          </a>
          <a href="https://shop999.1688.com/page/offerlist.htm">鸿海线缆有限公司</a>
        </div>
      </body>
    `);

    const items = extractSearchResultCandidatesFromDocument(dom.window.document, 5, { visibleOnly: false });

    expect(items).toHaveLength(1);
    expect(items[0]?.title).toBe("国标BV1.5 2.5 4 6平方单芯铜芯电线阻燃防水电源线电缆线批发");
    expect(items[0]?.title).not.toContain("点击可以直接和卖家交流");
  });

  it("does not treat standalone interaction prompt copy as a product title", () => {
    const dom = new JSDOM(`
      <body>
        <div class="searchOfferWrapper searchOfferItem">
          <a href="https://detail.1688.com/offer/10004.html" title="点此可以直接和卖家交流 或相互交流网购体验，还支持语音视频噢。"></a>
          <a href="https://shop10004.1688.com/page/offerlist.htm">深圳市品成电机有限公司</a>
          <a href="https://air.1688.com/app/ocms-fusion-components-1688/def_cbu_web_im/index.html?uid=tester">旺旺在线</a>
        </div>
      </body>
    `);

    const items = extractSearchResultCandidatesFromDocument(dom.window.document, 5, { visibleOnly: false });

    expect(items).toHaveLength(1);
    expect(items[0]?.title).toBe("");
  });
});

describe("looksLikeSearchResultTitle", () => {
  it("rejects merchant-like company names", () => {
    expect(looksLikeMerchantName("无锡博大聚五金有限公司")).toBe(true);
    expect(looksLikeSearchResultTitle("无锡博大聚五金有限公司")).toBe(false);
    expect(looksLikeSearchResultTitle("304不锈钢地脚螺栓Q235焊接地脚笼建筑预埋锚栓")).toBe(true);
  });
});
