Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/config/apiRetCode.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
/**
* API返回码常量定义
* 迁移自 legacy/src/app/apiretcode.jsx
* 与后端 mmfcapi 的 `ApiReturnCode` 枚举(src/WebApplication2/Types/ApiReturnCode.cs)保持一致。
* 4XXXX 为请求错误(前三位对应最接近的 HTTP 状态码),6XXXX 为账号错误。
*/

export const CODE_PERMISSION_DENIED = -127;
export const CODE_NOT_LOGGED_IN = -1;
export const CODE_SUCCESS = 114514;
export const CODE_ERROR = -114514;

export const CODE_ALREADY_LOGGED_IN = 0;
export const CODE_INVALID_INVITE_CODE = 1;
export const CODE_PASSWORD_IS_EMPTY = 2;
export const CODE_INVALID_EMAIL_ADDRESS = 3;
export const CODE_USERNAME_ALREADY_EXISTS = 4;
export const CODE_EMAIL_ALREADY_EXISTS = 5;
export const CODE_INVALID_VALUE = 6;
export const CODE_PAYLOAD_REQUIRED = 7;
export const CODE_FORMAT_NOT_SUPPORTED = 8;
export const CODE_PAYLOAD_TOO_LARGE = 9;
export const CODE_INVALID_CREDENTIALS = 10;
export const CODE_LOGIN_FAILED_PENDING_VERIFCATION = 11;
export const CODE_LOGIN_FAILED_USER_BANNED = 12;
// 请求错误 4XXXX
export const CODE_INVALID_VALUE = 40000;
export const CODE_PAYLOAD_REQUIRED = 40001;
export const CODE_NO_SUCH_ITEM = 40400;
export const CODE_ITEM_ALREADY_EXISTS = 40900;
export const CODE_ITEM_DELETED = 41000;
export const CODE_PAYLOAD_TOO_LARGE = 41300;
export const CODE_FORMAT_NOT_SUPPORTED = 41500;
export const CODE_TOO_MANY_REQUESTS = 42900;

export const CODE_NO_SUCH_ITEM = 13;
// 账号错误 6XXXX
export const CODE_NOT_LOGGED_IN = 60000;
export const CODE_ALREADY_LOGGED_IN = 60001;
export const CODE_PERMISSION_DENIED = 60002;
export const CODE_INVALID_CREDENTIALS = 60003;
export const CODE_LOGIN_FAILED_PENDING_VERIFCATION = 60004;
export const CODE_LOGIN_FAILED_USER_BANNED = 60005;

export const CODE_SUCCESS = 114514;
export const CODE_ERROR = -114514;
export const CODE_INVALID_INVITE_CODE = 60100;
export const CODE_PASSWORD_IS_EMPTY = 60101;
export const CODE_INVALID_EMAIL_ADDRESS = 60102;
export const CODE_USERNAME_ALREADY_EXISTS = 60103;
export const CODE_EMAIL_ALREADY_EXISTS = 60104;
2 changes: 1 addition & 1 deletion test/components/score/ScoreCount.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ it('renders nested players/acc.dx from the new all-player totals endpoint', asyn
});
it('shows the avatar from the player profile and looks up only players without one', async () => {
const fetch = vi.fn(async (url: string) => url.includes('/account/profile?')
? Response.json({ code: 13, message: 'No such user' }, { status: 404 })
? Response.json({ code: 40400, message: 'No such user' }, { status: 404 })
: Response.json({ geneTime: '2026-10-01T00:00:00Z', players: [
{ userId: 'u1', username: 'player', acc: { dx: 202.3456, classic: 205 }, player: { id: 'u1', username: 'player', nickname: 'Nick', avatarId: 'abc' } },
{ userId: 'u2', username: 'ghost', acc: { dx: 100, classic: 100 }, player: null },
Expand Down
2 changes: 1 addition & 1 deletion test/components/song/SongComments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('CommentSection', () => {

it('asks a signed-out visitor to sign in and shows failures with a retry', async () => {
currentUser = null;
fetchMock.mockResolvedValueOnce(json({ code: 13, message: 'No such chart' }, 404)).mockResolvedValueOnce(json(page([])));
fetchMock.mockResolvedValueOnce(json({ code: 40400, message: 'No such chart' }, 404)).mockResolvedValueOnce(json(page([])));
await render(<CommentSection chartId="chart" uploader="x" />);
expect(container.textContent).toContain('登录后即可评论');
expect(container.querySelector('a[href^="/login?redirect="]')).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion test/components/user/UserAvatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('UserAvatar', () => {
});

it('falls back to the default avatar when the user does not exist', async () => {
fetchMock.mockImplementation(async () => Response.json({ code: 13, message: 'No such user' }, { status: 404 }));
fetchMock.mockImplementation(async () => Response.json({ code: 40400, message: 'No such user' }, { status: 404 }));
await render(<UserAvatar username="ghost" />);
expect(sources()).toEqual(['/api3/api/avatar/default']);
});
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/favorites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ it('derives favorite collection counts from songHashs', async () => {
expect(result[0]).toMatchObject({ id: 'collection', count: 2 });
});
it('does not convert an unauthorized response into favorite collection data', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(Response.json({ code: -1 }, { status: 401 })));
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(Response.json({ code: 60000 }, { status: 401 })));
await expect(fetchFavoriteCollections(endpoints.favorite.list)).rejects.toMatchObject({ status: 401 });
});
2 changes: 1 addition & 1 deletion test/pages/ForginsterPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('email challenge authentication', () => {
});

it('requires a fresh challenge after registration validation consumes it', async () => {
fetchMock.mockResolvedValueOnce(new Response('key')).mockResolvedValueOnce(Response.json({ code: 4, message: 'Username exists' }, { status: 400 }));
fetchMock.mockResolvedValueOnce(new Response('key')).mockResolvedValueOnce(Response.json({ code: 60103, message: 'Username exists' }, { status: 400 }));
await render('/register'); fill('email', 'one@example.com'); await sendCode();
fill('username', 'player'); fill('nickname', 'player'); fill('password', 'x'); fill('password2', 'x'); fill('challengeCode', '12345678'); await submit();
expect(mocks.error).toHaveBeenCalledWith(i18n.t('auth/ForginsterPage.UsernameExists')); expect(submitButton().disabled).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions test/utils/apiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('backend response formats', () => {
});

it('preserves the backend code and message on failure', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(Response.json({ code: 10, message: 'Invalid code' }, { status: 400 })));
await expect(apiRequest('/api')).rejects.toMatchObject({ status: 400, code: 10, message: 'Invalid code' });
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(Response.json({ code: 60003, message: 'Invalid code' }, { status: 400 })));
await expect(apiRequest('/api')).rejects.toMatchObject({ status: 400, code: 60003, message: 'Invalid code' });
});

it('handles a proxy HTML error without displaying markup', async () => {
Expand Down
Loading