import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { beforeEach, expect, it, vi } from 'vitest';
import ChannelsPage from '@/pages/Channels';
import ChannelDetailPage from '@/pages/ChannelDetail';
import { apiRequest } from '@/services/api';
vi.mock('@/services/api', () => ({
apiRequest: vi.fn(),
jsonOptions: vi.fn((_method, body) => ({ body })),
}));
beforeEach(() => vi.clearAllMocks());
it('频道列表从独立接口加载并支持手动接入', async () => {
vi.mocked(apiRequest).mockImplementation(async (path) => {
if (path === '/channels') return { chat_id: '-100', type: 'channel' };
return {
items: [{ chat_id: '-100', title: '运营频道', type: 'channel', accessible: true, bot_status: 'administrator' }],
total: 1, page: 1, page_size: 20,
};
});
render();
expect(await screen.findByText('运营频道')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '接入频道' }));
fireEvent.change(screen.getByPlaceholderText('例如 -1001234567890'), { target: { value: '-100234' } });
fireEvent.click(screen.getByRole('button', { name: '校验并接入' }));
await waitFor(() => expect(apiRequest).toHaveBeenCalledWith('/channels', { body: { chat_id: '-100234', confirm: true } }));
});
it('频道详情展示频道资料、管理员与帖子记录', async () => {
vi.mocked(apiRequest).mockImplementation(async (path) => {
if (path === '/channels/-100') return {
chat_id: '-100', title: '运营频道', description: '频道简介', type: 'channel',
accessible: true, bot_status: 'administrator', bot_privileges: ['can_post_messages'],
};
if (path.endsWith('/invites')) return { items: [] };
if (path.endsWith('/admins')) return { items: [] };
if (path.includes('/posts?')) return { items: [{ post_id: 'p1', chat_id: '-100', message_id: '8', source: 'telegram', status: 'published', text: '频道新帖', pin: false, created_at: '2026-09-28T00:00:00Z' }], total: 1, page: 1, page_size: 20 };
return {};
});
render();
expect(await screen.findByText('频道简介')).toBeInTheDocument();
fireEvent.click(screen.getByText('帖子与定时'));
expect(await screen.findByText('频道新帖')).toBeInTheDocument();
});