import { describe, expect, test } from "bun:test";

import { isUnsafeRegex } from "../../../../../src/server/checker/expect/redos";

describe("isUnsafeRegex", () => {
  test("识别嵌套量词", () => {
    expect(isUnsafeRegex("(a+)+$")).toBe(true);
    expect(isUnsafeRegex("(a*)*")).toBe(true);
    expect(isUnsafeRegex("(a?)+")).toBe(true);
    expect(isUnsafeRegex("(\\d+)*x")).toBe(true);
    expect(isUnsafeRegex("(?:a+)+")).toBe(true);
  });

  test("识别重叠交替分支", () => {
    expect(isUnsafeRegex("(a|a)+")).toBe(true);
    expect(isUnsafeRegex("(a|aa)*")).toBe(true);
  });

  test("安全正则不误判", () => {
    expect(isUnsafeRegex("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")).toBe(false);
    expect(isUnsafeRegex("^(ok|healthy)$")).toBe(false);
    expect(isUnsafeRegex("^[a-z0-9_-]+$")).toBe(false);
    expect(isUnsafeRegex("([a+])+")).toBe(false);
  });
});
