API版本化:迁移聊天与账号任务入口

This commit is contained in:
Christ committed 2026-08-14 18:01:24 +08:00
1 parent 42e37f7898
commit d3648e9985
11 files changed
+166 -25

No files matched your search

+1 -1
View File
@@ -24,7 +24,7 @@ describe('online chat UI contract', () => {
test('frontend connects only to the application chat websocket', () => {
const chat = source('components/Chat.tsx');
expect(chat).toContain('/api/chat/ws');
expect(chat).toContain('/api/v1/chat/ws');
expect(chat).not.toContain('wss-goofish.dingtalk.com');
});
+1 -1
View File
@@ -212,7 +212,7 @@ const Chat: React.FC = () => {
if (disposed) return;
setLiveState('connecting');
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
socket = new WebSocket(`${protocol}//${window.location.host}/api/chat/ws`);
socket = new WebSocket(`${protocol}//${window.location.host}/api/v1/chat/ws`);
socket.onopen = () => { retry = 0; setLiveState('online'); };
socket.onmessage = event => {
try {
+49 -8
View File
@@ -34,11 +34,11 @@ import {
updateNotificationChannel, deleteNotificationChannel, setMessageNotification, deleteMessageNotification, deleteAccountNotifications, setAccountBindings, testNotificationChannel,
updateSystemSettings,
updateShippingRule,
getChatSessions,
getChatMessages,
sendChatMessage,
getChatSessions, getChatSessionPage,
getChatMessages, getChatMessagePage,
sendChatMessage, sendChatImage,
markChatRead,
updateAccountTaskSettings,
getAccountTaskSettings, updateAccountTaskSettings,
runAccountTask, login, initializeAdmin, verifySession,
} from './api';
@@ -66,8 +66,8 @@ test('chat APIs preserve account and conversation scope', async () => {
await getChatMessages('a1', 'c1', 9);
await sendChatMessage({ account_id: 'a1', chat_id: 'c1', buyer_id: 'b1', text: 'hi' });
await markChatRead('a1', 'c1');
expect(fetchMock.mock.calls[0][0]).toBe('/api/chat/sessions?account_id=a1');
expect(fetchMock.mock.calls[1][0]).toBe('/api/chat/messages?account_id=a1&chat_id=c1&before_id=9');
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' });
});
@@ -80,9 +80,9 @@ test('account task APIs keep rating and polish account-scoped', async () => {
auto_polish_enabled: true, polish_time: '03:00',
});
await runAccountTask('a1', 'auto_rate');
expect(fetchMock.mock.calls[0][0]).toBe('/api/account-tasks/a1');
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/account-tasks/a1/run');
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' });
});
@@ -1005,3 +1005,44 @@ const runVersionedSettingsCardNotificationAPITest = async () => {
};
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' } }));
vi.stubGlobal('fetch', 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);
+8 -8
View File
@@ -98,18 +98,18 @@ export const getAccountDetails = async (options?: RequestControlOptions): Promis
};
export const getAccountTaskSettings = async (id: string): Promise<AccountTaskSettingsResponse> =>
get(`/api/account-tasks/${id}`);
get(`/api/v1/account-tasks/${id}`);
export const updateAccountTaskSettings = async (id: string, settings: AccountTaskSettings): Promise<AccountTaskSettingsResponse> =>
put(`/api/account-tasks/${id}`, settings);
put(`/api/v1/account-tasks/${id}`, settings);
export const runAccountTask = async (id: string, taskType: 'auto_rate' | 'auto_polish'): Promise<AccountTaskRunResponseEnvelope> =>
post(`/api/account-tasks/${id}/run`, { task_type: taskType }, { timeoutMs: 120_000 });
post(`/api/v1/account-tasks/${id}/run`, { task_type: taskType }, { timeoutMs: 120_000 });
export interface ChatSessionPage { sessions: ChatSession[]; has_more: boolean; next_cursor?: number }
export const getChatSessionPage = async (accountId: string, cursor?: number, options?: RequestControlOptions, refresh = false): Promise<ChatSessionPage> => {
const result = await get<ChatSessionPage>('/api/chat/sessions', { account_id: accountId, cursor, refresh: refresh ? 1 : undefined },
const result = await get<ChatSessionPage>('/api/v1/chat/sessions', { account_id: accountId, cursor, refresh: refresh ? 1 : undefined },
refresh ? { timeoutMs: 60_000, ...options } : options);
return { sessions: result.sessions || [], has_more: result.has_more === true, next_cursor: result.next_cursor };
};
@@ -125,7 +125,7 @@ export interface ChatMessagePage {
}
export const getChatMessagePage = async (accountId: string, chatId: string, cursor?: number, beforeId?: number, options?: RequestControlOptions): Promise<ChatMessagePage> => {
const result = await get<ChatMessagePage>('/api/chat/messages', {
const result = await get<ChatMessagePage>('/api/v1/chat/messages', {
account_id: accountId, chat_id: chatId, cursor, before_id: beforeId,
}, options);
return { messages: result.messages || [], has_more: result.has_more === true, next_cursor: result.next_cursor, session: result.session };
@@ -137,7 +137,7 @@ export const getChatMessages = async (accountId: string, chatId: string, beforeI
export const sendChatMessage = async (input: {
account_id: string; chat_id: string; buyer_id: string; buyer_name?: string;
item_id?: string; item_title?: string; text: string;
}): Promise<{message: ChatMessage}> => post('/api/chat/messages', input);
}): Promise<{message: ChatMessage}> => post('/api/v1/chat/messages', input);
export const sendChatImage = async (input: {
account_id: string; chat_id: string; buyer_id: string; buyer_name?: string;
@@ -145,11 +145,11 @@ export const sendChatImage = async (input: {
}): Promise<{message: ChatMessage}> => {
const form = new FormData();
Object.entries(input).forEach(([key, value]) => form.append(key, value));
return postForm('/api/chat/images', form, { timeoutMs: 120_000 });
return postForm('/api/v1/chat/images', form, { timeoutMs: 120_000 });
};
export const markChatRead = async (accountId: string, chatId: string): Promise<ApiResponse> =>
post('/api/chat/read', { account_id: accountId, chat_id: chatId });
post('/api/v1/chat/read', { account_id: accountId, chat_id: chatId });
export interface AccountRuntimeStatus {
state: NonNullable<AccountDetail['runtime_state']>;