import assert from "node:assert/strict";
import test from "node:test";
import {
  extractXhsPublishUrl,
} from "../lib/publish-url.ts";

const longShareText =
  "81 【汕头街头的夏天，有点可爱 - pyeong | 小红书 - 你的生活兴趣社区】 😆 qpF2vquW4v6E0uq 😆 https://www.xiaohongshu.com/discovery/item/6a5a407d000000000e0363c7?source=webshare&xhsshare=pc_web&xsec_token=ABQTS76SGS2MKA1rYfSay-1cyULvb_8k7kNIw4mKVrcDA=&xsec_source=pc_share";
const shortShareText =
  "汕头街头的夏天，有点可爱 今天的关键词： 浅蓝色、草... http://xhslink.cn/o/6UDG4oUB8kR 保留这段，去【小红书】逛逛吧~";

test("extracts Xiaohongshu long and short URLs from full share text", () => {
  const longUrl = extractXhsPublishUrl(longShareText);
  const shortUrl = extractXhsPublishUrl(shortShareText);

  assert.equal(
    new URL(longUrl).pathname,
    "/discovery/item/6a5a407d000000000e0363c7",
  );
  assert.equal(new URL(longUrl).searchParams.get("source"), "webshare");
  assert.equal(shortUrl, "http://xhslink.cn/o/6UDG4oUB8kR");
});

test("rejects text that does not contain a Xiaohongshu URL", () => {
  assert.equal(extractXhsPublishUrl("只有文案，没有链接"), "");
  assert.equal(
    extractXhsPublishUrl("https://example.com/not-xhs"),
    "",
  );
});
