describe('Inventory Flow', () => {
  const ADMIN_TOKEN = 'c86ae7afbe10fabe3c1d5e1a7fee74feaadfd5dc7be2ab62'
  const authHeaders = { Authorization: `Bearer ${ADMIN_TOKEN}` }
  const TEST_OIL = '薰衣草'

  describe('API: inventory CRUD', () => {
    it('adds an oil to inventory', () => {
      cy.request({
        method: 'POST',
        url: '/api/inventory',
        headers: authHeaders,
        body: { oil_name: TEST_OIL }
      }).then(res => {
        expect(res.status).to.be.oneOf([200, 201])
      })
    })

    it('reads inventory and sees the oil', () => {
      cy.request({ url: '/api/inventory', headers: authHeaders }).then(res => {
        expect(res.body).to.be.an('array')
        expect(res.body).to.include(TEST_OIL)
      })
    })

    it('gets matching recipes for inventory', () => {
      cy.request({ url: '/api/inventory/recipes', headers: authHeaders }).then(res => {
        expect(res.status).to.eq(200)
        expect(res.body).to.be.an('array')
      })
    })

    it('removes the oil from inventory', () => {
      cy.request({
        method: 'DELETE',
        url: `/api/inventory/${encodeURIComponent(TEST_OIL)}`,
        headers: authHeaders
      }).then(res => {
        expect(res.status).to.eq(200)
      })
    })

    it('verifies oil is removed', () => {
      cy.request({ url: '/api/inventory', headers: authHeaders }).then(res => {
        expect(res.body).to.not.include(TEST_OIL)
      })
    })
  })

  describe('UI: inventory page', () => {
    it('page loads with oil picker', () => {
      cy.visit('/inventory', {
        onBeforeLoad(win) { win.localStorage.setItem('oil_auth_token', ADMIN_TOKEN) }
      })
      cy.contains('库存', { timeout: 10000 }).should('be.visible')
    })
  })
})
