postMock.test.js 5.51 KB
/**
 * POST Mock API 单元测试
 *
 * @description 测试 AI 测试环境下的 POST 请求 Mock 功能
 * @module utils/__tests__/postMock.test
 */

import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { mockPostAPI } from '../mockData'

describe('POST Mock API', () => {
  describe('URL 参数解析', () => {
    it('should parse action parameter correctly', async () => {
      const result = await mockPostAPI('/srv/?a=feedback', {})
      expect(result.code).toBeDefined()
    })

    it('should parse action and type parameters correctly', async () => {
      const result = await mockPostAPI('/srv/?a=favorite&t=add', { meta_id: 123 })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('收藏成功')
    })
  })

  describe('favorite 模块', () => {
    it('should mock favorite/add', async () => {
      const result = await mockPostAPI('/srv/?a=favorite&t=add', { meta_id: 123 })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('收藏成功')
    })

    it('should mock favorite/del', async () => {
      const result = await mockPostAPI('/srv/?a=favorite&t=del', { meta_id: 123 })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('取消成功')
    })
  })

  describe('event 模块', () => {
    it('should mock event/add', async () => {
      const result = await mockPostAPI('/srv/?a=event&t=add', {
        type: 'READ_FILE',
        object_id: 123
      })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('success')
    })
  })

  describe('sms 模块', () => {
    it('should mock sms', async () => {
      const result = await mockPostAPI('/srv/?a=sms', { phone: '13800138000' })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('发送成功')
    })
  })

  describe('upload 模块', () => {
    it('should mock upload (qiniu token)', async () => {
      const result = await mockPostAPI('/srv/?a=upload', {
        filename: 'test.jpg',
        file: 'data:image/jpeg;base64,...'
      })
      expect(result.code).toBe(1)
      expect(result.data.token).toContain('mock_qiniu_token_')
    })

    it('should mock upload/save_file', async () => {
      const result = await mockPostAPI('/srv/?a=upload&t=save_file', {
        format: 'jpg',
        hash: 'abc123',
        filekey: 'test.jpg'
      })
      expect(result.code).toBe(1)
      expect(result.data.src).toContain('cdn.ipadbiz.cn')
    })
  })

  describe('openid 模块', () => {
    it('should mock mini program auth', async () => {
      const result = await mockPostAPI('/srv/?a=openid', { code: 'test_code' })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('授权成功')
      expect(result.data.user).toBeDefined()
      expect(result.data.user.name).toBe('AI测试用户')
    })
  })

  describe('proposal 模块', () => {
    it('should mock proposal/add', async () => {
      const result = await mockPostAPI('/srv/?a=proposal&t=add', {
        customer_name: '测试客户',
        product_id: 1
      })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('创建成功')
      expect(result.data.order_id).toContain('mock_order_')
    })

    it('should mock proposal/del', async () => {
      const result = await mockPostAPI('/srv/?a=proposal&t=del', { order_id: '123' })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('删除成功')
    })

    it('should mock proposal/view', async () => {
      const result = await mockPostAPI('/srv/?a=proposal&t=view', { order_id: '123' })
      expect(result.code).toBe(1)
      expect(result.data.status).toBe(7)
      expect(result.data.pdf_url).toContain('proposal.pdf')
    })
  })

  describe('user 模块', () => {
    it('should mock user/login', async () => {
      const result = await mockPostAPI('/srv/?a=user&t=login', {
        uuid: 'test_uuid',
        password: 'test_pass'
      })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('登录成功')
      expect(result.data.userid).toBe('mock_test_user')
    })

    it('should mock user/logout', async () => {
      const result = await mockPostAPI('/srv/?a=user&t=logout', {})
      expect(result.code).toBe(1)
      expect(result.msg).toBe('退出成功')
    })

    it('should mock user/update_profile', async () => {
      const avatar = { src: 'avatar.jpg' }
      const result = await mockPostAPI('/srv/?a=user&t=update_profile', { avatar })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('更新成功')
      expect(result.data.avatar).toEqual(avatar)
    })
  })

  describe('feedback 模块', () => {
    it('should mock feedback/add', async () => {
      const result = await mockPostAPI('/srv/?a=feedback&t=add', {
        category: '1',
        note: '测试反馈内容',
        images: []
      })
      expect(result.code).toBe(1)
      expect(result.msg).toBe('提交成功')
      expect(result.data.id).toBeDefined()
    })
  })

  describe('wx_pay 模块', () => {
    it('should mock wechat pay', async () => {
      const result = await mockPostAPI('/srv/?a=icbc_pay_wxamp', { pay_id: 'test_pay_id' })
      expect(result.code).toBe(1)
      expect(result.data.timeStamp).toBeDefined()
      expect(result.data.nonceStr).toContain('mock_nonce_')
      expect(result.data.package).toContain('prepay_id')
    })
  })

  describe('未定义的 API', () => {
    it('should return default response for unknown API', async () => {
      const result = await mockPostAPI('/srv/?a=unknown_api', {})
      expect(result.code).toBe(1)
      expect(result.msg).toBe('success (mock)')
      expect(result.data).toBeNull()
    })
  })
})