nodePairingPanel.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { flushPromises, mount } from '@vue/test-utils'
  2. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  3. import QRCode from 'qrcode'
  4. import NodePairingPanel from '@/components/app/NodePairingPanel.vue'
  5. import { accountPairingStatus, createAccountPairing } from '@/api/accountPairing'
  6. vi.mock('qrcode', () => ({
  7. default: {
  8. toDataURL: vi.fn(async () => 'data:image/png;base64,cXItY29kZQ=='),
  9. },
  10. }))
  11. vi.mock('@/api/accountPairing', () => ({
  12. accountPairingStatus: vi.fn(),
  13. createAccountPairing: vi.fn(),
  14. }))
  15. beforeEach(() => {
  16. vi.useFakeTimers()
  17. vi.setSystemTime(new Date('2026-08-29T04:00:00.000Z'))
  18. vi.mocked(createAccountPairing).mockResolvedValue({
  19. protocolVersion: 1,
  20. serviceType: 'hermes-yaoyao',
  21. pairingId: 'account-pairing-1',
  22. expiresAt: Date.now() + 60_000,
  23. qrPayload: 'yaoyao://login?v=1&id=account-pairing-1',
  24. })
  25. vi.mocked(accountPairingStatus).mockResolvedValue({ state: 'pending' })
  26. })
  27. afterEach(() => {
  28. document.body.innerHTML = ''
  29. vi.clearAllTimers()
  30. vi.useRealTimers()
  31. vi.clearAllMocks()
  32. vi.restoreAllMocks()
  33. vi.unstubAllGlobals()
  34. Object.defineProperty(navigator, 'mediaDevices', { configurable: true, value: undefined })
  35. })
  36. describe('NodePairingPanel', () => {
  37. it('account 模式只创建账号登录二维码,并在停用时清理轮询', async () => {
  38. const wrapper = mount(NodePairingPanel, {
  39. attachTo: document.body,
  40. props: { active: true, mode: 'account', userName: 'admin' },
  41. global: { stubs: { AppIcon: true } },
  42. })
  43. await flushPromises()
  44. expect(createAccountPairing).toHaveBeenCalledOnce()
  45. expect(QRCode.toDataURL).toHaveBeenCalledWith(
  46. 'yaoyao://login?v=1&id=account-pairing-1',
  47. expect.objectContaining({ width: 296, margin: 2, errorCorrectionLevel: 'M' }),
  48. )
  49. expect(wrapper.text()).toContain('在 iOS 登录页选择扫码')
  50. expect(vi.getTimerCount()).toBe(1)
  51. await wrapper.setProps({ active: false })
  52. await flushPromises()
  53. expect(vi.getTimerCount()).toBe(0)
  54. wrapper.unmount()
  55. })
  56. })