import { describe, it, expect, vi, beforeEach } from 'vitest'; // ---- Mocks ---- const mockOutputSuccess = vi.fn(); const mockOutputError = vi.fn(); vi.mock('../../src/lib/output.js', () => ({ outputSuccess: (...args: any[]) => mockOutputSuccess(...args), outputError: (...args: any[]) => mockOutputError(...args), logStatus: vi.fn(), })); // Hoisted mock state for telegram client and entity classes const { mockConnect, mockDestroy, mockInvoke, MockUser, MockChannel, MockChat } = vi.hoisted(() => { class MockUser { className = 'User'; constructor(data: any) { Object.assign(this, data); } } class MockChannel { className = 'Channel'; constructor(data: any) { Object.assign(this, data); } } class MockChat { className = 'Chat'; constructor(data: any) { Object.assign(this, data); } } return { mockConnect: vi.fn().mockResolvedValue(undefined), mockDestroy: vi.fn().mockResolvedValue(undefined), mockInvoke: vi.fn().mockResolvedValue(true), MockUser, MockChannel, MockChat, }; }); const mockClientInstance = { connect: mockConnect, destroy: mockDestroy, invoke: mockInvoke, }; vi.mock('teleproto', () => ({ TelegramClient: vi.fn().mockImplementation(() => mockClientInstance), sessions: { StringSession: vi.fn().mockImplementation((s: string) => ({ _session: s })), }, Api: { User: MockUser, Channel: MockChannel, Chat: MockChat, contacts: { DeleteContacts: vi.fn(), }, }, })); // Mock config vi.mock('../../src/lib/config.js', () => ({ createConfig: vi.fn(() => ({ get: vi.fn(), set: vi.fn(), path: '/tmp/mock-config.json', })), getCredentialsOrThrow: vi.fn(() => ({ apiId: 12345, apiHash: 'testhash' })), })); // Mock session store const mockStoreWithLock = vi.fn().mockImplementation(async (_profile: string, fn: (s: string) => Promise) => { return fn('test-session'); }); vi.mock('../../src/lib/session-store.js', () => ({ SessionStore: vi.fn().mockImplementation(() => ({ withLock: mockStoreWithLock, filePath: (p: string) => `/mock/sessions/${p}.session`, })), })); // Mock client module vi.mock('../../src/lib/client.js', () => ({ withClient: vi.fn(async (_opts: any, fn: any) => fn(mockClientInstance)), })); // Mock peer resolution const mockResolveEntity = vi.fn(); vi.mock('../../src/lib/peer.js', () => ({ resolveEntity: (...args: any[]) => mockResolveEntity(...args), })); // Mock serialize vi.mock('../../src/lib/serialize.js', () => ({ bigIntToString: (v: any) => String(v), })); // Mock errors (use actual translateTelegramError) vi.mock('../../src/lib/errors.js', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, translateTelegramError: actual.translateTelegramError, }; }); import { contactDeleteAction } from '../../src/commands/contact/delete.js'; function createMockCommandContext(opts: Record = {}) { return { optsWithGlobals: vi.fn(() => ({ profile: 'default', quiet: false, config: undefined, json: true, human: false, verbose: false, ...opts, })), }; } describe('contactDeleteAction', () => { beforeEach(() => { vi.clearAllMocks(); mockInvoke.mockResolvedValue(true); }); it('deletes a user contact successfully', async () => { const user = new MockUser({}); Object.assign(user, { id: BigInt(100), firstName: 'Alice', username: 'alice', }); mockResolveEntity.mockResolvedValueOnce(user); const ctx = createMockCommandContext(); await contactDeleteAction.call(ctx as any, '@alice'); expect(mockInvoke).toHaveBeenCalledOnce(); expect(mockOutputSuccess).toHaveBeenCalledWith({ userId: '100', username: 'alice', firstName: 'Alice', action: 'deleted', }); }); it('rejects channel entity with NOT_A_USER', async () => { const channel = new MockChannel({}); Object.assign(channel, { id: BigInt(300) }); mockResolveEntity.mockResolvedValueOnce(channel); const ctx = createMockCommandContext(); await contactDeleteAction.call(ctx as any, '@somechannel'); expect(mockOutputError).toHaveBeenCalledWith( 'Not a user: this is a group/channel', 'NOT_A_USER', ); expect(mockInvoke).not.toHaveBeenCalled(); }); it('translates RPCError via translateTelegramError', async () => { const user = new MockUser({}); Object.assign(user, { id: BigInt(100), firstName: 'Alice', username: 'alice' }); mockResolveEntity.mockResolvedValueOnce(user); mockInvoke.mockRejectedValueOnce({ errorMessage: 'CONTACT_ID_INVALID' }); const ctx = createMockCommandContext(); await contactDeleteAction.call(ctx as any, '@alice'); expect(mockOutputError).toHaveBeenCalledWith( 'Contact not found', 'CONTACT_ID_INVALID', ); }); it('handles not-logged-in with NOT_AUTHENTICATED', async () => { mockStoreWithLock.mockImplementationOnce(async (_profile: string, fn: (s: string | null) => Promise) => { return fn(null as any); }); const ctx = createMockCommandContext(); await contactDeleteAction.call(ctx as any, '@alice'); expect(mockOutputError).toHaveBeenCalledWith( 'Not logged in. Run: tg auth login', 'NOT_AUTHENTICATED', ); }); });