import chalk from "chalk"; import { expect, test, describe, jest, beforeEach, afterEach, } from "@jest/globals"; import { Command, program } from "commander"; import { newScriptCommand } from "../src/commands/new-script.command"; import fs from "fs"; import * as promptUtil from "../src/utils/prompt.util"; import * as newScriptCore from "../src/core/new-script.core"; import * as inquirer from "@inquirer/prompts"; import { DOWNLOADING_EXAMPLE_SCRIPT_MESSAGE, SCRIPT_DOWNLOADED_SUCCESSFULLY_MESSAGE, } from "../src/consts/new-script.const"; import { Language } from "../src/types/language"; describe("new-script", () => { let agentqlProgram: Command; let getScriptTypeSpy: jest.SpiedFunction<() => Promise>; let getLanguageSpy: jest.SpiedFunction<() => Promise>; let downloadExampleScriptSpy: jest.SpiedFunction< (language: Language, isAsync: boolean) => Promise >; let consoleLogSpy: jest.SpiedFunction<() => void>; let selectSpy: jest.SpiedFunction<(config: never) => Promise>; beforeEach(() => { jest.clearAllMocks(); agentqlProgram = program; agentqlProgram.addCommand(newScriptCommand); getLanguageSpy = jest.spyOn(promptUtil, "requestLanguage"); selectSpy = jest.spyOn(inquirer, "select"); getScriptTypeSpy = jest.spyOn(newScriptCore, "getScriptType"); downloadExampleScriptSpy = jest.spyOn( newScriptCore, "downloadExampleScript" ); consoleLogSpy = jest.spyOn(console, "log"); }); test("should ask for script type", async () => { // getScriptTypeSpy.mockResolvedValue("sync") getLanguageSpy.mockResolvedValue("Python"); selectSpy.mockResolvedValue("sync"); await newScriptCommand.parseAsync(["node", "agentql", "new-script"]); expect(getScriptTypeSpy).toHaveBeenCalled(); }); test("should ask for language", async () => { getScriptTypeSpy.mockResolvedValue("sync"); selectSpy.mockResolvedValue("Python"); await newScriptCommand.parseAsync(["node", "agentql", "new-script"]); expect(getLanguageSpy).toHaveBeenCalled(); }); test("should create a new python sync script", async () => { getScriptTypeSpy.mockResolvedValue("sync"); getLanguageSpy.mockResolvedValue("Python"); await newScriptCommand.parseAsync(["node", "agentql", "new-script"]); expect(getScriptTypeSpy).toHaveBeenCalled(); expect(getLanguageSpy).toHaveBeenCalled(); expect(downloadExampleScriptSpy).toHaveBeenCalled(); expect(consoleLogSpy).toHaveBeenCalledWith( DOWNLOADING_EXAMPLE_SCRIPT_MESSAGE ); expect(consoleLogSpy).toHaveBeenCalledWith( chalk.bold(SCRIPT_DOWNLOADED_SUCCESSFULLY_MESSAGE) ); }); test("should create a new python async script", async () => { getScriptTypeSpy.mockResolvedValue("async"); getLanguageSpy.mockResolvedValue("Python"); await newScriptCommand.parseAsync(["node", "agentql", "new-script"]); expect(getScriptTypeSpy).toHaveBeenCalled(); expect(getLanguageSpy).toHaveBeenCalled(); expect(downloadExampleScriptSpy).toHaveBeenCalled(); expect(consoleLogSpy).toHaveBeenCalledWith( DOWNLOADING_EXAMPLE_SCRIPT_MESSAGE ); expect(consoleLogSpy).toHaveBeenCalledWith( chalk.bold(SCRIPT_DOWNLOADED_SUCCESSFULLY_MESSAGE) ); }); test("should create a new javascript script", async () => { getLanguageSpy.mockResolvedValue("JavaScript"); await newScriptCommand.parseAsync(["node", "agentql", "new-script"]); expect(getLanguageSpy).toHaveBeenCalled(); expect(downloadExampleScriptSpy).toHaveBeenCalled(); expect(consoleLogSpy).toHaveBeenCalledWith( DOWNLOADING_EXAMPLE_SCRIPT_MESSAGE ); expect(consoleLogSpy).toHaveBeenCalledWith( chalk.bold(SCRIPT_DOWNLOADED_SUCCESSFULLY_MESSAGE) ); }); afterEach(() => { jest.restoreAllMocks(); // delete the files created by the test // Check if the file exists before attempting to delete it if (fs.existsSync("template_script.py")) { fs.unlinkSync("template_script.py"); } }); });