/**
 * Regression tests for hashtag parsing in community posts.
 *
 * Two things these protect:
 *
 *  1. **Arabic tags.** The pattern is `\p{L}` with the `u` flag, not `\w`.
 *     Posts here are written in Arabic as often as English, and `#تطوع` has to
 *     be a tag. A `\w`-based pattern silently matches nothing in Arabic.
 *  2. **No `lastIndex` leak.** The module-level regex is `g`-flagged, so
 *     reusing the same object across calls would carry `lastIndex` over and
 *     start the next parse partway through the string. `splitHashtags` builds a
 *     fresh one per call; the repeat-call test below is what catches a
 *     regression there.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { splitHashtags } from "@/components/shared/hashtags";

const tags = (text: string) =>
  splitHashtags(text).filter((s) => s.kind === "tag").map((s) => s.value);

const rebuild = (text: string) =>
  splitHashtags(text)
    .map((s) => (s.kind === "tag" ? `#${s.value}` : s.value))
    .join("");

describe("splitHashtags", () => {
  test("finds a tag in the middle of a sentence", () => {
    assert.deepEqual(tags("join the #cleanup today"), ["cleanup"]);
  });

  test("keeps the surrounding text as its own segments", () => {
    const segments = splitHashtags("join the #cleanup today");
    assert.deepEqual(segments.map((s) => s.kind), ["text", "tag", "text"]);
    assert.equal(segments[0].value, "join the ");
    assert.equal(segments[2].value, " today");
  });

  test("matches Arabic tags", () => {
    assert.deepEqual(tags("تعالوا #تطوع معنا"), ["تطوع"]);
  });

  test("matches a mixed-script post", () => {
    assert.deepEqual(tags("#volunteer و #تطوع"), ["volunteer", "تطوع"]);
  });

  test("matches digits and underscores, which the pattern allows", () => {
    assert.deepEqual(tags("#beach_cleanup2026"), ["beach_cleanup2026"]);
  });

  test("stops a tag at punctuation", () => {
    assert.deepEqual(tags("#cleanup, #beach."), ["cleanup", "beach"]);
    assert.deepEqual(tags("#cleanup!"), ["cleanup"]);
  });

  test("handles a tag at the very start and very end", () => {
    assert.deepEqual(tags("#start of it"), ["start"]);
    assert.deepEqual(tags("ends with #end"), ["end"]);
    assert.deepEqual(tags("#only"), ["only"]);
  });

  test("handles adjacent tags with nothing between them", () => {
    assert.deepEqual(tags("#one #two #three"), ["one", "two", "three"]);
  });

  test("a bare # is not a tag", () => {
    assert.deepEqual(tags("# nope"), []);
    assert.deepEqual(tags("#"), []);
    assert.deepEqual(tags("a # b"), []);
  });

  test("text with no tags is one text segment", () => {
    const segments = splitHashtags("nothing to see");
    assert.equal(segments.length, 1);
    assert.deepEqual(segments[0], {
      kind: "text",
      value: "nothing to see",
      at: 0,
    });
  });

  test("empty input yields no segments", () => {
    assert.deepEqual(splitHashtags(""), []);
  });

  test("segments always reassemble into the original text", () => {
    for (const text of [
      "join the #cleanup today",
      "#one #two",
      "تعالوا #تطوع معنا",
      "no tags here",
      "#start middle #end",
      "trailing space #tag ",
      "#a,#b.#c",
    ]) {
      assert.equal(rebuild(text), text, text);
    }
  });

  test("repeated calls give the same answer — no regex lastIndex leak", () => {
    const text = "join the #cleanup today";
    const first = tags(text);
    for (let i = 0; i < 5; i++) {
      assert.deepEqual(tags(text), first, `call ${i + 2}`);
    }
  });

  test("`at` points at where each segment starts in the source", () => {
    const text = "ab #cd ef";
    const segments = splitHashtags(text);
    for (const segment of segments) {
      const literal = segment.kind === "tag" ? `#${segment.value}` : segment.value;
      assert.equal(text.substring(segment.at, segment.at + literal.length), literal);
    }
  });
});

import { describe as describeParity, test as testParity } from "node:test";

describeParity("parity with the backend extractor (CommunityText::hashtags)", () => {
  const tags = (text: string) => splitHashtags(text).filter((s) => s.kind === "tag").map((s) => s.value);

  testParity("an Arabic tag with diacritics is one whole tag", () => {
    assert.deepEqual(tags("مرحبا #رَمَضان كريم"), ["رَمَضان"]);
  });

  testParity("a # glued to a word is not a tag", () => {
    assert.deepEqual(tags("abc#x and a#b"), []);
    assert.deepEqual(tags("page 1#2"), []);
    assert.deepEqual(tags("word_#x"), []);
  });

  testParity("a tag after whitespace, punctuation or the start still works", () => {
    assert.deepEqual(tags("#first (#second) x,#third"), ["first", "second", "third"]);
  });
});
