import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createWebHistory } from 'vue-router'
import ExamNotice from '../ExamNotice.vue'

// Mock router
const mockRouter = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/course/:courseId/exam/:sectionId/notice',
      name: 'ExamNotice',
      component: ExamNotice
    },
    {
      path: '/course/:courseId/exam/:sectionId',
      name: 'Exam',
      component: { template: '<div>Exam Page</div>' }
    },
    {
      path: '/course/:courseId',
      name: 'CourseDetail',
      component: { template: '<div>Course Detail</div>' }
    }
  ]
})

describe('ExamNotice', () => {
  let wrapper: any

  beforeEach(async () => {
    // 设置路由参数
    await mockRouter.push('/course/1/exam/20/notice?courseName=测试课程&examName=期末考试')
    
    wrapper = mount(ExamNotice, {
      global: {
        plugins: [mockRouter]
      }
    })
  })

  it('应该正确渲染考前须知页面', () => {
    expect(wrapper.find('.exam-notice-page').exists()).toBe(true)
    expect(wrapper.find('.center-title').text()).toBe('考试中心')
    expect(wrapper.find('.notice-title').text()).toBe('考前须知')
  })

  it('应该显示所有考前须知条目', () => {
    const noticeItems = wrapper.findAll('.notice-item')
    expect(noticeItems.length).toBe(12) // 应该有12条须知
    
    // 检查第一条须知
    expect(noticeItems[0].find('.item-number').text()).toBe('1.')
    expect(noticeItems[0].find('.item-text').text()).toContain('考试时间为：2024年8月31日-9月30日')
  })

  it('应该有返回和开始考试按钮', () => {
    const backButton = wrapper.find('.btn-secondary')
    const startButton = wrapper.find('.btn-primary')

    expect(backButton.exists()).toBe(true)
    expect(startButton.exists()).toBe(true)
    expect(backButton.text()).toContain('返回上级')
    expect(startButton.text()).toBe('我已阅读，开始考试')
  })

  it('点击返回按钮应该跳转到课程详情页', async () => {
    const pushSpy = vi.spyOn(mockRouter, 'push')
    const backButton = wrapper.find('.btn-secondary')
    
    await backButton.trigger('click')
    
    expect(pushSpy).toHaveBeenCalledWith('/course/1')
  })

  it('点击开始考试按钮应该跳转到考试页面', async () => {
    const pushSpy = vi.spyOn(mockRouter, 'push')
    const startButton = wrapper.find('.btn-primary')

    await startButton.trigger('click')

    expect(pushSpy).toHaveBeenCalledWith({
      name: 'Exam',
      params: {
        courseId: 1,
        sectionId: 20
      },
      query: {
        courseName: '测试课程',
        examName: '期末考试',
        fromNotice: 'true'
      }
    })
  })

  it('应该正确显示浏览次数', () => {
    const viewCount = wrapper.find('.view-count')
    expect(viewCount.text()).toContain('浏览次数：1024')
  })

  it('应该有正确的页面标题和副标题', () => {
    const title = wrapper.find('.center-title')
    const subtitle = wrapper.find('.center-subtitle')
    
    expect(title.text()).toBe('考试中心')
    expect(subtitle.text()).toBe('诚信考试规范，考试过程规范，严格监考规范')
  })

  it('应该有正确的导航菜单', () => {
    const navItem = wrapper.find('.nav-menu .nav-item')
    expect(navItem.exists()).toBe(true)
    expect(navItem.find('.nav-text').text()).toBe('考前须知')
    expect(navItem.classes()).toContain('active')
  })

  it('应该没有页脚信息（已移除）', () => {
    const footer = wrapper.find('.footer')
    expect(footer.exists()).toBe(false)
  })
})
