fileAccessPanel.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { flushPromises, mount } from '@vue/test-utils'
  3. import FileAccessPanel from '@/components/app/FileAccessPanel.vue'
  4. import MarkdownContent from '@/components/messages/MarkdownContent.vue'
  5. const api = vi.hoisted(() => ({ request: vi.fn() }))
  6. vi.mock('@/api/client', () => ({ apiRequest: api.request, apiUrl: (path: string, query: Record<string, string>) => `${path}?${new URLSearchParams(query)}` }))
  7. afterEach(() => { api.request.mockReset(); vi.unstubAllGlobals() })
  8. describe('server file permission interface', () => {
  9. it('keeps cwd visible and saves the global switch to the server without local storage', async () => {
  10. api.request.mockResolvedValue({ mode: 'folders', folders: [], workingDirectory: '/remote/work' })
  11. const wrapper = mount(FileAccessPanel, { props: { profile: 'server' } })
  12. await flushPromises()
  13. expect(wrapper.text()).toContain('/remote/work')
  14. expect((wrapper.get('input[type=checkbox]').element as HTMLInputElement).checked).toBe(false)
  15. expect(api.request).toHaveBeenCalledWith('/api/app/settings/file-access?profile=server')
  16. await wrapper.get('input[type=checkbox]').setValue(true)
  17. expect(wrapper.get('textarea').attributes('disabled')).toBeDefined()
  18. await wrapper.get('form').trigger('submit')
  19. await flushPromises()
  20. expect(api.request).toHaveBeenLastCalledWith('/api/app/settings/file-access', { method: 'PUT', body: { mode: 'all', folders: [] } })
  21. expect(wrapper.text()).toContain('已保存')
  22. wrapper.unmount()
  23. })
  24. it.each(['flat', 'nested'])('shows a %s server rejection next to a failed image and provides retry', async shape => {
  25. const reason = '目录未获授权,请检查文件访问设置'
  26. vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: shape === 'flat' ? reason : { message: reason } }), { status: 403 })))
  27. const wrapper = mount(MarkdownContent, { attachTo: document.body, props: {
  28. content: 'MEDIA: /tmp/hello.png', legacyMedia: true, fileCards: true, fileProfile: 'server',
  29. } })
  30. await wrapper.get('img').trigger('error')
  31. await flushPromises()
  32. expect(wrapper.get('[role=alert]').text()).toContain('目录未获授权')
  33. const src = new URL(wrapper.get('img').attributes('src')!, 'http://localhost')
  34. expect(src.searchParams.get('profile')).toBe('server')
  35. expect(src.searchParams.get('path')).toBe('/tmp/hello.png')
  36. await wrapper.get('[role=alert] button').trigger('click')
  37. expect(wrapper.get('img').attributes('src')).toContain('_retry=')
  38. wrapper.unmount()
  39. })
  40. })