import { test, expect, beforeEach } from 'vitest'
import { page, userEvent } from 'vitest/browser'
import { createElement, useState } from 'react'
import { createRoot } from 'react-dom/client'
import { flushSync } from 'react-dom'
beforeEach(()=>{document.body.innerHTML=''})
const input=(id='entry',value='')=>{const el=document.createElement('input');el.setAttribute('aria-label',id);el.value=value;document.body.append(el);return el}
const trace=element=>{const events=[];for(const type of ['focus','click','keydown','keypress','beforeinput','input','keyup','change','blur'])element.addEventListener(type,event=>events.push({type,key:event.key??null,code:event.code??null,inputType:event.inputType??null,data:event.data??null,trusted:event.isTrusted,value:element.value,selection:[element.selectionStart,element.selectionEnd]}));return events}
const row=(name,element,events,extra={})=>console.log('KEYBOARD_RAW '+JSON.stringify({name,value:element.value,selection:[element.selectionStart,element.selectionEnd],events,...extra}))
test('type emits native character input and appends at the focused caret',async()=>{const el=input();const events=trace(el);await userEvent.type(page.getByLabelText('entry'),'aA1!');expect(el.value).toBe('aA1!');el.setSelectionRange(2,2);await userEvent.type(el,'xy');expect(el.value).toBe('aAxy1!');row('type native caret',el,events)})
test('type accepts a locator target and retains BMP Unicode bytes and events',async()=>{const el=input();const events=trace(el);await userEvent.type(page.getByLabelText('entry'),'é東京✓');expect(el.value).toBe('é東京✓');row('type BMP Unicode',el,events)})
test('keyboard edits the focused element with named keys and selection',async()=>{const el=input();const events=trace(el);await userEvent.type(el,'abcd');await userEvent.keyboard('{ArrowLeft}{ArrowLeft}{Backspace}');expect(el.value).toBe('acd');await userEvent.keyboard('{selectall}');await userEvent.type(el,'XY',{skipClick:true});expect(el.value).toBe('XY');row('keyboard native editing',el,events)})
test('skipClick types into the active element while retaining the requested locator',async()=>{const first=input('first','one'),second=input('second','two');first.focus();first.setSelectionRange(3,3);const firstEvents=trace(first),secondEvents=trace(second);await userEvent.type(page.getByLabelText('second'),'!',{skipClick:true});expect(first.value).toBe('one!');expect(second.value).toBe('two');row('type skipClick',first,firstEvents,{second:{value:second.value,events:secondEvents},active:document.activeElement.getAttribute('aria-label')})})
test('keyboard descriptor rejection leaves later native input available',async()=>{const el=input();const events=trace(el);await expect(userEvent.keyboard('{')).rejects.toThrow();await userEvent.type(el,'after');expect(el.value).toBe('after');row('keyboard recoverable rejection',el,events)})
test('native type drives controlled component render turns',async()=>{const host=document.createElement('div');document.body.append(host);function Counter(){const [value,setValue]=useState('');return createElement('div',null,createElement('input',{'aria-label':'controlled',value,onChange:event=>setValue(event.target.value)}),createElement('output',{'data-testid':'result'},value))}const root=createRoot(host);flushSync(()=>root.render(createElement(Counter)));try{const el=page.getByLabelText('controlled').element();const events=trace(el);await userEvent.type(page.getByLabelText('controlled'),'abc');await expect.element(page.getByTestId('result')).toHaveTextContent('abc');row('type component turns',el,events,{output:host.querySelector('output').textContent})}finally{flushSync(()=>root.unmount())}})
