1640 lines
93 KiB
TypeScript
1640 lines
93 KiB
TypeScript
import { afterEach,expect,test,vi } from 'vitest';
|
|
import {
|
|
addAccount,
|
|
cancelPasswordLogin,
|
|
checkPasswordLoginStatus,
|
|
checkQRLoginStatus,completeQRVerification,
|
|
deleteAccount,
|
|
generateQRLogin,
|
|
getAccountAISettings,
|
|
getAccountDetails,getAccountRuntimeStatuses,
|
|
getAccountTaskSettings,
|
|
getAllAISettings,
|
|
getLongLoginSettings,
|
|
passwordLogin,
|
|
refreshAccountProfile,
|
|
runAccountTask,
|
|
setLongLoginSettings,
|
|
updateAccountAISettings,
|
|
updateAccountAutoConfirm,
|
|
updateAccountCookie,
|
|
updateAccountLoginInfo,
|
|
updateAccountPauseDuration,
|
|
updateAccountRemark,
|
|
updateAccountSettings,
|
|
updateAccountStatus,
|
|
updateAccountTaskSettings,
|
|
} from './accounts/api';
|
|
import { appendCardData,batchCreateCards,createCard,deleteCard,getCardDetails,getCards,updateCard } from './cards/api';
|
|
import { getChatMessagePage,getChatMessages,getChatSessionPage,getChatSessions,markChatRead,sendChatImage,sendChatMessage } from './chat/api';
|
|
import { getDashboardStats,getOrderAnalytics,getValidOrders } from './dashboard/api';
|
|
import { cancelItemPublishBatch,createItem,deleteItem,deleteItemPublishBatch,getItemDetail,getItemPublishBatch,getItemPublishBatches,getItems,previewItemPublishBatch,publishItem,recommendPublishCategory,retryFailedItemPublishBatch,startItemPublishBatch,syncItemsFromAccount,updateItem } from './items/api';
|
|
import { createNotificationChannel,deleteAccountNotifications,deleteMessageNotification,deleteNotificationChannel,getAccountBindings,getMessageNotifications,getNotificationChannels,setAccountBindings,setMessageNotification,testNotificationChannel,updateNotificationChannel,updateSystemSettings as updateNotificationSystemSettings } from './notifications/api';
|
|
import { cancelOrderRefreshJob,deleteOrder,getAdminStats,getOrderDetail,getOrders,importOrders,manualShipOrder,syncOrders,syncSingleOrder,updateOrder } from './orders/api';
|
|
import { clearDefaultReplyRecords,deleteDefaultReply,deleteReplyRule,deleteShippingRule,getAutomationIssues,getDefaultReplies,getDefaultReply,getReplyRules,getShippingRules,getShippingRulesPage,resolveAutomationRun,resolveDeferredAutomationTask,updateDefaultReply,updateReplyRule,updateShippingRule } from './rules/api';
|
|
import { initializeAdmin,login,logout,verifySession } from './session/api';
|
|
import { changePassword,fetchAIModels,getSystemSettings,updateLoginCredentials,updateSystemSettings } from './settings/api';
|
|
import { getHealth } from './system/api';
|
|
import { normalizeSystemSettingsUpdate } from '../../shared/api-contract/settings';
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.restoreAllMocks();
|
|
} /* 测试回调验证:全局 API 适配器测试环境清理。 */);
|
|
|
|
// stubContractFetch 将 openapi-fetch 交给共享客户端的 Request 归一成历史断言使用的路径和请求体;生产运行时不会读取或重建该请求。
|
|
const stubContractFetch = (fetchMock: typeof fetch): void => {
|
|
vi.stubGlobal('fetch', async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
// request 保存 openapi-fetch 已序列化的请求;测试替身读取 clone,不会消费生产请求体。
|
|
const request = input instanceof Request ? input : new Request(input, init);
|
|
// requestURL 用于把 Node 测试环境中的绝对占位 URL 还原为断言使用的 API 相对路径。
|
|
const requestURL = new URL(request.url);
|
|
// contentType 决定请求体在测试替身中的可读方式,multipart 保持 FormData 便于断言重复文件字段。
|
|
const contentType = request.headers.get('content-type') || '';
|
|
// body 是仅供适配器断言检查的克隆请求体;GET/HEAD 没有请求体。
|
|
const body: BodyInit | undefined = request.method === 'GET' || request.method === 'HEAD'
|
|
? undefined
|
|
: contentType.includes('multipart/form-data')
|
|
? await request.clone().formData()
|
|
: await request.clone().text();
|
|
return fetchMock(`${requestURL.pathname}${requestURL.search}`, {
|
|
method: request.method,
|
|
headers: request.headers,
|
|
body,
|
|
signal: request.signal,
|
|
credentials: 'include',
|
|
});
|
|
} /* 测试 fetch 回调将 Request clone 归一给历史 fetchMock 断言,生产请求不会走该回调。 */);
|
|
};
|
|
|
|
test('updateSystemSettings uses one atomic bulk request', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await updateSystemSettings({ theme_color: 'blue', renewal_log_retention_days: 15 });
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/settings/system', expect.objectContaining({ method: 'PUT', credentials: 'include' }));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({ theme_color: 'blue', renewal_log_retention_days: 15 });
|
|
} /* 测试回调验证:updateSystemSettings uses one atomic bulk request。 */);
|
|
|
|
test('updateSystemSettings separates sensitive values into explicit commands', /* 当前回调验证敏感设置三态命令请求体。 */ async () => {
|
|
// fetchMock 是系统设置更新请求的 HTTP 替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
await updateSystemSettings({ ai_api_key: 'new-secret', smtp_password: '' });
|
|
// payload 是普通设置与敏感命令分离后的请求体。
|
|
const payload = JSON.parse(fetchMock.mock.calls[0][1].body);
|
|
expect(payload.values).toEqual({});
|
|
expect(payload.secrets).toEqual({
|
|
ai_api_key: { action: 'replace', value: 'new-secret' },
|
|
smtp_password: { action: 'clear' },
|
|
});
|
|
});
|
|
|
|
test('normalizeSystemSettingsUpdate separates every sensitive system setting', /* 当前回调验证共享归一器不会遗漏任一敏感系统设置。 */ () => {
|
|
// payload 是混合普通值和四类敏感值后的系统设置命令。
|
|
const payload = normalizeSystemSettingsUpdate({
|
|
theme_color: 'blue',
|
|
ai_api_key: 'test-ai-key',
|
|
smtp_password: 'test-smtp-password',
|
|
qq_reply_secret_key: 'test-qq-secret',
|
|
'captcha.remote_secret_key': 'test-captcha-secret',
|
|
});
|
|
expect(payload.values).toEqual({ theme_color: 'blue' });
|
|
expect(payload.secrets).toEqual({
|
|
ai_api_key: { action: 'replace', value: 'test-ai-key' },
|
|
smtp_password: { action: 'replace', value: 'test-smtp-password' },
|
|
qq_reply_secret_key: { action: 'replace', value: 'test-qq-secret' },
|
|
'captcha.remote_secret_key': { action: 'replace', value: 'test-captcha-secret' },
|
|
});
|
|
});
|
|
|
|
test('notification SMTP settings separate the authorization code into a secret command', /* 当前回调验证通知页不会把 SMTP 授权码写入普通设置。 */ async () => {
|
|
// fetchMock 是通知页系统设置更新请求的 HTTP 替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
await updateNotificationSystemSettings({
|
|
smtp_server: 'smtp.example.com', smtp_port: 465, smtp_user: 'sender@example.com',
|
|
smtp_password: 'test-smtp-secret', qq_reply_secret_key: 'test-qq-secret',
|
|
'captcha.remote_secret_key': 'test-captcha-secret',
|
|
});
|
|
// payload 是通知页适配器分流后的普通设置和敏感命令。
|
|
const payload = JSON.parse(fetchMock.mock.calls[0][1].body);
|
|
expect(payload.values).toEqual({ smtp_server: 'smtp.example.com', smtp_port: 465, smtp_user: 'sender@example.com' });
|
|
expect(payload.secrets).toEqual({
|
|
smtp_password: { action: 'replace', value: 'test-smtp-secret' },
|
|
qq_reply_secret_key: { action: 'replace', value: 'test-qq-secret' },
|
|
'captcha.remote_secret_key': { action: 'replace', value: 'test-captcha-secret' },
|
|
});
|
|
expect(payload.values).not.toHaveProperty('smtp_password');
|
|
});
|
|
|
|
test('health API exposes build metadata through the request boundary', async () => {
|
|
// fetchMock 是健康检查 API 的请求替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ version: '1.2.3', commit: 'abc123' }));
|
|
stubContractFetch(fetchMock);
|
|
const controller = new AbortController(); /* controller 表示controller。 */
|
|
|
|
await expect(getHealth({ signal: controller.signal })).resolves.toEqual({ version: '1.2.3', commit: 'abc123' });
|
|
expect(fetchMock).toHaveBeenCalledWith('/health', expect.objectContaining({ method: 'GET', signal: expect.any(AbortSignal) }));
|
|
} /* 测试回调验证:health API exposes build metadata through the request boundary。 */);
|
|
|
|
test('chat APIs preserve account and conversation scope', async () => {
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ sessions: [{ account_id: 'a1', chat_id: 'c1' }] }))
|
|
.mockResolvedValueOnce(jsonResponse({ messages: [{ account_id: 'a1', chat_id: 'c1', id: 1 }] }))
|
|
.mockImplementation(() => Promise.resolve(jsonResponse({ success: true, message: { id: 2 } })) /* 模拟新增动作接口返回包含标识的成功响应。 */); /* fetchMock 是本测试替代浏览器网络层的请求桩。 */
|
|
stubContractFetch(fetchMock);
|
|
await getChatSessions('a1');
|
|
await getChatMessages('a1', 'c1', 9);
|
|
await sendChatMessage({ account_id: 'a1', chat_id: 'c1', buyer_id: 'b1', text: 'hi' });
|
|
await markChatRead('a1', 'c1', [{ messageId: 'm1', sessionId: 'c1', cid: 'c1@goofish', conversationType: 1 }]);
|
|
expect(fetchMock.mock.calls[0][0]).toBe('/api/v1/chat/sessions?account_id=a1');
|
|
expect(fetchMock.mock.calls[1][0]).toBe('/api/v1/chat/messages?account_id=a1&chat_id=c1&before_id=9');
|
|
expect(JSON.parse(fetchMock.mock.calls[2][1].body)).toMatchObject({ account_id: 'a1', chat_id: 'c1', buyer_id: 'b1' });
|
|
expect(JSON.parse(fetchMock.mock.calls[3][1].body)).toEqual({ account_id: 'a1', chat_id: 'c1', message_ids: [{ messageId: 'm1', sessionId: 'c1', cid: 'c1@goofish', conversationType: 1 }] });
|
|
} /* 测试回调验证:chat APIs preserve account and conversation scope。 */);
|
|
|
|
test('Chat 会话、消息和发送 API 转发外部取消信号', async () => {
|
|
// fetchMock 验证会话切换、消息分页和文本/图片发送共享取消控制能力。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ sessions: [], has_more: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ messages: [], has_more: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, message: { message_key: 'm1' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, message: { message_key: 'm2' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
// controller 是 Chat feature Hook 使用的请求控制器。
|
|
const controller = new AbortController();
|
|
await getChatSessionPage('a1', undefined, { signal: controller.signal });
|
|
await getChatMessagePage('a1', 'c1', undefined, undefined, { signal: controller.signal });
|
|
await sendChatMessage({ account_id: 'a1', chat_id: 'c1', buyer_id: 'b1', text: 'hi' }, { signal: controller.signal });
|
|
await sendChatImage({ account_id: 'a1', chat_id: 'c1', buyer_id: 'b1', image: new File(['image'], 'chat.png', { type: 'image/png' }) }, { signal: controller.signal });
|
|
await markChatRead('a1', 'c1', [], { signal: controller.signal });
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/chat/sessions?account_id=a1', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/chat/messages?account_id=a1&chat_id=c1', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/chat/messages', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/chat/images', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/chat/read', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
} /* 测试回调验证:Chat 会话、消息和发送 API 转发外部取消信号。 */);
|
|
|
|
test('account task APIs keep rating and polish account-scoped', async () => {
|
|
const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(jsonResponse({ success: true, summary: { task_type: 'auto_rate' } })) /* 模拟账号任务执行后返回任务摘要。 */); /* fetchMock 是本测试替代浏览器网络层的请求桩。 */
|
|
stubContractFetch(fetchMock);
|
|
await updateAccountTaskSettings('a1', {
|
|
account_id: 'a1', auto_rate_enabled: true, rate_content: '交易愉快',
|
|
auto_polish_enabled: true, polish_time: '03:00',
|
|
});
|
|
await runAccountTask('a1', 'auto_rate');
|
|
expect(fetchMock.mock.calls[0][0]).toBe('/api/v1/account-tasks/a1');
|
|
expect(fetchMock.mock.calls[0][1].method).toBe('PUT');
|
|
expect(fetchMock.mock.calls[1][0]).toBe('/api/v1/account-tasks/a1/run');
|
|
expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({ task_type: 'auto_rate' });
|
|
} /* 测试回调验证:account task APIs keep rating and polish account-scoped。 */);
|
|
|
|
test('账号自动任务 API 转发外部取消信号', async () => {
|
|
// fetchMock 验证读取、保存和执行账号任务都支持请求取消。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ account_id: 'a1', auto_rate_enabled: true, rate_content: '交易愉快', auto_polish_enabled: false, polish_time: '03:00' }))
|
|
.mockResolvedValueOnce(jsonResponse({ account_id: 'a1', auto_rate_enabled: true, rate_content: '交易愉快', auto_polish_enabled: false, polish_time: '03:00' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, summary: { task_type: 'auto_rate', found: 1, success: 1, failed: 0, skipped: 0 } }));
|
|
stubContractFetch(fetchMock);
|
|
// controller 是 AccountAutomation feature Hook 使用的请求控制器。
|
|
const controller = new AbortController();
|
|
const settings = { account_id: 'a1', auto_rate_enabled: true, rate_content: '交易愉快', auto_polish_enabled: false, polish_time: '03:00' }; /* settings 表示settings。 */
|
|
await getAccountTaskSettings('a1', { signal: controller.signal });
|
|
await updateAccountTaskSettings('a1', settings, { signal: controller.signal });
|
|
await runAccountTask('a1', 'auto_rate', { signal: controller.signal });
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/account-tasks/a1', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/account-tasks/a1', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/account-tasks/a1/run', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
} /* 测试回调验证:账号自动任务 API 转发外部取消信号。 */);
|
|
|
|
test('getItemPublishBatches unwraps persisted batch list', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ batches: [{ id: 'batch-1', status: 'running' }] })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await expect(getItemPublishBatches(10)).resolves.toEqual([{ id: 'batch-1', status: 'running' }]);
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/items/publish-batches?limit=10', expect.objectContaining({ credentials: 'include' }));
|
|
} /* 测试回调验证:getItemPublishBatches unwraps persisted batch list。 */);
|
|
|
|
test('automation issue APIs expose and resolve quarantined work', async () => {
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ runs: [{ id: 1 }], pending_tasks: [{ id: 2 }] }))
|
|
.mockImplementation(() => Promise.resolve(jsonResponse({ success: true })) /* 模拟无消息体的成功操作响应。 */); /* fetchMock 是本测试替代浏览器网络层的请求桩。 */
|
|
stubContractFetch(fetchMock);
|
|
await expect(getAutomationIssues()).resolves.toEqual({ runs: [{ id: 1 }], pending_tasks: [{ id: 2 }] });
|
|
await resolveAutomationRun(1, 'continue');
|
|
await resolveDeferredAutomationTask(2, 'retry');
|
|
expect(fetchMock.mock.calls[1][0]).toBe('/api/v1/automation-runs/1/resolve');
|
|
expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({ resolution: 'continue' });
|
|
expect(fetchMock.mock.calls[2][0]).toBe('/api/v1/automation-pending-tasks/2/resolve');
|
|
} /* 测试回调验证:automation issue APIs expose and resolve quarantined work。 */);
|
|
|
|
test('order refresh uses JSON while file import keeps the shared authenticated multipart path', async () => {
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-1', status: 'running' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-1', status: 'succeeded', result: { partial_failure: false, message: '同步完成', summary: { discovered: 0, list_updated: 0, soft_deleted: 0, detail_total: 0, total: 0, updated: 0, no_change: 0, failed: 0 }, results: [] } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await syncOrders('acc1', 'pending_ship');
|
|
await importOrders(new FormData());
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/orders/refresh', expect.objectContaining({ method: 'POST', credentials: 'include', body: JSON.stringify({ cookie_id: 'acc1', status: 'pending_ship' }) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/orders/refresh/job-1', expect.objectContaining({ method: 'GET', credentials: 'include' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/orders/import', expect.objectContaining({ method: 'POST', credentials: 'include', body: expect.any(FormData) }));
|
|
} /* 测试回调验证:新版订单刷新使用 JSON,而订单文件导入保持共享 multipart 路径。 */);
|
|
|
|
test('syncOrders surfaces failed persisted job status', async () => {
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-failed', status: 'running' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-failed', status: 'failed', error_message: '平台会话已过期' })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await expect(syncOrders()).rejects.toThrow('平台会话已过期');
|
|
} /* 测试回调验证:syncOrders surfaces failed persisted job status。 */);
|
|
|
|
test('syncOrders aborts while waiting for persisted job status', async () => {
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-running', status: 'running' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-running', status: 'running' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-running', status: 'cancelled' })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
// controller 控制订单刷新轮询的取消信号。
|
|
const controller = new AbortController();
|
|
// pending 保存等待取消结果的订单刷新请求。
|
|
const pending = syncOrders(undefined, undefined, { signal: controller.signal });
|
|
await vi.waitFor(/* 等待创建和首次状态查询完成。 */ () => expect(fetchMock).toHaveBeenCalledTimes(2));
|
|
controller.abort();
|
|
await expect(pending).rejects.toThrow('请求已取消');
|
|
await vi.waitFor(/* 等待取消命令发出。 */ () => expect(fetchMock).toHaveBeenCalledWith('/api/v1/orders/refresh/job-running', expect.objectContaining({ method: 'DELETE', credentials: 'include' })));
|
|
} /* 测试回调验证:syncOrders aborts while waiting for persisted job status。 */);
|
|
|
|
test('syncOrders reaches the front-end budget, cancels, and returns a concurrent success terminal result', async () => {
|
|
// fetchMock 模拟轮询仍运行、取消命令返回冲突且复查已拿到成功终态的时间竞争。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-timeout', status: 'running' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-timeout', status: 'running' }))
|
|
.mockResolvedValueOnce(new Response(JSON.stringify({ error: { code: 'conflict', message: '任务已结束' } }), { status: 409, headers: { 'content-type': 'application/json' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-timeout', status: 'succeeded', result: { partial_failure: false, message: '终态成功', summary: { discovered: 0, list_updated: 0, soft_deleted: 0, detail_total: 0, total: 0, updated: 0, no_change: 0, failed: 0 }, results: [] } }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
// result 保存达到本地等待预算后由终态复查返回的成功订单刷新结果。
|
|
const result = await syncOrders(undefined, undefined, { pollLimit: 1, pollIntervalMs: 0 });
|
|
expect(result.message).toBe('终态成功');
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/orders/refresh/job-timeout', expect.objectContaining({ method: 'DELETE', credentials: 'include' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/orders/refresh/job-timeout', expect.objectContaining({ method: 'GET', credentials: 'include' }));
|
|
} /* 测试回调验证:syncOrders reaches the front-end budget, cancels, and returns a concurrent success terminal result。 */);
|
|
|
|
test('syncOrders reports a timeout only after cancellation and final terminal read remain non-terminal', async () => {
|
|
// fetchMock 模拟后台任务未在取消请求后及时进入成功或失败终态的场景。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-still-running', status: 'running' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-still-running', status: 'running' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-still-running', status: 'cancelled' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, job_id: 'job-still-running', status: 'cancelled' }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await expect(syncOrders(undefined, undefined, { pollLimit: 1, pollIntervalMs: 0 })).rejects.toThrow('订单刷新任务等待超时,已请求取消');
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/orders/refresh/job-still-running', expect.objectContaining({ method: 'DELETE', credentials: 'include' }));
|
|
} /* 测试回调验证:syncOrders reports a timeout only after cancellation and final terminal read remain non-terminal。 */);
|
|
|
|
test('cancelOrderRefreshJob sends a user-scoped delete command', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, job_id: 'job-cancel', status: 'cancelled' })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await expect(cancelOrderRefreshJob('job-cancel')).resolves.toEqual({ success: true, job_id: 'job-cancel', status: 'cancelled' });
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/orders/refresh/job-cancel', expect.objectContaining({ method: 'DELETE', credentials: 'include' }));
|
|
} /* 测试回调验证:cancelOrderRefreshJob sends a user-scoped delete command。 */);
|
|
|
|
test('legacy notification channel aliases are normalized for the editor', async () => {
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse([{ id: 1, name: '旧飞书', type: 'lark', config: 'not-json', enabled: true }])));
|
|
const result = await getNotificationChannels(); /* result 表示处理结果。 */
|
|
expect(result.data?.[0]).toMatchObject({ type: 'feishu', config: {} });
|
|
} /* 测试回调验证:legacy notification channel aliases are normalized for the editor。 */);
|
|
|
|
const jsonResponse = (body: unknown) => new Response(JSON.stringify(body), {
|
|
status: 200,
|
|
headers: { 'content-type': 'application/json' },
|
|
}); /* jsonResponse 表示json接口响应结果。 */
|
|
|
|
test('getOrders normalizes backend order fields', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
|
orders: [{ order_id: 'o1', order_status: 'shipped', quantity: '2' }],
|
|
total: 1,
|
|
})); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
const result = await getOrders(undefined, 'all', 1, 20, ' buyer '); /* result 表示处理结果。 */
|
|
expect(result.data[0]).toMatchObject({ id: 'o1', status: 'shipped', quantity: 2 });
|
|
expect(result.total).toBe(1);
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/orders?page=1&page_size=20&search=buyer', expect.objectContaining({ method: 'GET' }));
|
|
} /* 测试回调验证:getOrders normalizes backend order fields。 */);
|
|
|
|
test('getOrders maps unsupported backend statuses to unknown', async () => {
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse({
|
|
data: [{ order_id: 'o-unknown', order_status: 'legacy_status' }],
|
|
})));
|
|
const result = await getOrders(); /* result 表示处理结果。 */
|
|
expect(result.data[0].status).toBe('unknown');
|
|
} /* 测试回调验证:getOrders maps unsupported backend statuses to unknown。 */);
|
|
|
|
test('订单查询和导入 API 转发外部取消信号', async () => {
|
|
// fetchMock 是同时验证订单查询和文件上传请求控制参数的替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ data: [], total_pages: 1 }))
|
|
.mockResolvedValueOnce(jsonResponse({ success_count: 1, failed_count: 0, results: [] }));
|
|
stubContractFetch(fetchMock);
|
|
// controller 是 feature Hook 传入 API 的取消控制器。
|
|
const controller = new AbortController();
|
|
await getOrders(undefined, 'all', 1, 20, '', { signal: controller.signal });
|
|
await importOrders(new FormData(), { signal: controller.signal });
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/orders?page=1&page_size=20', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/orders/import', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
} /* 测试回调验证:订单查询和导入 API 转发外部取消信号。 */);
|
|
|
|
test('Dashboard 统计 API 转发外部取消信号', async () => {
|
|
// fetchMock 验证 Dashboard 的概览、趋势和订单明细共用同一个取消信号。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ total_cookies: 1, active_cookies: 1, available_card_stock: 2 }))
|
|
.mockResolvedValueOnce(jsonResponse([]))
|
|
.mockResolvedValueOnce(jsonResponse({ revenue_stats: { total_amount: 1, total_orders: 1 }, daily_stats: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ revenue_stats: { total_amount: 0, total_orders: 0 }, daily_stats: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ orders: [], total: 0, truncated: false }));
|
|
stubContractFetch(fetchMock);
|
|
// controller 是 Dashboard feature Hook 传入 API 的请求控制器。
|
|
const controller = new AbortController();
|
|
await getDashboardStats({ signal: controller.signal });
|
|
await getItems(undefined, { signal: controller.signal });
|
|
await getOrderAnalytics({ start_date: '2026-08-15', end_date: '2026-08-15' }, { signal: controller.signal });
|
|
await getValidOrders({ start_date: '2026-08-15', end_date: '2026-08-15' }, { signal: controller.signal });
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/analytics/dashboard', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, expect.stringContaining('/api/v1/analytics/orders?'), expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, expect.stringContaining('/api/v1/analytics/orders/valid?'), expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
} /* 测试回调验证:Dashboard 统计 API 转发外部取消信号。 */);
|
|
|
|
test('Settings 配置、模型和凭据 API 转发外部取消信号', async () => {
|
|
// fetchMock 验证 Settings 的读取、模型发现和凭据保存共享取消控制能力。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ data: { log_level: 'info' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ models: ['model-a'] }))
|
|
.mockResolvedValueOnce(jsonResponse({ authenticated: true, username: 'admin' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, message: '已更新' }));
|
|
stubContractFetch(fetchMock);
|
|
// controller 是 Settings feature Hook 使用的请求控制器。
|
|
const controller = new AbortController();
|
|
await getSystemSettings({ signal: controller.signal });
|
|
await fetchAIModels('https://ai.example.com', 'secret', { signal: controller.signal });
|
|
await verifySession({ signal: controller.signal });
|
|
await updateLoginCredentials({ current_password: 'old', new_username: 'admin' }, { signal: controller.signal });
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/settings/system', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/settings/ai-models', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/session', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/session/credentials', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
} /* 测试回调验证:Settings 配置、模型和凭据 API 转发外部取消信号。 */);
|
|
|
|
test('通知渠道和 SMTP API 转发外部取消信号', async () => {
|
|
// fetchMock 验证渠道读取、保存和 SMTP 读写都支持取消控制。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse([]))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ data: {} }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
// controller 是通知 feature 传给服务 API 的共享取消控制器。
|
|
const controller = new AbortController();
|
|
await getNotificationChannels({ signal: controller.signal });
|
|
await updateNotificationChannel('channel-1', { enabled: false }, { signal: controller.signal });
|
|
await getSystemSettings({ signal: controller.signal });
|
|
await updateSystemSettings({ smtp_server: 'smtp.example.com' }, { signal: controller.signal });
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/notifications/channels', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/notifications/channels/channel-1', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/settings/system', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/settings/system', expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
|
} /* 测试回调验证:通知渠道和 SMTP API 转发外部取消信号。 */);
|
|
|
|
test('getShippingRulesPage sends filters and preserves pagination metadata', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
|
data: [{ id: 7, name: '付款规则', trigger_type: 'order_paid', enabled: false, actions: [] }],
|
|
total: 21,
|
|
page: 2,
|
|
page_size: 20,
|
|
total_pages: 2,
|
|
trigger_counts: { order_paid: 8, buyer_reviewed: 7, review_missing_timeout: 6 },
|
|
})); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
const result = await getShippingRulesPage({
|
|
cookieId: 'acc1',
|
|
triggerType: 'order_paid',
|
|
enabled: false,
|
|
search: ' 商品 ',
|
|
page: 2,
|
|
pageSize: 20,
|
|
}); /* result 表示处理结果。 */
|
|
|
|
expect(result).toMatchObject({
|
|
total: 21,
|
|
page: 2,
|
|
page_size: 20,
|
|
total_pages: 2,
|
|
trigger_counts: { order_paid: 8, buyer_reviewed: 7, review_missing_timeout: 6 },
|
|
});
|
|
expect(result.data[0]).toMatchObject({ id: '7', name: '付款规则', enabled: false });
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'/api/v1/automation-rules?page=2&page_size=20&cookie_id=acc1&trigger_type=order_paid&enabled=false&search=%E5%95%86%E5%93%81',
|
|
expect.objectContaining({ method: 'GET', credentials: 'include' }),
|
|
);
|
|
} /* 测试回调验证:getShippingRulesPage sends filters and preserves pagination metadata。 */);
|
|
|
|
test('getValidOrders accepts wrapped responses', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
|
orders: [{ order_id: 'o2', order_status: 'completed', quantity: '3' }],
|
|
})); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-480);
|
|
const result = await getValidOrders({ start_date: '2026-01-01', end_date: '2026-01-02' }); /* result 表示处理结果。 */
|
|
expect(result).toEqual({
|
|
orders: [expect.objectContaining({ id: 'o2', status: 'completed', quantity: 3 })],
|
|
total: 1,
|
|
truncated: false,
|
|
});
|
|
expect(fetchMock.mock.calls[0][0]).toContain('timezone_offset_minutes=480');
|
|
} /* 测试回调验证:getValidOrders accepts wrapped responses。 */);
|
|
|
|
test('getOrderAnalytics sends the browser timezone offset', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ revenue_stats: {}, daily_stats: [], status_stats: [], city_stats: [] })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
vi.spyOn(Date.prototype, 'getTimezoneOffset').mockReturnValue(-330);
|
|
await getOrderAnalytics({ start_date: '2026-01-01', end_date: '2026-01-02' });
|
|
expect(fetchMock.mock.calls[0][0]).toContain('timezone_offset_minutes=330');
|
|
} /* 测试回调验证:getOrderAnalytics sends the browser timezone offset。 */);
|
|
|
|
test('getOrderAnalytics 支持数字天数参数', /* 当前回调验证订单分析默认日期范围构造。 */ async () => {
|
|
// fetchMock 是数字天数分析请求的网络替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ revenue_stats: {}, daily_stats: [], status_stats: [], city_stats: [] }));
|
|
stubContractFetch(fetchMock);
|
|
await getOrderAnalytics(3);
|
|
expect(fetchMock.mock.calls[0][0]).toContain('/api/v1/analytics/orders?');
|
|
expect(fetchMock.mock.calls[0][0]).toContain('start_date=');
|
|
expect(fetchMock.mock.calls[0][0]).toContain('end_date=');
|
|
});
|
|
|
|
test('getOrders 序列化账号和状态筛选参数', /* 当前回调验证订单查询筛选参数分支。 */ async () => {
|
|
// fetchMock 是订单筛选请求的网络替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ data: [], total: 0 }));
|
|
stubContractFetch(fetchMock);
|
|
await getOrders('account-1', 'pending_ship');
|
|
expect(fetchMock.mock.calls[0][0]).toContain('cookie_id=account-1');
|
|
expect(fetchMock.mock.calls[0][0]).toContain('status=pending_ship');
|
|
});
|
|
|
|
test('paid orders are normalized to pending shipment', async () => {
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse({ data: [{ order_id: 'o-paid', order_status: 'paid' }] })));
|
|
const result = await getOrders(); /* result 表示处理结果。 */
|
|
expect(result.data[0].status).toBe('pending_ship');
|
|
} /* 测试回调验证:paid orders are normalized to pending shipment。 */);
|
|
|
|
test('completeQRVerification sends only the immutable target account', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, account_id: 'acc1' })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await completeQRVerification('session-1', 'acc1');
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/qr-login/complete-verification/session-1', expect.objectContaining({
|
|
method: 'POST',
|
|
body: JSON.stringify({ target_account_id: 'acc1' }),
|
|
}));
|
|
} /* 测试回调验证:completeQRVerification sends only the immutable target account。 */);
|
|
|
|
test('deleteItemPublishBatch removes an abandoned preview', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await deleteItemPublishBatch('preview-1');
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/items/publish-batches/preview-1', expect.objectContaining({
|
|
method: 'DELETE',
|
|
credentials: 'include',
|
|
}));
|
|
} /* 测试回调验证:deleteItemPublishBatch removes an abandoned preview。 */);
|
|
|
|
test('publishItem allows virtual publishing without an optional location', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await publishItem({
|
|
cookie_id: 'acc1',
|
|
title: '虚拟商品',
|
|
description: '',
|
|
price: '12.50',
|
|
quantity: 1,
|
|
postage_mode: 'none',
|
|
images: [],
|
|
});
|
|
const body = fetchMock.mock.calls[0][1].body as FormData; /* body 表示请求体。 */
|
|
expect(body.get('location')).toBeNull();
|
|
} /* 测试回调验证:publishItem allows virtual publishing without an optional location。 */);
|
|
|
|
test('getItems normalizes multi-spec flags from backend values', async () => {
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse([{
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-1',
|
|
item_title: '普通商品',
|
|
is_multi_spec: '0',
|
|
multi_quantity_delivery: 0,
|
|
}, {
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-2',
|
|
item_title: '多规格商品',
|
|
is_multi_spec: '1',
|
|
multi_quantity_delivery: 1,
|
|
}])));
|
|
|
|
const items = await getItems(); /* items 表示商品集合。 */
|
|
expect(items[0]).toMatchObject({
|
|
id: 'cookie-1-item-1',
|
|
is_multi_spec: false,
|
|
is_multi_qty_ship: false,
|
|
multi_quantity_delivery: false,
|
|
});
|
|
expect(items[1]).toMatchObject({
|
|
id: 'cookie-1-item-2',
|
|
is_multi_spec: true,
|
|
is_multi_qty_ship: true,
|
|
multi_quantity_delivery: true,
|
|
});
|
|
} /* 测试回调验证:getItems normalizes multi-spec flags from backend values。 */);
|
|
|
|
test('getItems forwards the selected account filter', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse([])); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getItems('account-2');
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/items?cookie_id=account-2', expect.objectContaining({
|
|
method: 'GET',
|
|
credentials: 'include',
|
|
}));
|
|
} /* 测试回调验证:getItems forwards the selected account filter。 */);
|
|
|
|
test('getSystemSettings normalizes numeric renewal retention', async () => {
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse({
|
|
ai_model: 'qwen-plus',
|
|
renewal_log_retention_days: 'invalid',
|
|
})));
|
|
|
|
const settings = await getSystemSettings(); /* settings 表示settings。 */
|
|
expect(settings.ai_model).toBe('qwen-plus');
|
|
expect(settings.renewal_log_retention_days).toBe(10);
|
|
} /* 测试回调验证:getSystemSettings normalizes numeric renewal retention。 */);
|
|
|
|
// getSystemSettings 脱敏响应测试验证客户端只接收敏感配置状态。
|
|
test('getSystemSettings keeps only sensitive configuration markers', /* 当前回调验证脱敏设置归一化。 */ async () => {
|
|
// fetchMock 返回服务端的脱敏设置视图,不包含任何敏感明文。
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse({
|
|
ai_api_key_configured: 'true',
|
|
smtp_password_configured: 'false',
|
|
})));
|
|
|
|
// settings 是客户端归一化后的脱敏设置对象。
|
|
const settings = await getSystemSettings();
|
|
expect(settings.ai_api_key).toBeUndefined();
|
|
expect(settings.ai_api_key_configured).toBe(true);
|
|
expect(settings.smtp_password_configured).toBe(false);
|
|
});
|
|
|
|
test('logout calls backend session invalidation route', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await logout();
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/session/logout', expect.objectContaining({
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({});
|
|
} /* 测试回调验证:logout calls backend session invalidation route。 */);
|
|
|
|
test('account cookie APIs include login_method when provided', async () => {
|
|
const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(jsonResponse({ success: true })) /* 模拟取消操作的成功响应。 */); /* fetchMock 是本测试替代浏览器网络层的请求桩。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await addAccount('acc1', 'unb=acc1', 'qr_scan');
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/accounts', expect.objectContaining({
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
id: 'acc1',
|
|
value: 'unb=acc1',
|
|
login_method: 'qr_scan',
|
|
});
|
|
|
|
await updateAccountCookie('acc1', 'unb=acc1; x=1', 'qr_scan');
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/accounts/acc1', expect.objectContaining({
|
|
method: 'PUT',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({
|
|
id: 'acc1',
|
|
value: 'unb=acc1; x=1',
|
|
login_method: 'qr_scan',
|
|
});
|
|
} /* 测试回调验证:account cookie APIs include login_method when provided。 */);
|
|
|
|
test('account editor settings use one aggregate request', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
await updateAccountSettings('acc1', {
|
|
remark: 'main', auto_confirm: false, pause_duration: 5,
|
|
username: 'user', show_browser: true, channel_ids: [1, 2],
|
|
});
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/accounts/acc1/settings', expect.objectContaining({ method: 'PUT' }));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
remark: 'main', auto_confirm: false, pause_duration: 5,
|
|
username: 'user', show_browser: true, channel_ids: [1, 2],
|
|
});
|
|
} /* 测试回调验证:account editor settings use one aggregate request。 */);
|
|
|
|
test('getAccountDetails normalizes show_browser and never exposes password', async () => {
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse([{
|
|
id: 'acc1',
|
|
enabled: true,
|
|
auto_confirm: true,
|
|
remark: '主账号',
|
|
pause_duration: 0,
|
|
paused_until: 1780000000,
|
|
paused: true,
|
|
username: 'login-user',
|
|
show_browser: '1',
|
|
login_password: 'should-not-leak',
|
|
}])));
|
|
|
|
const accounts = await getAccountDetails(); /* accounts 表示账号集合。 */
|
|
expect(accounts[0]).toMatchObject({
|
|
id: 'acc1',
|
|
username: 'login-user',
|
|
show_browser: true,
|
|
paused_until: 1780000000,
|
|
paused: true,
|
|
});
|
|
expect(accounts[0]).not.toHaveProperty('login_password');
|
|
expect(accounts[0]).not.toHaveProperty('value');
|
|
} /* 测试回调验证:getAccountDetails normalizes show_browser and never exposes password。 */);
|
|
|
|
test('列表 API 将 null 和历史 data 包裹统一为空数组', async () => {
|
|
// fetchMock 依次模拟账号、商品、订单和规则接口的历史空响应。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse(null))
|
|
.mockResolvedValueOnce(jsonResponse({ data: null }))
|
|
.mockResolvedValueOnce(jsonResponse(null))
|
|
.mockResolvedValueOnce(jsonResponse({ data: null }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await expect(getAccountDetails()).resolves.toEqual([]);
|
|
await expect(getItems()).resolves.toEqual([]);
|
|
await expect(getOrders()).resolves.toMatchObject({ data: [], total: 0 });
|
|
await expect(getShippingRules()).resolves.toEqual([]);
|
|
} /* 回调函数验证列表接口的空集合契约。 */);
|
|
|
|
test('列表 API 接受历史命名包裹对象并保持具名 UI 结果', async () => {
|
|
// fetchMock 模拟订单、商品、卡密和批次接口各自的历史字段名称。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ orders: [{ order_id: 'o-wrap', order_status: 'paid' }] }))
|
|
.mockResolvedValueOnce(jsonResponse({ items: [{ cookie_id: 'a1', item_id: 'i1', item_title: '商品' }] }))
|
|
.mockResolvedValueOnce(jsonResponse({ cards: [{ id: 1, name: '库存', type: 'data', enabled: true }] }))
|
|
.mockResolvedValueOnce(jsonResponse({ batches: [{ id: 'b1', status: 'running' }] }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await expect(getOrders()).resolves.toMatchObject({ data: [{ id: 'o-wrap', status: 'pending_ship' }] });
|
|
await expect(getItems()).resolves.toMatchObject([{ id: 'a1-i1' }]);
|
|
await expect(getCards()).resolves.toMatchObject([{ id: 1, name: '库存' }]);
|
|
await expect(getItemPublishBatches()).resolves.toEqual([{ id: 'b1', status: 'running' }]);
|
|
} /* 回调函数验证历史包裹字段的兼容归一。 */);
|
|
|
|
test('getAccountDetails 归一化头像地址并保留非法地址', /* 当前回调验证头像缓存参数和 URL 兼容边界。 */ async () => {
|
|
// windowStub 是头像 URL 解析使用的浏览器位置替身。
|
|
vi.stubGlobal('window', { location: { origin: 'http://localhost' } });
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse([
|
|
{ id: 'acc1', enabled: true, avatar_url: 'https://avatar.alicdn.com/avatar.jpg' },
|
|
{ id: 'acc2', enabled: true, avatar_url: 'https://avatar.example.com/avatar.jpg' },
|
|
{ id: 'acc3', enabled: true, avatar_url: 'http://[invalid' },
|
|
])));
|
|
// accounts 是头像地址归一化后的账号列表。
|
|
const accounts = await getAccountDetails();
|
|
expect(accounts[0].avatar_url).toContain('avatar.alicdn.com/avatar.jpg?_v=');
|
|
expect(accounts[1].avatar_url).toBe('https://avatar.example.com/avatar.jpg');
|
|
expect(accounts[2].avatar_url).toBe('http://[invalid');
|
|
});
|
|
|
|
test('updateAccountLoginInfo sends exactly provided fields', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateAccountLoginInfo('acc1', { username: 'login-user', show_browser: false });
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/accounts/acc1/login-info', expect.objectContaining({
|
|
method: 'PUT',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
username: 'login-user',
|
|
show_browser: false,
|
|
});
|
|
} /* 测试回调验证:updateAccountLoginInfo sends exactly provided fields。 */);
|
|
|
|
test('updateAccountLoginInfo can request explicit password clearing', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateAccountLoginInfo('acc1', { username: 'login-user', clear_password: true, show_browser: false });
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
username: 'login-user',
|
|
clear_password: true,
|
|
show_browser: false,
|
|
});
|
|
} /* 测试回调验证:updateAccountLoginInfo can request explicit password clearing。 */);
|
|
|
|
test('updateItem sends only the fields selected by the editor', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateItem('acc1', 'item1', { item_title: '改名商品' });
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/items/acc1/item1', expect.objectContaining({
|
|
method: 'PUT',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
item_title: '改名商品',
|
|
});
|
|
} /* 测试回调验证:updateItem sends only the fields selected by the editor。 */);
|
|
|
|
test('password login service uses upstream-compatible routes', async () => {
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, session_id: 'sid', status: 'processing' }))
|
|
.mockResolvedValueOnce(jsonResponse({ status: 'success', account_id: 'acc1', cookie_count: 2 }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await passwordLogin({ account_id: 'acc1', account: 'u', password: 'p' });
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/password-login', expect.objectContaining({
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
account_id: 'acc1',
|
|
account: 'u',
|
|
password: 'p',
|
|
});
|
|
|
|
const status = await checkPasswordLoginStatus('sid'); /* status 表示status。 */
|
|
expect(status.status).toBe('success');
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/password-login/check/sid', expect.objectContaining({ method: 'GET' }));
|
|
|
|
await cancelPasswordLogin('sid');
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/password-login/cancel/sid', expect.objectContaining({ method: 'DELETE' }));
|
|
} /* 测试回调验证:password login service uses upstream-compatible routes。 */);
|
|
|
|
test('账号编辑子模块请求支持取消过期响应', async () => {
|
|
// fetchMock 是验证请求取消信号透传的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ can_open_long_login: true, enabled: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ ai_enabled: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, session_id: 'sid' }));
|
|
stubContractFetch(fetchMock);
|
|
const controller = new AbortController(); /* controller 表示controller。 */
|
|
|
|
await getLongLoginSettings('acc1', { signal: controller.signal });
|
|
await getAccountAISettings('acc1', { signal: controller.signal });
|
|
await passwordLogin({ account_id: 'acc1', account: 'u', password: 'p' }, { signal: controller.signal });
|
|
|
|
expect(fetchMock.mock.calls[0][1].signal).toBeInstanceOf(AbortSignal);
|
|
expect(fetchMock.mock.calls[1][1].signal).toBeInstanceOf(AbortSignal);
|
|
expect(fetchMock.mock.calls[2][1].signal).toBeInstanceOf(AbortSignal);
|
|
} /* 测试回调验证:账号编辑子模块请求支持取消过期响应。 */);
|
|
|
|
test('getShippingRules exposes buyer reviewed gift rules as automation rules', async () => {
|
|
stubContractFetch(vi.fn().mockResolvedValue(jsonResponse([{
|
|
id: 12,
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-1',
|
|
item_title: '测试商品',
|
|
name: '评价后发送赠品 - 测试商品',
|
|
trigger_type: 'buyer_reviewed',
|
|
enabled: true,
|
|
priority: 90,
|
|
config_json: '{}',
|
|
actions: [{
|
|
id: 33,
|
|
action_type: 'send_card',
|
|
card_id: 7,
|
|
card_name: '赠品库存',
|
|
delivery_count: 1,
|
|
config_json: '{"spec_name":"套餐","spec_value":"赠品"}',
|
|
enabled: true,
|
|
sort_order: 1,
|
|
}],
|
|
}])));
|
|
|
|
const rules = await getShippingRules(); /* rules 表示规则集合。 */
|
|
expect(rules[0]).toMatchObject({
|
|
id: '12',
|
|
trigger_type: 'buyer_reviewed',
|
|
card_group_id: 7,
|
|
card_group_name: '赠品库存',
|
|
});
|
|
expect(rules[0].variants[0]).toMatchObject({
|
|
spec_name: '套餐',
|
|
spec_value: '赠品',
|
|
card_id: 7,
|
|
});
|
|
} /* 测试回调验证:getShippingRules exposes buyer reviewed gift rules as automation rules。 */);
|
|
|
|
test('getReplyRules labels keyword matching according to engine contains behavior', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse([{
|
|
id: 42,
|
|
keyword: '发货',
|
|
reply: '马上安排',
|
|
type: 'image',
|
|
image_url: 'https://img.example/reply.png',
|
|
}])); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
const rules = await getReplyRules('acc1'); /* rules 表示规则集合。 */
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/reply-rules/acc1/typed', expect.objectContaining({ method: 'GET' }));
|
|
expect(rules[0]).toMatchObject({
|
|
id: '42',
|
|
keyword: '发货',
|
|
reply_content: '马上安排',
|
|
match_type: 'fuzzy',
|
|
type: 'image',
|
|
image_url: 'https://img.example/reply.png',
|
|
});
|
|
} /* 测试回调验证:getReplyRules labels keyword matching according to engine contains behavior。 */);
|
|
|
|
test('getReplyRules 没有账号时直接返回空列表', /* 当前回调验证关键词规则的账号守卫。 */ async () => {
|
|
await expect(getReplyRules()).resolves.toEqual([]);
|
|
});
|
|
|
|
test('getCards 解析 JSON 和损坏 JSON 的卡密接口配置', /* 当前回调验证卡密配置归一化边界。 */ async () => {
|
|
// fetchMock 是卡密列表接口的网络替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse([
|
|
{ id: 1, name: '有效', api_config: '{"endpoint":"https://example.com"}' },
|
|
{ id: 2, name: '损坏', api_config: '{broken' },
|
|
]));
|
|
stubContractFetch(fetchMock);
|
|
// cards 是卡密配置归一化后的库存列表。
|
|
const cards = await getCards();
|
|
expect(cards[0].api_config).toEqual({ endpoint: 'https://example.com', content_type: 'application/json' });
|
|
expect(cards[1].api_config).toBeUndefined();
|
|
});
|
|
|
|
test('getCards 将后端空响应归一化为空列表', /* 当前回调防止空卡密响应阻断自动化规则账号加载。 */ async () => {
|
|
// fetchMock 模拟兼容后端返回 JSON null 的卡密列表响应。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse(null));
|
|
stubContractFetch(fetchMock);
|
|
await expect(getCards()).resolves.toEqual([]);
|
|
});
|
|
|
|
test('默认回复 API 补齐空字段默认值', /* 当前回调验证默认回复字段归一化和保存载荷。 */ async () => {
|
|
// fetchMock 是默认回复读取和保存接口的网络替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ enabled: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
await expect(getDefaultReply('account-1')).resolves.toEqual({ cookie_id: 'account-1', enabled: false, reply_content: '', reply_once: false, reply_image_url: '' });
|
|
await updateDefaultReply('account-1', {});
|
|
expect(JSON.parse(fetchMock.mock.calls[1][1].body)).toEqual({ enabled: false, reply_content: '', reply_once: false, reply_image_url: '' });
|
|
});
|
|
|
|
test('updateReplyRule preserves keyword image metadata when saving text edits', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateReplyRule({ id: '42', keyword: '发货', reply_content: '稍后安排', item_id: 'item-1' }, 'acc1');
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/reply-rules/acc1/typed/42', expect.objectContaining({
|
|
method: 'PUT',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
keyword: '发货', reply: '稍后安排', item_id: 'item-1', type: 'text', image_url: '',
|
|
});
|
|
} /* 测试回调验证:updateReplyRule preserves keyword image metadata when saving text edits。 */);
|
|
|
|
test('updateReplyRule clears stale content when switching reply type', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateReplyRule({ id: '42', keyword: '发货', type: 'image', image_url: 'https://img.example/new.png' }, 'acc1');
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
keyword: '发货', reply: '', item_id: '', type: 'image', image_url: 'https://img.example/new.png',
|
|
});
|
|
} /* 测试回调验证:updateReplyRule clears stale content when switching reply type。 */);
|
|
|
|
test('deleteReplyRule deletes one stable keyword row instead of replacing the list', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await deleteReplyRule('42', 'acc1');
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/reply-rules/acc1/typed/42', expect.objectContaining({
|
|
method: 'DELETE',
|
|
credentials: 'include',
|
|
}));
|
|
} /* 测试回调验证:deleteReplyRule deletes one stable keyword row instead of replacing the list。 */);
|
|
|
|
test('createNotificationChannel persists email recipient as to_email config', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await createNotificationChannel({
|
|
name: '邮件通知',
|
|
type: 'email',
|
|
config: {
|
|
smtp_server: 'smtp.example.com',
|
|
smtp_port: 587,
|
|
smtp_user: 'from@example.com',
|
|
smtp_password: 'secret',
|
|
to_email: 'to@example.com',
|
|
},
|
|
});
|
|
|
|
const body = JSON.parse(fetchMock.mock.calls[0][1].body); /* body 表示请求体。 */
|
|
expect(body.type).toBe('email');
|
|
expect(JSON.parse(body.config)).toMatchObject({
|
|
to_email: 'to@example.com',
|
|
});
|
|
expect(JSON.parse(body.config)).not.toHaveProperty('from');
|
|
} /* 测试回调验证:createNotificationChannel persists email recipient as to_email config。 */);
|
|
|
|
test('createNotificationChannel allows email channel to rely on system SMTP settings', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await createNotificationChannel({
|
|
name: '邮件通知',
|
|
type: 'email',
|
|
config: {
|
|
to_email: 'to@example.com',
|
|
},
|
|
});
|
|
|
|
const body = JSON.parse(fetchMock.mock.calls[0][1].body); /* body 表示请求体。 */
|
|
expect(body.type).toBe('email');
|
|
expect(JSON.parse(body.config)).toEqual({
|
|
to_email: 'to@example.com',
|
|
});
|
|
} /* 测试回调验证:createNotificationChannel allows email channel to rely on system SMTP settings。 */);
|
|
|
|
test('updateNotificationChannel supports partial enabled updates', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateNotificationChannel('7', { enabled: false });
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/notifications/channels/7', expect.objectContaining({
|
|
method: 'PUT',
|
|
credentials: 'include',
|
|
}));
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
|
|
enabled: false,
|
|
});
|
|
} /* 测试回调验证:updateNotificationChannel supports partial enabled updates。 */);
|
|
|
|
test('updateNotificationChannel serializes config and event types', /* 当前回调验证通知渠道请求体序列化。 */ async () => {
|
|
// fetchMock 是通知渠道更新接口的网络替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
await updateNotificationChannel('7', { config: { server_url: 'https://example.com' }, event_types: ['system_error'] });
|
|
// body 是通知渠道更新请求体。
|
|
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
|
|
expect(body.config).toBe(JSON.stringify({ server_url: 'https://example.com' }));
|
|
expect(body.event_types).toBe(JSON.stringify(['system_error']));
|
|
});
|
|
|
|
test('getMessageNotifications 展开数组并忽略非法绑定值', /* 当前回调验证消息通知响应归一化。 */ async () => {
|
|
// fetchMock 是消息通知接口的网络替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
|
'account-1': [{ channel_id: 1, channel_name: '邮件', enabled: true }],
|
|
'account-2': null,
|
|
}));
|
|
stubContractFetch(fetchMock);
|
|
await expect(getMessageNotifications()).resolves.toEqual({ success: true, data: [{ cookie_id: 'account-1', channel_id: 1, channel_name: '邮件', enabled: true }] });
|
|
});
|
|
|
|
test('publishItem 序列化图片和地点字段', /* 当前回调验证商品发布 multipart 请求体。 */ async () => {
|
|
// fetchMock 是商品发布上传接口的网络替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
// image 是待上传的商品图片文件。
|
|
const image = new File(['image'], 'item.png', { type: 'image/png' });
|
|
await publishItem({ cookie_id: 'account-1', title: '商品', description: '描述', price: '10', quantity: 2, postage_mode: 'free', images: [image], location: { area: '区域', city: '城市', division_id: '1', longitude: 120, latitude: 30, poi_id: 'poi-1', poi_name: '地点', province: '省' } });
|
|
// body 是商品发布 multipart 请求体。
|
|
const body = fetchMock.mock.calls[0][1].body as FormData;
|
|
expect(body.get('location')).toContain('poi-1');
|
|
expect(body.getAll('images')).toHaveLength(1);
|
|
});
|
|
|
|
test('通知事件字段支持 JSON 数组和分隔符格式', /* 当前回调验证通知事件响应解析兼容性。 */ async () => {
|
|
// fetchMock 是返回多种通知事件编码的网络替身。
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse([
|
|
{ id: 1, name: 'JSON', type: 'bark', config: '{}', event_types: '["system_error", "order_paid"]', enabled: true },
|
|
{ id: 2, name: '分隔符', type: 'bark', config: '{}', event_types: 'system_error, order_paid; buyer_reviewed', enabled: true },
|
|
{ id: 3, name: '数组', type: 'bark', config: '{}', event_types: ['system_error'], enabled: true },
|
|
]));
|
|
stubContractFetch(fetchMock);
|
|
// result 是通知事件字段解析后的渠道列表。
|
|
const result = await getNotificationChannels();
|
|
expect(result.data?.[0].event_types).toEqual(['system_error', 'order_paid']);
|
|
expect(result.data?.[1].event_types).toEqual(['system_error', 'order_paid', 'buyer_reviewed']);
|
|
expect(result.data?.[2].event_types).toEqual(['system_error']);
|
|
});
|
|
|
|
test('updateShippingRule posts buyer reviewed gift payload to automation-rules', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 1 })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateShippingRule({
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-1',
|
|
trigger_type: 'buyer_reviewed',
|
|
enabled: true,
|
|
variants: [{
|
|
spec_name: '',
|
|
spec_value: '',
|
|
card_id: 7,
|
|
delivery_count: 1,
|
|
enabled: true,
|
|
}],
|
|
});
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('/api/v1/automation-rules', expect.objectContaining({
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
}));
|
|
const body = JSON.parse(fetchMock.mock.calls[0][1].body); /* body 表示请求体。 */
|
|
expect(body).toMatchObject({
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-1',
|
|
name: '评价后发送赠品 - item-1',
|
|
trigger_type: 'buyer_reviewed',
|
|
});
|
|
expect(body.actions).toEqual([
|
|
expect.objectContaining({
|
|
action_type: 'send_card',
|
|
card_id: 7,
|
|
sort_order: 1,
|
|
}),
|
|
]);
|
|
} /* 测试回调验证:updateShippingRule posts buyer reviewed gift payload to automation-rules。 */);
|
|
|
|
test('updateShippingRule posts every matching card action before confirm shipment', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 3 })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateShippingRule({
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-1',
|
|
trigger_type: 'order_paid',
|
|
enabled: true,
|
|
variants: [
|
|
{
|
|
spec_name: '套餐',
|
|
spec_value: '30天',
|
|
card_id: 8,
|
|
delivery_count: 1,
|
|
enabled: true,
|
|
},
|
|
{
|
|
spec_name: '套餐',
|
|
spec_value: '30天',
|
|
card_id: 9,
|
|
delivery_count: 2,
|
|
enabled: true,
|
|
delay_override: true,
|
|
delay_seconds: 0,
|
|
},
|
|
],
|
|
});
|
|
|
|
const body = JSON.parse(fetchMock.mock.calls[0][1].body); /* body 表示请求体。 */
|
|
expect(body.trigger_type).toBe('order_paid');
|
|
expect(body.actions).toEqual([
|
|
expect.objectContaining({
|
|
action_type: 'send_card',
|
|
card_id: 8,
|
|
sort_order: 1,
|
|
}),
|
|
expect.objectContaining({
|
|
action_type: 'send_card',
|
|
card_id: 9,
|
|
delivery_count: 2,
|
|
sort_order: 2,
|
|
}),
|
|
expect.objectContaining({
|
|
action_type: 'confirm_shipment',
|
|
sort_order: 3,
|
|
}),
|
|
]);
|
|
expect(JSON.parse(body.actions[0].config_json)).toEqual({ spec_name: '套餐', spec_value: '30天', delay_override: false });
|
|
expect(JSON.parse(body.actions[1].config_json)).toEqual({ spec_name: '套餐', spec_value: '30天', delay_override: true });
|
|
expect(body.actions[1].delay_seconds).toBe(0);
|
|
} /* 测试回调验证:updateShippingRule posts every matching card action before confirm shipment。 */);
|
|
|
|
test('updateShippingRule preserves text actions while editing card variants', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 4 })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateShippingRule({
|
|
id: '4',
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-1',
|
|
trigger_type: 'order_paid',
|
|
variants: [{ spec_name: '', spec_value: '', card_id: 8, delivery_count: 1, enabled: true }],
|
|
actions: [{ action_type: 'send_text', message_template: '发货提示', enabled: true, sort_order: 2 }],
|
|
});
|
|
|
|
const body = JSON.parse(fetchMock.mock.calls[0][1].body); /* body 表示请求体。 */
|
|
expect(body.actions.map((action: { /* action_type 表示自动化动作类型。 */ action_type: string }) => action.action_type /* action 是当前待断言排序的自动化动作。 */)).toEqual([
|
|
'send_card',
|
|
'send_text',
|
|
'confirm_shipment',
|
|
]);
|
|
expect(body.actions[1].message_template).toBe('发货提示');
|
|
} /* 测试回调验证:updateShippingRule preserves text actions while editing card variants。 */);
|
|
|
|
test('updateShippingRule posts review request text action without card requirement', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ success: true, id: 2 })); /* fetchMock 表示fetchMock。 */
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateShippingRule({
|
|
cookie_id: 'cookie-1',
|
|
item_id: 'item-1',
|
|
trigger_type: 'review_missing_timeout',
|
|
enabled: true,
|
|
config_json: '{"after_shipped_hours":48,"max_attempts":2}',
|
|
actions: [{
|
|
action_type: 'send_text',
|
|
message_template: '亲,方便的话麻烦给个评价~',
|
|
enabled: true,
|
|
sort_order: 1,
|
|
}],
|
|
});
|
|
|
|
const body = JSON.parse(fetchMock.mock.calls[0][1].body); /* body 表示请求体。 */
|
|
expect(body.trigger_type).toBe('review_missing_timeout');
|
|
expect(body.actions).toEqual([
|
|
expect.objectContaining({
|
|
action_type: 'send_text',
|
|
card_id: 0,
|
|
message_template: '亲,方便的话麻烦给个评价~',
|
|
}),
|
|
]);
|
|
} /* 测试回调验证:updateShippingRule posts review request text action without card requirement。 */);
|
|
|
|
// 会话 API 使用版本化兼容入口。
|
|
const runVersionedSessionAPITest = async () => {
|
|
// fetchMock 是会话 API 请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, authenticated: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await login({ username: 'admin', password: 'pw' });
|
|
await initializeAdmin('long-password');
|
|
await verifySession();
|
|
await logout();
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/session/login', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/session/initialize', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/session', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/session/logout', expect.objectContaining({ method: 'POST' }));
|
|
};
|
|
|
|
test('session APIs use versioned compatibility routes', runVersionedSessionAPITest);
|
|
|
|
// 账号摘要、详情和状态 API 使用版本化兼容入口。
|
|
const runVersionedAccountAPITest = async () => {
|
|
// fetchMock 是账号 API 请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse([{ id: 'acc1', enabled: true, remark: '主账号' }]))
|
|
.mockResolvedValueOnce(jsonResponse({ acc1: { state: 'error', connected: false, failures: 0, updated_at: '' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getAccountDetails();
|
|
await getAccountRuntimeStatuses();
|
|
await updateAccountStatus('acc1', false);
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/accounts/details', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/accounts/runtime-status', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/accounts/acc1/status', expect.objectContaining({ method: 'PUT' }));
|
|
};
|
|
|
|
test('account summary and status APIs use versioned compatibility routes', runVersionedAccountAPITest);
|
|
|
|
// 账号设置、长登录和资料 API 使用版本化兼容入口。
|
|
const runVersionedAccountSettingsAPITest = async () => {
|
|
// fetchMock 是账号设置与资料请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, paused_until: 0, paused: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ can_open_long_login: true, enabled: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ can_open_long_login: true, enabled: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, id: 'acc1', nickname: '主账号', avatar_url: '', profile_error: '' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, paused_until: 0, paused: false }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await updateAccountSettings('acc1', { remark: '主账号' });
|
|
await getLongLoginSettings('acc1');
|
|
await setLongLoginSettings('acc1', true);
|
|
await refreshAccountProfile('acc1');
|
|
await updateAccountRemark('acc1', '新的备注');
|
|
await updateAccountAutoConfirm('acc1', true);
|
|
await updateAccountPauseDuration('acc1', 15);
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/accounts/acc1/settings', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/accounts/acc1/long-login', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/accounts/acc1/long-login', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/accounts/acc1/refresh-profile', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/accounts/acc1/remark', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(6, '/api/v1/accounts/acc1/auto-confirm', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(7, '/api/v1/accounts/acc1/pause-duration', expect.objectContaining({ method: 'PUT' }));
|
|
};
|
|
|
|
test('account settings and profile APIs use versioned compatibility routes', runVersionedAccountSettingsAPITest);
|
|
|
|
// 订单列表、详情和更新 API 使用版本化兼容入口。
|
|
const runVersionedOrderAPITest = async () => {
|
|
// fetchMock 是订单 API 请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ data: [{ order_id: 'order-1', order_status: 'pending_ship' }], total: 1 }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, order_id: 'order-1', data: { order_id: 'order-1', order_status: 'pending_ship' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getOrders();
|
|
await getOrderDetail('order-1');
|
|
await updateOrder('order-1', { status: 'shipped' });
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/orders?page=1&page_size=20', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/orders/order-1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/orders/order-1', expect.objectContaining({ method: 'PUT' }));
|
|
};
|
|
|
|
test('order list, detail, and update APIs use versioned compatibility routes', runVersionedOrderAPITest);
|
|
|
|
// 订单刷新与批量操作 API 使用版本化兼容入口。
|
|
const runVersionedOrderRefreshAPITest = async () => {
|
|
// fetchMock 是订单刷新与批量请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await syncSingleOrder('order-1');
|
|
await manualShipOrder(['order-1'], 'status_only');
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/orders/order-1/refresh', expect.objectContaining({ method: 'POST', credentials: 'include' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/orders/manual-ship', expect.objectContaining({ method: 'POST', credentials: 'include' }));
|
|
};
|
|
|
|
test('order refresh and batch APIs use versioned compatibility routes', runVersionedOrderRefreshAPITest);
|
|
|
|
// 商品列表、详情、发布、更新和删除 API 使用版本化兼容入口。
|
|
const runVersionedItemAPITest = async () => {
|
|
// fetchMock 是商品 API 请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse([]))
|
|
.mockResolvedValueOnce(jsonResponse({ cookie_id: 'acc1', item_id: 'item-1', item_title: '商品' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, item_id: 'item-1' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getItems('acc1');
|
|
await getItemDetail('acc1', 'item-1');
|
|
await publishItem({
|
|
cookie_id: 'acc1', title: '商品', description: '', price: '1.00', quantity: 1,
|
|
postage_mode: 'none', images: [],
|
|
});
|
|
await updateItem('acc1', 'item-1', { item_title: '新商品名' });
|
|
await deleteItem('acc1', 'item-1');
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/items?cookie_id=acc1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/items/acc1/item-1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/items/publish', expect.objectContaining({ method: 'POST', body: expect.any(FormData) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/items/acc1/item-1', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/items/acc1/item-1', expect.objectContaining({ method: 'DELETE' }));
|
|
};
|
|
|
|
test('item list, detail, publish, update, and delete APIs use versioned compatibility routes', runVersionedItemAPITest);
|
|
|
|
// 商品同步、类目推荐和批量发布 API 使用版本化兼容入口。
|
|
const runVersionedItemBatchAPITest = async () => {
|
|
// fetchMock 是商品同步和批量发布请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, category: { cat_id: 'cat-1' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, preview_id: 'preview-1', total: 0, valid: 0, invalid: 0, rows: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, batch_id: 'batch-1' }))
|
|
.mockResolvedValueOnce(jsonResponse({ batches: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ id: 'batch-1', status: 'preview', rows: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, status: 'canceled' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, batch_id: 'batch-1' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await syncItemsFromAccount('acc1');
|
|
await recommendPublishCategory('acc1', '资料');
|
|
await previewItemPublishBatch({
|
|
file: new File(['order_id\nitem-1'], 'items.csv', { type: 'text/csv' }),
|
|
imagesZip: new File(['zip'], 'images.zip', { type: 'application/zip' }),
|
|
defaultCookieId: 'acc1',
|
|
fallbackCategory: { catId: 'cat-1', catName: '资料', channelCatId: 'channel-1', tbCatId: 'tb-1' },
|
|
location: { area: '区域', city: '城市', division_id: '1', longitude: 120, latitude: 30, poi_id: 'poi-1', poi_name: '地点', province: '省' },
|
|
publishIntervalSeconds: 12,
|
|
});
|
|
await startItemPublishBatch('preview-1');
|
|
await getItemPublishBatches(10);
|
|
await getItemPublishBatch('batch-1');
|
|
await cancelItemPublishBatch('batch-1');
|
|
await retryFailedItemPublishBatch('batch-1');
|
|
await deleteItemPublishBatch('batch-1');
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/items/get-all-from-account', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/items/publish-categories/recommend', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/items/publish-batches/preview', expect.objectContaining({ method: 'POST', body: expect.any(FormData) }));
|
|
// previewBody 保存批量预检表单,确保用户设置的间隔进入后端持久化边界。
|
|
const previewBody = fetchMock.mock.calls[2][1].body as FormData;
|
|
expect(previewBody.get('publish_interval_seconds')).toBe('12');
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/items/publish-batches', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/items/publish-batches?limit=10', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(6, '/api/v1/items/publish-batches/batch-1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(7, '/api/v1/items/publish-batches/batch-1/cancel', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(8, '/api/v1/items/publish-batches/batch-1/retry-failed', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(9, '/api/v1/items/publish-batches/batch-1', expect.objectContaining({ method: 'DELETE' }));
|
|
};
|
|
|
|
test('item sync and batch publish APIs use versioned compatibility routes', runVersionedItemBatchAPITest);
|
|
|
|
// 设置、卡券和通知 API 使用版本化兼容入口。
|
|
const runVersionedSettingsCardNotificationAPITest = async () => {
|
|
// fetchMock 是设置、卡券和通知请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ data: {} }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({}))
|
|
.mockResolvedValueOnce(jsonResponse({ ai_enabled: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ models: ['qwen-plus'] }))
|
|
.mockResolvedValueOnce(jsonResponse([]))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, id: 1 }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ id: 1, name: '卡券', type: 'text', text_content: 'CARD' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, total: 0, created: 0, failed: 0, rows: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, added: 1 }))
|
|
.mockResolvedValueOnce(jsonResponse([]))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, id: 1 }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({}))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ channel_ids: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getSystemSettings();
|
|
await updateSystemSettings({ theme_color: 'blue' });
|
|
await getAllAISettings();
|
|
await getAccountAISettings('acc1');
|
|
await updateAccountAISettings('acc1', { ai_enabled: true });
|
|
await fetchAIModels('https://ai.example.com');
|
|
await getCards();
|
|
await createCard({ name: '卡券', type: 'text', text_content: 'CARD' });
|
|
await updateCard(1, { enabled: false });
|
|
await deleteCard(1);
|
|
await getCardDetails(1);
|
|
await batchCreateCards(new File(['name,type,content\n卡券,text,CARD'], 'cards.csv', { type: 'text/csv' }));
|
|
await appendCardData(1, 'CARD-2');
|
|
await getNotificationChannels();
|
|
await createNotificationChannel({ name: '通知', type: 'bark', config: {} });
|
|
await updateNotificationChannel('1', { enabled: false });
|
|
await deleteNotificationChannel('1');
|
|
await getMessageNotifications();
|
|
await setMessageNotification('acc1', 1, true);
|
|
await deleteMessageNotification('1');
|
|
await deleteAccountNotifications('acc1');
|
|
await getAccountBindings('acc1');
|
|
await setAccountBindings('acc1', [1]);
|
|
await testNotificationChannel('1');
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/settings/system', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/settings/system', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/settings/ai-reply', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/settings/ai-reply/acc1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/settings/ai-reply/acc1', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(6, '/api/v1/settings/ai-models', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(7, '/api/v1/cards', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(8, '/api/v1/cards', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(9, '/api/v1/cards/1', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(10, '/api/v1/cards/1', expect.objectContaining({ method: 'DELETE' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(11, '/api/v1/cards/1/details', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(12, '/api/v1/cards/batch', expect.objectContaining({ method: 'POST', body: expect.any(FormData) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(13, '/api/v1/cards/1/append-data', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(14, '/api/v1/notifications/channels', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(15, '/api/v1/notifications/channels', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(16, '/api/v1/notifications/channels/1', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(17, '/api/v1/notifications/channels/1', expect.objectContaining({ method: 'DELETE' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(18, '/api/v1/notifications/messages', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(19, '/api/v1/notifications/accounts/acc1/bindings', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(20, '/api/v1/notifications/messages/1', expect.objectContaining({ method: 'DELETE' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(21, '/api/v1/notifications/messages/account/acc1', expect.objectContaining({ method: 'DELETE' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(22, '/api/v1/notifications/accounts/acc1/bindings', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(23, '/api/v1/notifications/accounts/acc1/bindings', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(24, '/api/v1/notifications/channels/1/test', expect.objectContaining({ method: 'POST' }));
|
|
};
|
|
|
|
test('settings, card, and notification APIs use versioned compatibility routes', runVersionedSettingsCardNotificationAPITest);
|
|
|
|
// 聊天和账号任务 API 使用版本化兼容入口。
|
|
const runVersionedChatTaskAPITest = async () => {
|
|
// fetchMock 是聊天和账号任务请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ sessions: [], has_more: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ messages: [], has_more: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ sessions: [], has_more: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ messages: [], has_more: false }))
|
|
.mockResolvedValueOnce(jsonResponse({ message: { message_key: 'message-1' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ message: { message_key: 'message-2' } }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ account_id: 'acc1' }))
|
|
.mockResolvedValueOnce(jsonResponse({ account_id: 'acc1' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, summary: { task_type: 'auto_rate' } }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getChatSessionPage('acc1', 3, undefined, true);
|
|
await getChatMessagePage('acc1', 'chat-1', 4, 9);
|
|
await getChatSessions('acc1');
|
|
await getChatMessages('acc1', 'chat-1', 9);
|
|
await sendChatMessage({ account_id: 'acc1', chat_id: 'chat-1', buyer_id: 'buyer-1', text: '你好' });
|
|
await sendChatImage({ account_id: 'acc1', chat_id: 'chat-1', buyer_id: 'buyer-1', image: new File(['image'], 'chat.png', { type: 'image/png' }) });
|
|
await markChatRead('acc1', 'chat-1', []);
|
|
await getAccountTaskSettings('acc1');
|
|
await updateAccountTaskSettings('acc1', { account_id: 'acc1', auto_rate_enabled: true, rate_content: '交易愉快', auto_polish_enabled: false, polish_time: '03:00' });
|
|
await runAccountTask('acc1', 'auto_rate');
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/chat/sessions?account_id=acc1&cursor=3&refresh=1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/chat/messages?account_id=acc1&chat_id=chat-1&cursor=4&before_id=9', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/chat/sessions?account_id=acc1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/chat/messages?account_id=acc1&chat_id=chat-1&before_id=9', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/chat/messages', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(6, '/api/v1/chat/images', expect.objectContaining({ method: 'POST', body: expect.any(FormData) }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(7, '/api/v1/chat/read', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(8, '/api/v1/account-tasks/acc1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(9, '/api/v1/account-tasks/acc1', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(10, '/api/v1/account-tasks/acc1/run', expect.objectContaining({ method: 'POST' }));
|
|
};
|
|
|
|
test('chat and account task APIs use versioned compatibility routes', runVersionedChatTaskAPITest);
|
|
|
|
// 关键词回复、指定商品回复和默认回复 API 使用版本化兼容入口。
|
|
const runVersionedReplyAPITest = async () => {
|
|
// fetchMock 是关键词和默认回复请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse([]))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, id: 7 }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({}))
|
|
.mockResolvedValueOnce(jsonResponse({ enabled: true, reply_content: '欢迎', reply_once: false, reply_image_url: '' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getReplyRules('acc1');
|
|
await updateReplyRule({ id: '42', keyword: '发货', reply_content: '稍后安排' }, 'acc1');
|
|
await updateReplyRule({ keyword: '售后', reply_content: '请联系客服' }, 'acc1');
|
|
await deleteReplyRule('42', 'acc1');
|
|
await getDefaultReplies();
|
|
await getDefaultReply('acc1');
|
|
await updateDefaultReply('acc1', { enabled: true, reply_content: '欢迎' });
|
|
await deleteDefaultReply('acc1');
|
|
await clearDefaultReplyRecords('acc1');
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/reply-rules/acc1/typed', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/reply-rules/acc1/typed/42', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/v1/reply-rules/acc1/items', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, '/api/v1/reply-rules/acc1/typed/42', expect.objectContaining({ method: 'DELETE' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(5, '/api/v1/default-replies', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(6, '/api/v1/default-replies/acc1', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(7, '/api/v1/default-replies/acc1', expect.objectContaining({ method: 'PUT' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(8, '/api/v1/default-replies/acc1', expect.objectContaining({ method: 'DELETE' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(9, '/api/v1/default-replies/acc1/clear-records', expect.objectContaining({ method: 'POST' }));
|
|
};
|
|
|
|
test('keyword and default reply APIs use versioned compatibility routes', runVersionedReplyAPITest);
|
|
|
|
// 管理员、仪表盘和订单分析 API 使用版本化兼容入口。
|
|
const runVersionedAdminAnalyticsAPITest = async () => {
|
|
// fetchMock 是管理员和统计分析请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ total_users: 1, total_cookies: 1, active_cookies: 1, total_cards: 0, total_keywords: 0, total_orders: 0 }))
|
|
.mockResolvedValueOnce(jsonResponse({ total_cookies: 1, active_cookies: 1, total_cards: 0, available_card_stock: 0, total_keywords: 0, total_orders: 0 }))
|
|
.mockResolvedValueOnce(jsonResponse({ revenue_stats: {}, daily_stats: [], status_stats: [], city_stats: [], item_stats: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ orders: [], total: 0, page: 1, page_size: 500, truncated: false }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await getAdminStats();
|
|
await getDashboardStats();
|
|
await getOrderAnalytics({ start_date: '2026-01-01', end_date: '2026-01-02' });
|
|
await getValidOrders({ start_date: '2026-01-01', end_date: '2026-01-02' });
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/admin/stats', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/analytics/dashboard', expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(3, expect.stringContaining('/api/v1/analytics/orders?start_date=2026-01-01&end_date=2026-01-02'), expect.objectContaining({ method: 'GET' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(4, expect.stringContaining('/api/v1/analytics/orders/valid?start_date=2026-01-01&end_date=2026-01-02'), expect.objectContaining({ method: 'GET' }));
|
|
};
|
|
|
|
test('admin and analytics APIs use versioned compatibility routes', runVersionedAdminAnalyticsAPITest);
|
|
|
|
// 二维码生成和状态轮询使用版本化兼容入口。
|
|
const runVersionedQRLoginAPITest = async () => {
|
|
// fetchMock 是二维码生成和状态轮询请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, session_id: 'session-1', qr_code_url: 'data:image/png;base64,abc' }))
|
|
.mockResolvedValueOnce(jsonResponse({ status: 'waiting', session_id: 'session-1' }));
|
|
stubContractFetch(fetchMock);
|
|
await generateQRLogin();
|
|
await checkQRLoginStatus('session-1');
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/v1/qr-login/generate', expect.objectContaining({ method: 'POST' }));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/v1/qr-login/check/session-1', expect.objectContaining({ method: 'GET' }));
|
|
};
|
|
|
|
test('QR login generation and polling use versioned routes', runVersionedQRLoginAPITest);
|
|
|
|
// 密码登录、会话凭证、账号删除、自动化以及剩余订单商品调用使用版本化入口。
|
|
const runVersionedRemainingAPITest = async () => {
|
|
// fetchMock 是剩余公共 API 请求的测试替身。
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true, session_id: 'sid' }))
|
|
.mockResolvedValueOnce(jsonResponse({ status: 'failed' }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse([]))
|
|
.mockResolvedValueOnce(jsonResponse({ data: [], total: 0, page: 1, page_size: 10, total_pages: 0 }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ runs: [], pending_tasks: [] }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }))
|
|
.mockResolvedValueOnce(jsonResponse({ success: true }));
|
|
stubContractFetch(fetchMock);
|
|
|
|
await changePassword('old-password', 'new-password');
|
|
await updateLoginCredentials({ current_password: 'old-password', new_username: 'new-user' });
|
|
await deleteAccount('acc1');
|
|
await passwordLogin({ account_id: 'acc1', account: 'user', password: 'password' });
|
|
await checkPasswordLoginStatus('sid');
|
|
await cancelPasswordLogin('sid');
|
|
await deleteOrder('order-1');
|
|
await createItem('acc1', { item_title: '新商品' });
|
|
await getShippingRules();
|
|
await getShippingRulesPage();
|
|
await updateShippingRule({ cookie_id: 'acc1', trigger_type: 'order_paid' });
|
|
await deleteShippingRule('7');
|
|
await getAutomationIssues();
|
|
await resolveAutomationRun(1, 'retry');
|
|
await resolveDeferredAutomationTask(2, 'dismiss');
|
|
|
|
// paths 是请求层实际发出的版本化 URL 顺序。
|
|
const paths: unknown[] = [];
|
|
// index 是当前请求调用在模拟调用列表中的位置。
|
|
let index = 0;
|
|
for (index = 0; index < fetchMock.mock.calls.length; index += 1) {
|
|
paths.push(fetchMock.mock.calls[index][0]);
|
|
}
|
|
expect(paths).toEqual([
|
|
'/api/v1/session/password',
|
|
'/api/v1/session/credentials',
|
|
'/api/v1/accounts/acc1',
|
|
'/api/v1/password-login',
|
|
'/api/v1/password-login/check/sid',
|
|
'/api/v1/password-login/cancel/sid',
|
|
'/api/v1/orders/order-1',
|
|
'/api/v1/items/acc1',
|
|
'/api/v1/automation-rules',
|
|
'/api/v1/automation-rules?page=1&page_size=10',
|
|
'/api/v1/automation-rules',
|
|
'/api/v1/automation-rules/7',
|
|
'/api/v1/automation-issues',
|
|
'/api/v1/automation-runs/1/resolve',
|
|
'/api/v1/automation-pending-tasks/2/resolve',
|
|
]);
|
|
};
|
|
|
|
test('remaining public APIs use versioned compatibility routes', runVersionedRemainingAPITest);
|