import { saveConfig, loadConfig, Config } from "./auth.js";
import fs from "fs";
import path from "path";
import os from "os";
jest.mock("fs");
jest.mock("os");
describe("Auth", () => {
const mockHomedir = "/mock/home";
const configDir = path.join(mockHomedir, ".clickup-mcp");
const configFile = path.join(configDir, "config.json");
beforeEach(() => {
(os.homedir as jest.Mock).mockReturnValue(mockHomedir);
(fs.existsSync as jest.Mock).mockReturnValue(false);
(fs.readFileSync as jest.Mock).mockReturnValue("");
(fs.writeFileSync as jest.Mock).mockImplementation(() => {});
(fs.mkdirSync as jest.Mock).mockImplementation(() => {});
});
afterEach(() => {
jest.clearAllMocks();
});
describe("saveConfig", () => {
it("should create directory if it does not exist", () => {
(fs.existsSync as jest.Mock).mockReturnValue(false);
const config: Config = { clientId: "123" };
saveConfig(config);
expect(fs.mkdirSync).toHaveBeenCalledWith(configDir);
expect(fs.writeFileSync).toHaveBeenCalledWith(
configFile,
JSON.stringify(config, null, 2),
);
});
it("should not create directory if it exists", () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
const config: Config = { clientId: "123" };
saveConfig(config);
expect(fs.mkdirSync).not.toHaveBeenCalled();
expect(fs.writeFileSync).toHaveBeenCalledWith(
configFile,
JSON.stringify(config, null, 2),
);
});
});
describe("loadConfig", () => {
it("should return empty object if file does not exist", () => {
(fs.existsSync as jest.Mock).mockReturnValue(false);
const config = loadConfig();
expect(config).toEqual({});
});
it("should return parsed config if file exists", () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
const mockConfig = { clientId: "123" };
(fs.readFileSync as jest.Mock).mockReturnValue(
JSON.stringify(mockConfig),
);
const config = loadConfig();
expect(config).toEqual(mockConfig);
});
});
});