import { expect, test } from '@playwright/test'

test('core ERP keyboard contract remains consistent', async ({ page }) => {
  await page.goto('/oms/orders/new')
  await page.keyboard.press('F2')
  await page.keyboard.press('Escape')
  await page.keyboard.press('Tab')
  await page.keyboard.press('Shift+Tab')
  await page.keyboard.press('Enter')
  await page.keyboard.press('F8')
  await expect(page.locator('body')).toBeVisible()
})

test('search screen keeps F3 and mouse-complete behavior', async ({ page }) => {
  await page.goto('/oms/orders')
  await page.keyboard.press('F3')
  await expect(page.getByRole('button',{name:/조회/})).toBeVisible()
})

test('menu search is available by mouse and Ctrl+K', async ({ page }) => {
  await page.goto('/')
  await expect(page.getByRole('button',{name:/메뉴 검색/})).toBeVisible()
  await page.keyboard.press('Control+K')
  await page.keyboard.type('재고')
  await page.keyboard.press('ArrowDown')
  await page.keyboard.press('Enter')
})

test('component tabs support arrow, Home and End navigation', async ({ page }) => {
  await page.goto('/internal/kbx/catalog?density=compact')
  const basic=page.getByRole('tab',{name:'기본'})
  const audit=page.getByRole('tab',{name:/변경이력/})
  await basic.focus()
  await page.keyboard.press('ArrowRight')
  await expect(audit).toHaveAttribute('aria-selected','true')
  await page.keyboard.press('Home')
  await expect(basic).toHaveAttribute('aria-selected','true')
  await page.keyboard.press('End')
  await expect(audit).toHaveAttribute('aria-selected','true')
  await page.keyboard.press('ArrowLeft')
  await expect(basic).toHaveAttribute('aria-selected','true')
})


test('lookup Escape returns focus to the invoking field instead of losing keyboard context', async ({ page }) => {
  await page.goto('/oms/orders/new')
  const customerCode=page.getByRole('textbox',{name:'거래처 코드'})
  await customerCode.focus()
  await page.keyboard.press('F2')
  await expect(page.getByRole('dialog')).toBeVisible()
  await page.keyboard.press('Escape')
  await expect(customerCode).toBeFocused()
})
