import { ACCEPT, formatFileSize, validateBackgroundFile, validateEnterpriseWebsite } from '@/utils/teamBackgroundFile' import request from '@/utils/request2' import { completePublicUpload, createTeamBackgroundSession, disableTeamBackgroundFile, downloadTeamBackgroundFile, getPublicUploadSession, listTeamBackgroundFiles, uploadPublicTeamBackgroundFile, uploadTeamBackgroundFile, verifyPublicUploadCode } from '@/api/teamBackgroundFile' jest.mock('@/utils/request2', () => jest.fn()) class TestFormData { constructor() { this.entries = [] } append(name, value) { this.entries.push([name, value]) } } global.FormData = TestFormData beforeEach(() => { request.mockReset() }) test('accepts all approved extensions case-insensitively', () => { for (const ext of ['pdf','doc','docx','xls','xlsx','ppt','pptx','txt','md','csv']) { expect(validateBackgroundFile({ name: `file.${ext.toUpperCase()}`, size: 1 })).toBe('') } }) test('rejects archives, 50MB overflow and names over 255 chars', () => { expect(validateBackgroundFile({ name: 'file.zip', size: 1 })).toContain('文件类型') expect(validateBackgroundFile({ name: 'file.pdf', size: 50 * 1024 * 1024 + 1 })).toContain('50MB') expect(validateBackgroundFile({ name: `${'a'.repeat(252)}.pdf`, size: 1 })).toContain('255') }) test('accepts a filename containing exactly 255 characters', () => { const name = `${'a'.repeat(251)}.pdf` expect(name).toHaveLength(255) expect(validateBackgroundFile({ name, size: 1 })).toBe('') }) test('accepts a file containing exactly 50MiB', () => { expect(validateBackgroundFile({ name: 'file.pdf', size: 50 * 1024 * 1024 })).toBe('') }) test('rejects missing, unnamed and zero-size files', () => { expect(validateBackgroundFile()).toBe('请选择非空文件') expect(validateBackgroundFile({ name: '', size: 1 })).toBe('请选择非空文件') expect(validateBackgroundFile({ name: 'empty.pdf', size: 0 })).toBe('请选择非空文件') }) test('exports the exact file-picker accept list', () => { expect(ACCEPT).toBe('.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.md,.csv') }) test('formats byte counts across byte, KB and MB thresholds', () => { expect(formatFileSize(0)).toBe('0 B') expect(formatFileSize(1)).toBe('1 B') expect(formatFileSize(1023)).toBe('1023 B') expect(formatFileSize(1024)).toBe('1 KB') expect(formatFileSize(1536)).toBe('1.5 KB') expect(formatFileSize(1024 * 1024)).toBe('1 MB') expect(formatFileSize(1.5 * 1024 * 1024)).toBe('1.5 MB') }) test('enforces the backend enterprise website 500-character boundary', () => { const website = length => `https://example.com/${'a'.repeat(length - 20)}` expect(validateEnterpriseWebsite(website(499))).toBe('') expect(validateEnterpriseWebsite(website(500))).toBe('') expect(validateEnterpriseWebsite(website(501))).toContain('500') }) test('uses backend-controlled authenticated file routes', () => { listTeamBackgroundFiles(7) createTeamBackgroundSession(7) disableTeamBackgroundFile(7, 12) downloadTeamBackgroundFile(7, 12) expect(request.mock.calls).toEqual([ [{ url: '/core/userteam/7/background-files', method: 'get' }], [{ url: '/core/userteam/7/background-upload-session', method: 'post' }], [{ url: '/core/userteam/7/background-files/12', method: 'delete' }], [{ url: '/core/userteam/7/background-files/12/download', method: 'get', responseType: 'blob' }] ]) }) test('uploads authenticated files without client-supplied identity fields', () => { const file = { name: 'brief.pdf' } uploadTeamBackgroundFile(7, file) const config = request.mock.calls[0][0] expect(config).toEqual({ url: '/core/userteam/7/background-files', method: 'post', data: expect.any(TestFormData) }) expect(config.data.entries).toEqual([['file', file]]) }) test('uses fixed public routes and sends only server-issued credentials', () => { const file = { name: 'brief.pdf' } const onProgress = jest.fn() getPublicUploadSession('session-token') verifyPublicUploadCode('123456') completePublicUpload('upload-token') uploadPublicTeamBackgroundFile('upload-token', file, onProgress) const uploadData = request.mock.calls[3][0].data expect(request.mock.calls).toEqual([ [{ url: '/public/team-background-upload/session', method: 'get', params: { token: 'session-token' } }], [{ url: '/public/team-background-upload/verify', method: 'post', data: { code: '123456' } }], [{ url: '/public/team-background-upload/complete', method: 'post', headers: { 'X-Upload-Token': 'upload-token' } }], [{ url: '/public/team-background-upload/files', method: 'post', data: uploadData, headers: { 'X-Upload-Token': 'upload-token' }, onUploadProgress: onProgress }] ]) expect(uploadData).toEqual(expect.any(TestFormData)) expect(uploadData.entries).toEqual([['file', file]]) })