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

import { build1688OfferSearchUrl, replaceRawQueryParam } from "./search-results-url-utils.js";

describe("build1688OfferSearchUrl", () => {
  it("encodes mixed Chinese and ASCII keywords with GBK percent-encoding", () => {
    expect(build1688OfferSearchUrl("岩棉板 TR10 容重100kg")).toBe(
      "https://s.1688.com/selloffer/offer_search.htm?keywords=%D1%D2%C3%DE%B0%E5%20TR10%20%C8%DD%D6%D8100kg",
    );
  });
});

describe("replaceRawQueryParam", () => {
  it("preserves raw non-UTF8 keyword bytes when removing beginPage", () => {
    const url = "https://s.1688.com/selloffer/offer_search.htm?keywords=%C7%F2%C4%AB%D6%FD%CC%FA%BE%AE%B8%C7+%A6%C6700%D6%D8%D0%CD&beginPage=3&spm=a260k.home2025.searchbox.0";

    expect(replaceRawQueryParam(url, "beginPage", null)).toBe(
      "https://s.1688.com/selloffer/offer_search.htm?keywords=%C7%F2%C4%AB%D6%FD%CC%FA%BE%AE%B8%C7+%A6%C6700%D6%D8%D0%CD&spm=a260k.home2025.searchbox.0",
    );
  });

  it("appends beginPage without touching the original encoded keywords or hash", () => {
    const url = "https://s.1688.com/selloffer/offer_search.htm?keywords=%C7%F2%C4%AB%D6%FD%CC%FA%BE%AE%B8%C7+%A6%C6700%D6%D8%D0%CD&spm=a260k.home2025.searchbox.0#list";

    expect(replaceRawQueryParam(url, "beginPage", "2")).toBe(
      "https://s.1688.com/selloffer/offer_search.htm?keywords=%C7%F2%C4%AB%D6%FD%CC%FA%BE%AE%B8%C7+%A6%C6700%D6%D8%D0%CD&spm=a260k.home2025.searchbox.0&beginPage=2#list",
    );
  });
});