"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
jest.mock("../src/services/S3", () => ({
    S3Service: jest.fn().mockImplementation(() => ({
        uploadFile: jest.fn(),
        downloadFile: jest.fn(),
        uploadToPresignedUrl: jest.fn(),
        downloadFromPresignedUrl: jest.fn(),
    })),
}));
jest.mock("@aws-sdk/client-s3", () => ({
    S3Client: jest.fn(),
}));
jest.mock("fs");
jest.mock("../src/plugins/plugins");
jest.mock("../src/utils");
jest.mock("../src/agents/tools/lintFile");
jest.mock("../src/index");
const fs = __importStar(require("fs"));
const tools_1 = require("../src/agents/tools");
const services_1 = require("../src/services");
const patch_1 = require("../src/agents/tools/patch");
const utils = __importStar(require("../src/utils"));
const lintFile_1 = require("../src/agents/tools/lintFile");
const index_1 = require("../src/index");
const mockFs = jest.mocked(fs);
const mockServices = jest.mocked(services_1.services);
const mockUtils = jest.mocked(utils);
const mockLintFile = jest.mocked(lintFile_1.lintFile);
const mockEmbed = jest.mocked(index_1.embed);
describe("Agent Tools Tests", () => {
    test("searchFiles should call the embeddings plugin with the correct keyword", async () => {
        const { Plugins } = mockServices();
        const expectedResult = JSON.stringify({ files: ["test1.js", "test2.js"] });
        const mocked = Plugins;
        const keyword = "test";
        mocked.call.mockResolvedValue(expectedResult);
        const result = await (0, tools_1.embeddingSearch)(keyword);
        expect(Plugins.call).toHaveBeenCalledWith("embeddings", keyword);
        expect(result).toBe(expectedResult);
    });
    test("readFile should return the content of a file", async () => {
        const filePath = "test.txt";
        const fileContent = "Hello World";
        mockUtils.fileExists.mockResolvedValue(true);
        mockFs.readFileSync.mockReturnValue(fileContent);
        const result = await (0, tools_1.readFile)(filePath);
        expect(mockFs.readFileSync).toHaveBeenCalledWith(filePath, "utf8");
        expect(result).toContain(fileContent);
    });
    test("scanFile should return the contents of a specified range of lines from a file", () => {
        const filePath = "test.txt";
        const fileContentLines = ["Line1", "Line2", "Line3", "Line4", "Line5"];
        const startLine = 3;
        const endLine = 3;
        mockFs.readFileSync.mockReturnValue(fileContentLines.join("\n"));
        const result = (0, tools_1.scanFile)(filePath, startLine, endLine);
        expect(mockFs.readFileSync).toHaveBeenCalledWith(filePath, "utf8");
        expect(result).toBe(JSON.stringify([
            [1, "Line1"],
            [2, "Line2"],
            [3, "Line3"],
            [4, "Line4"],
            [5, "Line5"],
        ]));
    });
    test("writeFile should write the full contents to a file", () => {
        const filePath = "testWrite.txt";
        const contentToWrite = "Writing to file";
        mockFs.writeFileSync.mockImplementation(() => { });
        const result = (0, tools_1.writeFile)(filePath, contentToWrite);
        expect(mockFs.writeFileSync).toHaveBeenCalledWith(filePath, contentToWrite);
        expect(result).toBe(`File ${filePath} written`);
    });
    test("applyPatchFile should apply a patch to a file", async () => {
        const filePath = "testPatch.txt";
        const originalContent = "Original content\n";
        const patch = "@@ -1,1 +1,1 @@\n-Original content\n+Patched content\n";
        mockFs.existsSync.mockReturnValue(true);
        mockUtils.readFile.mockResolvedValue(originalContent);
        mockUtils.writeFile.mockResolvedValue(undefined);
        mockUtils.fileExists.mockResolvedValue(true);
        mockUtils.mkdir.mockResolvedValue(undefined);
        mockUtils.splitByNewLines.mockImplementation((text) => text.split(/\r?\n/));
        mockLintFile.mockResolvedValue("");
        mockEmbed.mockResolvedValue(undefined);
        const result = await (0, patch_1.patchFile)(filePath, patch);
        expect(result).toContain("Patch applied successfully");
    }, 60000);
    test("execCommand should execute a system command and return its output", async () => {
        const command = 'echo "Hello World"';
        const expectedOutput = '$ echo "Hello World"\nHello World\n';
        const result = await (0, tools_1.execCommand)(command);
        expect(result).toEqual(expectedOutput);
    });
    test("execCommand should return an error message if the command fails", async () => {
        const command = "exit 1";
        const expectedOutput = "$ exit 1\nCommand failed: exit 1";
        const result = await (0, tools_1.execCommand)(command);
        expect(result.trim()).toEqual(expectedOutput);
    });
    test("it should run a test", () => {
        expect(true).toEqual(true);
    });
});
//# sourceMappingURL=test.spec.js.map