telegram-mcp/cli/tests/unit/auth.test.ts
2026-11-05 14:26:28 +03:00

493 lines
19 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { randomUUID } from 'node:crypto';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { existsSync, rmSync, mkdirSync, writeFileSync } from 'node:fs';
// ---- Mocks ----
// Mock prompt
const mockAsk = vi.fn();
const mockAskSecret = vi.fn();
const mockClose = vi.fn();
vi.mock('../../src/lib/prompt.js', () => ({
createPrompt: vi.fn(() => ({ ask: mockAsk, askSecret: mockAskSecret, close: mockClose })),
}));
// Mock output
const mockOutputSuccess = vi.fn();
const mockOutputError = vi.fn();
const mockLogStatus = vi.fn();
vi.mock('../../src/lib/output.js', () => ({
outputSuccess: (...args: any[]) => mockOutputSuccess(...args),
outputError: (...args: any[]) => mockOutputError(...args),
logStatus: (...args: any[]) => mockLogStatus(...args),
}));
// Hoisted mock state for telegram client
const {
mockStart,
mockConnect,
mockDestroy,
mockCheckAuthorization,
mockGetMe,
mockInvoke,
mockSessionSave,
} = vi.hoisted(() => ({
mockStart: vi.fn().mockResolvedValue(undefined),
mockConnect: vi.fn().mockResolvedValue(undefined),
mockDestroy: vi.fn().mockResolvedValue(undefined),
mockCheckAuthorization: vi.fn().mockResolvedValue(true),
mockGetMe: vi.fn().mockResolvedValue({ id: 123, phone: '+15551234567', username: 'testuser', firstName: 'Test' }),
mockInvoke: vi.fn().mockResolvedValue(true),
mockSessionSave: vi.fn().mockReturnValue('saved-session-string'),
}));
const mockClientInstance = {
start: mockStart,
connect: mockConnect,
destroy: mockDestroy,
checkAuthorization: mockCheckAuthorization,
getMe: mockGetMe,
invoke: mockInvoke,
session: { save: mockSessionSave },
};
vi.mock('teleproto', () => ({
TelegramClient: vi.fn().mockImplementation(() => mockClientInstance),
sessions: {
StringSession: vi.fn().mockImplementation((s: string) => ({ _session: s })),
},
Api: {
auth: {
LogOut: vi.fn().mockImplementation(() => ({ className: 'auth.LogOut' })),
},
},
}));
// Mock config
const mockConfigGet = vi.fn();
const mockConfigSet = vi.fn();
const mockConfigDelete = vi.fn();
const mockConfigObj = {
get: mockConfigGet,
set: mockConfigSet,
delete: mockConfigDelete,
path: '/tmp/mock-config.json',
};
vi.mock('../../src/lib/config.js', () => ({
createConfig: vi.fn(() => mockConfigObj),
resolveCredentials: vi.fn(),
getCredentialsOrThrow: vi.fn(() => ({ apiId: 12345, apiHash: 'testhash' })),
}));
// We need to mock SessionStore at the module level
const mockStoreSave = vi.fn().mockResolvedValue(undefined);
const mockStoreLoad = vi.fn().mockResolvedValue('');
const mockStoreDelete = vi.fn().mockResolvedValue(undefined);
const mockStoreDeleteUnlocked = vi.fn();
const mockStoreWithLock = vi.fn().mockImplementation(async (_profile: string, fn: (s: string) => Promise<any>) => {
const session = await mockStoreLoad();
return fn(session);
});
vi.mock('../../src/lib/session-store.js', () => ({
SessionStore: vi.fn().mockImplementation(() => ({
save: vi.fn(),
saveUnlocked: mockStoreSave,
load: mockStoreLoad,
delete: mockStoreDelete,
deleteUnlocked: mockStoreDeleteUnlocked,
withLock: mockStoreWithLock,
filePath: (p: string) => `/mock/sessions/${p}.session`,
})),
}));
// Mock client module
vi.mock('../../src/lib/client.js', () => ({
createClientForAuth: vi.fn(async () => mockClientInstance),
withClient: vi.fn(async (_opts: any, fn: any) => fn(mockClientInstance, new AbortController().signal)),
}));
// Import after mocks
import { loginAction } from '../../src/commands/auth/login.js';
import { statusAction } from '../../src/commands/auth/status.js';
import { logoutAction } from '../../src/commands/auth/logout.js';
import { createAuthCommand } from '../../src/commands/auth/index.js';
// Create a mock Command context for action handlers
function createMockCommandContext(opts: Record<string, any> = {}) {
return {
optsWithGlobals: vi.fn(() => ({
profile: 'default',
quiet: false,
config: undefined,
json: true,
human: false,
verbose: false,
...opts,
})),
};
}
describe('auth --daemon', () => {
it('status refuses --daemon instead of opening a second client', async () => {
const ctx = createMockCommandContext({ daemon: true });
await statusAction.call(ctx as any);
expect(mockOutputError).toHaveBeenCalledWith(
expect.stringContaining('--daemon'),
'DAEMON_PROXY_UNAVAILABLE',
);
expect(mockStoreWithLock).not.toHaveBeenCalled();
});
it('login refuses --daemon', async () => {
const ctx = createMockCommandContext({ daemon: true });
await loginAction.call(ctx as any);
expect(mockOutputError).toHaveBeenCalledWith(
expect.stringContaining('--daemon'),
'DAEMON_PROXY_UNAVAILABLE',
);
});
it('logout refuses --daemon', async () => {
const ctx = createMockCommandContext({ daemon: true });
await logoutAction.call(ctx as any);
expect(mockOutputError).toHaveBeenCalledWith(
expect.stringContaining('--daemon'),
'DAEMON_PROXY_UNAVAILABLE',
);
});
});
describe('loginAction', () => {
const originalIsTTY = process.stdin.isTTY;
async function captureLoginBanner(opts: Record<string, unknown> = {}, tty = { stdout: true, stderr: true }) {
const stdoutTTY = Object.getOwnPropertyDescriptor(process.stdout, 'isTTY');
const stderrTTY = Object.getOwnPropertyDescriptor(process.stderr, 'isTTY');
Object.defineProperty(process.stdout, 'isTTY', { configurable: true, value: tty.stdout });
Object.defineProperty(process.stderr, 'isTTY', { configurable: true, value: tty.stderr });
const stdout = vi.spyOn(process.stdout, 'write').mockReturnValue(true);
const stderr = vi.spyOn(process.stderr, 'write').mockReturnValue(true);
try {
await loginAction.call(createMockCommandContext(opts) as any);
return {
stdout: stdout.mock.calls.map(([chunk]) => String(chunk)).join(''),
stderr: stderr.mock.calls.map(([chunk]) => String(chunk)).join(''),
};
} finally {
stdout.mockRestore();
stderr.mockRestore();
if (stdoutTTY) Object.defineProperty(process.stdout, 'isTTY', stdoutTTY);
else Reflect.deleteProperty(process.stdout, 'isTTY');
if (stderrTTY) Object.defineProperty(process.stderr, 'isTTY', stderrTTY);
else Reflect.deleteProperty(process.stderr, 'isTTY');
}
}
beforeEach(() => {
vi.clearAllMocks();
mockAsk.mockResolvedValue('+15551234567');
mockAskSecret.mockResolvedValue('secret123');
// Default: simulate interactive TTY
Object.defineProperty(process.stdin, 'isTTY', { value: true, writable: true, configurable: true });
});
afterEach(() => {
Object.defineProperty(process.stdin, 'isTTY', { value: originalIsTTY, writable: true, configurable: true });
vi.unstubAllEnvs();
});
it('writes the login mascot only to terminal stderr', async () => {
vi.stubEnv('TERM', 'xterm-256color');
vi.stubEnv('NO_COLOR', '1');
const captured = await captureLoginBanner();
expect(captured.stdout).toBe('');
expect(captured.stderr).toContain('.------.');
expect(captured.stderr).toContain('Telegram, from your terminal.');
expect(captured.stderr).not.toContain('\x1b[');
expect(mockOutputSuccess).toHaveBeenCalledWith(expect.objectContaining({ loggedIn: true }));
});
it.each([
{ label: 'quiet', opts: { quiet: true }, tty: { stdout: true, stderr: true }, term: 'xterm-256color' },
{ label: 'redirected stdout', opts: {}, tty: { stdout: false, stderr: true }, term: 'xterm-256color' },
{ label: 'redirected stderr', opts: {}, tty: { stdout: true, stderr: false }, term: 'xterm-256color' },
{ label: 'dumb terminal', opts: {}, tty: { stdout: true, stderr: true }, term: 'dumb' },
])('omits the login mascot for $label', async ({ opts, tty, term }) => {
vi.stubEnv('TERM', term);
const captured = await captureLoginBanner(opts, tty);
expect(captured).toEqual({ stdout: '', stderr: '' });
expect(mockStart).toHaveBeenCalledOnce();
});
it('calls client.start() with phone, code, and password callbacks', async () => {
const ctx = createMockCommandContext();
await loginAction.call(ctx as any);
expect(mockStart).toHaveBeenCalledOnce();
const startArgs = mockStart.mock.calls[0][0];
expect(startArgs).toHaveProperty('phoneNumber');
expect(startArgs).toHaveProperty('phoneCode');
expect(startArgs).toHaveProperty('password');
expect(startArgs).toHaveProperty('onError');
expect(typeof startArgs.phoneNumber).toBe('function');
expect(typeof startArgs.phoneCode).toBe('function');
expect(typeof startArgs.password).toBe('function');
});
it('saves session string after successful login', async () => {
const ctx = createMockCommandContext();
await loginAction.call(ctx as any);
expect(mockStoreSave).toHaveBeenCalledWith('default', 'saved-session-string');
});
it('asks for the phone before fetching credentials or opening a connection', async () => {
await loginAction.call(createMockCommandContext() as any);
const { getCredentialsOrThrow } = await import('../../src/lib/config.js');
expect(mockAsk.mock.calls[0][0]).toBe('Phone number (international format): ');
expect(mockAsk.mock.invocationCallOrder[0]).toBeLessThan(vi.mocked(getCredentialsOrThrow).mock.invocationCallOrder[0]);
expect(mockAsk.mock.invocationCallOrder[0]).toBeLessThan(mockConnect.mock.invocationCallOrder[0]);
const params = mockStart.mock.calls[0][0];
expect(await params.phoneNumber()).toBe('+15551234567');
expect(mockAsk).toHaveBeenCalledOnce();
});
it('passes a normalized --phone literally to gramjs without asking for it', async () => {
await loginAction.call(createMockCommandContext({ phone: '+1 (555) 123-4567' }) as any);
expect(mockAsk).not.toHaveBeenCalled();
expect(mockStart.mock.calls[0][0].phoneNumber).toBe('+15551234567');
expect(mockConfigSet).toHaveBeenCalledWith('profiles.default', expect.objectContaining({ phone: '+15551234567' }));
});
it.each(['', '@username', '+012345678', '+123', '+1234567890123456'])('rejects invalid --phone %s before connecting', async phone => {
await loginAction.call(createMockCommandContext({ phone }) as any);
expect(mockConnect).not.toHaveBeenCalled();
expect(mockStart).not.toHaveBeenCalled();
expect(mockStoreSave).not.toHaveBeenCalled();
expect(mockOutputError).toHaveBeenCalledWith(expect.stringContaining('Phone number must'), 'INVALID_INPUT');
expect(mockClose).toHaveBeenCalledOnce();
});
it('asks for a corrected phone when the interactive SDK flow retries it', async () => {
mockAsk.mockResolvedValueOnce('+15551234567').mockResolvedValueOnce('+15557654321');
mockStart.mockImplementationOnce(async params => {
expect(await params.phoneNumber()).toBe('+15551234567');
expect(await params.phoneNumber()).toBe('+15557654321');
});
await loginAction.call(createMockCommandContext() as any);
expect(mockConfigSet).toHaveBeenCalledWith('profiles.default', expect.objectContaining({ phone: '+15557654321' }));
});
it.each([false, true])('pauses SDK diagnostics during code/2FA input and restores them (reject=%s)', async reject => {
const { createGramjsLogger } = await import('../../src/lib/gramjs-logger.js');
const { setVerboseMode } = await import('../../src/lib/cli-mode.js');
const logger = createGramjsLogger();
(mockClientInstance as any).logger = logger;
const stderr = vi.spyOn(process.stderr, 'write').mockReturnValue(true);
setVerboseMode(true);
const answer = async () => {
logger.debug('hidden ping during input');
logger.error('hidden diagnostic during input');
if (reject) throw new Error('prompt cancelled');
return 'synthetic-code';
};
mockAsk.mockImplementationOnce(answer);
mockAskSecret.mockImplementationOnce(answer);
mockStart.mockImplementationOnce(async params => {
await params.phoneCode(true);
await params.password();
});
try {
await loginAction.call(createMockCommandContext({ phone: '+15551234567' }) as any);
logger.debug('diagnostics restored');
const output = stderr.mock.calls.map(([chunk]) => String(chunk)).join('');
expect(output).not.toContain('hidden ping');
expect(output).not.toContain('hidden diagnostic');
expect(output).toContain('diagnostics restored');
} finally {
delete (mockClientInstance as any).logger;
setVerboseMode(false);
stderr.mockRestore();
mockAsk.mockReset();
mockAskSecret.mockReset();
}
});
it('stops after exhausted connection retries without starting auth or saving a session', async () => {
mockConnect.mockResolvedValueOnce(false);
await loginAction.call(createMockCommandContext() as any);
expect(mockOutputError).toHaveBeenCalledWith(expect.any(String), 'CONNECTION_FAILED');
expect(mockStart).not.toHaveBeenCalled();
expect(mockStoreSave).not.toHaveBeenCalled();
expect(mockConfigSet).not.toHaveBeenCalled();
expect(mockDestroy).toHaveBeenCalledOnce();
expect(mockClose).toHaveBeenCalledOnce();
});
it('outputs login metadata without disclosing the session string', async () => {
const ctx = createMockCommandContext();
await loginAction.call(ctx as any);
expect(mockOutputSuccess).toHaveBeenCalledOnce();
const data = mockOutputSuccess.mock.calls[0][0];
expect(data).toMatchObject({ loggedIn: true, profile: 'default' });
expect(data).not.toHaveProperty('session');
expect(JSON.stringify(data)).not.toContain('saved-session');
expect(data).toHaveProperty('phone');
});
it('destroys client and closes prompt on completion', async () => {
const ctx = createMockCommandContext();
await loginAction.call(ctx as any);
expect(mockDestroy).toHaveBeenCalled();
expect(mockClose).toHaveBeenCalled();
});
it('destroys client and closes prompt even on error', async () => {
mockStart.mockRejectedValueOnce(new Error('auth failed'));
const ctx = createMockCommandContext();
// Should not throw (error is handled gracefully)
await loginAction.call(ctx as any);
expect(mockDestroy).toHaveBeenCalled();
expect(mockClose).toHaveBeenCalled();
});
it('outputs NOT_INTERACTIVE error when stdin is not a TTY', async () => {
Object.defineProperty(process.stdin, 'isTTY', { value: false, writable: true, configurable: true });
const ctx = createMockCommandContext();
await loginAction.call(ctx as any);
expect(mockOutputError).toHaveBeenCalledWith(
expect.stringContaining('Interactive login requires a terminal'),
'NOT_INTERACTIVE',
);
// Should NOT have tried to start auth
expect(mockStart).not.toHaveBeenCalled();
});
it('uses askSecret for 2FA password callback (not ask)', async () => {
// Make client.start call the password callback
mockStart.mockImplementationOnce(async (opts: any) => {
await opts.phoneNumber();
await opts.phoneCode(true);
await opts.password('my hint');
});
const ctx = createMockCommandContext();
await loginAction.call(ctx as any);
// Phone and code should use ask()
expect(mockAsk).toHaveBeenCalledWith('Phone number (international format): ');
expect(mockAsk).toHaveBeenCalledWith('Code (from Telegram app): ');
// Password should use askSecret() for masked input
expect(mockAskSecret).toHaveBeenCalledWith('2FA password (hint: my hint): ');
});
});
describe('statusAction', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('outputs authorized: false when no session file exists', async () => {
mockStoreLoad.mockResolvedValueOnce('');
const ctx = createMockCommandContext();
await statusAction.call(ctx as any);
expect(mockOutputSuccess).toHaveBeenCalledOnce();
const data = mockOutputSuccess.mock.calls[0][0];
expect(data.authorized).toBe(false);
});
it('checks authorization with existing session', async () => {
mockStoreLoad.mockResolvedValueOnce('existing-session');
mockCheckAuthorization.mockResolvedValueOnce(true);
const ctx = createMockCommandContext();
await statusAction.call(ctx as any);
expect(mockOutputSuccess).toHaveBeenCalledOnce();
const data = mockOutputSuccess.mock.calls[0][0];
expect(data.authorized).toBe(true);
});
it('serializes gramjs user.id as a string, not a BigInteger object', async () => {
mockStoreLoad.mockResolvedValueOnce('existing-session');
mockCheckAuthorization.mockResolvedValueOnce(true);
// gramjs big-integer: JSON.stringify produces {} unless converted via toString()
const gramjsId = {
toString: () => '123456789012345',
toJSON: () => ({}),
};
mockGetMe.mockResolvedValueOnce({
id: gramjsId,
phone: '+15551234567',
username: 'testuser',
firstName: 'Test',
});
const ctx = createMockCommandContext();
await statusAction.call(ctx as any);
const data = mockOutputSuccess.mock.calls[0][0];
expect(data.authorized).toBe(true);
expect(data.user.id).toBe('123456789012345');
expect(JSON.parse(JSON.stringify(data.user)).id).toBe('123456789012345');
});
});
describe('logoutAction', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('outputs error when not logged in', async () => {
mockStoreLoad.mockResolvedValueOnce('');
const ctx = createMockCommandContext();
await logoutAction.call(ctx as any);
expect(mockOutputError).toHaveBeenCalledOnce();
});
it('invokes auth.LogOut and deletes session file (without re-locking)', async () => {
mockStoreLoad.mockResolvedValueOnce('existing-session');
const ctx = createMockCommandContext();
await logoutAction.call(ctx as any);
expect(mockInvoke).toHaveBeenCalled();
// Must use deleteUnlocked (not delete) to avoid deadlock inside withLock
expect(mockStoreDeleteUnlocked).toHaveBeenCalledWith('default');
expect(mockStoreDelete).not.toHaveBeenCalled();
});
it('outputs loggedOut: true on success', async () => {
mockStoreLoad.mockResolvedValueOnce('existing-session');
const ctx = createMockCommandContext();
await logoutAction.call(ctx as any);
expect(mockOutputSuccess).toHaveBeenCalledOnce();
const data = mockOutputSuccess.mock.calls[0][0];
expect(data.loggedOut).toBe(true);
});
});
describe('createAuthCommand', () => {
it('returns a Command with auth name', () => {
const cmd = createAuthCommand();
expect(cmd.name()).toBe('auth');
});
it('has login, status, and logout subcommands', () => {
const cmd = createAuthCommand();
const subcommands = cmd.commands.map((c: any) => c.name());
expect(subcommands).toContain('login');
expect(subcommands).toContain('status');
expect(subcommands).toContain('logout');
});
});