32 lines
2.3 KiB
TypeScript
32 lines
2.3 KiB
TypeScript
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
|
||
import { afterEach, expect, it, vi } from 'vitest';
|
||
import { CloudChannelEnableConfirmation } from '@/pages/CloudAgents/CloudChannelEnableConfirmation';
|
||
|
||
const api = vi.hoisted(() => ({ call: vi.fn() }));
|
||
vi.mock('@/lib/cloud-agents-api', () => ({ cloudAgentsApi: api }));
|
||
afterEach(() => { cleanup(); vi.resetAllMocks(); });
|
||
const binding = { id: 'wechat', address: 'wechat:creator', display_name: '创作微信', target_agent_address: 'yuxi:makelore:agent',
|
||
route_revision: 1, provider_generation: 'generation', binding_id: 'binding', enabled: false, status: 'bound', worker_online: true, access_mode: 'self_only' };
|
||
|
||
it('shows actual budget values and published audience before enabling, including a zero limit', async () => {
|
||
api.call.mockResolvedValue({ request_limit_points: '0.00', daily_limit_points: null });
|
||
const enable = vi.fn();
|
||
render(<CloudChannelEnableConfirmation slug="agent" binding={binding} publishedVersion={2} busy={false} onEnable={enable} onCancel={() => undefined} />);
|
||
expect(screen.getByRole('button', { name: '确认启用', exact: true })).toBeDisabled();
|
||
await screen.findByText('每次任务上限:0.00 词元点数');
|
||
expect(screen.getByText('每日上限:不设额外上限')).toBeVisible();
|
||
expect(screen.getByRole('dialog')).toHaveTextContent('当前发布版本 v2,允许仅本人');
|
||
fireEvent.click(screen.getByRole('button', { name: '确认启用', exact: true }));
|
||
expect(enable).toHaveBeenCalledOnce();
|
||
});
|
||
|
||
it('keeps enable unavailable until a failed budget read can be retried', async () => {
|
||
api.call.mockRejectedValueOnce(new Error('费用服务暂不可用')).mockResolvedValueOnce({ request_limit_points: '2.00', daily_limit_points: '10.00' });
|
||
render(<CloudChannelEnableConfirmation slug="agent" binding={binding} publishedVersion={2} busy={false} onEnable={() => undefined} onCancel={() => undefined} />);
|
||
await screen.findByRole('alert');
|
||
expect(screen.getByRole('button', { name: '确认启用', exact: true })).toBeDisabled();
|
||
fireEvent.click(screen.getByRole('button', { name: '重新读取上限' }));
|
||
await screen.findByText('每日上限:10.00 词元点数');
|
||
expect(screen.getByRole('button', { name: '确认启用', exact: true })).toBeEnabled();
|
||
});
|