// src/tools/search/grep.test.ts
// Copyright (C) 2025 DTP Technologies, LLC
// All Rights Reserved

import { describe, it, expect, beforeEach } from "vitest";
import GrepTool from "./grep.js";

describe("GrepTool", () => {
  let tool: typeof GrepTool;
  let mockSession: any;

  beforeEach(() => {
    tool = GrepTool;
    mockSession = {
      _id: "test-session-id",
      user: "test-user-id",
    };
  });

  describe("definition", () => {
    it("should have correct tool name", () => {
      expect(tool.definition.function.name).toBe("grep");
    });

    it("should have correct category", () => {
      expect(tool.metadata.category).toBe("search");
    });

    it("should support all modes", () => {
      expect(tool.metadata.modes).toHaveLength(5);
    });
  });

  describe("execute", () => {
    it("should return error for missing pattern", async () => {
      const result = await tool.execute(
        { session: mockSession },
        { path: "src" },
      );

      expect(result).toContain("MISSING_PARAMETER");
      expect(result).toContain("pattern is required");
    });

    it("should return error for missing path", async () => {
      const result = await tool.execute(
        { session: mockSession },
        { pattern: "test" },
      );

      expect(result).toContain("MISSING_PARAMETER");
      expect(result).toContain("path is required");
    });

    it("should find matches in files", async () => {
      const result = await tool.execute(
        { session: mockSession },
        { pattern: "function", path: "src" },
      );

      expect(result).toContain("match");
    });

    it("should support case insensitive search", async () => {
      const result = await tool.execute(
        { session: mockSession },
        { pattern: "FUNCTION", path: "src", caseInsensitive: true },
      );

      expect(result).toContain("match");
    });

    it("should return error for invalid regex", async () => {
      const result = await tool.execute(
        { session: mockSession },
        { pattern: "[invalid", path: "src" },
      );

      expect(result).toContain("INVALID_PARAMETER");
      expect(result).toContain("Invalid regex");
    });
  });
});
