mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 14:56:38 +08:00
2f60329886
代码质量全量清理,零行为变化:golangci 扩展集 13 类 linter(gosec/modernize/perfsprint/canonicalheader/usestdlibvars/wastedassign/intrange/errorlint/forcetypeassert/recvcheck/exhaustive/unparam)全量修复,测试代码质量(testifylint/thelper/usetesting)25→0,frpc 进程生命周期真 bug(进程组击杀)、全仓 go test -race 6 类数据竞争(含 1 个生产竞争)、SPDX license 头补齐 131 文件、前端测试套件 next-intl 迁移后 44 失败→全绿、过期 swagger 文档重新生成、pnpm-workspace 构建审批。 Experiments: #2-#17, #18, #20, #21, #23 Metric: total_issues 108 → 8 (-92.6%)
150 lines
4.5 KiB
TypeScript
150 lines
4.5 KiB
TypeScript
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { SyncTasksPanel } from '@/app/(main)/cloudflare/components/sync-tasks-panel';
|
|
import { AdminTaskService } from '@/lib/services/admin';
|
|
import type { TaskExecution } from '@/lib/services/admin';
|
|
import { NextIntlClientProvider } from 'next-intl';
|
|
import zhCN from '@/messages/zh-CN.json';
|
|
|
|
vi.mock('@/lib/services/admin', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@/lib/services/admin')>();
|
|
return {
|
|
...actual,
|
|
AdminTaskService: {
|
|
...actual.AdminTaskService,
|
|
listTaskExecutions: vi.fn(),
|
|
getTaskExecution: vi.fn(),
|
|
retryTaskExecution: vi.fn(),
|
|
},
|
|
};
|
|
});
|
|
|
|
function renderPanel() {
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false, gcTime: 0 } },
|
|
});
|
|
render(
|
|
<NextIntlClientProvider locale='zh-CN' messages={zhCN} timeZone='Asia/Shanghai'>
|
|
<QueryClientProvider client={client}>
|
|
<SyncTasksPanel />
|
|
</QueryClientProvider>
|
|
</NextIntlClientProvider>,
|
|
);
|
|
}
|
|
|
|
function execution(
|
|
partial: Partial<TaskExecution> &
|
|
Pick<TaskExecution, 'id' | 'task_type' | 'task_name'>,
|
|
): TaskExecution {
|
|
return {
|
|
task_id: `task_${partial.id}`,
|
|
status: 'succeeded',
|
|
retryable: true,
|
|
max_retry: 2,
|
|
retry_count: 0,
|
|
log: '',
|
|
error_message: '',
|
|
result: 'ok',
|
|
started_at: null,
|
|
finished_at: null,
|
|
duration: 100,
|
|
payload: '{}',
|
|
triggered_by: 'manual',
|
|
created_at: '2026-08-04T10:00:00Z',
|
|
updated_at: '2026-08-04T10:00:01Z',
|
|
...partial,
|
|
};
|
|
}
|
|
|
|
describe('Cloudflare sync tasks panel', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(AdminTaskService.listTaskExecutions).mockReset();
|
|
});
|
|
|
|
it('queries only sync_member and sync_group and hides other tasks', async () => {
|
|
vi.mocked(AdminTaskService.listTaskExecutions).mockImplementation(
|
|
async (request = {}) => {
|
|
if (request.task_type === 'cloudflare:sync_member') {
|
|
return {
|
|
items: [
|
|
execution({
|
|
id: '1',
|
|
task_type: 'cloudflare:sync_member',
|
|
task_name: 'Cloudflare 域名同步',
|
|
created_at: '2026-08-04T12:00:00Z',
|
|
}),
|
|
],
|
|
total: 1,
|
|
page: 1,
|
|
page_size: 50,
|
|
};
|
|
}
|
|
if (request.task_type === 'cloudflare:sync_group') {
|
|
return {
|
|
items: [
|
|
execution({
|
|
id: '2',
|
|
task_type: 'cloudflare:sync_group',
|
|
task_name: 'Cloudflare 分组同步',
|
|
created_at: '2026-08-04T11:00:00Z',
|
|
}),
|
|
// Defensive: even if API returns unrelated rows, panel must drop them.
|
|
execution({
|
|
id: '99',
|
|
task_type: 'system:cleanup',
|
|
task_name: '系统垃圾清理',
|
|
triggered_by: 'schedule',
|
|
created_at: '2026-08-04T13:00:00Z',
|
|
}),
|
|
],
|
|
total: 2,
|
|
page: 1,
|
|
page_size: 50,
|
|
};
|
|
}
|
|
return { items: [], total: 0, page: 1, page_size: 50 };
|
|
},
|
|
);
|
|
|
|
renderPanel();
|
|
|
|
expect(await screen.findByText('Cloudflare 域名同步')).toBeVisible();
|
|
expect(screen.getByText('Cloudflare 分组同步')).toBeVisible();
|
|
expect(screen.queryByText('系统垃圾清理')).not.toBeInTheDocument();
|
|
expect(screen.queryByText('system:cleanup')).not.toBeInTheDocument();
|
|
|
|
await waitFor(() => {
|
|
expect(AdminTaskService.listTaskExecutions).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
task_type: 'cloudflare:sync_member',
|
|
page: 1,
|
|
page_size: 50,
|
|
}),
|
|
);
|
|
expect(AdminTaskService.listTaskExecutions).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
task_type: 'cloudflare:sync_group',
|
|
page: 1,
|
|
page_size: 50,
|
|
}),
|
|
);
|
|
});
|
|
expect(AdminTaskService.listTaskExecutions).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('shows empty state when no cloudflare tasks exist', async () => {
|
|
vi.mocked(AdminTaskService.listTaskExecutions).mockResolvedValue({
|
|
items: [],
|
|
total: 0,
|
|
page: 1,
|
|
page_size: 50,
|
|
});
|
|
|
|
renderPanel();
|
|
|
|
expect(await screen.findByText('暂无同步任务')).toBeVisible();
|
|
});
|
|
});
|