import '@testing-library/jest-dom/vitest'
import { cleanup } from '@testing-library/react'
import { afterEach, expect } from 'vitest'
import { toHaveNoViolations } from 'vitest-axe/dist/matchers.js'

expect.extend({ toHaveNoViolations })

afterEach(() => {
  cleanup()
})

// jsdom lacks the pointer/scroll/observer APIs Radix overlay primitives call.
// Additive shims — guard so we never clobber a real impl.
if (!Element.prototype.hasPointerCapture) {
  Element.prototype.hasPointerCapture = () => false
  Element.prototype.setPointerCapture = () => {}
  Element.prototype.releasePointerCapture = () => {}
}
if (!Element.prototype.scrollIntoView) {
  Element.prototype.scrollIntoView = () => {}
}
if (typeof globalThis.ResizeObserver === 'undefined') {
  globalThis.ResizeObserver = class {
    observe() {}
    unobserve() {}
    disconnect() {}
  } as unknown as typeof ResizeObserver
}
if (typeof HTMLCanvasElement !== 'undefined') {
  Object.defineProperty(HTMLCanvasElement.prototype, 'getContext', {
    configurable: true,
    value: () => null,
  })
}
// jsdom never lays out elements — its own offsetWidth/offsetHeight getters
// always report 0 — @tanstack/react-virtual reads these synchronously to
// size its scroll viewport and would compute an empty visible range without
// this shim (deliberately overrides jsdom's own always-0 accessor).
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', { configurable: true, value: 500 })
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { configurable: true, value: 500 })
