import { beforeEach, describe, expect, it, vi } from 'vitest'

const harness = vi.hoisted(() => {
  const renderMock = vi.fn()
  const createRootMock = vi.fn(() => ({ render: renderMock }))
  return { renderMock, createRootMock }
})

vi.mock('react-dom/client', () => ({
  createRoot: harness.createRootMock,
}))

vi.mock('./App', () => ({
  default: function MockApp() {
    return null
  },
}))

describe('main entrypoint', () => {
  beforeEach(() => {
    vi.resetModules()
    document.body.innerHTML = '<div id="root"></div>'
    harness.createRootMock.mockClear()
    harness.renderMock.mockClear()
  })

  it('mounts the app into #root', async () => {
    await import('./main')

    expect(harness.createRootMock).toHaveBeenCalled()
    expect(harness.renderMock).toHaveBeenCalled()
  })

  it('fails fast when the root node is missing', async () => {
    document.body.innerHTML = ''

    await expect(import('./main')).rejects.toThrow('Missing <div id="root"></div> in index.html')
  })
})
