import { execSync } from "node:child_process"; import { mkdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; // Mock the CLI module functions vi.mock("../src/file", () => ({ initializeState: vi.fn(), checkStatus: vi.fn(), syncState: vi.fn(), saveAiTranslations: vi.fn(), })); vi.mock("../src/translate", () => ({ translate: vi.fn(), })); describe("CLI", () => { let tempDir: string; let originalCwd: string; beforeEach(() => { tempDir = join(tmpdir(), `onekey-test-${Date.now()}`); mkdirSync(tempDir, { recursive: true }); originalCwd = process.cwd(); process.chdir(tempDir); // Create mock translations directory mkdirSync(join(tempDir, "translations"), { recursive: true }); mkdirSync(join(tempDir, "translations", "en"), { recursive: true }); writeFileSync( join(tempDir, "translations", "en", "main.json"), JSON.stringify({ hello: "Hello" }), ); }); afterEach(() => { process.chdir(originalCwd); rmSync(tempDir, { recursive: true, force: true }); vi.clearAllMocks(); }); describe("init command", () => { it("should initialize state with default parameters", async () => { const { initializeState } = await import("../src/file"); // Mock successful initialization (initializeState as ReturnType).mockResolvedValue( undefined, ); // This would require testing the actual CLI execution // For now, we'll test the imported functions directly expect(initializeState).toBeDefined(); }); it("should handle initialization errors", async () => { const { initializeState } = await import("../src/file"); // Mock failed initialization (initializeState as ReturnType).mockRejectedValue( new Error("Initialization failed"), ); expect(initializeState).toBeDefined(); }); }); describe("sync command", () => { it("should sync state successfully", async () => { const { syncState } = await import("../src/file"); // Mock successful sync (syncState as ReturnType).mockResolvedValue(0); expect(syncState).toBeDefined(); }); it("should handle sync errors", async () => { const { syncState } = await import("../src/file"); // Mock failed sync (syncState as ReturnType).mockRejectedValue( new Error("Sync failed"), ); expect(syncState).toBeDefined(); }); }); describe("status command", () => { it("should check status successfully", async () => { const { checkStatus } = await import("../src/file"); // Mock successful status check (checkStatus as ReturnType).mockResolvedValue(0); expect(checkStatus).toBeDefined(); }); it("should handle status check errors", async () => { const { checkStatus } = await import("../src/file"); // Mock failed status check (checkStatus as ReturnType).mockRejectedValue( new Error("Status check failed"), ); expect(checkStatus).toBeDefined(); }); }); describe("translate command", () => { it("should handle translation with environment variables", async () => { const { saveAiTranslations } = await import("../src/file"); // Mock successful translation (saveAiTranslations as ReturnType).mockResolvedValue( undefined, ); expect(saveAiTranslations).toBeDefined(); }); it("should handle translation errors", async () => { const { saveAiTranslations } = await import("../src/file"); // Mock failed translation (saveAiTranslations as ReturnType).mockRejectedValue( new Error("Translation failed"), ); expect(saveAiTranslations).toBeDefined(); }); }); describe("deprecated check command", () => { it("should warn about deprecated check command", async () => { const { checkStatus } = await import("../src/file"); // Mock successful status check (checkStatus as ReturnType).mockResolvedValue(0); expect(checkStatus).toBeDefined(); }); }); });