import { describe, test, expect } from "bun:test";
import { DEFAULT_IGNORED_COMMENT_LOGINS } from "@/core/constants.js";
import { parseIgnoredCommentLogins, shouldIgnoreComment } from "@/poller/comment-filter.js";

const BOT_USER = "hu-agent[bot]";
const DEFAULT = [...DEFAULT_IGNORED_COMMENT_LOGINS];

describe("shouldIgnoreComment", () => {
  test("ignores the bot's own login", () => {
    expect(
      shouldIgnoreComment({
        login: BOT_USER,
        botUser: BOT_USER,
        ignoredLogins: DEFAULT,
      }),
    ).toBe(true);
  });

  test("ignores cursor[bot]", () => {
    expect(
      shouldIgnoreComment({
        login: "cursor[bot]",
        botUser: BOT_USER,
        ignoredLogins: DEFAULT,
      }),
    ).toBe(true);
  });

  test("ignores hu-reviewer[bot]", () => {
    expect(
      shouldIgnoreComment({
        login: "hu-reviewer[bot]",
        botUser: BOT_USER,
        ignoredLogins: DEFAULT,
      }),
    ).toBe(true);
  });

  test("does not ignore a human login", () => {
    expect(
      shouldIgnoreComment({
        login: "julian",
        botUser: BOT_USER,
        ignoredLogins: DEFAULT,
      }),
    ).toBe(false);
  });

  test("does not ignore when login is null (unknown author)", () => {
    expect(
      shouldIgnoreComment({
        login: null,
        botUser: BOT_USER,
        ignoredLogins: DEFAULT,
      }),
    ).toBe(false);
  });

  test("accepts ignoredLogins as an array or a Set", () => {
    expect(
      shouldIgnoreComment({
        login: "cursor[bot]",
        botUser: BOT_USER,
        ignoredLogins: new Set(DEFAULT),
      }),
    ).toBe(true);
  });
});

describe("parseIgnoredCommentLogins", () => {
  test("returns DEFAULT_IGNORED_COMMENT_LOGINS when input is undefined", () => {
    expect(parseIgnoredCommentLogins(undefined)).toEqual([...DEFAULT_IGNORED_COMMENT_LOGINS]);
  });

  test("returns DEFAULT_IGNORED_COMMENT_LOGINS when input is the empty string", () => {
    expect(parseIgnoredCommentLogins("")).toEqual([...DEFAULT_IGNORED_COMMENT_LOGINS]);
  });

  test("splits a comma-separated list, trimming whitespace", () => {
    expect(parseIgnoredCommentLogins("a[bot], b[bot] ,c[bot]")).toEqual([
      "a[bot]",
      "b[bot]",
      "c[bot]",
    ]);
  });

  test("drops empty segments produced by stray commas", () => {
    expect(parseIgnoredCommentLogins("a[bot],,b[bot]")).toEqual(["a[bot]", "b[bot]"]);
  });

  test("env override replaces — not extends — the default", () => {
    expect(parseIgnoredCommentLogins("only[bot]")).toEqual(["only[bot]"]);
  });

  test("returns a fresh array (callers may mutate without affecting the default)", () => {
    const a = parseIgnoredCommentLogins(undefined);
    const b = parseIgnoredCommentLogins(undefined);
    a.push("mutation[bot]");
    expect(b).not.toContain("mutation[bot]");
  });
});
