import React from 'react'
import { createRoot } from 'react-dom/client'
import { flushSync } from 'react-dom'
import { test, expect, beforeEach, afterEach } from 'vitest'
import { userEvent } from 'vitest/browser'
let mounted
beforeEach(()=>{document.body.innerHTML='';mounted=undefined})
afterEach(async()=>{await userEvent.cleanup();if(mounted)flushSync(()=>mounted.unmount())})
function input(){const el=document.createElement('input');el.setAttribute('aria-label','entry');document.body.append(el);el.focus();return el}
function trace(el){const rows=[];for(const type of ['keydown','keypress','beforeinput','input','keyup','contextmenu'])el.addEventListener(type,event=>rows.push({type,key:event.key??null,code:event.code??null,location:event.location??null,keyCode:event.keyCode??null,charCode:event.charCode??null,shift:event.shiftKey??null,ctrl:event.ctrlKey??null,alt:event.altKey??null,meta:event.metaKey??null,repeat:event.repeat??null,capsLock:typeof event.getModifierState==='function'?event.getModifierState('CapsLock'):null,inputType:event.inputType??null,data:event.data??null,button:event.button??null,trusted:event.isTrusted,value:el.value,selection:[el.selectionStart,el.selectionEnd]}));return rows}
function record(name,el,events,extra={}){console.log('NAMED_KEY_RAW '+JSON.stringify({name,value:el.value,selection:[el.selectionStart,el.selectionEnd],events,...extra}))}
test('CapsLock named and physical descriptors retain native events and release ownership',async()=>{const el=input(),events=trace(el),callBoundaries=[];await userEvent.keyboard('{CapsLock>}');callBoundaries.push(events.length);await userEvent.keyboard('[/CapsLock]');callBoundaries.push(events.length);await userEvent.keyboard('[CapsLock>]');callBoundaries.push(events.length);await userEvent.keyboard('{/CapsLock}');callBoundaries.push(events.length);record('CapsLock release',el,events,{callBoundaries});expect(el.value).toBe('');expect(callBoundaries).toEqual([1,2,3,4])})
test('PageUp and PageDown preserve native key events and input selection',async()=>{const el=input(),events=trace(el);await userEvent.type(el,'abcd');await userEvent.keyboard('{PageUp}{PageDown}[PageUp][PageDown]');record('page key selection',el,events);expect(el.value).toBe('abcd')})
test('ContextMenu named and physical descriptors retain actual native menu events',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('{ContextMenu}[ContextMenu]');record('context menu',el,events);expect(el.value).toBe('')})
test('default map matching retains mixed-case names and physical codes',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('{cApSlOcK}[cApSlOcK]{pAgEuP}[pAgEdOwN]{cOnTeXtMeNu}[cOnTeXtMeNu]');record('mixed case defaults',el,events);expect(el.value).toBe('')})
test('default-map key repeats and named physical release share native state across commands',async()=>{const el=input(),events=trace(el),callBoundaries=[];await userEvent.keyboard('[PageDown>]');callBoundaries.push(events.length);await userEvent.keyboard('{/PageDown}');callBoundaries.push(events.length);await userEvent.keyboard('{PageUp>2/}');callBoundaries.push(events.length);await userEvent.keyboard('[PageDown]');callBoundaries.push(events.length);record('default repeat and release',el,events,{callBoundaries});expect(el.value).toBe('');expect(events.filter(event=>event.type==='keydown'&&event.key==='PageUp').map(event=>event.repeat)).toEqual([false,true])})
test('default-map native keyboard events commit actual React component renders',async()=>{function App(){const[keys,setKeys]=React.useState([]);return React.createElement('div',null,React.createElement('input',{'aria-label':'entry',onKeyDown:event=>setKeys(previous=>[...previous,event.key])}),React.createElement('output',null,keys.join('|')))}const container=document.createElement('div');document.body.append(container);mounted=createRoot(container);flushSync(()=>mounted.render(React.createElement(App)));const el=container.querySelector('input'),events=trace(el);el.focus();await userEvent.keyboard('{CapsLock}[PageUp]{PageDown}[ContextMenu]');record('default controlled render',el,events,{output:container.querySelector('output').textContent});expect(el.value).toBe('');expect(container.querySelector('output').textContent).toBe('CapsLock|PageUp|PageDown|ContextMenu')})
