-
- {{ item.label }}
-
-
- {{
- item.bound
- ? t('profile.authBindings.status.bound')
- : t('profile.authBindings.status.notBound')
- }}
-
+
+
+
+
+ {{ providerInitial(item.provider) }}
-
-
+
- {{ user?.email }}
+ {{ displayName }}
+
+ {{ user?.email }}
+
{{ user?.role === 'admin' ? t('profile.administrator') : t('profile.user') }}
@@ -47,438 +50,23 @@
-
-
-
- {{ hint.text }}
-
-
-
-
-
-
-
- {{ t('profile.avatar.title') }}
-
-
- {{ t('profile.avatar.description') }}
-
-
-
-
-
-
-
-
-
-
-
-
- {{ t('profile.avatar.uploadHint') }}
-
-
-
-
-
-
diff --git a/frontend/src/components/user/profile/__tests__/ProfileAvatarCard.spec.ts b/frontend/src/components/user/profile/__tests__/ProfileAvatarCard.spec.ts
new file mode 100644
index 00000000..e5f57d69
--- /dev/null
+++ b/frontend/src/components/user/profile/__tests__/ProfileAvatarCard.spec.ts
@@ -0,0 +1,214 @@
+import { mount } from '@vue/test-utils'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import ProfileAvatarCard from '@/components/user/profile/ProfileAvatarCard.vue'
+import type { User } from '@/types'
+
+const {
+ updateProfileMock,
+ showSuccessMock,
+ showErrorMock,
+ authStoreState
+} = vi.hoisted(() => ({
+ updateProfileMock: vi.fn(),
+ showSuccessMock: vi.fn(),
+ showErrorMock: vi.fn(),
+ authStoreState: {
+ user: null as User | null
+ }
+}))
+
+vi.mock('@/api', () => ({
+ userAPI: {
+ updateProfile: updateProfileMock
+ }
+}))
+
+vi.mock('@/stores/auth', () => ({
+ useAuthStore: () => authStoreState
+}))
+
+vi.mock('@/stores/app', () => ({
+ useAppStore: () => ({
+ showSuccess: showSuccessMock,
+ showError: showErrorMock
+ })
+}))
+
+vi.mock('@/utils/apiError', () => ({
+ extractApiErrorMessage: (error: unknown) => (error as Error).message || 'request failed'
+}))
+
+vi.mock('vue-i18n', async (importOriginal) => {
+ const actual = await importOriginal
()
+ return {
+ ...actual,
+ useI18n: () => ({
+ t: (key: string, params?: Record) => {
+ if (key === 'profile.avatar.title') return 'Profile avatar'
+ if (key === 'profile.avatar.description') return 'Upload and manage your avatar'
+ if (key === 'profile.avatar.uploadAction') return 'Upload image'
+ if (key === 'profile.avatar.uploadHint') return 'Uploaded images are compressed to 20KB when possible'
+ if (key === 'profile.avatar.saveSuccess') return 'Avatar updated'
+ if (key === 'profile.avatar.deleteSuccess') return 'Avatar removed'
+ if (key === 'profile.avatar.invalidType') return 'Please choose an image file'
+ if (key === 'profile.avatar.gifTooLarge') return 'GIF avatars must already be 20KB or smaller'
+ if (key === 'profile.avatar.compressTooLarge') return 'Unable to compress this image below 20KB'
+ if (key === 'profile.avatar.compressFailed') return 'Failed to compress the selected image'
+ if (key === 'profile.avatar.readFailed') return 'Failed to read the selected image'
+ if (key === 'common.save') return 'Save'
+ if (key === 'common.delete') return 'Delete'
+ if (key === 'profile.avatar.compressedReady') return `Compressed from ${params?.from} to ${params?.to}`
+ if (key === 'profile.avatar.sizeReady') return `Ready: ${params?.size}`
+ return key
+ }
+ })
+ }
+})
+
+function createUser(overrides: Partial = {}): User {
+ return {
+ id: 5,
+ username: 'alice',
+ email: 'alice@example.com',
+ avatar_url: null,
+ role: 'user',
+ balance: 10,
+ concurrency: 2,
+ status: 'active',
+ allowed_groups: null,
+ balance_notify_enabled: true,
+ balance_notify_threshold: null,
+ balance_notify_extra_emails: [],
+ created_at: '2026-04-20T00:00:00Z',
+ updated_at: '2026-04-20T00:00:00Z',
+ ...overrides
+ }
+}
+
+async function flushAsyncWork(): Promise {
+ await Promise.resolve()
+ await Promise.resolve()
+}
+
+const originalFileReader = globalThis.FileReader
+const originalImage = globalThis.Image
+const originalCreateElement = document.createElement.bind(document)
+
+function installAvatarCompressionMocks(blobSize = 8 * 1024) {
+ class MockFileReader {
+ result: string | ArrayBuffer | null = null
+ onload: ((this: FileReader, ev: ProgressEvent) => any) | null = null
+ onerror: ((this: FileReader, ev: ProgressEvent) => any) | null = null
+ error: DOMException | null = null
+
+ readAsDataURL(blob: Blob) {
+ if (blob.type === 'image/webp') {
+ this.result = 'data:image/webp;base64,' + Buffer.from('compressed-avatar').toString('base64')
+ } else {
+ this.result = 'data:image/png;base64,' + Buffer.from('original-avatar').toString('base64')
+ }
+ this.onload?.call(this as unknown as FileReader, new ProgressEvent('load'))
+ }
+ }
+
+ class MockImage {
+ naturalWidth = 1200
+ naturalHeight = 1200
+ onload: (() => void) | null = null
+ onerror: (() => void) | null = null
+
+ set src(_value: string) {
+ this.onload?.()
+ }
+ }
+
+ globalThis.FileReader = MockFileReader as unknown as typeof FileReader
+ globalThis.Image = MockImage as unknown as typeof Image
+ vi.spyOn(document, 'createElement').mockImplementation(((tagName: string, options?: ElementCreationOptions) => {
+ if (tagName === 'canvas') {
+ return {
+ width: 0,
+ height: 0,
+ getContext: () => ({
+ clearRect: vi.fn(),
+ drawImage: vi.fn()
+ }),
+ toBlob: (callback: BlobCallback) => {
+ callback(new Blob([new Uint8Array(blobSize)], { type: 'image/webp' }))
+ }
+ } as unknown as HTMLCanvasElement
+ }
+ return originalCreateElement(tagName, options)
+ }) as typeof document.createElement)
+}
+
+describe('ProfileAvatarCard', () => {
+ beforeEach(() => {
+ updateProfileMock.mockReset()
+ showSuccessMock.mockReset()
+ showErrorMock.mockReset()
+ authStoreState.user = null
+ })
+
+ afterEach(() => {
+ globalThis.FileReader = originalFileReader
+ globalThis.Image = originalImage
+ vi.restoreAllMocks()
+ })
+
+ it('compresses an uploaded image that exceeds the 20KB target before saving', async () => {
+ installAvatarCompressionMocks()
+ const updatedUser = createUser({ avatar_url: 'data:image/webp;base64,Y29tcHJlc3NlZC1hdmF0YXI=' })
+ updateProfileMock.mockResolvedValue(updatedUser)
+ authStoreState.user = createUser()
+
+ const wrapper = mount(ProfileAvatarCard, {
+ props: {
+ user: authStoreState.user
+ },
+ global: {
+ stubs: {
+ Icon: true
+ }
+ }
+ })
+
+ const fileInput = wrapper.get('[data-testid="profile-avatar-file-input"]')
+ Object.defineProperty(fileInput.element, 'files', {
+ value: [new File([new Uint8Array(220 * 1024)], 'avatar.png', { type: 'image/png' })],
+ configurable: true
+ })
+
+ await fileInput.trigger('change')
+ await flushAsyncWork()
+ await wrapper.get('[data-testid="profile-avatar-save"]').trigger('click')
+
+ expect(updateProfileMock).toHaveBeenCalledWith({
+ avatar_url: 'data:image/webp;base64,Y29tcHJlc3NlZC1hdmF0YXI='
+ })
+ expect(showErrorMock).not.toHaveBeenCalled()
+ })
+
+ it('deletes the current avatar', async () => {
+ const updatedUser = createUser({ avatar_url: null })
+ updateProfileMock.mockResolvedValue(updatedUser)
+ authStoreState.user = createUser({ avatar_url: 'https://cdn.example.com/old.png' })
+
+ const wrapper = mount(ProfileAvatarCard, {
+ props: {
+ user: authStoreState.user
+ },
+ global: {
+ stubs: {
+ Icon: true
+ }
+ }
+ })
+
+ await wrapper.get('[data-testid="profile-avatar-delete"]').trigger('click')
+
+ expect(updateProfileMock).toHaveBeenCalledWith({ avatar_url: '' })
+ expect(authStoreState.user?.avatar_url).toBeNull()
+ expect(showSuccessMock).toHaveBeenCalledWith('Avatar removed')
+ })
+})
diff --git a/frontend/src/components/user/profile/__tests__/ProfileInfoCard.spec.ts b/frontend/src/components/user/profile/__tests__/ProfileInfoCard.spec.ts
index 4c2b25ca..0ee9aebb 100644
--- a/frontend/src/components/user/profile/__tests__/ProfileInfoCard.spec.ts
+++ b/frontend/src/components/user/profile/__tests__/ProfileInfoCard.spec.ts
@@ -1,75 +1,29 @@
import { mount } from '@vue/test-utils'
-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { describe, expect, it, vi } from 'vitest'
import ProfileInfoCard from '@/components/user/profile/ProfileInfoCard.vue'
import type { User } from '@/types'
-const {
- updateProfileMock,
- showSuccessMock,
- showErrorMock,
- authStoreState
-} = vi.hoisted(() => ({
- updateProfileMock: vi.fn(),
- showSuccessMock: vi.fn(),
- showErrorMock: vi.fn(),
- authStoreState: {
- user: null as User | null
- }
-}))
-
-vi.mock('@/api', () => ({
- userAPI: {
- updateProfile: updateProfileMock
- }
-}))
-
vi.mock('@/stores/auth', () => ({
- useAuthStore: () => authStoreState
+ useAuthStore: () => ({
+ user: null
+ })
}))
vi.mock('@/stores/app', () => ({
useAppStore: () => ({
- showSuccess: showSuccessMock,
- showError: showErrorMock
+ showError: vi.fn(),
+ showSuccess: vi.fn()
})
}))
-vi.mock('@/utils/apiError', () => ({
- extractApiErrorMessage: (error: unknown) => (error as Error).message || 'request failed'
-}))
-
vi.mock('vue-i18n', async (importOriginal) => {
const actual = await importOriginal()
return {
...actual,
useI18n: () => ({
- t: (key: string, params?: Record) => {
+ t: (key: string) => {
if (key === 'profile.administrator') return 'Administrator'
if (key === 'profile.user') return 'User'
- if (key === 'profile.avatar.title') return 'Profile avatar'
- if (key === 'profile.avatar.description') return 'Set avatar by image URL or upload'
- if (key === 'profile.avatar.inputLabel') return 'Avatar URL or data URL'
- if (key === 'profile.avatar.inputPlaceholder') return 'https://cdn.example.com/avatar.png'
- if (key === 'profile.avatar.uploadAction') return 'Upload image'
- if (key === 'profile.avatar.uploadHint') return 'Images must be 100KB or smaller and will be compressed to 20KB'
- if (key === 'profile.avatar.saveSuccess') return 'Avatar updated'
- if (key === 'profile.avatar.deleteSuccess') return 'Avatar removed'
- if (key === 'profile.avatar.invalidType') return 'Please choose an image file'
- if (key === 'profile.avatar.fileTooLarge') return 'Avatar image must be 100KB or smaller'
- if (key === 'profile.avatar.gifTooLarge') return 'GIF avatars must already be 20KB or smaller'
- if (key === 'profile.avatar.compressTooLarge') return 'Unable to compress this image below 20KB'
- if (key === 'profile.avatar.compressFailed') return 'Failed to compress the selected image'
- if (key === 'profile.avatar.readFailed') return 'Failed to read the selected image'
- if (key === 'profile.avatar.invalidValue') return 'Enter a valid avatar URL or image data URL'
- if (key === 'profile.avatar.emptyDeleteHint') return 'Avatar already removed'
- if (key === 'profile.authBindings.providers.email') return 'Email'
- if (key === 'profile.authBindings.providers.linuxdo') return 'LinuxDo'
- if (key === 'profile.authBindings.providers.wechat') return 'WeChat'
- if (key === 'profile.authBindings.providers.oidc') return params?.providerName || 'OIDC'
- if (key === 'profile.authBindings.source.avatar') return `Avatar synced from ${params?.providerName || 'provider'}`
- if (key === 'profile.authBindings.source.username') return `Username synced from ${params?.providerName || 'provider'}`
- if (key === 'common.save') return 'Save'
- if (key === 'common.delete') return 'Delete'
return key
}
})
@@ -96,204 +50,23 @@ function createUser(overrides: Partial = {}): User {
}
}
-async function flushAsyncWork(): Promise {
- await Promise.resolve()
- await Promise.resolve()
-}
-
-const originalFileReader = globalThis.FileReader
-const originalImage = globalThis.Image
-const originalCreateElement = document.createElement.bind(document)
-
-function installAvatarCompressionMocks() {
- class MockFileReader {
- result: string | ArrayBuffer | null = null
- onload: ((this: FileReader, ev: ProgressEvent) => any) | null = null
- onerror: ((this: FileReader, ev: ProgressEvent) => any) | null = null
- error: DOMException | null = null
-
- readAsDataURL(blob: Blob) {
- if (blob.type === 'image/webp') {
- this.result = 'data:image/webp;base64,' + Buffer.from('compressed-avatar').toString('base64')
- } else {
- this.result = 'data:image/png;base64,' + Buffer.from('original-avatar').toString('base64')
- }
- this.onload?.call(this as unknown as FileReader, new ProgressEvent('load'))
- }
- }
-
- class MockImage {
- naturalWidth = 1200
- naturalHeight = 1200
- onload: (() => void) | null = null
- onerror: (() => void) | null = null
-
- set src(_value: string) {
- this.onload?.()
- }
- }
-
- globalThis.FileReader = MockFileReader as unknown as typeof FileReader
- globalThis.Image = MockImage as unknown as typeof Image
- vi.spyOn(document, 'createElement').mockImplementation(((tagName: string, options?: ElementCreationOptions) => {
- if (tagName === 'canvas') {
- return {
- width: 0,
- height: 0,
- getContext: () => ({
- clearRect: vi.fn(),
- drawImage: vi.fn(),
- }),
- toBlob: (callback: BlobCallback) => {
- callback(new Blob([new Uint8Array(8 * 1024)], { type: 'image/webp' }))
- },
- } as unknown as HTMLCanvasElement
- }
- return originalCreateElement(tagName, options)
- }) as typeof document.createElement)
-}
-
describe('ProfileInfoCard', () => {
- beforeEach(() => {
- updateProfileMock.mockReset()
- showSuccessMock.mockReset()
- showErrorMock.mockReset()
- authStoreState.user = null
- })
-
- afterEach(() => {
- globalThis.FileReader = originalFileReader
- globalThis.Image = originalImage
- vi.restoreAllMocks()
- })
-
- it('saves a remote avatar URL and updates the auth store', async () => {
- const updatedUser = createUser({ avatar_url: 'https://cdn.example.com/new.png' })
- updateProfileMock.mockResolvedValue(updatedUser)
- authStoreState.user = createUser()
-
+ it('renders basic account information without avatar or bindings actions', () => {
const wrapper = mount(ProfileInfoCard, {
props: {
- user: authStoreState.user
+ user: createUser()
},
global: {
stubs: {
- Icon: true,
- ProfileIdentityBindingsSection: true
+ Icon: true
}
}
})
- await wrapper.get('[data-testid="profile-avatar-input"]').setValue('https://cdn.example.com/new.png')
- await wrapper.get('[data-testid="profile-avatar-save"]').trigger('click')
-
- expect(updateProfileMock).toHaveBeenCalledWith({ avatar_url: 'https://cdn.example.com/new.png' })
- expect(authStoreState.user?.avatar_url).toBe('https://cdn.example.com/new.png')
- expect(showSuccessMock).toHaveBeenCalledWith('Avatar updated')
- })
-
- it('rejects an oversized data URL before sending the request', async () => {
- authStoreState.user = createUser()
- const oversized = `data:image/png;base64,${Buffer.from(new Uint8Array(102401)).toString('base64')}`
-
- const wrapper = mount(ProfileInfoCard, {
- props: {
- user: authStoreState.user
- },
- global: {
- stubs: {
- Icon: true,
- ProfileIdentityBindingsSection: true
- }
- }
- })
-
- await wrapper.get('[data-testid="profile-avatar-input"]').setValue(oversized)
- await wrapper.get('[data-testid="profile-avatar-save"]').trigger('click')
-
- expect(updateProfileMock).not.toHaveBeenCalled()
- expect(showErrorMock).toHaveBeenCalledWith('Avatar image must be 100KB or smaller')
- })
-
- it('compresses uploaded images under 100KB before saving', async () => {
- installAvatarCompressionMocks()
- const updatedUser = createUser({ avatar_url: 'data:image/webp;base64,Y29tcHJlc3NlZC1hdmF0YXI=' })
- updateProfileMock.mockResolvedValue(updatedUser)
- authStoreState.user = createUser()
-
- const wrapper = mount(ProfileInfoCard, {
- props: {
- user: authStoreState.user
- },
- global: {
- stubs: {
- Icon: true,
- ProfileIdentityBindingsSection: true
- }
- }
- })
-
- const fileInput = wrapper.get('[data-testid="profile-avatar-file-input"]')
- Object.defineProperty(fileInput.element, 'files', {
- value: [new File([new Uint8Array(80 * 1024)], 'avatar.png', { type: 'image/png' })],
- configurable: true
- })
-
- await fileInput.trigger('change')
- await flushAsyncWork()
- await wrapper.get('[data-testid="profile-avatar-save"]').trigger('click')
-
- expect(updateProfileMock).toHaveBeenCalledWith({
- avatar_url: 'data:image/webp;base64,Y29tcHJlc3NlZC1hdmF0YXI='
- })
- })
-
- it('deletes the current avatar', async () => {
- const updatedUser = createUser({ avatar_url: null })
- updateProfileMock.mockResolvedValue(updatedUser)
- authStoreState.user = createUser({ avatar_url: 'https://cdn.example.com/old.png' })
-
- const wrapper = mount(ProfileInfoCard, {
- props: {
- user: authStoreState.user
- },
- global: {
- stubs: {
- Icon: true,
- ProfileIdentityBindingsSection: true
- }
- }
- })
-
- await wrapper.get('[data-testid="profile-avatar-delete"]').trigger('click')
-
- expect(updateProfileMock).toHaveBeenCalledWith({ avatar_url: '' })
- expect(authStoreState.user?.avatar_url).toBeNull()
- expect(showSuccessMock).toHaveBeenCalledWith('Avatar removed')
- })
-
- it('renders third-party source hints from profile_sources', () => {
- authStoreState.user = createUser({
- avatar_url: 'https://cdn.example.com/linuxdo.png',
- profile_sources: {
- avatar: { provider: 'linuxdo', source: 'linuxdo' },
- username: { provider: 'linuxdo', source: 'linuxdo' }
- }
- })
-
- const wrapper = mount(ProfileInfoCard, {
- props: {
- user: authStoreState.user
- },
- global: {
- stubs: {
- Icon: true,
- ProfileIdentityBindingsSection: true
- }
- }
- })
-
- expect(wrapper.text()).toContain('Avatar synced from LinuxDo')
- expect(wrapper.text()).toContain('Username synced from LinuxDo')
+ expect(wrapper.text()).toContain('alice@example.com')
+ expect(wrapper.text()).toContain('alice')
+ expect(wrapper.text()).toContain('User')
+ expect(wrapper.find('[data-testid="profile-avatar-save"]').exists()).toBe(false)
+ expect(wrapper.find('[data-testid="profile-binding-email-status"]').exists()).toBe(false)
})
})
diff --git a/frontend/src/i18n/locales/en.ts b/frontend/src/i18n/locales/en.ts
index 4c948f64..7b37009f 100644
--- a/frontend/src/i18n/locales/en.ts
+++ b/frontend/src/i18n/locales/en.ts
@@ -943,15 +943,14 @@ export default {
},
avatar: {
title: 'Profile Avatar',
- description: 'Set your avatar with a remote image URL or upload an image under 100KB. Uploaded images are compressed to 20KB.',
+ description: 'Set your avatar with a remote image URL or upload an image. Static uploads are compressed to 20KB before saving.',
inputLabel: 'Avatar URL or data URL',
inputPlaceholder: 'https://cdn.example.com/avatar.png',
uploadAction: 'Upload image',
- uploadHint: 'Uploaded images must be 100KB or smaller. Static images are compressed to 20KB.',
+ uploadHint: 'Static uploads are compressed to 20KB when possible. GIF uploads must already be within 20KB.',
saveSuccess: 'Avatar updated',
deleteSuccess: 'Avatar removed',
invalidType: 'Please choose an image file',
- fileTooLarge: 'Avatar image must be 100KB or smaller',
gifTooLarge: 'GIF avatars must already be 20KB or smaller',
compressTooLarge: 'Unable to compress this image below 20KB. Try a smaller image.',
compressFailed: 'Failed to compress the selected image.',
diff --git a/frontend/src/i18n/locales/zh.ts b/frontend/src/i18n/locales/zh.ts
index 9e6c0d58..97de9c9c 100644
--- a/frontend/src/i18n/locales/zh.ts
+++ b/frontend/src/i18n/locales/zh.ts
@@ -947,15 +947,14 @@ export default {
},
avatar: {
title: '资料头像',
- description: '支持填写远程图片 URL,或上传不超过 100KB 的头像图片;上传图片会自动压缩到 20KB 以内。',
+ description: '支持填写远程图片 URL,或上传头像图片;静态图片会自动压缩到 20KB 以内后再保存。',
inputLabel: '头像 URL 或 data URL',
inputPlaceholder: 'https://cdn.example.com/avatar.png',
uploadAction: '上传图片',
- uploadHint: '上传图片需不超过 100KB,静态图片会自动压缩到 20KB 以内',
+ uploadHint: '上传图片时会自动压缩静态图片到 20KB 以内,GIF 需自行控制在 20KB 以内',
saveSuccess: '头像已更新',
deleteSuccess: '头像已删除',
invalidType: '请选择图片文件',
- fileTooLarge: '头像图片必须不超过 100KB',
gifTooLarge: 'GIF 头像必须在 20KB 以内',
compressTooLarge: '无法将图片压缩到 20KB 以内,请换一张更小的图片',
compressFailed: '压缩所选图片失败',
diff --git a/frontend/src/views/user/ProfileView.vue b/frontend/src/views/user/ProfileView.vue
index 9e72776f..463a6434 100644
--- a/frontend/src/views/user/ProfileView.vue
+++ b/frontend/src/views/user/ProfileView.vue
@@ -22,7 +22,11 @@
/>