describe('Performance', () => {
  it('page loads within 5 seconds', () => {
    const start = Date.now()
    cy.visit('/', {
      onBeforeLoad(win) {
        win.localStorage.setItem('sp_login_expires', String(Date.now() + 86400000))
      }
    })
    cy.get('header').should('be.visible').then(() => {
      const elapsed = Date.now() - start
      expect(elapsed).to.be.lessThan(5000)
    })
  })

  it('API responses are under 1 second', () => {
    const apis = ['/api/notes', '/api/todos', '/api/reminders', '/api/sleep', '/api/bugs']
    apis.forEach(api => {
      const start = Date.now()
      cy.request(api).then(() => {
        const elapsed = Date.now() - start
        expect(elapsed).to.be.lessThan(1000)
      })
    })
  })

  it('tab switching is instantaneous', () => {
    cy.visit('/', {
      onBeforeLoad(win) {
        win.localStorage.setItem('sp_login_expires', String(Date.now() + 86400000))
      }
    })
    cy.get('header').should('be.visible')
    const tabs = ['待办', '提醒', '身体', '音乐', '文档', '日程', '随手记']
    tabs.forEach(tab => {
      cy.get('.tab-btn').contains(tab).click()
      cy.get('.tab-btn').contains(tab).should('have.class', 'active')
    })
  })

  it('creating many notes does not degrade', () => {
    cy.visit('/', {
      onBeforeLoad(win) {
        win.localStorage.setItem('sp_login_expires', String(Date.now() + 86400000))
      }
    })
    cy.get('header').should('be.visible')
    // Create 10 notes rapidly
    for (let i = 0; i < 10; i++) {
      cy.request('POST', '/api/notes', {
        id: `perf_${i}_${Date.now()}`,
        text: `Performance test note ${i}`,
        tag: '灵感'
      })
    }
    // Reload and verify it still loads
    cy.reload()
    cy.get('header', { timeout: 5000 }).should('be.visible')
  })
})
