puter-cli
Version:
Command line interface for Puter cloud platform
206 lines (168 loc) • 5.71 kB
JavaScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
login, logout, getUserInfo, isAuthenticated, getAuthToken, getCurrentUserName,
getUsageInfo
} from '../src/commands/auth.js';
import chalk from 'chalk';
import Conf from 'conf';
import { PROJECT_NAME } from '../src/commons.js';
import * as PuterModule from '../src/modules/PuterModule.js';
// Mock console to prevent actual logging
vi.spyOn(console, 'log').mockImplementation(() => { });
vi.spyOn(console, 'error').mockImplementation(() => { });
// Mock dependencies
vi.mock('inquirer');
vi.mock('chalk', () => ({
default: {
green: vi.fn(text => text),
red: vi.fn(text => text),
dim: vi.fn(text => text),
yellow: vi.fn(text => text),
cyan: vi.fn(text => text),
}
}));
vi.mock('../src/modules/PuterModule.js');
// Mock ProfileModule
const mockProfileModule = {
switchProfileWizard: vi.fn(),
getAuthToken: vi.fn(),
getCurrentProfile: vi.fn(),
};
vi.mock('../src/modules/ProfileModule.js', () => ({
getProfileModule: vi.fn(() => mockProfileModule),
initProfileModule: vi.fn(),
}));
// Create a mock spinner object
const mockSpinner = {
start: vi.fn().mockReturnThis(),
succeed: vi.fn().mockReturnThis(),
fail: vi.fn().mockReturnThis(),
info: vi.fn().mockReturnThis(),
};
// Mock ora
vi.mock('ora', () => ({
default: vi.fn(() => mockSpinner)
}));
// Mock Conf
vi.mock('conf', () => {
return {
default: vi.fn().mockImplementation(() => ({
set: vi.fn(),
get: vi.fn(),
clear: vi.fn(),
})),
};
});
describe('auth.js', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('login', () => {
it('should login successfully with valid credentials', async () => {
await login({});
expect(mockProfileModule.switchProfileWizard).toHaveBeenCalled();
});
it('should fail login with invalid credentials', async () => {
mockProfileModule.switchProfileWizard.mockRejectedValue(new Error('Invalid credentials'));
await expect(login({})).rejects.toThrow('Invalid credentials');
expect(mockProfileModule.switchProfileWizard).toHaveBeenCalled();
});
it.skip('should handle login error', async () => {
// This test needs to be updated to reflect the new login flow
});
});
describe('logout', () => {
let config;
beforeEach(() => {
vi.clearAllMocks();
config = new Conf({ projectName: PROJECT_NAME });
});
it.skip('should logout successfully', async () => {
// This test needs to be updated to reflect the new login flow
});
it('should handle already logged out', async () => {
config.get = vi.fn().mockReturnValue(null);
await logout();
expect(mockSpinner.info).toHaveBeenCalledWith(chalk.yellow('Already logged out'));
});
it.skip('should handle logout error', async () => {
// This test needs to be updated to reflect the new login flow
});
});
describe('getUserInfo', () => {
const mockPuter = {
auth: {
getUser: vi.fn(),
},
};
beforeEach(() => {
vi.spyOn(PuterModule, 'getPuter').mockReturnValue(mockPuter);
});
it('should fetch user info successfully', async () => {
mockPuter.auth.getUser.mockResolvedValue({
username: 'testuser',
uuid: 'testuuid',
email: 'test@puter.com',
email_confirmed: true,
is_temp: false,
human_readable_age: '1 day',
feature_flags: {},
});
await getUserInfo();
expect(mockPuter.auth.getUser).toHaveBeenCalled();
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('User Information:'));
});
it('should handle fetch user info error', async () => {
mockPuter.auth.getUser.mockRejectedValue(new Error('Network error'));
await getUserInfo();
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Failed to get user info.'));
});
});
describe('Authentication', () => {
it('should return false if auth token does not exist', () => {
mockProfileModule.getAuthToken.mockReturnValue(null);
const result = isAuthenticated();
expect(result).toBe(false);
});
it('should return null if the auth_token is not defined', () => {
mockProfileModule.getAuthToken.mockReturnValue(null);
const result = getAuthToken();
expect(result).toBe(null);
});
it('should return the current username if it is defined', () => {
mockProfileModule.getCurrentProfile.mockReturnValue({ username: 'testuser' });
const result = getCurrentUserName();
expect(result).toBe('testuser');
});
});
describe('getUsageInfo', () => {
const mockPuter = {
auth: {
getMonthlyUsage: vi.fn(),
},
};
beforeEach(() => {
vi.spyOn(PuterModule, 'getPuter').mockReturnValue(mockPuter);
});
it('should fetch usage info successfully', async () => {
mockPuter.auth.getMonthlyUsage.mockResolvedValue({
allowanceInfo: {
monthUsageAllowance: 1000,
remaining: 500,
},
usage: {
total: 500,
},
appTotals: {},
});
await getUsageInfo();
expect(mockPuter.auth.getMonthlyUsage).toHaveBeenCalled();
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Allowance Information:'));
});
it('should handle fetch usage info error', async () => {
mockPuter.auth.getMonthlyUsage.mockRejectedValue(new Error('Network error'));
await getUsageInfo();
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Failed to fetch usage information.'));
});
});
});