import { expect, test, describe, jest, beforeEach, afterEach, } from "@jest/globals"; import { program } from "commander"; import { initCommand } from "../src/commands/init.command"; import * as initCore from "../src/core/init.core"; import * as inquirer from "@inquirer/prompts"; import * as agentqlUtil from "../src/utils/agentql.util"; import * as prompt from "../src/utils/prompt.util"; import * as processUtil from "../src/utils/process.util"; import { LANGUAGE } from "../src/enums/language.enum"; jest.mock("child_process", () => ({ exec: jest.fn(), })); describe("init command", () => { let agentqlProgram: any; let requestInstallDependenciesSpy: any; let installDependenciesSpy: any; let installPlaywrightDependenciesPythonSpy: any; let isPythonPlaywrightInstalledSpy: any; let installPlaywrightDependenciesJavascriptSpy: any; let isJavascriptPlaywrightInstalledSpy: any; let downloadSampleScriptSpy: any; let consoleLogSpy: any; let languageSpy: any; let passwordInquirerSpy: any; let validateApiKeySpy: any; let processExitSpy: any; beforeEach(async () => { jest.clearAllMocks(); // init core mocks requestInstallDependenciesSpy = jest .spyOn(initCore, "requestInstallDependencies") .mockResolvedValue(true); installDependenciesSpy = jest .spyOn(initCore, "installDependencies") .mockResolvedValue(); installPlaywrightDependenciesPythonSpy = jest .spyOn(processUtil, "installPlaywrightDependenciesPython") .mockResolvedValue(); installPlaywrightDependenciesJavascriptSpy = jest .spyOn(processUtil, "installPlaywrightDependenciesJavascript") .mockResolvedValue(); languageSpy = jest .spyOn(prompt, "requestLanguage") .mockResolvedValue(LANGUAGE.PYTHON); downloadSampleScriptSpy = jest.spyOn( initCore, "requestDownloadSampleScript" ); jest .spyOn(processUtil, "isJavascriptPlaywrightInstalled") .mockResolvedValue(true); // mock process exit to return the number because jest won't run if we don't do this processExitSpy = jest .spyOn(process, "exit") .mockImplementation((number) => { return number as never; }); // console mocks consoleLogSpy = jest.spyOn(console, "log"); // inquirer mocks passwordInquirerSpy = jest .spyOn(inquirer, "password") .mockResolvedValue("valid_api_key"); // agentql util mocks validateApiKeySpy = jest .spyOn(agentqlUtil, "validateApiKey") .mockResolvedValue(true); // child process mocks jest.mock("child_process"); agentqlProgram = program; agentqlProgram.addCommand(initCommand); }); afterEach(() => { jest.restoreAllMocks(); }); test("should call all core functions", async () => { downloadSampleScriptSpy = downloadSampleScriptSpy.mockResolvedValue(); await agentqlProgram.parseAsync(["node", "agentql", "init"]); expect(languageSpy).toHaveBeenCalled(); expect(requestInstallDependenciesSpy).toHaveBeenCalled(); expect(initCore.installDependencies).toHaveBeenCalled(); expect(downloadSampleScriptSpy).toHaveBeenCalled(); }); test("should install dependencies", async () => { requestInstallDependenciesSpy = jest .spyOn(initCore, "requestInstallDependencies") .mockResolvedValue(true); jest.spyOn(initCore, "installDependencies").mockRestore(); installDependenciesSpy = jest.spyOn(initCore, "installDependencies"); downloadSampleScriptSpy = downloadSampleScriptSpy.mockResolvedValue(); await initCommand.parseAsync(["node", "agentql", "init"]); expect(requestInstallDependenciesSpy).toHaveBeenCalled(); expect(installDependenciesSpy).toHaveBeenCalled(); expect(consoleLogSpy).toHaveBeenCalledWith("Installing dependencies..."); }); test("should handle example python script download", async () => { jest.spyOn(inquirer, "confirm").mockResolvedValue(true); jest.spyOn(prompt, "requestLanguage").mockResolvedValue(LANGUAGE.PYTHON); await agentqlProgram.parseAsync(["node", "agentql", "init"]); expect(downloadSampleScriptSpy).toHaveBeenCalled(); }); test("should handle example javascript script download", async () => { jest.spyOn(inquirer, "confirm").mockResolvedValue(true); jest .spyOn(prompt, "requestLanguage") .mockResolvedValue(LANGUAGE.JAVASCRIPT); await agentqlProgram.parseAsync(["node", "agentql", "init"]); expect(downloadSampleScriptSpy).toHaveBeenCalled(); }); });