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'])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,inputType:event.inputType??null,data:event.data??null,trusted:event.isTrusted,value:el.value,selection:[el.selectionStart,el.selectionEnd]}));return rows}
function record(name,el,events,extra={}){console.log('PHYSICAL_KEY_RAW '+JSON.stringify({name,value:el.value,selection:[el.selectionStart,el.selectionEnd],events,...extra}))}
test('physical alphanumeric descriptors retain key code and native text',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('[KeyA][Digit1][keyB][digit2]');record('physical alphanumeric',el,events);expect(el.value).toBe('a1b2')})
test('physical left modifier holds across keyboard calls and releases',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('[ShiftLeft>]');await userEvent.keyboard('[KeyA]');await userEvent.keyboard('[/ShiftLeft][KeyB]');record('physical left modifier',el,events);expect(el.value).toBe('ab');expect(events.filter(e=>e.type==='keydown'&&e.key!=='Shift').map(e=>e.shift)).toEqual([true,false])})
test('physical right modifier follows the reference native key identity',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('[ShiftRight>][KeyC][/ShiftRight]');record('physical right modifier',el,events);expect(el.value).toBe('c')})
test('physical editing descriptors preserve repeated native events and selection',async()=>{const el=input(),events=trace(el);await userEvent.type(el,'abcd');await userEvent.keyboard('[ArrowLeft>2/][Backspace>2/]');record('physical repeated editing',el,events);expect(el.value).toBe('cd')})
test('physical and named descriptors share actual held key ownership',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('{Shift>}');await userEvent.keyboard('[KeyA][/ShiftRight]');await userEvent.keyboard('{b>}');await userEvent.keyboard('[/KeyB][KeyC]');record('physical named ownership',el,events);expect(el.value).toBe('abc')})
test('setup keyboard owners retain independent physical-key cleanup state',async()=>{const el=input(),events=trace(el),first=userEvent.setup(),second=userEvent.setup();try{await first.keyboard('[ShiftLeft>]');await second.keyboard('[KeyA]');await second.cleanup();await first.cleanup();await second.keyboard('[KeyB]');record('physical setup ownership',el,events);expect(el.value).toBe('ab');expect(events.filter(e=>e.type==='keydown'&&e.key!=='Shift').map(e=>e.shift)).toEqual([true,false])}finally{await first.cleanup();await second.cleanup()}})
test('physical brackets and space preserve escaped literal bracket input',async()=>{const el=input(),events=trace(el);await userEvent.keyboard('[[KeyA][BracketLeft][BracketRight][Space]');record('physical punctuation',el,events);expect(el.value).toBe('[KeyA][] ')})
test('physical keyboard native input commits a controlled React render',async()=>{function App(){const[value,setValue]=React.useState('');return React.createElement('div',null,React.createElement('input',{'aria-label':'entry',value,onChange:event=>setValue(event.target.value)}),React.createElement('output',null,value))}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('[KeyA][Digit1][ArrowLeft][KeyB]');record('physical controlled render',el,events,{output:container.querySelector('output').textContent});expect(el.value).toBe('ab1');expect(container.querySelector('output').textContent).toBe('ab1')})
test('physical Control codes retain native modifier identity and named release ownership',async()=>{const el=input(),events=trace(el),callBoundaries=[];await userEvent.keyboard('[ControlLeft>]');callBoundaries.push(events.length);await userEvent.keyboard('[/ControlRight]');callBoundaries.push(events.length);await userEvent.keyboard('[ControlRight>]');callBoundaries.push(events.length);await userEvent.keyboard('[/ControlLeft]');callBoundaries.push(events.length);await userEvent.keyboard('{Control>}');callBoundaries.push(events.length);await userEvent.keyboard('[/ControlRight]');callBoundaries.push(events.length);record('physical Control modifiers',el,events,{callBoundaries});expect(el.value).toBe('');expect(callBoundaries).toEqual([1,2,3,4,5,6])})
test('physical Alt codes retain native modifier identity and named release ownership',async()=>{const el=input(),events=trace(el),callBoundaries=[];await userEvent.keyboard('[AltLeft>]');callBoundaries.push(events.length);await userEvent.keyboard('[/AltRight]');callBoundaries.push(events.length);await userEvent.keyboard('[AltRight>]');callBoundaries.push(events.length);await userEvent.keyboard('[/AltLeft]');callBoundaries.push(events.length);await userEvent.keyboard('{Alt>}');callBoundaries.push(events.length);await userEvent.keyboard('[/AltRight]');callBoundaries.push(events.length);record('physical Alt modifiers',el,events,{callBoundaries});expect(el.value).toBe('');expect(callBoundaries).toEqual([1,2,3,4,5,6])})
test('physical Meta codes retain native modifier identity and named release ownership',async()=>{const el=input(),events=trace(el),callBoundaries=[];await userEvent.keyboard('[MetaLeft>]');callBoundaries.push(events.length);await userEvent.keyboard('[/MetaRight]');callBoundaries.push(events.length);await userEvent.keyboard('[MetaRight>]');callBoundaries.push(events.length);await userEvent.keyboard('[/MetaLeft]');callBoundaries.push(events.length);await userEvent.keyboard('{Meta>}');callBoundaries.push(events.length);await userEvent.keyboard('[/MetaRight]');callBoundaries.push(events.length);record('physical Meta modifiers',el,events,{callBoundaries});expect(el.value).toBe('');expect(callBoundaries).toEqual([1,2,3,4,5,6])})
