diff --git a/tests/commands/superGenezio/superGenezio.test.ts b/tests/commands/superGenezio/superGenezio.test.ts new file mode 100644 index 000000000..a97622d80 --- /dev/null +++ b/tests/commands/superGenezio/superGenezio.test.ts @@ -0,0 +1,109 @@ +import { vi, describe, beforeEach, test, expect } from "vitest"; +import { vol } from "memfs"; +import fs from "fs/promises"; +import inquirer from "inquirer"; +import { genezioCommand } from "../../../src/commands/superGenezio/superGenezio"; +import { IFs } from "memfs"; +import { deployCommand } from "../../../src/commands/deploy"; +import { startLocalEnvironment } from "../../../src/commands/local"; +import colors from "colors"; +import { loginCommand } from "../../../src/commands/login"; + +vi.mock("fs", async () => { + const memfs = await vi.importActual<{ fs: IFs }>("memfs"); + return { default: memfs.fs }; +}); +vi.mock("fs/promises", async () => { + const memfs = await vi.importActual<{ fs: IFs }>("memfs"); + return { default: memfs.fs.promises }; +}); +vi.mock("../../../src/commands/deploy", async () => { + return { deployCommand: vi.fn(() => Promise.resolve()) }; +}); +vi.mock("../../../src/commands/local", async () => { + return { startLocalEnvironment: vi.fn(() => Promise.resolve()) }; +}); +vi.mock("../../../src/commands/login", async (original) => { + return { loginCommand: vi.fn(() => Promise.resolve()) }; +}); + +describe("superGenezio", () => { + beforeEach(() => { + // Clean up the mocked file system before each test + vol.reset(); + }); + + test("detects genezio.yaml and deploys project", async () => { + // Create environment + await fs.mkdir(process.cwd(), { recursive: true }); + await fs.writeFile("genezio.yaml", "test"); + + const promptSpy = vi.spyOn(inquirer, "prompt"); + + // Mock user selecting Deploy + promptSpy.mockResolvedValueOnce({ command: "deploy" }); + + // Run the super command + await genezioCommand(); + + // Check if inquirer.prompt was called with the correct arguments + expect(promptSpy).toHaveBeenCalledWith([ + { + type: "list", + name: "command", + message: colors.magenta("Genezio project detected. What would you like to do?"), + choices: [ + { + name: "Deploy your project (genezio deploy)", + value: "deploy", + }, + { + name: "Start a Local server (genezio local)", + value: "local", + }, + ], + }, + ]); + + // Login should be called because the memfs doesn't have the auth token stored in .geneziorc + expect(loginCommand).toHaveBeenCalledOnce(); + // Check if the deploy command was called + expect(deployCommand).toHaveBeenCalledOnce(); + }); + + test("detects genezio.yaml and starts local", async () => { + // Create environment + await fs.mkdir(process.cwd(), { recursive: true }); + await fs.writeFile("genezio.yaml", "test"); + + const promptSpy = vi.spyOn(inquirer, "prompt"); + + // Mock user selecting Local + promptSpy.mockResolvedValueOnce({ command: "local" }); + + // Run the super command + await genezioCommand(); + + // Check if inquirer.prompt was called with the correct arguments + expect(promptSpy).toHaveBeenCalledWith([ + { + type: "list", + name: "command", + message: colors.magenta("Genezio project detected. What would you like to do?"), + choices: [ + { + name: "Deploy your project (genezio deploy)", + value: "deploy", + }, + { + name: "Start a Local server (genezio local)", + value: "local", + }, + ], + }, + ]); + + // Check if the local environment was started + expect(startLocalEnvironment).toHaveBeenCalled(); + }); +});